diff --git a/client/src/components/Assistant/AssistantWelcome.test.tsx b/client/src/components/Assistant/AssistantWelcome.test.tsx new file mode 100644 index 000000000..e0bf65041 --- /dev/null +++ b/client/src/components/Assistant/AssistantWelcome.test.tsx @@ -0,0 +1,45 @@ +/** + * FE-AI-WELCOME-001 to FE-AI-WELCOME-002 + * + * The empty chat asks what fits the chat it is: a journey's conversation on + * the journal page asks about the journal, the same conversation in TREK + * Studio about the book, and each tells a reader what they may not change. + */ +import { afterEach, describe, expect, it } from 'vitest'; + +import { render, screen } from '../../../tests/helpers/render'; +import { useAssistantStore } from '../../store/assistantStore'; +import AssistantWelcome from './AssistantWelcome'; +import { AssistantViewContext } from './assistantViewContext'; + +const INITIAL = useAssistantStore.getState(); +const JOURNAL_KEY = -12; + +afterEach(() => { + useAssistantStore.setState(INITIAL, true); +}); + +function welcome(canEdit: boolean) { + return render( + + + + ); +} + +describe('AssistantWelcome', () => { + it("FE-AI-WELCOME-001: a journey's chat on its page asks about the journal", () => { + welcome(false); + expect(screen.getByText('What should go into the journal?')).toBeInTheDocument(); + expect(screen.getByText(/You can only read this journal/)).toBeInTheDocument(); + }); + + it('FE-AI-WELCOME-002: the same chat hosted by the Studio asks about the book', () => { + useAssistantStore.setState({ + studioHosts: { [JOURNAL_KEY]: { render: () => undefined, flush: async () => true } }, + }); + welcome(true); + expect(screen.getByText('What should the book look like?')).toBeInTheDocument(); + expect(screen.queryByText(/You can only/)).not.toBeInTheDocument(); + }); +}); diff --git a/client/src/components/Assistant/AssistantWelcome.tsx b/client/src/components/Assistant/AssistantWelcome.tsx index 084742434..eb3bd834d 100644 --- a/client/src/components/Assistant/AssistantWelcome.tsx +++ b/client/src/components/Assistant/AssistantWelcome.tsx @@ -1,6 +1,7 @@ import { ThreadPrimitive } from '@assistant-ui/react'; import { useTranslation } from '../../i18n'; +import { useAssistantStore } from '../../store/assistantStore'; import EmptyState from '../shared/EmptyState'; import AssistantHints, { MAX_VISIBLE_HINTS } from './AssistantHints'; import { useAssistantScope, useAssistantView } from './assistantViewContext'; @@ -28,32 +29,40 @@ export default function AssistantWelcome({ }) { const { t } = useTranslation(); const { tripId, variant } = useAssistantView(); - // The Studio's chat is about a book, not a plan; the home chat about all of the user's travels. - const scope = useAssistantScope().kind; - const book = scope === 'journey'; + // The Studio's chat is about a book, not a plan; the journey's chat on its own page about the journal, + // the home chat about all of the user's travels. A journey's chat the Studio does not host is the journal's. + const hosted = useAssistantStore((s) => !!s.studioHosts[tripId]); + const about = useAssistantScope().kind; + const scope: WelcomeScope = about === 'journey' && !hosted ? 'journal' : about; const hints = useAssistantHints(tripId, { withStays: true }); const crowded = Math.min(hints.hints.length, MAX_VISIBLE_HINTS) > SUGGESTIONS_UNDER_HINTS; const offered = crowded ? suggestions.slice(0, SUGGESTIONS_UNDER_HINTS) : suggestions; return (
- {!canEdit && ( -

- {book ? t('journey.studio.assistant.readOnly') : t('assistant.empty.readOnly')} -

- )} + {!canEdit &&

{readOnlyNote(scope, t)}

} {withSuggestions && offered.length > 0 && }
); } +/** What an empty chat is about: a trip, a journey's book in the Studio, a journal on its page, or all of them. */ +type WelcomeScope = 'trip' | 'journey' | 'journal' | 'home'; + /** The question over the empty chat, by what the chat is about. */ -function welcomeTitle(scope: 'trip' | 'journey' | 'home', t: (key: string) => string): string { +function welcomeTitle(scope: WelcomeScope, t: (key: string) => string): string { if (scope === 'journey') return t('journey.studio.assistant.welcome'); + if (scope === 'journal') return t('assistant.journal.welcome'); return scope === 'home' ? t('assistant.home.welcome') : t('assistant.empty.title'); } +/** What the empty chat tells someone who may only read. */ +function readOnlyNote(scope: WelcomeScope, t: (key: string) => string): string { + if (scope === 'journey') return t('journey.studio.assistant.readOnly'); + return scope === 'journal' ? t('assistant.journal.readOnly') : t('assistant.empty.readOnly'); +} + export function AssistantSuggestions({ suggestions, layout, diff --git a/client/src/components/Assistant/AssistantWindow.tsx b/client/src/components/Assistant/AssistantWindow.tsx index 1ee1add70..af4bfe461 100644 --- a/client/src/components/Assistant/AssistantWindow.tsx +++ b/client/src/components/Assistant/AssistantWindow.tsx @@ -15,7 +15,7 @@ import { FRAME_CLASS, useAssistantFrame } from './assistantFrame'; import { plannerSearchOf } from './assistantShowOnMap'; import type { AssistantChatTrip } from './assistantViewContext'; import type { EntityTarget } from './cards/cardTypes'; -import { useAssistantHome } from './useAssistantHome'; +import { chatPagePath, useAssistantHome, useRouteJournalChat } from './useAssistantHome'; import { useAssistantHotkey } from './useAssistantHotkey'; import { useAssistantThread } from './useAssistantThread'; @@ -33,7 +33,9 @@ import { useAssistantThread } from './useAssistantThread'; * * Outside a trip it opens on the home chat, which knows all of the user's * trips; its head lists them to go on in one trip's own chat, and a trip's - * chat opened that way has the way back home in its head. + * chat opened that way has the way back home in its head. On a journey's + * page it opens on that journal's chat, the journey's conversation it shares + * with TREK Studio. * * Mounted once for the whole app; it shows nothing until the assistant has a * connection this user can use. @@ -198,13 +200,16 @@ export default function AssistantWindow({ trip, onOpen, onFocusDay, onRevealMap if (!open) setPicked(null); } - const current = trip ?? picked ?? home.chat; + const journal = useRouteJournalChat(); + // The page's own chat first: the trip in the planner, else the journal on screen. + const routeChat = trip ?? journal; + const current = routeChat ?? picked ?? home.chat; const atHome = current.id === HOME_CHAT_KEY; - // A change in a trip the planner does not show is shown by opening that trip; a tab opens on it. + // A change in a trip the planner does not show is shown by opening that trip (a journal, its page); a tab opens on it. const openPicked = useCallback( (target: EntityTarget) => { if (target.kind === 'page') void navigate(target.path); - else if (picked) void navigate(`/trips/${picked.id}${target.kind === 'tab' ? plannerSearchOf(target) : ''}`); + else if (picked) void navigate(`${chatPagePath(picked)}${target.kind === 'tab' ? plannerSearchOf(target) : ''}`); }, [navigate, picked] ); @@ -216,19 +221,21 @@ export default function AssistantWindow({ trip, onOpen, onFocusDay, onRevealMap [navigate] ); const goHome = useCallback(() => setPicked(null), []); - // Another page leaves the trip: the chat stays on it, as if it had been picked. + // Another page leaves the trip or the journal: the chat stays on it, as if it had been picked. const openOnScreen = useCallback( (target: EntityTarget) => { - if (target.kind === 'page' && trip) setPicked(trip); + if (target.kind === 'page' && routeChat) setPicked(routeChat); onOpen?.(target); }, - [onOpen, trip] + [onOpen, routeChat] ); + // The round button's hint dot reads a trip's plan; a journal has none. + const launcherTrip = trip?.id ?? null; if (!available) return null; - if (!open) return trip ? : null; + if (!open) return routeChat ? : null; - const onScreen = !!trip && current.id === trip.id; + const onScreen = !!routeChat && current.id === routeChat.id; let openTarget = onScreen ? onOpen && openOnScreen : openPicked; if (atHome) openTarget = openFromHome; let tripSwitch: { onPick?: (trip: WindowTrip) => void; onHome?: () => void } | undefined; @@ -241,13 +248,13 @@ export default function AssistantWindow({ trip, onOpen, onFocusDay, onRevealMap hidden={minimized} onOpen={openTarget} // Only the planner on screen has days to select and a map to bring back. - onFocusDay={onScreen ? onFocusDay : undefined} - onRevealMap={onScreen ? onRevealMap : undefined} + onFocusDay={onScreen && trip ? onFocusDay : undefined} + onRevealMap={onScreen && trip ? onRevealMap : undefined} onOpenTripChat={atHome ? home.openTripChat : undefined} tripSwitch={tripSwitch} /> {/* Hidden while an answer may still come in: the round button brings it back, on any page. */} - {minimized && } + {minimized && } ); } diff --git a/client/src/components/Assistant/useAssistantHome.test.tsx b/client/src/components/Assistant/useAssistantHome.test.tsx index dd559c9de..344546353 100644 --- a/client/src/components/Assistant/useAssistantHome.test.tsx +++ b/client/src/components/Assistant/useAssistantHome.test.tsx @@ -1,18 +1,19 @@ /** - * FE-AI-HOME-001 to FE-AI-HOME-004 + * FE-AI-HOME-001 to FE-AI-HOME-006 * * The home chat's shared hooks: its key and name, the way into one trip's own * chat with the question waiting there (a question already typed there * stays), the way in from the pages outside a trip, and the questions the - * empty home chat offers. + * empty home chat offers, and the journal on screen as a chat. */ import type { AiStatusResponse } from '@trek/shared'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { fireEvent, render, screen } from '../../../tests/helpers/render'; import { chatOf, useAssistantStore } from '../../store/assistantStore'; +import { useJourneyStore } from '../../store/journeyStore'; import { HOME_SUGGESTIONS } from './assistantModel'; -import { useAssistantHome, useAssistantHomeLauncher } from './useAssistantHome'; +import { chatPagePath, useAssistantHome, useAssistantHomeLauncher, useRouteJournalChat } from './useAssistantHome'; import { useAssistantSuggestions } from './useAssistantSuggestions'; const INITIAL = useAssistantStore.getState(); @@ -100,3 +101,29 @@ describe('the empty home chat', () => { expect(screen.getByRole('list')).toHaveAttribute('data-can-edit', 'true'); }); }); + +function RouteJournal() { + const chat = useRouteJournalChat(); + return

{chat ? `journal ${chat.id} ${chat.title}` : 'no journal'}

; +} + +describe('the journal on screen', () => { + it("FE-AI-HOME-005: a journey's page is its journal's chat, under the journey's key, but not in its Studio", () => { + useJourneyStore.setState({ current: { id: 12, title: 'Japan 2026' } as never }); + render(, { initialEntries: ['/journey/12'] }); + expect(screen.getByText('journal -12 Japan 2026')).toBeInTheDocument(); + render(, { initialEntries: ['/journey/12/studio'] }); + expect(screen.getAllByText('no journal')).toHaveLength(1); + render(, { initialEntries: ['/journey'] }); + expect(screen.getAllByText('no journal')).toHaveLength(2); + // Another journey loaded: the key is the route's, the title waits for its own. + render(, { initialEntries: ['/journey/13'] }); + expect(screen.getByText(/^journal -13\s*$/)).toBeInTheDocument(); + useJourneyStore.setState({ current: null }); + }); + + it('FE-AI-HOME-006: a chat switched to from home opens on its own page', () => { + expect(chatPagePath({ id: 5, title: 'Kyoto' })).toBe('/trips/5'); + expect(chatPagePath({ id: -12, title: 'Japan 2026' })).toBe('/journey/12'); + }); +}); diff --git a/client/src/components/Assistant/useAssistantHome.ts b/client/src/components/Assistant/useAssistantHome.ts index 1d4774989..a7ea0d88d 100644 --- a/client/src/components/Assistant/useAssistantHome.ts +++ b/client/src/components/Assistant/useAssistantHome.ts @@ -1,8 +1,10 @@ import { useCallback, useMemo } from 'react'; +import { useMatch } from 'react-router'; import { useTranslation } from '../../i18n'; -import { HOME_CHAT_KEY } from '../../store/assistantChatKey'; +import { assistantChatKey, assistantScopeOf, HOME_CHAT_KEY } from '../../store/assistantChatKey'; import { useAssistantStore } from '../../store/assistantStore'; +import { useJourneyStore } from '../../store/journeyStore'; import type { AssistantChatTrip } from './assistantViewContext'; /** @@ -42,3 +44,27 @@ export function useAssistantHomeLauncher() { const open = useAssistantStore((s) => s.open); return { available, open }; } + +/** + * The journal on screen as a chat: on a journey's own page the journey's + * conversation (the one TREK Studio holds too), under its key and with the + * journey's title once the page has loaded it; null elsewhere and in the + * Studio, whose column holds that chat itself. + */ +export function useRouteJournalChat(): AssistantChatTrip | null { + const match = useMatch('/journey/:id/*'); + const inStudio = useMatch('/journey/:id/studio/*') !== null; + const id = Number(match?.params.id); + const journeyId = !inStudio && Number.isInteger(id) && id > 0 ? id : null; + const title = useJourneyStore((s) => (s.current && s.current.id === journeyId ? s.current.title : '')); + return useMemo( + () => (journeyId === null ? null : { id: assistantChatKey({ kind: 'journey', journeyId }), title }), + [journeyId, title] + ); +} + +/** The page a chat switched to from the home chat lives on: a journey's for its key, a trip's otherwise. */ +export function chatPagePath(chat: AssistantChatTrip): string { + const scope = assistantScopeOf(chat.id); + return scope.kind === 'journey' ? `/journey/${scope.journeyId}` : `/trips/${chat.id}`; +} diff --git a/client/src/mobile/screens/assistant/MAssistantHomeScreen.tsx b/client/src/mobile/screens/assistant/MAssistantHomeScreen.tsx index 705fb6443..86bef6d86 100644 --- a/client/src/mobile/screens/assistant/MAssistantHomeScreen.tsx +++ b/client/src/mobile/screens/assistant/MAssistantHomeScreen.tsx @@ -3,7 +3,7 @@ import { useMatch, useNavigate } from 'react-router'; import type { AssistantChatTrip } from '../../../components/Assistant/assistantViewContext'; import type { EntityTarget } from '../../../components/Assistant/cards/cardTypes'; -import { useAssistantHome } from '../../../components/Assistant/useAssistantHome'; +import { chatPagePath, useAssistantHome, useRouteJournalChat } from '../../../components/Assistant/useAssistantHome'; import { useAssistantStore } from '../../../store/assistantStore'; import { AssistantScreen, type MapHops } from '../trip/assistant/MAssistantScreen'; @@ -15,8 +15,9 @@ import { AssistantScreen, type MapHops } from '../trip/assistant/MAssistantScree * screen and the question waiting in its composer. Another page a card points * at closes the chat and opens there. * - * Inside a trip the trip's own screen (MAssistantScreen) shows its chat, so - * this one stays away there. Nothing shows until the assistant has a + * On a journal's page it shows that journal's chat instead. Inside a trip + * the trip's own screen (MAssistantScreen) shows its chat, so this one stays + * away there. Nothing shows until the assistant has a * connection this user can use. */ export default function MAssistantHomeScreen() { @@ -28,9 +29,12 @@ export default function MAssistantHomeScreen() { const loadStatus = useAssistantStore((s) => s.loadStatus); const close = useAssistantStore((s) => s.close); - // The trip's screen takes over with the window still open, so its chat comes up there. - const switchTo = useCallback((trip: AssistantChatTrip) => void navigate(`/trips/${trip.id}`), [navigate]); + // The trip's screen (or the journal's) takes over with the window still open, so its chat comes up there. + const switchTo = useCallback((chat: AssistantChatTrip) => void navigate(chatPagePath(chat)), [navigate]); const home = useAssistantHome(switchTo); + // On a journal's page the chat is that journal's, the journey's conversation it shares with TREK Studio. + const journal = useRouteJournalChat(); + const chat = journal ?? home.chat; const hops = useMemo( (): MapHops => ({ open: (target: EntityTarget) => { @@ -50,11 +54,11 @@ export default function MAssistantHomeScreen() { if (inTrip || !available || !open || minimized) return null; return (