feat(notes): aggregate-on-read for yachts, companies, residential clients
Extends the listForClientAggregated pattern to three new symmetric
helpers in notes.service so the Notes tab on yacht / company /
residential-client detail pages surfaces the full timeline (own notes
+ related-entity notes) instead of just rows on the entity itself.
- listForYachtAggregated: yacht own + owner client (when ownership
is polymorphic 'client') + linked interest notes.
- listForCompanyAggregated: company own + company-owned yacht notes
+ interests linked to those yachts.
- listForResidentialClientAggregated: own + residential interests.
Generalises NotesList so aggregate=true works for all four entity
types via SELF_SOURCE / AGGREGATABLE / SOURCE_BADGE_CLASS / SOURCE_LABEL
maps; cross-source notes render with a coloured chip and are read-only
(rep edits on the source entity's page so the right timeline records
the change).
Wires ?aggregate=true into the yacht / company / residential-client
notes routes; the yacht / company / residential-client tabs now pass
aggregate. Drops the legacy single-textarea spots on the companies
overview tab and the residential-interest "Initial brief" row in
favour of the threaded feed.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,14 @@ import { Textarea } from '@/components/ui/textarea';
|
||||
import { Avatar, AvatarFallback } from '@/components/ui/avatar';
|
||||
import { apiFetch } from '@/lib/api/client';
|
||||
|
||||
type NoteSource =
|
||||
| 'client'
|
||||
| 'interest'
|
||||
| 'yacht'
|
||||
| 'company'
|
||||
| 'residential_client'
|
||||
| 'residential_interest';
|
||||
|
||||
interface Note {
|
||||
id: string;
|
||||
content: string;
|
||||
@@ -19,26 +27,72 @@ interface Note {
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
/** Aggregated-mode only: which child entity this note came from. */
|
||||
source?: 'client' | 'interest' | 'yacht';
|
||||
source?: NoteSource;
|
||||
sourceId?: string;
|
||||
sourceLabel?: string;
|
||||
}
|
||||
|
||||
type NotesEntityType =
|
||||
| 'clients'
|
||||
| 'interests'
|
||||
| 'yachts'
|
||||
| 'companies'
|
||||
| 'residential_clients'
|
||||
| 'residential_interests';
|
||||
|
||||
/** Maps the entity-type the list is rendered for to the `source` value
|
||||
* the aggregator uses when a note came from THAT entity itself
|
||||
* (vs. a related entity). Used to decide whether a note is editable
|
||||
* in-place or read-only with an "Open source" affordance. */
|
||||
const SELF_SOURCE: Record<NotesEntityType, NoteSource | null> = {
|
||||
clients: 'client',
|
||||
yachts: 'yacht',
|
||||
companies: 'company',
|
||||
residential_clients: 'residential_client',
|
||||
// Aggregate-mode is only meaningful for the entities above. Interests
|
||||
// and residential_interests are leaf nodes — there's nothing to roll
|
||||
// up to them.
|
||||
interests: null,
|
||||
residential_interests: null,
|
||||
};
|
||||
|
||||
const AGGREGATABLE: ReadonlySet<NotesEntityType> = new Set([
|
||||
'clients',
|
||||
'yachts',
|
||||
'companies',
|
||||
'residential_clients',
|
||||
]);
|
||||
|
||||
const SOURCE_BADGE_CLASS: Record<NoteSource, string> = {
|
||||
client: 'bg-violet-100 text-violet-900',
|
||||
interest: 'bg-blue-100 text-blue-900',
|
||||
yacht: 'bg-emerald-100 text-emerald-900',
|
||||
company: 'bg-amber-100 text-amber-900',
|
||||
residential_client: 'bg-violet-100 text-violet-900',
|
||||
residential_interest: 'bg-blue-100 text-blue-900',
|
||||
};
|
||||
|
||||
const SOURCE_LABEL: Record<NoteSource, string> = {
|
||||
client: 'Client',
|
||||
interest: 'Interest',
|
||||
yacht: 'Yacht',
|
||||
company: 'Company',
|
||||
residential_client: 'Resident',
|
||||
residential_interest: 'Inquiry',
|
||||
};
|
||||
|
||||
interface NotesListProps {
|
||||
entityType:
|
||||
| 'clients'
|
||||
| 'interests'
|
||||
| 'yachts'
|
||||
| 'companies'
|
||||
| 'residential_clients'
|
||||
| 'residential_interests';
|
||||
entityType: NotesEntityType;
|
||||
entityId: string;
|
||||
currentUserId?: string;
|
||||
/**
|
||||
* When `entityType='clients'` and this is true, the list aggregates
|
||||
* notes from the client + their interests + directly-owned yachts.
|
||||
* Notes from interests/yachts render with a source chip and are
|
||||
* read-only here (edit them on the source entity's page).
|
||||
* Aggregate-on-read: union the entity's own notes with notes from
|
||||
* related entities (interests, owned yachts / company yachts, owner
|
||||
* client). Cross-source notes render with a source chip and are
|
||||
* read-only here — open the source entity's page to edit.
|
||||
*
|
||||
* Supported for entityType in {clients, yachts, companies,
|
||||
* residential_clients}. Ignored for interests / residential_interests.
|
||||
*/
|
||||
aggregate?: boolean;
|
||||
}
|
||||
@@ -48,10 +102,17 @@ const NOTE_EDIT_WINDOW_MS = 15 * 60 * 1000; // 15 minutes
|
||||
/** Sort by source then chronologically inside each source.
|
||||
* Used by the aggregated view's "Group by source" toggle. */
|
||||
function sortByGroup(notes: Note[]): Note[] {
|
||||
const sourceOrder: Record<string, number> = { client: 0, interest: 1, yacht: 2 };
|
||||
const sourceOrder: Record<string, number> = {
|
||||
client: 0,
|
||||
company: 1,
|
||||
yacht: 2,
|
||||
interest: 3,
|
||||
residential_client: 0,
|
||||
residential_interest: 1,
|
||||
};
|
||||
return [...notes].sort((a, b) => {
|
||||
const aRank = sourceOrder[a.source ?? 'client'] ?? 99;
|
||||
const bRank = sourceOrder[b.source ?? 'client'] ?? 99;
|
||||
const aRank = sourceOrder[a.source ?? ''] ?? 99;
|
||||
const bRank = sourceOrder[b.source ?? ''] ?? 99;
|
||||
if (aRank !== bRank) return aRank - bRank;
|
||||
const aLabel = a.sourceLabel ?? '';
|
||||
const bLabel = b.sourceLabel ?? '';
|
||||
@@ -67,7 +128,7 @@ export function NotesList({ entityType, entityId, currentUserId, aggregate }: No
|
||||
const [editContent, setEditContent] = useState('');
|
||||
const [groupBySource, setGroupBySource] = useState(false);
|
||||
|
||||
const aggregateOn = aggregate && entityType === 'clients';
|
||||
const aggregateOn = !!aggregate && AGGREGATABLE.has(entityType);
|
||||
const baseEndpoint = `/api/v1/${entityType}/${entityId}/notes`;
|
||||
const listEndpoint = aggregateOn ? `${baseEndpoint}?aggregate=true` : baseEndpoint;
|
||||
const queryKey = [entityType, entityId, 'notes', aggregateOn ? 'aggregated' : 'own'];
|
||||
@@ -107,10 +168,12 @@ export function NotesList({ entityType, entityId, currentUserId, aggregate }: No
|
||||
function canEdit(note: Note): boolean {
|
||||
if (note.authorId !== currentUserId) return false;
|
||||
if (note.isLocked) return false;
|
||||
// Aggregated view: only client-level notes are editable in-place.
|
||||
// Notes from interests/yachts must be edited on their own page so
|
||||
// the right entity timeline records the change.
|
||||
if (aggregateOn && note.source && note.source !== 'client') return false;
|
||||
// Aggregated view: only notes from THIS entity itself are editable
|
||||
// in-place. Notes pulled in from related entities (e.g. interests
|
||||
// surfaced under a client) must be edited on the source page so the
|
||||
// owning entity's timeline records the change.
|
||||
const selfSource = SELF_SOURCE[entityType];
|
||||
if (aggregateOn && note.source && note.source !== selfSource) return false;
|
||||
const elapsed = Date.now() - new Date(note.createdAt).getTime();
|
||||
return elapsed < NOTE_EDIT_WINDOW_MS;
|
||||
}
|
||||
@@ -192,18 +255,17 @@ export function NotesList({ entityType, entityId, currentUserId, aggregate }: No
|
||||
<span className="text-muted-foreground">
|
||||
{formatDistanceToNow(new Date(note.createdAt), { addSuffix: true })}
|
||||
</span>
|
||||
{aggregateOn && note.source && note.source !== 'client' && note.sourceLabel && (
|
||||
<span
|
||||
className={
|
||||
note.source === 'interest'
|
||||
? 'inline-flex items-center rounded-full bg-blue-100 text-blue-900 px-1.5 py-0.5 text-[10px] font-medium'
|
||||
: 'inline-flex items-center rounded-full bg-emerald-100 text-emerald-900 px-1.5 py-0.5 text-[10px] font-medium'
|
||||
}
|
||||
title={`From ${note.source}`}
|
||||
>
|
||||
{note.source === 'interest' ? 'Interest' : 'Yacht'} · {note.sourceLabel}
|
||||
</span>
|
||||
)}
|
||||
{aggregateOn &&
|
||||
note.source &&
|
||||
note.source !== SELF_SOURCE[entityType] &&
|
||||
note.sourceLabel && (
|
||||
<span
|
||||
className={`inline-flex items-center rounded-full px-1.5 py-0.5 text-[10px] font-medium ${SOURCE_BADGE_CLASS[note.source]}`}
|
||||
title={`From ${note.source}`}
|
||||
>
|
||||
{SOURCE_LABEL[note.source]} · {note.sourceLabel}
|
||||
</span>
|
||||
)}
|
||||
{note.isLocked && <Lock className="h-3 w-3 text-muted-foreground" />}
|
||||
{canEdit(note) && (
|
||||
<span className="text-xs text-muted-foreground">{getTimeRemaining(note)}</span>
|
||||
|
||||
Reference in New Issue
Block a user