"use client"; import { ColumnDef } from "@tanstack/react-table"; import { ShareLinksDataTable } from "./ShareLinksDataTable"; 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 CreateResourceForm from "./CreateResourceForm"; 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 { ArrayElement } from "@server/types/ArrayElement"; import { ListAccessTokensResponse } from "@server/routers/accessToken"; import moment from "moment"; import CreateShareLinkForm from "./CreateShareLinkForm"; import { constructShareLink } from "@app/lib/shareLinks"; import { useTranslations } from 'next-intl'; export type ShareLinkRow = { accessTokenId: string; resourceId: number; resourceName: string; title: string | null; createdAt: number; expiresAt: number | null; siteName: string | null; }; type ShareLinksTableProps = { shareLinks: ShareLinkRow[]; orgId: string; }; export default function ShareLinksTable({ shareLinks, orgId }: ShareLinksTableProps) { const router = useRouter(); const t = useTranslations(); const api = createApiClient(useEnvContext()); const [isCreateModalOpen, setIsCreateModalOpen] = useState(false); const [rows, setRows] = useState(shareLinks); function formatLink(link: string) { return link.substring(0, 20) + "..." + link.substring(link.length - 20); } async function deleteSharelink(id: string) { await api.delete(`/access-token/${id}`).catch((e) => { toast({ title: "Failed to delete link", description: formatAxiosError( e, "An error occurred deleting link" ) }); }); const newRows = rows.filter((r) => r.accessTokenId !== id); setRows(newRows); toast({ title: "Link deleted", description: "The link has been deleted" }); } const columns: ColumnDef[] = [ { id: "actions", cell: ({ row }) => { const router = useRouter(); const resourceRow = row.original; return ( <>
{ deleteSharelink( resourceRow.accessTokenId ); }} >
); } }, { accessorKey: "resourceName", header: ({ column }) => { return ( ); }, cell: ({ row }) => { const r = row.original; return ( ); } }, { accessorKey: "title", header: ({ column }) => { return ( ); } }, // { // accessorKey: "domain", // header: "Link", // cell: ({ row }) => { // const r = row.original; // // const link = constructShareLink( // r.resourceId, // r.accessTokenId, // r.tokenHash // ); // // return ( //
// // {formatLink(link)} // // //
// ); // } // }, { accessorKey: "createdAt", header: ({ column }) => { return ( ); }, cell: ({ row }) => { const r = row.original; return moment(r.createdAt).format("lll"); } }, { accessorKey: "expiresAt", header: ({ column }) => { return ( ); }, cell: ({ row }) => { const r = row.original; if (r.expiresAt) { return moment(r.expiresAt).format("lll"); } return t('never'); } }, { id: "delete", cell: ({ row }) => (
) } ]; return ( <> { setRows([val, ...rows]); }} /> { setIsCreateModalOpen(true); }} /> ); }