"use client"; import { ColumnDef } from "@tanstack/react-table"; import { ResourcesDataTable } from "./ResourcesDataTable"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@app/components/ui/dropdown-menu"; import { Button } from "@app/components/ui/button"; import { Copy, ArrowRight, ArrowUpDown, MoreHorizontal, Check, ArrowUpRight, ShieldOff, ShieldCheck } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useState } from "react"; import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog"; import { formatAxiosError } from "@app/lib/api"; import { toast } from "@app/hooks/useToast"; import { createApiClient } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import CopyToClipboard from "@app/components/CopyToClipboard"; import { Switch } from "@app/components/ui/switch"; import { AxiosResponse } from "axios"; import { UpdateResourceResponse } from "@server/routers/resource"; import { useTranslations } from "next-intl"; export type ResourceRow = { id: number; name: string; orgId: string; domain: string; site: string; siteId: string; authState: string; http: boolean; protocol: string; proxyPort: number | null; enabled: boolean; }; type ResourcesTableProps = { resources: ResourceRow[]; orgId: string; }; export default function SitesTable({ resources, orgId }: ResourcesTableProps) { const router = useRouter(); const t = useTranslations(); const api = createApiClient(useEnvContext()); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [selectedResource, setSelectedResource] = useState(); const deleteResource = (resourceId: number) => { api.delete(`/resource/${resourceId}`) .catch((e) => { console.error(t("resourceErrorDelte"), e); toast({ variant: "destructive", title: t("resourceErrorDelte"), description: formatAxiosError(e, t("resourceErrorDelte")) }); }) .then(() => { router.refresh(); setIsDeleteModalOpen(false); }); }; async function toggleResourceEnabled(val: boolean, resourceId: number) { const res = await api .post>( `resource/${resourceId}`, { enabled: val } ) .catch((e) => { toast({ variant: "destructive", title: t("resourcesErrorUpdate"), description: formatAxiosError( e, t("resourcesErrorUpdateDescription") ) }); }); } const columns: ColumnDef[] = [ { accessorKey: "name", header: ({ column }) => { return ( ); } }, { accessorKey: "site", header: ({ column }) => { return ( ); }, cell: ({ row }) => { const resourceRow = row.original; return ( ); } }, { accessorKey: "protocol", header: t("protocol"), cell: ({ row }) => { const resourceRow = row.original; return {resourceRow.protocol.toUpperCase()}; } }, { accessorKey: "domain", header: t("access"), cell: ({ row }) => { const resourceRow = row.original; return (
{!resourceRow.http ? ( ) : ( )}
); } }, { accessorKey: "authState", header: ({ column }) => { return ( ); }, cell: ({ row }) => { const resourceRow = row.original; return (
{resourceRow.authState === "protected" ? ( {t("protected")} ) : resourceRow.authState === "not_protected" ? ( {t("notProtected")} ) : ( - )}
); } }, { accessorKey: "enabled", header: t("enabled"), cell: ({ row }) => ( toggleResourceEnabled(val, row.original.id) } /> ) }, { id: "actions", cell: ({ row }) => { const resourceRow = row.original; return (
{t("viewSettings")} { setSelectedResource(resourceRow); setIsDeleteModalOpen(true); }} > {t("delete")}
); } } ]; return ( <> {selectedResource && ( { setIsDeleteModalOpen(val); setSelectedResource(null); }} dialog={

{t("resourceQuestionRemove", { selectedResource: selectedResource?.name || selectedResource?.id })}

{t("resourceMessageRemove")}

{t("resourceMessageConfirm")}

} buttonText={t("resourceDeleteConfirm")} onConfirm={async () => deleteResource(selectedResource!.id)} string={selectedResource.name} title={t("resourceDelete")} /> )} { router.push(`/${orgId}/settings/resources/create`); }} /> ); }