import { ChevronDownIcon, CopyIcon } from '@primer/octicons-react'; import { Link } from 'react-router'; import { useMemo } from 'react'; import Menu from '~/components/Menu'; import StatusCircle from '~/components/StatusCircle'; import { toast } from '~/components/Toaster'; import type { Machine, Route, User, HostInfo } from '~/types'; import { cn } from '~/utils/cn'; import * as hinfo from '~/utils/host-info'; import MenuOptions from './menu'; interface Props { machine: Machine; routes: Route[]; users: User[]; magic?: string; stats?: HostInfo; } export default function MachineRow({ machine, routes, magic, users, stats }: Props) { const expired = machine.expiry === '0001-01-01 00:00:00' || machine.expiry === '0001-01-01T00:00:00Z' || machine.expiry === null ? false : new Date(machine.expiry).getTime() < Date.now(); const tags = [...machine.forcedTags, ...machine.validTags]; if (expired) { tags.unshift('Expired'); } const prefix = magic?.startsWith('[user]') ? magic.replace('[user]', machine.user.name) : magic; // This is much easier with Object.groupBy but it's too new for us const { exit, subnet, subnetApproved } = routes.reduce<{ exit: Route[]; subnetApproved: Route[]; subnet: Route[]; }>( (acc, route) => { if (route.prefix === '::/0' || route.prefix === '0.0.0.0/0') { acc.exit.push(route); return acc; } if (route.enabled) { acc.subnetApproved.push(route); return acc; } acc.subnet.push(route); return acc; }, { exit: [], subnetApproved: [], subnet: [] }, ); const exitEnabled = useMemo(() => { if (exit.length !== 2) return false; return exit[0].enabled && exit[1].enabled; }, [exit]); if (exitEnabled) { tags.unshift('Exit Node'); } if (subnetApproved.length > 0) { tags.unshift('Subnets'); } return (

{machine.givenName}

{machine.name}

{tags.map((tag) => ( {tag} ))}
{machine.ipAddresses[0]} {machine.ipAddresses.map((ip) => ( { await navigator.clipboard.writeText(ip); toast('Copied IP address to clipboard'); }} > {ip} ))} {magic ? ( { const ip = `${machine.givenName}.${prefix}`; await navigator.clipboard.writeText(ip); toast('Copied hostname to clipboard'); }} > {machine.givenName}.{prefix} ) : undefined}
{/** {stats !== undefined ? ( <>

{hinfo.getTSVersion(stats)}

{hinfo.getOSInfo(stats)}

) : (

Unknown

)} **/}

{machine.online && !expired ? 'Connected' : new Date(machine.lastSeen).toLocaleString()}

); }