import { Check, CheckCheck, ChevronDown, ListChecks, LoaderCircle, MapPin, Undo2 } from 'lucide-react'; import { type ReactNode, useEffect, useMemo, useState } from 'react'; import { useTranslation } from '../../i18n'; import { blockKeyOf } from '../../store/assistantMapModel'; import { chatOf, useAssistantStore } from '../../store/assistantStore'; import { useTripStore } from '../../store/tripStore'; import { DialogButton, NEUTRAL_TINT } from '../shared/DialogShell'; import { useToast } from '../shared/Toast'; import { Tooltip } from '../shared/Tooltip'; import AssistantCompareSwitch from './AssistantCompareSwitch'; import AssistantFitCheck from './AssistantFitCheck'; import AssistantWeekOverview from './AssistantWeekOverview'; import { type ChangeItem, undoLeftMs } from './assistantParts'; import { useAssistantView } from './assistantViewContext'; import { ChangeRows } from './cards/ChangeCardSlots'; import CardHeader from './cards/parts/CardHeader'; import { CardFoot } from './cards/parts/CardSection'; import { ICON_BUTTON } from './cards/parts/assistantTones'; import { useChangeExclusions } from './cards/useChangeExclusions'; import { changePoints, type DayChange } from './dayAfterModel'; import { useWeekDays } from './useWeekDays'; /** * The changes the assistant proposes in one go, as one card in plain words: * what happens to which place, note or item, field by field. Nothing is * written before the user applies it. Each change keeps its own decision, so * when the card holds several, each has a switch and the button applies the * ones left on. Once decided, the card turns into a receipt per change, with * a way to the change on the map and, for a while, undo. * * Every change wears its kind: a tinted tile with the icon of what it touches * and a badge naming the verb, in the tone of what happens (green adds, blue * changes, amber moves, red takes away). Its fields sit as badges holding a * label badge, so a row of them reads at a glance. The rows themselves, and a * change that carries a card of its own, are drawn in cards/ChangeCardSlots. * * While it waits, the card shows the days it touches as they would read * afterwards, and each row lights its pin on the map (and the pin its row). * Once applied, the head offers the plan before and after the block on the * map, and taking the whole block back at once. */ /** The time now, ticking once more when the next undo window closes. */ function useUndoClock(items: ChangeItem[]): number { const [now, setNow] = useState(() => Date.now()); const next = Math.min(...items.map((i) => undoLeftMs(i.applied, now)).filter((ms) => ms > 0)); useEffect(() => { if (!Number.isFinite(next)) return; const timer = window.setTimeout(() => setNow(Date.now()), next + 50); return () => window.clearTimeout(timer); }, [next]); return now; } /** Whether an applied change can still be taken back now. */ function undoableNow(item: ChangeItem, undone: Readonly>, now: number): boolean { return ( item.state === 'applied' && !undone[item.toolCallId] && !!item.preview?.undoable && undoLeftMs(item.applied, now) > 0 ); } /** The band of a card whose changes are all decided: the green of a done booking. */ const DONE_TINT = 'color-mix(in srgb, var(--success) 9%, transparent)'; /** The waiting changes the user left on that touch the day plan. */ function dayChangesOf(items: readonly ChangeItem[], excluded: ReadonlySet): DayChange[] { return items.flatMap((i) => i.state === 'pending' && i.preview?.effect && !excluded.has(i.toolCallId) ? [{ toolCallId: i.toolCallId, effect: i.preview.effect, subject: i.preview.subject }] : [] ); } /** The change card's band: tinted green once everything in it is decided, what it holds and its actions. */ function CardHead({ open, count, actions }: { open: boolean; count: number; actions?: ReactNode }) { const { t } = useTranslation(); const Icon = open ? ListChecks : CheckCheck; return (