diff --git a/client/src/components/Notifications/useNotificationItemActions.test.ts b/client/src/components/Notifications/useNotificationItemActions.test.ts index 21d406d02..a9bf7afd3 100644 --- a/client/src/components/Notifications/useNotificationItemActions.test.ts +++ b/client/src/components/Notifications/useNotificationItemActions.test.ts @@ -60,6 +60,42 @@ describe('compactTime', () => { }); describe('useNotificationItemActions', () => { + it.each(['notif.vacay_invite.title', 'notif.vacay_share.title'])( + 'opens the Vacay list for %s, including stored detail links', + async (title_key) => { + const { result } = renderHook(() => useNotificationItemActions(notification({ + title_key, navigate_target: '/vacay/42', + }))); + await act(() => result.current.handleNavigate()); + expect(navigate).toHaveBeenCalledWith('/vacay'); + } + ); + + it.each(['/trips/42', '/trips/42?tab=collab'])( + 'opens Collab for both old and new message links: %s', + async (navigate_target) => { + const { result } = renderHook(() => useNotificationItemActions(notification({ + title_key: 'notif.collab_message.title', navigate_target, + }))); + await act(() => result.current.handleNavigate()); + expect(navigate).toHaveBeenCalledWith('/trips/42?tab=collab'); + } + ); + + it('uses a trip-scoped message target when its link is missing, otherwise falls back to Dashboard', async () => { + const scoped = renderHook(() => useNotificationItemActions(notification({ + title_key: 'notif.collab_message.title', scope: 'trip', target: 42, navigate_target: null, + }))); + await act(() => scoped.result.current.handleNavigate()); + expect(navigate).toHaveBeenLastCalledWith('/trips/42?tab=collab'); + + const missing = renderHook(() => useNotificationItemActions(notification({ + title_key: 'notif.collab_message.title', navigate_target: '/dashboard', + }))); + await act(() => missing.result.current.handleNavigate()); + expect(navigate).toHaveBeenLastCalledWith('/dashboard'); + }); + it('FE-COMP-NOTIFITEM-ACTIONS-002: navigating marks an unread item read first, then follows and closes', async () => { const onClose = vi.fn(); const { result } = renderHook(() => useNotificationItemActions(notification(), onClose)); diff --git a/client/src/components/Notifications/useNotificationItemActions.ts b/client/src/components/Notifications/useNotificationItemActions.ts index 48a082c25..663a054f5 100644 --- a/client/src/components/Notifications/useNotificationItemActions.ts +++ b/client/src/components/Notifications/useNotificationItemActions.ts @@ -3,6 +3,20 @@ import { useNavigate } from 'react-router'; import { type InAppNotification, useInAppNotificationStore } from '../../store/inAppNotificationStore'; +/** Resolve old inbox rows too: their stored links predate the current destinations. */ +function notificationTarget(notification: InAppNotification): string | null { + if (notification.title_key === 'notif.vacay_invite.title' || notification.title_key === 'notif.vacay_share.title') { + return '/vacay'; + } + if (notification.title_key === 'notif.collab_message.title') { + const tripId = + /^\/trips\/([1-9]\d*)(?:[/?#]|$)/.exec(notification.navigate_target ?? '')?.[1] ?? + (notification.scope === 'trip' && notification.target > 0 ? String(notification.target) : null); + return tripId ? `/trips/${tripId}?tab=collab` : '/dashboard'; + } + return notification.navigate_target; +} + /** Compact relative timestamp ("5m" / "3h" / "2d"); under a minute it reads `justNow`. */ export function compactTime(dateStr: string, justNow: string): string { const minutes = Math.floor((Date.now() - new Date(dateStr).getTime()) / 60000); @@ -32,8 +46,9 @@ export function useNotificationItemActions(notification: InAppNotification, onCl const handleNavigate = async () => { if (!notification.is_read) await markRead(notification.id); - if (notification.navigate_target) { - navigate(notification.navigate_target); + const target = notificationTarget(notification); + if (target) { + navigate(target); onClose?.(); } }; diff --git a/client/src/pages/tripPlanner/useAssistantDeepLink.ts b/client/src/pages/tripPlanner/useAssistantDeepLink.ts index 46187dde6..027329462 100644 --- a/client/src/pages/tripPlanner/useAssistantDeepLink.ts +++ b/client/src/pages/tripPlanner/useAssistantDeepLink.ts @@ -62,11 +62,10 @@ interface AssistantTabLinkOptions { } /** - * `?tab=` 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. + * `?tab=` from a notification or a card in the chat, including while the + * planner is already open. Both desktop and mobile take the requested tab + * once and then drop it from the URL. What the tab reads beside it stays + * for that tab to take (`?tab=finanzplan&expense=7`, for example). */ export function useAssistantTabLink({ searchParams, diff --git a/client/src/pages/tripPlanner/useAssistantEntityLinks.test.tsx b/client/src/pages/tripPlanner/useAssistantEntityLinks.test.tsx index f6b02b1b5..2c3c2df41 100644 --- a/client/src/pages/tripPlanner/useAssistantEntityLinks.test.tsx +++ b/client/src/pages/tripPlanner/useAssistantEntityLinks.test.tsx @@ -1,8 +1,7 @@ /** * 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 + * The links the chat opens the planner with while it is on screen: the parts' own links * (tours, bookings, road trip) are each handed the planner, the workspace and * the query (each part's own test covers what it does with them). */ @@ -37,10 +36,10 @@ function setup(path: string) { } describe('useAssistantEntityLinks', () => { - it('FE-PLANNER-ENTITYLINK-001: another tab switches there and leaves the URL, what it reads stays', () => { + it('FE-PLANNER-ENTITYLINK-001: leaves tab selection to the shared planner', () => { const { planner, hook } = setup('/trips/5?tab=finanzplan&expense=7'); - expect(planner.handleTabChange).toHaveBeenCalledWith('finanzplan'); - expect(hook.result.current.search).toBe('?expense=7'); + expect(planner.handleTabChange).not.toHaveBeenCalled(); + expect(hook.result.current.search).toBe('?tab=finanzplan&expense=7'); }); it("FE-PLANNER-ENTITYLINK-002: each part's own link gets the planner, the workspace and the query; nothing switches without a tab", () => { diff --git a/client/src/pages/tripPlanner/useAssistantEntityLinks.ts b/client/src/pages/tripPlanner/useAssistantEntityLinks.ts index 50a144c4a..a04700600 100644 --- a/client/src/pages/tripPlanner/useAssistantEntityLinks.ts +++ b/client/src/pages/tripPlanner/useAssistantEntityLinks.ts @@ -3,7 +3,6 @@ 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'; @@ -14,18 +13,12 @@ interface AssistantEntityLinkOptions { /** * 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. + * 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. The shared + * planner handles `?tab=` for both desktop and mobile. */ 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); diff --git a/client/src/pages/tripPlanner/usePlannerDialogs.test.ts b/client/src/pages/tripPlanner/usePlannerDialogs.test.ts index bf4cf13cb..98e8e973a 100644 --- a/client/src/pages/tripPlanner/usePlannerDialogs.test.ts +++ b/client/src/pages/tripPlanner/usePlannerDialogs.test.ts @@ -136,13 +136,12 @@ describe('usePlannerDialogs', () => { expect(url.get('create')).toBe('expense') }) - it('FE-TP-DIALOGS-006: ?tab= is dropped from the URL once the planner has read it', () => { + it('FE-TP-DIALOGS-006: leaves ?tab= for the shared planner to handle', () => { url = new URLSearchParams('tab=dateien&keep=1') const { result } = renderDialogs({ searchParams: url }) - expect(setSearchParams).toHaveBeenCalledTimes(1) - expect(setSearchParams.mock.calls[0][1]).toEqual({ replace: true }) - expect(url.toString()).toBe('keep=1') + expect(setSearchParams).not.toHaveBeenCalled() + expect(url.toString()).toBe('tab=dateien&keep=1') expect(result.current.showPlaceForm).toBe(false) }) diff --git a/client/src/pages/tripPlanner/usePlannerDialogs.ts b/client/src/pages/tripPlanner/usePlannerDialogs.ts index bdb2840a1..00ffed539 100644 --- a/client/src/pages/tripPlanner/usePlannerDialogs.ts +++ b/client/src/pages/tripPlanner/usePlannerDialogs.ts @@ -19,9 +19,7 @@ interface PlannerDialogsOptions extends Pick { - if (searchParams.get('tab') === null) return - setSearchParams(p => { p.delete('tab'); return p }, { replace: true }) - }, [searchParams]) const [showTripForm, setShowTripForm] = useState(false) const [showMembersModal, setShowMembersModal] = useState(false) const [showReservationModal, setShowReservationModal] = useState(false) diff --git a/client/src/pages/tripPlanner/useTripPlanner.test.tsx b/client/src/pages/tripPlanner/useTripPlanner.test.tsx index e2a0e0a3e..0b9b2f46b 100644 --- a/client/src/pages/tripPlanner/useTripPlanner.test.tsx +++ b/client/src/pages/tripPlanner/useTripPlanner.test.tsx @@ -524,6 +524,21 @@ describe('useTripPlanner — tabs', () => { expect(result.current.activeTab).toBe('collab') }) + it('a message link switches an already mounted planner to Collab and remembers the tab', async () => { + vi.mocked(addonsApi.enabled).mockResolvedValue({ addons: [{ id: 'collab' }] }) + seedTrip() + const { result, rerender } = await renderPlanner() + await waitFor(() => expect(result.current.TRIP_TABS.map(t => t.id)).toContain('collab')) + expect(result.current.activeTab).toBe('plan') + + searchParams = new URLSearchParams('tab=collab') + rerender() + + await waitFor(() => expect(result.current.activeTab).toBe('collab')) + expect(searchParams.has('tab')).toBe(false) + expect(sessionStorage.getItem('trip-tab-42')).toBe('collab') + }) + it('FE-TP-HOOK-110: but a tab whose addon really is off still falls back to plan', async () => { vi.mocked(addonsApi.enabled).mockResolvedValue({ addons: [] }) searchParams = new URLSearchParams('tab=finanzplan') diff --git a/client/src/pages/tripPlanner/useTripPlanner.ts b/client/src/pages/tripPlanner/useTripPlanner.ts index da83662d3..4cfcc08ea 100644 --- a/client/src/pages/tripPlanner/useTripPlanner.ts +++ b/client/src/pages/tripPlanner/useTripPlanner.ts @@ -50,7 +50,7 @@ import { useTripRosterSync } from './useTripRosterSync' import { useDayAdd } from './useDayAdd' import { usePlannerDialogs } from './usePlannerDialogs' import { usePlaceDeepLink } from './usePlaceDeepLink' -import { useAssistantDeepLink } from './useAssistantDeepLink' +import { useAssistantDeepLink, useAssistantTabLink } from './useAssistantDeepLink' import { useAssistantCompareView, useAssistantMapLink } from './useAssistantMapLink' import { useAssistantPlaceDraft } from './useAssistantPlaceDraft' import { usePlannerMapView } from './usePlannerMapView' @@ -289,6 +289,9 @@ export function useTripPlanner() { if (tabId === 'dateien' && (!files || files.length === 0)) tripActions.loadFiles?.(tripId) } + // Notifications can open a tab while this trip is already mounted, on either layout. + useAssistantTabLink({ searchParams, setSearchParams, activeTab, selectTab: handleTabChange }) + // handleTabChange is where a tab's lazy load and its session memory happen, and // the tab we *start* on never goes through it — neither a ?tab= deep link nor a // tab restored from a previous visit. Catch both up once per trip, or opening diff --git a/server/src/nest/notifications/notifications.service.ts b/server/src/nest/notifications/notifications.service.ts index 6f4d5b835..f15d79b69 100644 --- a/server/src/nest/notifications/notifications.service.ts +++ b/server/src/nest/notifications/notifications.service.ts @@ -221,7 +221,7 @@ const EVENT_NOTIFICATION_CONFIG: Record = { titleKey: 'notif.vacay_invite.title', textKey: 'notif.vacay_invite.text', navigateTextKey: 'notif.action.view_vacay', - navigateTarget: (p) => (p.planId ? `/vacay/${p.planId}` : null), + navigateTarget: () => '/vacay', }, vacay_share: { inAppType: 'navigate', @@ -249,7 +249,7 @@ const EVENT_NOTIFICATION_CONFIG: Record = { titleKey: 'notif.collab_message.title', textKey: 'notif.collab_message.text', navigateTextKey: 'notif.action.view_collab', - navigateTarget: (p) => (p.tripId ? `/trips/${p.tripId}` : null), + navigateTarget: (p) => (p.tripId ? `/trips/${p.tripId}?tab=collab` : '/dashboard'), }, packing_tagged: { inAppType: 'navigate', diff --git a/server/tests/unit/nest/notifications.service.test.ts b/server/tests/unit/nest/notifications.service.test.ts index 2d2f8a974..617579865 100644 --- a/server/tests/unit/nest/notifications.service.test.ts +++ b/server/tests/unit/nest/notifications.service.test.ts @@ -511,8 +511,8 @@ describe('send() — in-app notification content', () => { const { user } = createUser(testDb); setNotificationChannels(testDb, 'none'); - // vacay_invite without planId → no navigate target → simple type - await send({ event: 'vacay_invite', actorId: null, scope: 'user', targetId: user.id, params: { actor: 'Alice' } }); + // trip_invite without tripId has no destination. + await send({ event: 'trip_invite', actorId: null, scope: 'user', targetId: user.id, params: { actor: 'Alice' } }); const notifs = await getInAppNotifications(user.id); expect(notifs.length).toBe(1); @@ -520,6 +520,31 @@ describe('send() — in-app notification content', () => { expect(notifs[0].navigate_target).toBeNull(); }); + it.each([ + { event: 'vacay_invite', params: { planId: '42' }, expected: '/vacay' }, + { event: 'vacay_invite', params: {}, expected: '/vacay' }, + { event: 'vacay_share', params: {}, expected: '/vacay' }, + { event: 'collab_message', params: { tripId: '42' }, expected: '/trips/42?tab=collab' }, + { event: 'collab_message', params: {}, expected: '/dashboard' }, + ] as const)( + 'routes $event with $params to $expected on in-app and external channels', + async ({ event, params, expected }) => { + const { user } = createUser(testDb); + await setUserWebhookUrl(user.id); + setNotificationChannels(testDb, 'webhook'); + + await send({ event, actorId: null, scope: 'user', targetId: user.id, params: { actor: 'Alice', ...params } }); + + const [notif] = await getInAppNotifications(user.id); + expect(notif.type).toBe('navigate'); + expect(notif.navigate_target).toBe(expected); + expect(fetchMock).toHaveBeenCalledTimes(1); + const body = JSON.parse(fetchMock.mock.calls[0][1].body); + const link = new URL(body.link); + expect(link.pathname + link.search).toBe(expected); + } + ); + it('NSVC-014 — navigate_target uses /admin for version_available event', async () => { const { user: admin } = createAdmin(testDb); setNotificationChannels(testDb, 'none');