Files
pn-new-crm/src/components/clients/client-detail.tsx
Matt e4daa482de feat(tenancies-p6): module-gate entity tabs (berth / client / yacht)
- PortProvider exposes tenanciesModuleByPort + a useTenanciesModuleEnabled()
  hook that returns the flag for the currently-active port. Synchronous
  read off context (server-resolved in the dashboard layout), so no
  fetch latency / hydration flicker when the rep flips ports.
- buildBerthTabs / getClientTabs / getYachtTabs gain a
  tenanciesModuleEnabled option. When false, the Tenancies tab is
  filtered out entirely. When true, it slots into the entity-specific
  position (after Interests on berth + yacht; after Companies on client).
- BerthDetail / ClientDetail / YachtDetail pass the hook value through.
  Hook call ordered above the early-return so React's rules-of-hooks
  stays satisfied. Existing read-only tab content (Active tenancy card
  + History + the berth-side BerthReserveDialog "Create tenancy" CTA
  from P2) stays untouched — it just becomes visible when the module
  is on.

Deferred (separate ship): generic TenancyCreateDialog that pre-fills
clientId / yachtId from the parent entity context, so client / yacht
tabs can mint a tenancy without bouncing through the berth detail page.
Today client/yacht Tenancies tabs are read-only (the create entry-point
is the berth tab); the generic dialog will land alongside the Edit /
Renew / Transfer / End dialogs (design § P6 sub-tasks).

Verified: tsc clean, 1493/1493 vitest.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-25 15:29:22 +02:00

149 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
import { useEffect } from 'react';
import { useQuery } from '@tanstack/react-query';
import { useParams } from 'next/navigation';
import { DetailLayout } from '@/components/shared/detail-layout';
import { DetailNotFound } from '@/components/shared/detail-not-found';
import { useMobileChrome } from '@/components/layout/mobile/mobile-layout-provider';
import { ClientDetailHeader } from '@/components/clients/client-detail-header';
import { getClientTabs } from '@/components/clients/client-tabs';
import { useTenanciesModuleEnabled } from '@/providers/port-provider';
import { useRealtimeInvalidation } from '@/hooks/use-realtime-invalidation';
import { useBreadcrumbHint } from '@/hooks/use-breadcrumb-hint';
import { apiFetch } from '@/lib/api/client';
import type { Address } from '@/components/shared/addresses-editor';
interface ClientData {
id: string;
portId: string;
fullName: string;
nationalityIso: string | null;
preferredContactMethod: string | null;
preferredLanguage: string | null;
timezone: string | null;
source: string | null;
sourceDetails: string | null;
archivedAt: string | null;
clientPortalEnabled: boolean;
createdAt: string;
updatedAt: string;
contacts: Array<{
id: string;
channel: string;
value: string;
valueE164: string | null;
valueCountry: string | null;
label: string | null;
isPrimary: boolean;
notes: string | null;
}>;
tags: Array<{
id: string;
name: string;
color: string;
}>;
yachts: Array<{
id: string;
name: string;
hullNumber: string | null;
registration: string | null;
lengthFt: string | null;
widthFt: string | null;
status: string;
}>;
companies: Array<{
membershipId: string;
role: string;
isPrimary: boolean;
startDate: string | Date;
company: {
id: string;
name: string;
legalName: string | null;
status: string;
};
}>;
activeTenancies: Array<{
id: string;
berthId: string;
yachtId: string;
startDate: string | Date;
tenureType: string;
status: string;
}>;
addresses: Address[];
}
interface ClientDetailProps {
clientId: string;
currentUserId?: string;
}
export function ClientDetail({ clientId, currentUserId }: ClientDetailProps) {
const params = useParams<{ portSlug: string }>();
const portSlug = params?.portSlug ?? '';
const { data, isLoading, error } = useQuery<ClientData>({
queryKey: ['clients', clientId],
queryFn: () =>
apiFetch<{ data: ClientData }>(`/api/v1/clients/${clientId}`).then((r) => r.data),
retry: (failureCount, err) => {
const status = (err as { status?: number } | null | undefined)?.status;
if (status === 404 || status === 403) return false;
return failureCount < 2;
},
});
const { setChrome } = useMobileChrome();
const titleForChrome: string | null = data?.fullName ?? null;
useEffect(() => {
setChrome({ title: titleForChrome, showBackButton: true });
return () => setChrome({ title: null, showBackButton: false });
}, [titleForChrome, setChrome]);
// Topbar breadcrumb hint: replaces "Clients <uuid>" with
// "Clients Mary Smith". Hint clears on unmount.
useBreadcrumbHint(data ? { parents: [], current: data.fullName } : null);
useRealtimeInvalidation({
'client:updated': [['clients', clientId]],
'client:archived': [['clients', clientId]],
'client:restored': [['clients', clientId]],
'yacht:ownership_transferred': [['clients', clientId]],
'company_membership:added': [['clients', clientId]],
'company_membership:ended': [['clients', clientId]],
'berth_tenancy:activated': [['clients', clientId]],
'berth_tenancy:ended': [['clients', clientId]],
'berth_tenancy:cancelled': [['clients', clientId]],
});
const tenanciesModuleEnabled = useTenanciesModuleEnabled();
if (error && !isLoading) {
const status = (error as { status?: number } | null | undefined)?.status;
return (
<DetailNotFound
entity="client"
backHref={`/${portSlug}/clients`}
backLabel="Back to clients"
status={status}
/>
);
}
const tabs = data
? getClientTabs({ clientId, currentUserId, client: data, tenanciesModuleEnabled })
: [];
return (
<DetailLayout
header={data ? <ClientDetailHeader client={data} /> : null}
tabs={tabs}
defaultTab="overview"
isLoading={isLoading}
/>
);
}