feat(assistant): a card framework every addon package plugs into

This commit is contained in:
Maurice
2026-10-22 12:14:24 +02:00
parent d2a2728cde
commit 3967ca2790
514 changed files with 9868 additions and 1274 deletions
@@ -0,0 +1,15 @@
import type { PlannerSearchParams } from '../plannerTypes';
import type { useTourWorkspace } from '../useTourWorkspace';
import type { useTripPlanner } from '../useTripPlanner';
/**
* What a part of TREK's deep link into the planner gets: the planner, the
* tour workspace and the URL's query, so a link the chat opened (`?tour=`,
* `?booking=`, `?stop=`) can be taken once and dropped.
*/
export interface DeepLinkProps {
planner: ReturnType<typeof useTripPlanner>;
workspace: ReturnType<typeof useTourWorkspace>;
searchParams: PlannerSearchParams[0];
setSearchParams: PlannerSearchParams[1];
}
@@ -0,0 +1,9 @@
import type { DeepLinkProps } from './deepLinkTypes';
/**
* Where the bookings open a booking the chat points at. Nothing yet: the booking cards
* fill it in, and until then the planner keeps its own links.
*/
export function useBookingDeepLink(_links: DeepLinkProps): void {
// Filled in by the booking cards.
}
@@ -0,0 +1,9 @@
import type { DeepLinkProps } from './deepLinkTypes';
/**
* Where the road trip opens a stop or a day the chat points at. Nothing yet: the road trip cards
* fill it in, and until then the planner keeps its own links.
*/
export function useRoadtripDeepLink(_links: DeepLinkProps): void {
// Filled in by the road trip cards.
}
@@ -0,0 +1,9 @@
import type { DeepLinkProps } from './deepLinkTypes';
/**
* Where the tours open a tour the chat points at (`?tour=`). Nothing yet: the tour cards
* fill it in, and until then the planner keeps its own links.
*/
export function useTourDeepLink(_links: DeepLinkProps): void {
// Filled in by the tour cards.
}
@@ -3,7 +3,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
import { useAssistantStore } from '../../store/assistantStore';
import type { Day } from '../../types';
import { useAssistantDeepLink } from './useAssistantDeepLink';
import { useAssistantDeepLink, useAssistantTabLink } from './useAssistantDeepLink';
const day = (id: number, tripId = 5) => ({ id, trip_id: tripId }) as Day;
@@ -59,3 +59,33 @@ describe('useAssistantDeepLink', () => {
expect(useAssistantStore.getState().window.open).toBe(false);
});
});
describe('useAssistantTabLink', () => {
function setupTab(query: string, activeTab: string) {
const searchParams = new URLSearchParams(query);
const setSearchParams = vi.fn((update: (params: URLSearchParams) => URLSearchParams) => update(searchParams));
const selectTab = vi.fn();
renderHook(() =>
useAssistantTabLink({ searchParams, setSearchParams: setSearchParams as never, activeTab, selectTab })
);
return { searchParams, setSearchParams, selectTab };
}
it('FE-PLANNER-ASSISTLINK-005: a ?tab= the chat opens while the planner is open switches the tab, once', () => {
const { searchParams, selectTab } = setupTab('tab=finanzplan&expense=7', 'plan');
expect(selectTab).toHaveBeenCalledWith('finanzplan');
expect(searchParams.has('tab')).toBe(false);
// What the tab reads beside it stays for the tab to take.
expect(searchParams.get('expense')).toBe('7');
});
it('FE-PLANNER-ASSISTLINK-006: the tab on screen is not switched again; a tab it cannot open is left alone', () => {
const same = setupTab('tab=finanzplan', 'finanzplan');
expect(same.selectTab).not.toHaveBeenCalled();
expect(same.searchParams.has('tab')).toBe(false);
const unknown = setupTab('tab=nowhere', 'plan');
expect(unknown.selectTab).not.toHaveBeenCalled();
expect(unknown.setSearchParams).not.toHaveBeenCalled();
});
});
@@ -1,5 +1,6 @@
import { useEffect } from 'react';
import { isDeepLinkableTripTab } from '../../constants/tripTabs';
import { useAssistantStore } from '../../store/assistantStore';
import type { Day } from '../../types';
import type { PlannerSearchParams } from './plannerTypes';
@@ -50,3 +51,41 @@ export function useAssistantDeepLink({
);
}, [openRequested, requestedDay, known, dayDone, selectDay, setSearchParams]);
}
interface AssistantTabLinkOptions {
searchParams: PlannerSearchParams[0];
setSearchParams: PlannerSearchParams[1];
/** The tab on screen. */
activeTab: string;
/** Switches the planner's tab the way its tab bar does. */
selectTab: (tabId: string) => void;
}
/**
* `?tab=<id>` while the planner is already open: what a card in the chat
* opens in another tab of this trip (`?tab=finanzplan&expense=7`). The
* planner reads the tab once when it opens; this takes a later one live,
* once, and then drops it from the URL. What the tab reads beside it stays
* for that tab to take.
*/
export function useAssistantTabLink({
searchParams,
setSearchParams,
activeTab,
selectTab,
}: AssistantTabLinkOptions): void {
const requested = searchParams.get('tab');
const linkable = requested !== null && isDeepLinkableTripTab(requested);
useEffect(() => {
if (!linkable || requested === null) return;
if (requested !== activeTab) selectTab(requested);
setSearchParams(
(params) => {
params.delete('tab');
return params;
},
{ replace: true }
);
}, [linkable, requested, activeTab, selectTab, setSearchParams]);
}
@@ -0,0 +1,45 @@
/**
* FE-PLANNER-ENTITYLINK-001 to FE-PLANNER-ENTITYLINK-002
*
* The links the chat opens the planner with while it is on screen: another
* tab of the trip switches there and leaves the URL, and the parts' own links
* (tours, bookings, road trip) take nothing yet.
*/
import { renderHook } from '@testing-library/react';
import type { ReactNode } from 'react';
import { MemoryRouter, useLocation } from 'react-router';
import { describe, expect, it, vi } from 'vitest';
import { useAssistantEntityLinks } from './useAssistantEntityLinks';
type Options = Parameters<typeof useAssistantEntityLinks>[0];
function setup(path: string) {
const planner = { activeTab: 'plan', handleTabChange: vi.fn() } as unknown as Options['planner'];
const workspace = {} as Options['workspace'];
const wrapper = ({ children }: { children: ReactNode }) => (
<MemoryRouter initialEntries={[path]}>{children}</MemoryRouter>
);
const hook = renderHook(
() => {
useAssistantEntityLinks({ planner, workspace });
return useLocation();
},
{ wrapper }
);
return { planner, hook };
}
describe('useAssistantEntityLinks', () => {
it('FE-PLANNER-ENTITYLINK-001: another tab switches there and leaves the URL, what it reads stays', () => {
const { planner, hook } = setup('/trips/5?tab=finanzplan&expense=7');
expect(planner.handleTabChange).toHaveBeenCalledWith('finanzplan');
expect(hook.result.current.search).toBe('?expense=7');
});
it("FE-PLANNER-ENTITYLINK-002: the parts' own links take nothing yet, and nothing switches without a tab", () => {
const { planner, hook } = setup('/trips/5?tour=12');
expect(planner.handleTabChange).not.toHaveBeenCalled();
expect(hook.result.current.search).toBe('?tour=12');
});
});
@@ -0,0 +1,33 @@
import { useSearchParams } from 'react-router';
import { useBookingDeepLink } from './deepLinks/useBookingDeepLink';
import { useRoadtripDeepLink } from './deepLinks/useRoadtripDeepLink';
import { useTourDeepLink } from './deepLinks/useTourDeepLink';
import { useAssistantTabLink } from './useAssistantDeepLink';
import type { useTourWorkspace } from './useTourWorkspace';
import type { useTripPlanner } from './useTripPlanner';
interface AssistantEntityLinkOptions {
planner: ReturnType<typeof useTripPlanner>;
workspace: ReturnType<typeof useTourWorkspace>;
}
/**
* The links the chat opens the planner with while it is already on screen:
* another tab of the trip (`?tab=`), and what each part of TREK opens in its
* own tab (a tour, a booking, a stop of the road trip). Each is taken once
* and then leaves the URL.
*/
export function useAssistantEntityLinks({ planner, workspace }: AssistantEntityLinkOptions): void {
const [searchParams, setSearchParams] = useSearchParams();
useAssistantTabLink({
searchParams,
setSearchParams,
activeTab: planner.activeTab,
selectTab: planner.handleTabChange,
});
const links = { planner, workspace, searchParams, setSearchParams };
useTourDeepLink(links);
useBookingDeepLink(links);
useRoadtripDeepLink(links);
}
@@ -11,6 +11,7 @@ import { usePlanMapViewport } from '../../hooks/usePlanMapViewport'
import { usePaddleObstacleStore } from '../../store/paddleObstacleStore'
import { useHighlightedRoute } from '../../components/Tours/bike/useHighlightedRoute'
import { useLegVariantLines } from '../../components/Tours/planner/legVariantPaint'
import { useAssistantEntityLinks } from './useAssistantEntityLinks'
import { useMapLocationSearch } from './useMapLocationSearch'
import { usePlannerFocusPoints } from './usePlannerFocusPoints'
import { useTripPlanner } from './useTripPlanner'
@@ -33,8 +34,11 @@ export function useTripPlannerPage() {
const canPlaceEdit = can('place_edit', trip)
const canDayEdit = can('day_edit', trip)
const workspace = useTourWorkspace(planner, canPlaceEdit, canDayEdit)
const { tourPlanner, viewImportedTour, tourGpxImport, wandererBrowser, tourConversion, recordedTrack, trackPaddlePlan, paddleChecklist, skiClassification, tourDetailActions } =
useTourWorkspace(planner, canPlaceEdit, canDayEdit)
workspace
// What the chat opens in another tab of this trip, and in each part's own tab.
useAssistantEntityLinks({ planner, workspace })
const tourDetailOpenerRef = useRef<{ placeId: number; element: HTMLElement } | null>(null)
const { focusPoints, lastPlanMapViewportRef } = usePlannerFocusPoints(activeTab, places, tourPlanner, wandererBrowser)