From 841f8d90f6826d211539a369cc7a489c3dafdd95 Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 26 Sep 2026 11:54:38 -0600 Subject: [PATCH] app: useSearchChannels follows its caller's tags The hook read its tag argument only as the initial state, so the Other tab never refetched when the filter modal changed the URL's tags while the tab stayed mounted; the list, including a stale "No Channels", was whatever the mount fetched. The internal tag state now follows the argument's contents, keyed on the serialized list because the caller builds a new array every render. Tests change the hook argument after a loaded result and expect a second search and a reset to pending, check that a new array with the same tags does not refetch, and keep the slow-response drop. Co-Authored-By: Claude Fable 5.1 --- app/src/hooks/customHooks.js | 8 +++++++ app/src/hooks/customHooks.test.js | 38 +++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/app/src/hooks/customHooks.js b/app/src/hooks/customHooks.js index 668aab21..8836aab2 100644 --- a/app/src/hooks/customHooks.js +++ b/app/src/hooks/customHooks.js @@ -2107,6 +2107,14 @@ export const useSearchChannels = (defaultTagNames) => { } }; + // The caller's tags come from the URL and change in place while the view stays mounted, so + // follow their contents. Keyed on the serialized list because the caller builds a new array + // every render; adopting it by reference would refetch forever. + const tagKey = JSON.stringify(defaultTagNames || []); + useEffect(() => { + setTagNames(prev => JSON.stringify(prev) === tagKey ? prev : JSON.parse(tagKey)); + }, [tagKey]); + useEffect(() => { localSearchChannels(); }, [tagNames]); diff --git a/app/src/hooks/customHooks.test.js b/app/src/hooks/customHooks.test.js index 707a5b63..0df9eaab 100644 --- a/app/src/hooks/customHooks.test.js +++ b/app/src/hooks/customHooks.test.js @@ -482,16 +482,44 @@ describe('useSearchChannels', () => { } }); - test('changing tags resets to pending, and a slow response for the old tags is dropped', async () => { + test('a changed tag argument refetches and resets a loaded result to pending', async () => { + // The Other tab passes the URL's tags on every render; the filter modal changes them in + // place while the tab stays mounted. + searchChannels.mockResolvedValue({channels: [{id: 1, name: 'Old'}]}); + const {result, rerender} = renderHook(({tags}) => useSearchChannels(tags), {initialProps: {tags: ['old']}}); + await act(async () => { + }); + expect(result.current.channels).toEqual([{id: 1, name: 'Old'}]); + expect(searchChannels).toHaveBeenCalledTimes(1); + + searchChannels.mockReturnValue(new Promise(() => { + })); + rerender({tags: ['new']}); + expect(searchChannels).toHaveBeenCalledTimes(2); + expect(searchChannels).toHaveBeenLastCalledWith(['new']); + expect(result.current.channels).toBeNull(); + expect(result.current.loading).toBe(true); + }); + + test('a new array with the same tags does not refetch', async () => { + searchChannels.mockResolvedValue({channels: []}); + const {rerender} = renderHook(({tags}) => useSearchChannels(tags), {initialProps: {tags: ['a']}}); + await act(async () => { + }); + rerender({tags: ['a']}); + await act(async () => { + }); + expect(searchChannels).toHaveBeenCalledTimes(1); + }); + + test('a slow response for the old tags is dropped after the tags change', async () => { let resolveOld; searchChannels.mockReturnValueOnce(new Promise(res => resolveOld = res)); - const {result} = renderHook(() => useSearchChannels(['old'])); + const {result, rerender} = renderHook(({tags}) => useSearchChannels(tags), {initialProps: {tags: ['old']}}); expect(result.current.loading).toBe(true); searchChannels.mockResolvedValue({channels: [{id: 2, name: 'New'}]}); - act(() => { - result.current.setTagNames(['new']); - }); + rerender({tags: ['new']}); expect(result.current.channels).toBeNull(); expect(result.current.loading).toBe(true); await act(async () => {