mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
fix(notifications): update navigation targets for trip and vacay events
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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?.();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -62,11 +62,10 @@ interface AssistantTabLinkOptions {
|
||||
}
|
||||
|
||||
/**
|
||||
* `?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.
|
||||
* `?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).
|
||||
*/
|
||||
export function useAssistantTabLink({
|
||||
searchParams,
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
|
||||
@@ -19,9 +19,7 @@ interface PlannerDialogsOptions extends Pick<PlannerBase, 'tripId' | 'tripAction
|
||||
* stop form, the stop popup, the stay release question, the trip and member dialogs,
|
||||
* the booking, transport and transit editors, the two importers and the booking detail.
|
||||
*
|
||||
* Its effects run where useTripPlanner calls it, in the order they always had. That is
|
||||
* also why the ?tab= cleanup sits here although it belongs to the tabs: it runs after
|
||||
* the ?create=place intent and before the AirTrail sync.
|
||||
* Its effects run where useTripPlanner calls it, in the order they always had.
|
||||
*/
|
||||
export function usePlannerDialogs(options: PlannerDialogsOptions) {
|
||||
const { tripId, tripActions, searchParams, setSearchParams } = options
|
||||
@@ -94,14 +92,6 @@ export function usePlannerDialogs(options: PlannerDialogsOptions) {
|
||||
}
|
||||
}, [searchParams])
|
||||
|
||||
// ?tab= has done its job in the activeTab initializer in useTripPlanner, so drop it
|
||||
// and the URL stops claiming a tab the user may have since switched away from. The
|
||||
// session memory keeps the choice across a reload. It runs here, right after the
|
||||
// ?create=place intent, because that is the order the two effects have always had.
|
||||
useEffect(() => {
|
||||
if (searchParams.get('tab') === null) return
|
||||
setSearchParams(p => { p.delete('tab'); return p }, { replace: true })
|
||||
}, [searchParams])
|
||||
const [showTripForm, setShowTripForm] = useState<boolean>(false)
|
||||
const [showMembersModal, setShowMembersModal] = useState<boolean>(false)
|
||||
const [showReservationModal, setShowReservationModal] = useState<boolean>(false)
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -221,7 +221,7 @@ const EVENT_NOTIFICATION_CONFIG: Record<string, EventNotifConfig> = {
|
||||
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<string, EventNotifConfig> = {
|
||||
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',
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user