'use client'; import Link from 'next/link'; import { format, formatDistanceToNowStrict } from 'date-fns'; import { MoreHorizontal, Pencil, Archive, MessageSquare } from 'lucide-react'; import type { ColumnDef } from '@tanstack/react-table'; import { Button } from '@/components/ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { Badge } from '@/components/ui/badge'; import { TagBadge } from '@/components/shared/tag-badge'; import { stageBadgeClass, stageLabel } from '@/lib/constants'; import { computeUrgencyBadges, type InterestUrgencyInput } from '@/components/interests/urgency'; export interface InterestRow { id: string; clientId: string; clientName: string | null; berthId: string | null; berthMooringNumber: string | null; pipelineStage: string; leadCategory: string | null; source: string | null; archivedAt: string | null; createdAt: string; /** Surfaced by listInterests for the row-level sales-triage signals * (last-activity relative time, comment-icon, urgency badges). */ updatedAt?: string; dateLastContact?: string | null; dateEoiSent?: string | null; dateDepositReceived?: string | null; eoiStatus?: string | null; outcome?: string | null; notesCount?: number; tags?: Array<{ id: string; name: string; color: string }>; } const CATEGORY_LABELS: Record = { general_interest: 'General Interest', specific_qualified: 'Specific Qualified', hot_lead: 'Hot Lead', }; const SOURCE_LABELS: Record = { website: 'Website', manual: 'Manual', referral: 'Referral', broker: 'Broker', }; interface GetColumnsOptions { portSlug: string; onEdit: (interest: InterestRow) => void; onArchive: (interest: InterestRow) => void; } export function getInterestColumns({ portSlug, onEdit, onArchive, }: GetColumnsOptions): ColumnDef[] { return [ { id: 'clientName', accessorKey: 'clientName', header: 'Client', cell: ({ row }) => { const notesCount = row.original.notesCount ?? 0; return (
e.stopPropagation()} > {row.original.clientName ?? '-'} {notesCount > 0 ? ( ) : null}
); }, }, { id: 'berthMooringNumber', accessorKey: 'berthMooringNumber', header: 'Berth', cell: ({ row }) => { if (!row.original.berthId || !row.original.berthMooringNumber) { return -; } return ( e.stopPropagation()} > {row.original.berthMooringNumber} ); }, }, { id: 'pipelineStage', accessorKey: 'pipelineStage', header: 'Stage', cell: ({ row }) => { const stage = row.original.pipelineStage; const badges = computeUrgencyBadges(row.original satisfies InterestUrgencyInput); return (
{stageLabel(stage)} {badges.length > 0 ? (
{badges.map((b) => ( {b.label} ))}
) : null}
); }, }, { id: 'leadCategory', accessorKey: 'leadCategory', header: 'Category', cell: ({ getValue }) => { const cat = getValue() as string | null; if (!cat) return -; return ( {CATEGORY_LABELS[cat] ?? cat} ); }, }, { id: 'source', accessorKey: 'source', header: 'Source', cell: ({ getValue }) => { const source = getValue() as string | null; if (!source) return -; return ( {SOURCE_LABELS[source] ?? source} ); }, }, { id: 'tags', header: 'Tags', enableSorting: false, cell: ({ row }) => { const rowTags = row.original.tags ?? []; if (rowTags.length === 0) return -; return (
{rowTags.slice(0, 3).map((tag) => ( ))} {rowTags.length > 3 && ( +{rowTags.length - 3} )}
); }, }, { // Sales-triage default: prefer the explicit dateLastContact, fall back // to updatedAt. Sortable on dateLastContact server-side; the column // header label ("Last activity") makes the fallback semantics clear. id: 'dateLastContact', accessorKey: 'dateLastContact', header: 'Last activity', cell: ({ row }) => { const lastIso = row.original.dateLastContact ?? row.original.updatedAt ?? null; if (!lastIso) { return -; } const d = new Date(lastIso); return ( {formatDistanceToNowStrict(d, { addSuffix: true })} ); }, }, { id: 'actions', header: '', enableSorting: false, size: 48, cell: ({ row }) => ( onEdit(row.original)}> Edit onArchive(row.original)}> Archive ), }, ]; }