import { ChevronDownIcon, CopyIcon } from '@primer/octicons-react'; import { useMemo } from 'react'; import { Link } from 'react-router'; import Chip from '~/components/Chip'; import Menu from '~/components/Menu'; import StatusCircle from '~/components/StatusCircle'; import type { HostInfo, Machine, Route, User } from '~/types'; import cn from '~/utils/cn'; import * as hinfo from '~/utils/host-info'; import toast from '~/utils/toast'; import MenuOptions from './menu'; interface Props { machine: Machine; routes: Route[]; users: User[]; isAgent?: boolean; magic?: string; stats?: HostInfo; } export default function MachineRow({ machine, routes, users, isAgent, magic, 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 = [...new Set([...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'); } if (isAgent) { tags.unshift('Headplane Agent'); } const ipOptions = useMemo(() => { if (magic) { return [...machine.ipAddresses, `${machine.givenName}.${prefix}`]; } return machine.ipAddresses; }, [magic, machine.ipAddresses]); return (
{machine.givenName}
{machine.name}
{hinfo.getTSVersion(stats)}
{hinfo.getOSInfo(stats)}
> ) : (Unknown
)}{machine.online && !expired ? 'Connected' : new Date(machine.lastSeen).toLocaleString()}