import { InformationCircleIcon } from '@heroicons/react/24/outline' import { type ActionFunctionArgs, json, type LoaderFunctionArgs } from '@remix-run/node' import { useFetcher, useLoaderData } from '@remix-run/react' import { Button, Tooltip, TooltipTrigger } from 'react-aria-components' import Code from '~/components/Code' import { type Machine } from '~/types' import { cn } from '~/utils/cn' import { getConfig, getContext } from '~/utils/config' import { del, post, pull } from '~/utils/headscale' import { getSession } from '~/utils/sessions' import { useLiveData } from '~/utils/useLiveData' import MachineRow from './machine' export async function loader({ request }: LoaderFunctionArgs) { const session = await getSession(request.headers.get('Cookie')) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const data = await pull<{ nodes: Machine[] }>('v1/node', session.get('hsApiKey')!) const context = await getContext() let magic: string | undefined if (context.hasConfig) { const config = await getConfig() if (config.dns_config.magic_dns) { magic = config.dns_config.base_domain } } return { nodes: data.nodes, magic } } export async function action({ request }: ActionFunctionArgs) { const session = await getSession(request.headers.get('Cookie')) if (!session.has('hsApiKey')) { return json({ message: 'Unauthorized' }, { status: 401 }) } const data = await request.formData() if (!data.has('_method') || !data.has('id')) { return json({ message: 'No method or ID provided' }, { status: 400 }) } const id = String(data.get('id')) const method = String(data.get('_method')) switch (method) { case 'delete': { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion await del(`v1/node/${id}`, session.get('hsApiKey')!) return json({ message: 'Machine removed' }) } case 'rename': { if (!data.has('name')) { return json({ message: 'No name provided' }, { status: 400 }) } const name = String(data.get('name')) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion await post(`v1/node/${id}/rename/${name}`, session.get('hsApiKey')!) return json({ message: 'Machine renamed' }) } default: { return json({ message: 'Invalid method' }, { status: 400 }) } } } export default function Page() { useLiveData({ interval: 3000 }) const data = useLoaderData() const fetcher = useFetcher() return ( <>

Machines

{data.nodes.map(machine => ( ))}
Name
Addresses {data.magic ? ( Since MagicDNS is enabled, you can access devices based on their name and also at {' '} [name].[user].{data.magic} ) : undefined}
Last Seen
) }