"use client"; import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog"; import { Button } from "@app/components/ui/button"; import { DataTable, ExtendedColumnDef } from "@app/components/ui/data-table"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@app/components/ui/dropdown-menu"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { toast } from "@app/hooks/useToast"; import { createApiClient, formatAxiosError } from "@app/lib/api"; import { ArrowRight, ArrowUpDown, MoreHorizontal, CircleSlash } from "lucide-react"; import { useTranslations } from "next-intl"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useMemo, useState, useTransition } from "react"; import { Badge } from "./ui/badge"; export type ClientRow = { id: number; name: string; subnet: string; // siteIds: string; mbIn: string; mbOut: string; orgId: string; online: boolean; olmVersion?: string; olmUpdateAvailable: boolean; userId: string | null; username: string | null; userEmail: string | null; niceId: string; agent: string | null; archived?: boolean; blocked?: boolean; approvalState: "approved" | "pending" | "denied"; }; type ClientTableProps = { machineClients: ClientRow[]; orgId: string; }; export default function MachineClientsTable({ machineClients, orgId }: ClientTableProps) { const router = useRouter(); const t = useTranslations(); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [selectedClient, setSelectedClient] = useState( null ); const api = createApiClient(useEnvContext()); const [isRefreshing, startTransition] = useTransition(); const defaultMachineColumnVisibility = { subnet: false, userId: false, niceId: false }; const refreshData = () => { startTransition(() => { try { router.refresh(); } catch (error) { toast({ title: t("error"), description: t("refreshError"), variant: "destructive" }); } }); }; const deleteClient = (clientId: number) => { api.delete(`/client/${clientId}`) .catch((e) => { console.error("Error deleting client", e); toast({ variant: "destructive", title: "Error deleting client", description: formatAxiosError(e, "Error deleting client") }); }) .then(() => { startTransition(() => { router.refresh(); setIsDeleteModalOpen(false); }); }); }; const archiveClient = (clientId: number) => { api.post(`/client/${clientId}/archive`) .catch((e) => { console.error("Error archiving client", e); toast({ variant: "destructive", title: "Error archiving client", description: formatAxiosError(e, "Error archiving client") }); }) .then(() => { startTransition(() => { router.refresh(); }); }); }; const unarchiveClient = (clientId: number) => { api.post(`/client/${clientId}/unarchive`) .catch((e) => { console.error("Error unarchiving client", e); toast({ variant: "destructive", title: "Error unarchiving client", description: formatAxiosError(e, "Error unarchiving client") }); }) .then(() => { startTransition(() => { router.refresh(); }); }); }; const blockClient = (clientId: number) => { api.post(`/client/${clientId}/block`) .catch((e) => { console.error("Error blocking client", e); toast({ variant: "destructive", title: "Error blocking client", description: formatAxiosError(e, "Error blocking client") }); }) .then(() => { startTransition(() => { router.refresh(); }); }); }; const unblockClient = (clientId: number) => { api.post(`/client/${clientId}/unblock`) .catch((e) => { console.error("Error unblocking client", e); toast({ variant: "destructive", title: "Error unblocking client", description: formatAxiosError(e, "Error unblocking client") }); }) .then(() => { startTransition(() => { router.refresh(); }); }); }; // Check if there are any rows without userIds in the current view's data const hasRowsWithoutUserId = useMemo(() => { return machineClients.some((client) => !client.userId) ?? false; }, [machineClients]); const columns: ExtendedColumnDef[] = useMemo(() => { const baseColumns: ExtendedColumnDef[] = [ { accessorKey: "name", enableHiding: false, friendlyName: "Name", header: ({ column }) => { return ( ); }, cell: ({ row }) => { const r = row.original; return (
{r.name} {r.archived && ( {t("archived")} )} {r.blocked && ( {t("blocked")} )}
); } }, { accessorKey: "niceId", friendlyName: "Identifier", header: ({ column }) => { return ( ); } }, { accessorKey: "online", friendlyName: "Connectivity", header: ({ column }) => { return ( ); }, cell: ({ row }) => { const originalRow = row.original; if (originalRow.online) { return (
Connected
); } else { return (
Disconnected
); } } }, { accessorKey: "mbIn", friendlyName: "Data In", header: ({ column }) => { return ( ); } }, { accessorKey: "mbOut", friendlyName: "Data Out", header: ({ column }) => { return ( ); } }, { accessorKey: "client", friendlyName: t("agent"), header: ({ column }) => { return ( ); }, cell: ({ row }) => { const originalRow = row.original; return (
{originalRow.agent && originalRow.olmVersion ? ( {originalRow.agent + " v" + originalRow.olmVersion} ) : ( "-" )} {/*originalRow.olmUpdateAvailable && ( )*/}
); } }, { accessorKey: "subnet", friendlyName: "Address", header: ({ column }) => { return ( ); } } ]; // Only include actions column if there are rows without userIds if (hasRowsWithoutUserId) { baseColumns.push({ id: "actions", enableHiding: false, header: () => , cell: ({ row }) => { const clientRow = row.original; return !clientRow.userId ? (
{ if (clientRow.archived) { unarchiveClient(clientRow.id); } else { archiveClient(clientRow.id); } }} > {clientRow.archived ? "Unarchive" : "Archive"} { if (clientRow.blocked) { unblockClient(clientRow.id); } else { blockClient(clientRow.id); } }} > {clientRow.blocked ? "Unblock" : "Block"} { setSelectedClient(clientRow); setIsDeleteModalOpen(true); }} > Delete
) : null; } }); } return baseColumns; }, [hasRowsWithoutUserId, t]); return ( <> {selectedClient && ( { setIsDeleteModalOpen(val); setSelectedClient(null); }} dialog={

{t("deleteClientQuestion")}

{t("clientMessageRemove")}

} buttonText="Confirm Delete Client" onConfirm={async () => deleteClient(selectedClient!.id)} string={selectedClient.name} title="Delete Client" /> )} router.push(`/${orgId}/settings/clients/machine/create`) } addButtonText={t("createClient")} onRefresh={refreshData} isRefreshing={isRefreshing} enableColumnVisibility={true} persistColumnVisibility="machine-clients" columnVisibility={defaultMachineColumnVisibility} stickyLeftColumn="name" stickyRightColumn="actions" filters={[ { id: "status", label: t("status") || "Status", multiSelect: true, displayMode: "calculated", options: [ { id: "active", label: t("active") || "Active", value: "active" }, { id: "archived", label: t("archived") || "Archived", value: "archived" }, { id: "blocked", label: t("blocked") || "Blocked", value: "blocked" } ], filterFn: ( row: ClientRow, selectedValues: (string | number | boolean)[] ) => { if (selectedValues.length === 0) return true; const rowArchived = row.archived || false; const rowBlocked = row.blocked || false; const isActive = !rowArchived && !rowBlocked; if (selectedValues.includes("active") && isActive) return true; if ( selectedValues.includes("archived") && rowArchived ) return true; if ( selectedValues.includes("blocked") && rowBlocked ) return true; return false; }, defaultValues: ["active"] // Default to showing active clients } ]} /> ); }