headplane/app/routes/machines/overview.tsx

132 lines
4 KiB
TypeScript
Raw Normal View History

2024-12-31 10:30:14 +05:30
import { InfoIcon } from '@primer/octicons-react';
2024-12-31 10:31:50 +05:30
import type { ActionFunctionArgs, LoaderFunctionArgs } from 'react-router';
2024-12-31 10:30:14 +05:30
import { useLoaderData } from 'react-router';
2024-12-31 10:30:14 +05:30
import Code from '~/components/Code';
import { ErrorPopup } from '~/components/Error';
2024-12-31 10:30:14 +05:30
import Link from '~/components/Link';
import type { Machine, Route, User } from '~/types';
2025-01-28 17:46:16 -05:00
import cn from '~/utils/cn';
2024-12-31 10:30:14 +05:30
import { pull } from '~/utils/headscale';
import { getSession } from '~/utils/sessions.server';
2025-02-01 15:28:04 -05:00
import Tooltip from '~/components/Tooltip';
2025-02-19 18:09:42 -05:00
import { hs_getConfig } from '~/utils/config/loader';
import useAgent from '~/utils/useAgent';
import { hp_getConfig, hp_getSingletonUnsafe } from '~server/context/global';
2024-12-31 10:30:14 +05:30
import { menuAction } from './action';
import MachineRow from './components/machine';
import NewMachine from './dialogs/new';
export async function loader({ request }: LoaderFunctionArgs) {
2024-12-31 10:30:14 +05:30
const session = await getSession(request.headers.get('Cookie'));
const [machines, routes, users] = await Promise.all([
pull<{ nodes: Machine[] }>('v1/node', session.get('hsApiKey')!),
pull<{ routes: Route[] }>('v1/routes', session.get('hsApiKey')!),
pull<{ users: User[] }>('v1/user', session.get('hsApiKey')!),
2024-12-31 10:30:14 +05:30
]);
const context = hp_getConfig();
2025-02-13 12:35:12 -05:00
const { mode, config } = hs_getConfig();
2024-12-31 10:30:14 +05:30
let magic: string | undefined;
2025-02-13 12:35:12 -05:00
if (mode !== 'no') {
if (config.dns.magic_dns) {
2024-12-31 10:30:14 +05:30
magic = config.dns.base_domain;
}
}
return {
2024-05-01 02:47:45 -04:00
nodes: machines.nodes,
routes: routes.routes,
users: users.users,
magic,
server: context.headscale.url,
publicServer: context.headscale.public_url,
agents: [...(hp_getSingletonUnsafe('ws_agents') ?? []).keys()],
2024-12-31 10:30:14 +05:30
};
}
export async function action({ request }: ActionFunctionArgs) {
2024-12-31 10:30:14 +05:30
return menuAction(request);
}
export default function Page() {
2024-12-31 10:30:14 +05:30
const data = useLoaderData<typeof loader>();
const { data: stats } = useAgent(data.nodes.map((node) => node.nodeKey));
return (
<>
2025-02-04 17:21:03 -05:00
<div className="flex justify-between items-center mb-6">
<div className="flex flex-col w-2/3">
2025-02-04 17:21:03 -05:00
<h1 className="text-2xl font-medium mb-2">Machines</h1>
<p>
2024-12-31 10:30:14 +05:30
Manage the devices connected to your Tailnet.{' '}
<Link
to="https://tailscale.com/kb/1372/manage-devices"
name="Tailscale Manage Devices Documentation"
>
Learn more
</Link>
</p>
</div>
<NewMachine
server={data.publicServer ?? data.server}
users={data.users}
/>
</div>
<table className="table-auto w-full rounded-lg">
2025-02-04 17:21:03 -05:00
<thead className="text-headplane-600 dark:text-headplane-300">
2025-02-01 15:28:04 -05:00
<tr className="text-left px-0.5">
2025-02-04 17:21:03 -05:00
<th className="uppercase text-xs font-bold pb-2">Name</th>
<th className="pb-2 w-1/4">
<div className="flex items-center gap-x-1">
2025-02-01 15:28:04 -05:00
<p className="uppercase text-xs font-bold">Addresses</p>
2024-12-31 10:30:14 +05:30
{data.magic ? (
2025-02-01 15:28:04 -05:00
<Tooltip>
<InfoIcon className="w-4 h-4" />
<Tooltip.Body className="font-normal">
2024-12-31 10:30:14 +05:30
Since MagicDNS is enabled, you can access devices based on
their name and also at{' '}
<Code>
[name].
{data.magic}
</Code>
2025-02-01 15:28:04 -05:00
</Tooltip.Body>
</Tooltip>
2024-12-31 10:30:14 +05:30
) : undefined}
</div>
</th>
<th className="uppercase text-xs font-bold pb-2">Version</th>
2025-02-04 17:21:03 -05:00
<th className="uppercase text-xs font-bold pb-2">Last Seen</th>
</tr>
</thead>
2024-12-31 10:30:14 +05:30
<tbody
className={cn(
2025-02-04 17:21:03 -05:00
'divide-y divide-headplane-100 dark:divide-headplane-800 align-top',
'border-t border-headplane-100 dark:border-headplane-800',
2024-12-31 10:30:14 +05:30
)}
>
2024-12-31 10:30:14 +05:30
{data.nodes.map((machine) => (
<MachineRow
key={machine.id}
machine={machine}
2024-12-31 10:30:14 +05:30
routes={data.routes.filter(
(route) => route.node.id === machine.id,
)}
users={data.users}
magic={data.magic}
stats={stats?.[machine.nodeKey]}
isAgent={data.agents.includes(machine.id)}
/>
))}
</tbody>
</table>
</>
2024-12-31 10:30:14 +05:30
);
}
export function ErrorBoundary() {
return <ErrorPopup type="embedded" />;
}