'use client'; /** * Client-detail multi-step "Send documents" dialog (Phase 7 §5.7). * * The client header action opens this dialog. The rep picks one of the * client's interest-linked berths (to send a per-berth PDF) OR a brochure * (defaults to the port default when unspecified). The actual send flow * delegates to {@link SendDocumentDialog}; this wrapper is the picker. */ import { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { FileText, Loader2, Mail } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { SendDocumentDialog } from '@/components/shared/send-document-dialog'; import { apiFetch } from '@/lib/api/client'; interface SendDocumentsDialogProps { open: boolean; onOpenChange: (open: boolean) => void; clientId: string; clientName: string; /** When the rep is launching from a specific interest, pin it. */ interestId?: string; } interface BrochureOption { id: string; label: string; isDefault: boolean; archivedAt: string | null; versionCount: number; } interface BrochuresResponse { data: BrochureOption[]; } export function SendDocumentsDialog({ open, onOpenChange, clientId, clientName, interestId, }: SendDocumentsDialogProps) { const [activeSend, setActiveSend] = useState< | { kind: 'brochure'; brochureId?: string } | { kind: 'berth_pdf'; berthId: string; mooring: string } | null >(null); // Lightweight brochures fetch — only fires once dialog is opened. const brochuresQuery = useQuery({ queryKey: ['brochures', 'list'], queryFn: () => apiFetch('/api/v1/admin/brochures'), enabled: open, }); const usableBrochures = brochuresQuery.data?.data.filter((b) => !b.archivedAt && b.versionCount > 0) ?? []; return ( <> Send documents to {clientName} Pick a brochure or open the berth detail page to send a per-berth spec sheet.

Brochures

{brochuresQuery.isLoading && (
Loading brochures…
)} {!brochuresQuery.isLoading && usableBrochures.length === 0 && (

No brochures uploaded yet. Add one in /admin/brochures.

)}
{usableBrochures.map((b) => ( ))}
{activeSend?.kind === 'brochure' && ( { if (!o) { setActiveSend(null); onOpenChange(false); } }} documentKind="brochure" recipient={{ clientId, interestId }} context={{ brochureId: activeSend.brochureId }} title={`Send brochure to ${clientName}`} onSent={() => setActiveSend(null)} /> )} {activeSend?.kind === 'berth_pdf' && ( { if (!o) { setActiveSend(null); onOpenChange(false); } }} documentKind="berth_pdf" recipient={{ clientId, interestId }} context={{ berthId: activeSend.berthId }} title={`Send berth ${activeSend.mooring} spec sheet`} onSent={() => setActiveSend(null)} /> )} ); }