Files
TREK/client/src/components/Assistant/useAssistantTripPicker.ts
T
Maurice b64da26651 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-22 21:30:12 +00:00

73 lines
2.3 KiB
TypeScript

import { useEffect, useState } from 'react';
import { tripRepo } from '../../repo/tripRepo';
import { localIsoDate } from '../../utils/localDate';
export interface PickerTrip {
id: number;
title: string;
/** The trip runs today, or is the next to start. */
badge: 'now' | 'next' | null;
}
type DatedTrip = { id: number; title: string; start_date?: string | null; end_date?: string | null };
const MAX_TRIPS = 8;
/**
* The trips worth asking about first: the one running today, then the ones
* still to come (soonest first), then the ones behind (latest first). The
* running trip is marked "now" and the first one to come "next".
*/
export function rankPickerTrips(trips: readonly DatedTrip[], today: string): PickerTrip[] {
const phase = (t: DatedTrip) => {
if (t.start_date && t.start_date <= today && (!t.end_date || today <= t.end_date)) return 0;
if (!t.start_date || t.start_date > today) return 1;
return 2;
};
const ranked = [...trips].sort((a, b) => {
const pa = phase(a);
const pb = phase(b);
if (pa !== pb) return pa - pb;
const sa = a.start_date ?? '';
const sb = b.start_date ?? '';
// Undated trips to come go last among them; the past runs from the latest back.
if (pa === 1) return (sa === '' ? 1 : 0) - (sb === '' ? 1 : 0) || sa.localeCompare(sb);
return sb.localeCompare(sa);
});
let nextGiven = false;
return ranked.slice(0, MAX_TRIPS).map((t) => {
const p = phase(t);
let badge: PickerTrip['badge'] = null;
if (p === 0) badge = 'now';
else if (p === 1 && t.start_date && !nextGiven) {
badge = 'next';
nextGiven = true;
}
return { id: t.id, title: t.title, badge };
});
}
/** The trips the picker offers, loaded when it is shown; null while loading. */
export function useAssistantTripPicker(): PickerTrip[] | null {
const [trips, setTrips] = useState<PickerTrip[] | null>(null);
useEffect(() => {
let cancelled = false;
tripRepo
.list()
.then(({ trips: list }) => {
if (!cancelled) setTrips(rankPickerTrips(list, localIsoDate()));
})
.catch(() => {
// Nothing to pick from is what the picker shows; the window stays usable to close.
if (!cancelled) setTrips([]);
});
return () => {
cancelled = true;
};
}, []);
return trips;
}