'use client'; import { useState } from 'react'; import { useQueryClient } from '@tanstack/react-query'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { usePaginatedQuery } from '@/hooks/use-paginated-query'; import { apiFetch } from '@/lib/api/client'; interface DocumentRow { id: string; documentType: string; title: string; status: string; createdAt: string; signers?: Array<{ status: string }>; } interface DocumentListProps { interestId?: string; clientId?: string; } const STATUS_COLORS: Record = { draft: 'secondary', sent: 'default', partially_signed: 'default', completed: 'outline', expired: 'destructive', cancelled: 'destructive', }; const TYPE_LABELS: Record = { eoi: 'EOI', contract: 'Contract', nda: 'NDA', reservation_agreement: 'Reservation', other: 'Other', }; export function DocumentList({ interestId, clientId }: DocumentListProps) { const queryClient = useQueryClient(); const queryParams = new URLSearchParams(); if (interestId) queryParams.set('interestId', interestId); if (clientId) queryParams.set('clientId', clientId); const { data, isLoading } = usePaginatedQuery({ queryKey: ['documents', { interestId, clientId }], endpoint: `/api/v1/documents?${queryParams.toString()}`, filterDefinitions: [], }); const handleDelete = async (doc: DocumentRow) => { if (!confirm(`Delete "${doc.title}"? This cannot be undone.`)) return; try { await apiFetch(`/api/v1/documents/${doc.id}`, { method: 'DELETE' }); queryClient.invalidateQueries({ queryKey: ['documents', { interestId, clientId }] }); } catch { // silent } }; const handleSend = async (doc: DocumentRow) => { try { await apiFetch(`/api/v1/documents/${doc.id}/send`, { method: 'POST' }); queryClient.invalidateQueries({ queryKey: ['documents', { interestId, clientId }] }); } catch { // silent } }; const getSignerProgress = (doc: DocumentRow) => { if (!doc.signers) return '—'; const signed = doc.signers.filter((s) => s.status === 'signed').length; return `${signed}/${doc.signers.length} signed`; }; if (isLoading) { return
Loading documents...
; } if (!data || data.length === 0) { return
No documents yet.
; } return (
{data.map((doc) => ( ))}
Type Title Status Signers Created Actions
{TYPE_LABELS[doc.documentType] ?? doc.documentType} {doc.title} {doc.status} {getSignerProgress(doc)} {new Date(doc.createdAt).toLocaleDateString('en-GB')} {doc.status === 'draft' && ( handleSend(doc)}> Send for Signing )} handleDelete(doc)} className="text-destructive focus:text-destructive" > Delete
); }