mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
fix(planner): take a ?tab=, ?day= or ?assistant= link once per URL
React Router lands the URL without the link as a transition. Until it does, every render still read the link, and the tab link took it again on each of them: selectTab is a new function per render and onLinked sets state, so a notification's ?tab= link rendered the planner into React's update limit and the route's error page replaced the trip on an iPad or an unfolded foldable. Both link hooks now take a link once per URL and wait for the cleaned one; the same link in a new URL is taken again.
This commit is contained in:
@@ -96,4 +96,47 @@ describe('useAssistantTabLink', () => {
|
||||
expect(setupTab('tab=collab', 'plan').onLinked).toHaveBeenCalledWith('collab');
|
||||
expect(setupTab('tab=collab', 'collab').onLinked).toHaveBeenCalledWith('collab');
|
||||
});
|
||||
|
||||
it('FE-PLANNER-ASSISTLINK-008: a link is taken once while the cleaned URL is still on its way', () => {
|
||||
// React Router lands the URL without the link as a transition: until then
|
||||
// the hook keeps reading the link, with a new selectTab on every render.
|
||||
const searchParams = new URLSearchParams('tab=collab');
|
||||
const setSearchParams = vi.fn();
|
||||
const onLinked = vi.fn();
|
||||
const selects: ReturnType<typeof vi.fn>[] = [];
|
||||
const { rerender } = renderHook(() => {
|
||||
const selectTab = vi.fn();
|
||||
selects.push(selectTab);
|
||||
useAssistantTabLink({
|
||||
searchParams,
|
||||
setSearchParams: setSearchParams as never,
|
||||
activeTab: 'plan',
|
||||
selectTab,
|
||||
onLinked,
|
||||
});
|
||||
});
|
||||
rerender();
|
||||
rerender();
|
||||
expect(selects.filter((select) => select.mock.calls.length > 0)).toHaveLength(1);
|
||||
expect(onLinked).toHaveBeenCalledTimes(1);
|
||||
expect(setSearchParams).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('FE-PLANNER-ASSISTLINK-009: the same link in a new URL is taken again', () => {
|
||||
const setSearchParams = vi.fn();
|
||||
const onLinked = vi.fn();
|
||||
const { rerender } = renderHook(
|
||||
({ query }) =>
|
||||
useAssistantTabLink({
|
||||
searchParams: new URLSearchParams(query),
|
||||
setSearchParams: setSearchParams as never,
|
||||
activeTab: 'plan',
|
||||
selectTab: vi.fn(),
|
||||
onLinked,
|
||||
}),
|
||||
{ initialProps: { query: 'tab=collab' } }
|
||||
);
|
||||
rerender({ query: 'tab=collab' });
|
||||
expect(onLinked).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
import { isDeepLinkableTripTab } from '../../constants/tripTabs';
|
||||
import { useAssistantStore } from '../../store/assistantStore';
|
||||
@@ -37,8 +37,14 @@ export function useAssistantDeepLink({
|
||||
const known = requestedDay !== null && tripDays.some((day) => day.id === Number(requestedDay));
|
||||
const dayDone = requestedDay !== null && (known || tripDays.length > 0);
|
||||
|
||||
const taken = useRef<URLSearchParams | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!openRequested && !dayDone) return;
|
||||
// Once per URL: React Router lands the cleaned URL as a transition, and
|
||||
// until it does the link must not be taken again (see useAssistantTabLink).
|
||||
if (taken.current === searchParams) return;
|
||||
taken.current = searchParams;
|
||||
if (openRequested) useAssistantStore.getState().open();
|
||||
if (known) selectDay(Number(requestedDay));
|
||||
setSearchParams(
|
||||
@@ -49,7 +55,7 @@ export function useAssistantDeepLink({
|
||||
},
|
||||
{ replace: true }
|
||||
);
|
||||
}, [openRequested, requestedDay, known, dayDone, selectDay, setSearchParams]);
|
||||
}, [searchParams, openRequested, requestedDay, known, dayDone, selectDay, setSearchParams]);
|
||||
}
|
||||
|
||||
interface AssistantTabLinkOptions {
|
||||
@@ -79,9 +85,17 @@ export function useAssistantTabLink({
|
||||
}: AssistantTabLinkOptions): void {
|
||||
const requested = searchParams.get('tab');
|
||||
const linkable = requested !== null && isDeepLinkableTripTab(requested);
|
||||
const taken = useRef<URLSearchParams | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (requested === null) return;
|
||||
// Once per URL. React Router lands the URL without the link as a
|
||||
// transition, so the renders before it still read the link; selectTab is a
|
||||
// new function on each of them and onLinked sets state, and taking the link
|
||||
// again on every one of them rendered the planner into React's update limit
|
||||
// (a ?tab= link on an iPad or an unfolded Duo).
|
||||
if (taken.current === searchParams) return;
|
||||
taken.current = searchParams;
|
||||
if (linkable) {
|
||||
if (requested !== activeTab) selectTab(requested);
|
||||
onLinked?.(requested);
|
||||
@@ -93,5 +107,5 @@ export function useAssistantTabLink({
|
||||
},
|
||||
{ replace: true }
|
||||
);
|
||||
}, [linkable, requested, activeTab, selectTab, onLinked, setSearchParams]);
|
||||
}, [searchParams, linkable, requested, activeTab, selectTab, onLinked, setSearchParams]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user