'use client'; import { useQuery } from '@tanstack/react-query'; import { useParams } from 'next/navigation'; import { Loader2 } from 'lucide-react'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { Badge } from '@/components/ui/badge'; import { EmptyState } from '@/components/shared/empty-state'; import { OwnerLink } from '@/components/yachts/yacht-detail-header'; import { apiFetch } from '@/lib/api/client'; interface OwnershipHistoryRow { id: string; yachtId: string; ownerType: 'client' | 'company'; ownerId: string; startDate: string; endDate: string | null; transferReason: string | null; transferNotes: string | null; createdBy: string; createdAt: string; } interface YachtOwnershipHistoryProps { yachtId: string; } const REASON_LABELS: Record = { sale: 'Sale', inheritance: 'Inheritance', gift: 'Gift', company_restructure: 'Company restructure', other: 'Other', }; function formatDate(value: string | null): string { if (!value) return '—'; const date = new Date(value); if (Number.isNaN(date.getTime())) return value; return date.toLocaleDateString(); } export function YachtOwnershipHistory({ yachtId }: YachtOwnershipHistoryProps) { const params = useParams<{ portSlug: string }>(); const portSlug = params?.portSlug ?? ''; const { data, isLoading } = useQuery({ queryKey: ['yachts', yachtId, 'ownership-history'], queryFn: () => apiFetch<{ data: OwnershipHistoryRow[] }>(`/api/v1/yachts/${yachtId}/ownership-history`).then( (r) => r.data, ), }); if (isLoading) { return (
); } if (!data || data.length === 0) { return ( ); } return (
Start Date End Date Owner Reason Notes {data.map((row) => ( {formatDate(row.startDate)} {row.endDate ? ( formatDate(row.endDate) ) : ( Current )} {row.transferReason ? (REASON_LABELS[row.transferReason] ?? row.transferReason) : '—'} {row.transferNotes ?? '—'} ))}
); }