import type { AssistantChangePreview, AssistantMessage, AssistantPlanEdits } from '@trek/shared'; import { ArrowRightLeft, FileText, MapPin, Minus, type LucideIcon } from 'lucide-react'; import { useMemo } from 'react'; import { useShallow } from 'zustand/react/shallow'; import { useTranslation } from '../../i18n'; import { chatOf, useAssistantStore } from '../../store/assistantStore'; import { useSettingsStore } from '../../store/settingsStore'; import { useTripStore } from '../../store/tripStore'; import type { Place } from '../../types'; import { formatTime } from '../../utils/formatters'; import { TimePill } from '../Planner/planParts'; import { getCategoryIcon } from '../shared/categoryIcons'; import { isDayNoteProposal, isVisitProposal, planField, proposalNote } from './assistantPlanFieldsModel'; /** What a waiting change does to one day, as a row of the preview. */ interface PreviewRow { toolCallId: string; kind: 'add' | 'in' | 'remove' | 'out' | 'note'; name: string; categoryId: number | null; categoryName?: string; start?: string; end?: string; detail?: string; } /** The changes no one has decided on yet, by their call. */ function waitingCalls(messages: readonly AssistantMessage[]): Set { const ids = new Set(); for (const m of messages) { for (const p of m.parts) { if (p.type === 'tool-call' && p.approval && !p.approval.resolution && p.approval.approved === undefined) { ids.add(p.toolCallId); } } } return ids; } function rowOf( preview: AssistantChangePreview, dayId: number, places: readonly Place[], edits: AssistantPlanEdits | undefined ): PreviewRow | null { const { toolCallId, effect, subject } = preview; if (preview.dayId === dayId && isDayNoteProposal(preview)) { return { toolCallId, kind: 'note', name: edits?.noteTitle || planField(preview, 'title') || subject, categoryId: null, detail: proposalNote(preview, edits) ?? undefined, }; } const kind = visitKind(preview, dayId); if (!kind) return null; const place = places.find((p) => p.id === effect?.placeId); return { toolCallId, kind, ...visitSubject(preview, place, edits), ...visitDetails(preview, place, edits) }; } function visitSubject( preview: AssistantChangePreview, place: Place | undefined, edits: AssistantPlanEdits | undefined ) { const proposed = preview.effect?.place; const categoryId = edits?.categoryId; return { name: edits?.place?.name ?? proposed?.name ?? place?.name ?? preview.subject, categoryId: categoryId === undefined ? (proposed?.categoryId ?? place?.category_id ?? null) : categoryId, categoryName: categoryId === undefined ? planField(preview, 'category') : undefined, }; } function editedText(value: string | null | undefined, original: string | undefined): string | undefined { return value === undefined ? original : (value ?? undefined); } function visitDetails( preview: AssistantChangePreview, place: Place | undefined, edits: AssistantPlanEdits | undefined ) { return { start: editedText(edits?.startTime, planField(preview, 'time')), end: editedText(edits?.endTime, planField(preview, 'endTime')), detail: editedText(edits?.notes, planField(preview, 'notes')) || editedText(edits?.place?.description, planField(preview, 'description')) || place?.description || editedText(edits?.place?.address, planField(preview, 'address')) || place?.address || undefined, }; } function visitKind(preview: AssistantChangePreview, dayId: number): PreviewRow['kind'] | null { const { effect } = preview; if (!effect) return preview.dayId === dayId && isVisitProposal(preview) ? 'add' : null; if (effect.dayId === dayId) { if (effect.kind === 'remove') return 'remove'; if (effect.kind === 'move') return 'in'; return 'add'; } return effect.kind === 'move' && effect.fromDayId === dayId ? 'out' : null; } const LOOK: Record = { add: { icon: MapPin, row: 'border-dashed border-accent bg-accent-subtle', name: 'text-content', badgeKey: 'assistant.preview.suggested', }, in: { icon: ArrowRightLeft, row: 'border-dashed border-accent bg-accent-subtle', name: 'text-content', badgeKey: 'assistant.preview.movedHere', }, remove: { icon: Minus, row: 'border-dashed border-edge', name: 'text-content-faint line-through', badgeKey: 'assistant.preview.removed', }, out: { icon: ArrowRightLeft, row: 'border-dashed border-edge', name: 'text-content-faint line-through', badgeKey: 'assistant.preview.movedAway', }, note: { icon: FileText, row: 'border-dashed border-accent bg-accent-subtle', name: 'text-content', badgeKey: 'assistant.preview.suggested', }, }; /** * The assistant's waiting changes inside the day they touch, in the planner's * own day list: a place it would add as a dashed row, one it would take off * struck through, a move on both of its days. Resting on a row lights the * change's pin and card; a click brings the chat forward to decide. Nothing * here writes; the change card stays the one place a change is applied. */ export default function AssistantDayPreview({ tripId, dayId }: { tripId: number; dayId: number }) { const { t, locale } = useTranslation(); const timeFormat = useSettingsStore((s) => s.settings.time_format) || '24h'; const messages = useAssistantStore((s) => chatOf(s, tripId).messages); const savedEdits = useAssistantStore((s) => chatOf(s, tripId).planEdits); const liveEdit = useAssistantStore((s) => chatOf(s, tripId).planEditPreview); const choices = useAssistantStore((s) => chatOf(s, tripId).changeChoices); const hovered = useAssistantStore((s) => (s.mapLink?.tripId === tripId ? s.mapLink.hoveredId : null)); const { setMapHover, open } = useAssistantStore(useShallow((s) => ({ setMapHover: s.setMapHover, open: s.open }))); const places = useTripStore((s) => s.places); const categories = useTripStore((s) => s.categories); const rows = useMemo(() => { const waiting = waitingCalls(messages); if (waiting.size === 0) return []; return messages.flatMap((message) => message.parts.flatMap((part) => { if (part.type !== 'data-trek-change' || !waiting.delete(part.data.toolCallId)) return []; const id = part.data.toolCallId; if (choices[id] === false) return []; const edits = liveEdit?.toolCallId === id ? liveEdit.edits : savedEdits[id]; const row = rowOf(part.data, dayId, places, edits); return row ? [row] : []; }) ); }, [messages, places, dayId, savedEdits, liveEdit, choices]); if (rows.length === 0) return null; return (
    {rows.map((row, i) => { const look = LOOK[row.kind]; const category = categories.find((entry) => row.categoryId ? entry.id === row.categoryId : entry.name === row.categoryName ); const Icon = category ? getCategoryIcon(category.icon) : look.icon; const time = [row.start, row.end] .filter((value): value is string => !!value) .map((value) => formatTime(value, locale, timeFormat)) .join(' – '); const lit = hovered === row.toolCallId; return (
  • ); })}
); }