'use client'; import { useState } from 'react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { Pencil, Archive, RotateCcw, Mail, Phone } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { TagBadge } from '@/components/shared/tag-badge'; import { ArchiveConfirmDialog } from '@/components/shared/archive-confirm-dialog'; import { ClientForm } from '@/components/clients/client-form'; import { apiFetch } from '@/lib/api/client'; interface ClientDetailHeaderProps { client: { id: string; fullName: string; companyName?: string | null; nationality?: string | null; isProxy?: boolean; proxyType?: string | null; actualOwnerName?: string | null; yachtName?: string | null; berthSizeDesired?: string | null; preferredContactMethod?: string | null; preferredLanguage?: string | null; timezone?: string | null; source?: string | null; sourceDetails?: string | null; archivedAt?: string | null; contacts?: Array<{ channel: string; value: string; isPrimary: boolean; label?: string | null }>; tags?: Array<{ id: string; name: string; color: string }>; }; } type ClientFormClient = { id: string; fullName: string; companyName?: string | null; nationality?: string | null; isProxy?: boolean; proxyType?: string | null; actualOwnerName?: string | null; yachtName?: string | null; berthSizeDesired?: string | null; preferredContactMethod?: string | null; preferredLanguage?: string | null; timezone?: string | null; source?: string | null; sourceDetails?: string | null; contacts?: Array<{ channel: string; value: string; label?: string | null; isPrimary?: boolean }>; tags?: Array<{ id: string }>; }; const SOURCE_LABELS: Record = { website: 'Website', manual: 'Manual', referral: 'Referral', broker: 'Broker', }; export function ClientDetailHeader({ client }: ClientDetailHeaderProps) { const queryClient = useQueryClient(); const [editOpen, setEditOpen] = useState(false); const [archiveOpen, setArchiveOpen] = useState(false); const isArchived = !!client.archivedAt; const archiveMutation = useMutation({ mutationFn: () => apiFetch(`/api/v1/clients/${client.id}`, { method: 'DELETE' }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['clients', client.id] }); queryClient.invalidateQueries({ queryKey: ['clients'] }); setArchiveOpen(false); }, }); const restoreMutation = useMutation({ mutationFn: () => apiFetch(`/api/v1/clients/${client.id}/restore`, { method: 'POST' }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['clients', client.id] }); queryClient.invalidateQueries({ queryKey: ['clients'] }); setArchiveOpen(false); }, }); const primaryEmail = client.contacts?.find((c) => c.channel === 'email' && c.isPrimary) ?? client.contacts?.find((c) => c.channel === 'email'); const primaryPhone = client.contacts?.find((c) => c.channel === 'phone' && c.isPrimary) ?? client.contacts?.find((c) => c.channel === 'phone'); return ( <>

{client.fullName}

{isArchived && ( Archived )} {client.isProxy && ( Proxy {client.proxyType ? `(${client.proxyType.replace('_', ' ')})` : ''} )}
{client.companyName && (

{client.companyName}

)}
{client.source && ( Source:{' '} {SOURCE_LABELS[client.source] ?? client.source} )} {primaryEmail && ( {primaryEmail.value} )} {primaryPhone && ( {primaryPhone.value} )}
{client.tags && client.tags.length > 0 && (
{client.tags.map((tag) => ( ))}
)}
{/* Actions */}
{ if (isArchived) { restoreMutation.mutate(); } else { archiveMutation.mutate(); } }} isLoading={archiveMutation.isPending || restoreMutation.isPending} /> ); }