fix(notifications): update navigation targets for trip and vacay events

This commit is contained in:
Luffy
2026-11-13 16:30:48 +08:00
committed by Maurice
parent d3ec833ab7
commit b068e593fc
11 changed files with 116 additions and 42 deletions
@@ -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?.();
}
};