From 49812ee20d5f0c2a3cc3faa416e798fd89c7e5d0 Mon Sep 17 00:00:00 2001 From: Nicholas-Xiong <2482929840@qq.com> Date: Wed, 19 Aug 2026 13:38:31 +0800 Subject: [PATCH] fix: clear stale urlSearchResults for non-matching HTTPS URLs in useUrlSearch (#16700) Co-authored-by: vasco --- .../features/home/use-url-search.test.tsx | 40 +++++++++++++++++++ .../home/git-repo-dropdown/use-url-search.tsx | 2 + 2 files changed, 42 insertions(+) diff --git a/__tests__/components/features/home/use-url-search.test.tsx b/__tests__/components/features/home/use-url-search.test.tsx index 70489f5d95..17de80a730 100644 --- a/__tests__/components/features/home/use-url-search.test.tsx +++ b/__tests__/components/features/home/use-url-search.test.tsx @@ -238,4 +238,44 @@ describe("useUrlSearch", () => { }); }); }); + it("should clear prior results when an HTTPS URL does not match repo pattern", async () => { + mockSearchGitRepositories.mockResolvedValue({ + items: [ + { + id: "1", + full_name: "owner/repo", + git_provider: "github", + is_public: true, + }, + ], + next_page_id: null, + }); + + const { result, rerender } = renderHook( + ({ inputValue, provider }) => useUrlSearch(inputValue, provider), + { + initialProps: { + inputValue: "https://github.com/owner/repo", + provider: "github" as const, + }, + }, + ); + + await waitFor(() => { + expect(result.current.urlSearchResults).toHaveLength(1); + }); + + rerender({ + inputValue: "https://example.com/", + provider: "github" as const, + }); + + await waitFor(() => { + expect(result.current.urlSearchResults).toEqual([]); + }); + + // Only the initial search for owner/repo should have triggered a call; + // the non-matching HTTPS URL must not issue a second request. + expect(mockSearchGitRepositories).toHaveBeenCalledTimes(1); + }); }); diff --git a/src/components/features/home/git-repo-dropdown/use-url-search.tsx b/src/components/features/home/git-repo-dropdown/use-url-search.tsx index c6d8181fa7..d375cad6a7 100644 --- a/src/components/features/home/git-repo-dropdown/use-url-search.tsx +++ b/src/components/features/home/git-repo-dropdown/use-url-search.tsx @@ -38,6 +38,8 @@ export function useUrlSearch( } finally { setIsUrlSearchLoading(false); } + } else { + setUrlSearchResults([]); } } else { setUrlSearchResults([]);