Files
TREK/client/src/components/Assistant/AssistantDayPreview.tsx
T
Maurice 86e98fde65 fix(ci): repair trip assistant checks
Fix the mobile dashboard import and align the assistant UI with client layer and lint rules. Document the OpenRouter setting, share the reservation status schema, and update coverage for external POI identifiers and the empty costs view.
2026-12-02 22:56:54 +01:00

238 lines
9.2 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.
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<string> {
const ids = new Set<string>();
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<PreviewRow['kind'], { icon: LucideIcon; row: string; name: string; badgeKey: string }> = {
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 (
<ul className="m-0 flex list-none flex-col gap-1 px-3 pb-2 pt-1" aria-label={t('assistant.preview.title')}>
{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 (
<li
key={`${row.toolCallId}-${row.kind}`}
className="trek-assistant-enter"
style={{ animationDelay: `${i * 50}ms` }}
>
<button
type="button"
onClick={open}
onMouseEnter={() => setMapHover(tripId, row.toolCallId, 'card')}
onMouseLeave={() => setMapHover(tripId, null, 'card')}
className={`flex w-full items-center gap-2 rounded-xl border px-2.5 py-1.5 text-start transition-all ${look.row} ${
lit ? 'shadow-md ring-2 ring-accent' : 'hover:shadow-sm'
}`}
>
<span
className="grid h-8 w-8 flex-shrink-0 place-items-center rounded-full bg-surface-card text-content-muted shadow-sm"
style={category?.color ? { backgroundColor: category.color, color: 'white' } : undefined}
>
<Icon size={14} aria-hidden="true" />
{(category?.name || row.categoryName) && (
<span className="sr-only">{category?.name ?? row.categoryName}</span>
)}
</span>
<span className="min-w-0 flex-1">
<bdi className={`block truncate text-body font-medium ${look.name}`}>{row.name}</bdi>
{time && (
<span className="mt-0.5 flex">
<TimePill quiet>{time}</TimePill>
</span>
)}
{row.detail && (
<bdi className="mt-0.5 line-clamp-2 break-words text-caption text-content-muted">{row.detail}</bdi>
)}
</span>
<span
className={row.kind === 'add' || row.kind === 'note' ? 'sr-only' : 'flex-shrink-0 text-content-muted'}
>
{t(look.badgeKey)}
</span>
</button>
</li>
);
})}
</ul>
);
}