diff --git a/client/src/pages/tripPlanner/useAssistantDeepLink.test.ts b/client/src/pages/tripPlanner/useAssistantDeepLink.test.ts index f2d574b9f..c1a4925ea 100644 --- a/client/src/pages/tripPlanner/useAssistantDeepLink.test.ts +++ b/client/src/pages/tripPlanner/useAssistantDeepLink.test.ts @@ -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[] = []; + 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); + }); }); diff --git a/client/src/pages/tripPlanner/useAssistantDeepLink.ts b/client/src/pages/tripPlanner/useAssistantDeepLink.ts index 1ef0f1011..993b04764 100644 --- a/client/src/pages/tripPlanner/useAssistantDeepLink.ts +++ b/client/src/pages/tripPlanner/useAssistantDeepLink.ts @@ -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(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(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]); }