Files
TREK/client/src/components/Assistant/useAssistantFocus.ts
T
Maurice 214984f398 feat(assistant): trip assistant chat with your own AI connection
Adds an in-app assistant that reads and plans the open trip. Users connect
their own provider (Sign in with ChatGPT, or an Anthropic, OpenAI, Gemini or
OpenRouter key) or use the one the admin set up, including a custom
OpenAI-compatible server. It is an addon and only shows while a connection
is usable.

The chat runs on the existing MCP tools, pinned to the trip. Every change is
held as a change card the user applies or discards, and applied changes can
be undone. On the desktop it is a floating window that can be moved and
resized; on the phone it is fullscreen. Search results show as place cards
that can go onto a day, weather lookups as a strip, proposed places as
ghost markers on the map with a before/after switch, and an answer can be
shared into the trip chat. Booking files can be dropped in and are read
into change cards.
2026-10-21 22:29:56 +02:00

76 lines
2.9 KiB
TypeScript

import { useEffect, useMemo } from 'react';
import { useTranslation } from '../../i18n';
import { chatOf, useAssistantStore } from '../../store/assistantStore';
import { useTripStore } from '../../store/tripStore';
import type { Place } from '../../types';
import { dayLabel } from '../../utils/dayLabel';
/**
* What the next question is about: the place the user asked from, or else the
* day selected in the planner. The chip above the composer names it, and the
* suggestions turn to it.
*/
/**
* The selected day becomes the focus by itself, quietly, and follows the
* selection. A removed chip stays away until another day is selected, and a
* place asked about is never replaced by the day. Run once per chat.
*/
export function useFollowSelectedDay(tripId: number): void {
const setFocus = useAssistantStore((s) => s.setFocus);
// The planner's own store holds the trip it shows; for any other trip there is no selection to follow.
const here = useTripStore((s) => s.trip?.id === tripId);
const selectedDayId = useTripStore((s) => s.selectedDayId);
useEffect(() => {
if (!here) return;
const current = chatOf(useAssistantStore.getState(), tripId).focus;
if (current?.placeIds?.length) return;
if (selectedDayId === null) {
if (current) setFocus(tripId, null);
} else if (current?.dayId !== selectedDayId) {
setFocus(tripId, { dayId: selectedDayId });
}
}, [here, selectedDayId, tripId, setFocus]);
}
interface AssistantFocusView {
/** The place or day named on the chip; null when nothing is in focus or it is not at hand. */
label: string | null;
kind: 'place' | 'day' | null;
/** The place on a place chip, for its picture. */
place: Place | null;
clear: () => void;
}
export function useAssistantFocus(tripId: number): AssistantFocusView {
const { t, locale } = useTranslation();
const focus = useAssistantStore((s) => chatOf(s, tripId).focus);
const setFocus = useAssistantStore((s) => s.setFocus);
const here = useTripStore((s) => s.trip?.id === tripId);
const days = useTripStore((s) => s.days);
const places = useTripStore((s) => s.places);
const named = useMemo((): { label: string; kind: 'place' | 'day'; place: Place | null } | null => {
if (!here || !focus) return null;
const placeIds = focus.placeIds ?? [];
if (placeIds.length > 0) {
const first = places.find((p) => p.id === placeIds[0]);
if (!first) return null;
const label = placeIds.length > 1 ? `${first.name} +${placeIds.length - 1}` : first.name;
return { kind: 'place', label, place: first };
}
const index = days.findIndex((d) => d.id === focus.dayId);
const day = days[index];
return day ? { kind: 'day', label: dayLabel(day, index, t, locale), place: null } : null;
}, [here, focus, places, days, t, locale]);
return {
label: named?.label ?? null,
kind: named?.kind ?? null,
place: named?.place ?? null,
clear: () => setFocus(tripId, null),
};
}