import { useStore } from '@tanstack/react-form';
import type { ColumnDef, Table, VisibilityState } from '@tanstack/react-table';
import { Link } from '@tanstack/react-router';
import { Eye, EyeOff, Lock, Unlock, Wrench } from 'lucide-react';
import {
type AdminNode,
deleteAdminNodeInput,
useAdminNodeSystemInformation,
useDeleteAdminNode,
} from '@/api/admin/nodes/queries';
import { NodeStatusBadge } from '@/components/admin/nodes/NodeStatusBadge';
import Button from '@/components/elements/Button';
import { Dialog } from '@/components/elements/dialog';
import Icon from '@/components/elements/Icon';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import DataTableColumnHeader from '@/components/elements/table/DataTableColumnHeader';
import { actionsColumn, DeleteAction, EditLinkAction, RowActions } from '@/components/elements/table/RowActions';
import { Form, useAppForm } from '@/components/form';
import { cn } from '@/lib/cn';
import dayjs from '@/lib/dayjs';
import { bytesToString, mbToBytes } from '@/lib/formatters';
import { relationshipAttributes } from '@/api/relationships';
interface NodeDialogProps {
node: AdminNode;
open: boolean;
onClose: () => void;
}
const NodeLiveness = ({ node }: { node: AdminNode }) => {
const { data, error } = useAdminNodeSystemInformation(node.attributes.id, {
refetchInterval: 60_000,
retry: false,
});
if (data) {
return ;
}
if (error) {
return ;
}
return ;
};
function DeleteNodeDialog({ node, open, onClose }: NodeDialogProps) {
const { attributes } = node;
const deleteNode = useDeleteAdminNode();
const deleteForm = useAppForm({
defaultValues: { confirm: '' },
onSubmit: async () => {
try {
await deleteNode.mutateAsync(deleteAdminNodeInput(attributes.id, attributes.name));
onClose();
} catch {
// Error toast is handled by the mutation.
}
},
});
const isSubmitting = useStore(deleteForm.store, (state) => state.isSubmitting);
return (
);
}
const NodeIdentityCell = ({ node }: { node: AdminNode }) => {
const { attributes } = node;
return (
{attributes.name}
#{attributes.id} ยท {attributes.fqdn}
);
};
const NodeStatusCell = ({ node }: { node: AdminNode }) => (
{node.attributes.maintenance_mode ? (
Maintenance
) : null}
);
const LocationCell = ({ node }: { node: AdminNode }) => {
const location = relationshipAttributes(node.attributes.relationships?.location);
if (!location) {
return #{node.attributes.location_id};
}
return (
{location.short}
{location.long ? (
{location.long}
) : null}
);
};
const ResourcesHeader = ({ table }: { table: Table }) => {
const memory = table.getColumn('memory');
const disk = table.getColumn('disk');
if (!memory || !disk) {
return Resources;
}
return (
);
};
const ResourcesCell = ({ node }: { node: AdminNode }) => (
{bytesToString(mbToBytes(node.attributes.memory), 0)} memory
{bytesToString(mbToBytes(node.attributes.disk), 0)} disk
);
const AccessCell = ({ node }: { node: AdminNode }) => {
const { attributes } = node;
const secure = attributes.scheme === 'https';
return (
{attributes.scheme.toUpperCase()}
{attributes.public ? 'Public' : 'Private'}
);
};
const NodeActionsCell = ({ node }: { node: AdminNode }) => {
const { id, name } = node.attributes;
return (
}>
{({ open, onClose }) => }
);
};
export const nodeColumns = [
{
id: 'name',
accessorFn: (node) => node.attributes.name,
header: ({ column }) => ,
cell: ({ row }) => ,
enableSorting: true,
meta: { headerClassName: 'min-w-40', cellClassName: 'min-w-40' },
},
{
id: 'status',
header: 'Status',
cell: ({ row }) => ,
enableSorting: false,
meta: { headerClassName: 'w-28', cellClassName: 'w-28' },
},
{
id: 'location',
header: 'Location',
cell: ({ row }) => ,
enableSorting: false,
meta: { headerClassName: 'hidden w-32 @4xl:table-cell', cellClassName: 'hidden w-32 @4xl:table-cell' },
},
{
id: 'servers_count',
accessorFn: (node) => node.attributes.servers_count,
header: 'Servers',
cell: ({ row }) => {row.original.attributes.servers_count},
enableSorting: false,
meta: { headerClassName: 'w-20', cellClassName: 'w-20' },
},
{
id: 'resources',
header: ({ table }) => ,
cell: ({ row }) => ,
enableSorting: false,
meta: { headerClassName: 'w-40', cellClassName: 'w-40', sortColumnIds: ['memory', 'disk'] },
},
{
id: 'memory',
accessorFn: (node) => node.attributes.memory,
header: 'Memory',
enableSorting: true,
sortDescFirst: true,
},
{
id: 'disk',
accessorFn: (node) => node.attributes.disk,
header: 'Disk',
enableSorting: true,
sortDescFirst: true,
},
{
id: 'access',
header: 'Access',
cell: ({ row }) => ,
enableSorting: false,
meta: { headerClassName: 'hidden w-20 @2xl:table-cell', cellClassName: 'hidden w-20 @2xl:table-cell' },
},
{
id: 'created_at',
accessorFn: (node) => node.attributes.created_at,
header: ({ column }) => ,
cell: ({ row }) => (
),
enableSorting: true,
sortDescFirst: true,
meta: { headerClassName: 'hidden w-28 @4xl:table-cell', cellClassName: 'hidden w-28 @4xl:table-cell' },
},
actionsColumn(2, (node) => ),
] satisfies ColumnDef[];
export const nodeColumnVisibility: VisibilityState = { memory: false, disk: false };