fix(notifications): drop a tab the planner lacks from the URL, link booking mails to the reservations tab, open the chat on the phone

This commit is contained in:
Maurice
2026-11-14 16:49:18 +01:00
parent bfdbf4d811
commit a76a96ccca
9 changed files with 61 additions and 16 deletions
@@ -118,6 +118,16 @@ describe('useNotificationItemActions', () => {
expect(navigate).toHaveBeenLastCalledWith('/dashboard');
});
it('FE-COMP-NOTIFITEM-ACTIONS-006: an old booking mail link names the reservations tab by its id', async () => {
const { result } = renderHook(() =>
useNotificationItemActions(
notification({ title_key: 'notif.mail_booking_added.title', navigate_target: '/trips/7?tab=bookings' })
)
);
await act(() => result.current.handleNavigate());
expect(navigate).toHaveBeenLastCalledWith('/trips/7?tab=buchungen');
});
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));
@@ -14,7 +14,8 @@ function notificationTarget(notification: InAppNotification): string | null {
(notification.scope === 'trip' && notification.target > 0 ? String(notification.target) : null);
return tripId ? `/trips/${tripId}?tab=collab` : '/dashboard';
}
return notification.navigate_target;
// Booking mails used to link a tab by its English name, which the planner does not have.
return notification.navigate_target?.replace(/([?&]tab=)bookings(?=&|#|$)/, '$1buchungen') ?? null;
}
/** Compact relative timestamp ("5m" / "3h" / "2d"); under a minute it reads `justNow`. */
@@ -340,6 +340,13 @@ export default function MTripShell({
if (mode === 'browse') setMode('go')
}
// A notification link opens its tab the way the dock does, and a message one the chat.
useEffect(() => {
if (!planner.tabLink) return
setMode(m => (m === 'browse' ? 'go' : m))
if (planner.tabLink.tab === 'collab') setCollabTab('chat')
}, [planner.tabLink])
const setTravelMode = (next: MTripMode) => {
setBrowseFromEdit(next === 'browse' && mode === 'edit')
setMode(next)
@@ -65,10 +65,11 @@ describe('useAssistantTabLink', () => {
const searchParams = new URLSearchParams(query);
const setSearchParams = vi.fn((update: (params: URLSearchParams) => URLSearchParams) => update(searchParams));
const selectTab = vi.fn();
const onLinked = vi.fn();
renderHook(() =>
useAssistantTabLink({ searchParams, setSearchParams: setSearchParams as never, activeTab, selectTab })
useAssistantTabLink({ searchParams, setSearchParams: setSearchParams as never, activeTab, selectTab, onLinked })
);
return { searchParams, setSearchParams, selectTab };
return { searchParams, setSearchParams, selectTab, onLinked };
}
it('FE-PLANNER-ASSISTLINK-005: a ?tab= the chat opens while the planner is open switches the tab, once', () => {
@@ -79,13 +80,20 @@ describe('useAssistantTabLink', () => {
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', () => {
it('FE-PLANNER-ASSISTLINK-006: the tab on screen is not switched again; a tab it cannot open is dropped from the URL', () => {
const same = setupTab('tab=finanzplan', 'finanzplan');
expect(same.selectTab).not.toHaveBeenCalled();
expect(same.searchParams.has('tab')).toBe(false);
const unknown = setupTab('tab=nowhere', 'plan');
const unknown = setupTab('tab=nowhere&place=3', 'plan');
expect(unknown.selectTab).not.toHaveBeenCalled();
expect(unknown.setSearchParams).not.toHaveBeenCalled();
expect(unknown.onLinked).not.toHaveBeenCalled();
expect(unknown.searchParams.has('tab')).toBe(false);
expect(unknown.searchParams.get('place')).toBe('3');
});
it('FE-PLANNER-ASSISTLINK-007: every link it takes is reported, also one to the tab already on screen', () => {
expect(setupTab('tab=collab', 'plan').onLinked).toHaveBeenCalledWith('collab');
expect(setupTab('tab=collab', 'collab').onLinked).toHaveBeenCalledWith('collab');
});
});
@@ -59,26 +59,33 @@ interface AssistantTabLinkOptions {
activeTab: string;
/** Switches the planner's tab the way its tab bar does. */
selectTab: (tabId: string) => void;
/** Told about every link it took, so a shell can open the part of the tab a link means. */
onLinked?: (tabId: string) => void;
}
/**
* `?tab=<id>` 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).
* once and then drop it from the URL. A tab the planner does not have is
* dropped too, so it does not linger in the address bar. What the tab reads
* beside it stays for that tab to take (`?tab=finanzplan&expense=7`, for example).
*/
export function useAssistantTabLink({
searchParams,
setSearchParams,
activeTab,
selectTab,
onLinked,
}: AssistantTabLinkOptions): void {
const requested = searchParams.get('tab');
const linkable = requested !== null && isDeepLinkableTripTab(requested);
useEffect(() => {
if (!linkable || requested === null) return;
if (requested !== activeTab) selectTab(requested);
if (requested === null) return;
if (linkable) {
if (requested !== activeTab) selectTab(requested);
onLinked?.(requested);
}
setSearchParams(
(params) => {
params.delete('tab');
@@ -86,5 +93,5 @@ export function useAssistantTabLink({
},
{ replace: true }
);
}, [linkable, requested, activeTab, selectTab, setSearchParams]);
}, [linkable, requested, activeTab, selectTab, onLinked, setSearchParams]);
}
@@ -290,7 +290,10 @@ export function useTripPlanner() {
}
// Notifications can open a tab while this trip is already mounted, on either layout.
useAssistantTabLink({ searchParams, setSearchParams, activeTab, selectTab: handleTabChange })
// Each link it takes is counted, so the phone shell can open the right part of the tab.
const [tabLink, setTabLink] = useState<{ tab: string; seq: number } | null>(null)
const onTabLinked = useCallback((tab: string) => setTabLink(prev => ({ tab, seq: (prev?.seq ?? 0) + 1 })), [])
useAssistantTabLink({ searchParams, setSearchParams, activeTab, selectTab: handleTabChange, onLinked: onTabLinked })
// 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
@@ -741,7 +744,7 @@ export function useTripPlanner() {
dawarichTrailShown, toggleDawarichTrail, dawarichTrail, dawarichHiddenDates, dawarichEnabled: !!enabledAddons.dawarich,
followTrack, roadtripViaCounts,
allowedFileTypes, tripMembers, setTripMembers, refreshMembers, loadAccommodations,
TRANSPORT_TYPES, TRIP_TABS, activeTab, setActiveTab, handleTabChange,
TRANSPORT_TYPES, TRIP_TABS, activeTab, setActiveTab, handleTabChange, tabLink,
leftWidth, rightWidth, leftCollapsed, rightCollapsed, setLeftCollapsed, setRightCollapsed,
leftHidden, rightHidden, toggleLeft, toggleRight, narrowPanels,
startResizeLeft, startResizeRight, nudgeLeft, nudgeRight, resizeMin, resizeMax,
@@ -593,6 +593,15 @@ describe('MTripShell', () => {
expect(shellApi.collabTab).toBe('notes')
})
it('FE-MOB-SHELL-036b: a message notification opens the chat, not the collab part last open', () => {
const { planner, rerenderShell } = renderShell({ activeTab: 'collab' } as Partial<TripPlanner>)
fireEvent.click(screen.getByRole('button', { name: 'collab.tabs.notes' }))
expect(shellApi.collabTab).toBe('notes')
planner.tabLink = { tab: 'collab', seq: 1 }
rerenderShell()
expect(shellApi.collabTab).toBe('chat')
})
it('FE-MOB-SHELL-037: setTrTab from a slot routes through the planner and resets browse', () => {
const { planner } = renderShell()
fireEvent.click(screen.getByRole('button', { name: 'trip.mobilePlaces' }))
@@ -296,7 +296,7 @@ const EVENT_NOTIFICATION_CONFIG: Record<string, EventNotifConfig> = {
titleKey: 'notif.mail_booking_added.title',
textKey: (p) => (p.role === 'traveller' ? MAIL_BOOKING_TEXT.traveller : MAIL_BOOKING_TEXT.forwarder).textKey,
navigateTextKey: 'notif.action.view_trip',
navigateTarget: (p) => (p.tripId ? `/trips/${p.tripId}?tab=bookings` : null),
navigateTarget: (p) => (p.tripId ? `/trips/${p.tripId}?tab=buchungen` : null),
},
// Bookings from forwarded mails wait in the person's inbox for a decision.
mail_booking_pending: {
@@ -682,8 +682,8 @@ describe('send() — in-app notification content', () => {
const notifs = await getInAppNotifications(user.id);
expect(notifs.map((n) => [n.text_key, n.navigate_target])).toEqual(
expect.arrayContaining([
['notif.mail_booking_added.text', '/trips/7?tab=bookings'],
['notif.mail_booking_added.shared', '/trips/7?tab=bookings'],
['notif.mail_booking_added.text', '/trips/7?tab=buchungen'],
['notif.mail_booking_added.shared', '/trips/7?tab=buchungen'],
['notif.mail_booking_pending.text', '/dashboard?inbox=mail'],
['notif.mail_ingest_paused.text', null],
]),