From 08d51c7bf20f7c0b720fa807fa41d78054d95c35 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:10:16 -0800 Subject: [PATCH] fix(native-chat): stop routed GitHub previews rescanning repos on unrelated store updates Co-Authored-By: Claude Opus 5.5 --- .../previews/NativeChatGitHubLinkPeek.tsx | 14 ++- .../native-chat-github-link-preview.ts | 5 +- ...-chat-github-routed-preview-store.test.tsx | 115 ++++++++++++++++++ 3 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 src/renderer/src/components/native-chat/links/previews/native-chat-github-routed-preview-store.test.tsx diff --git a/src/renderer/src/components/native-chat/links/previews/NativeChatGitHubLinkPeek.tsx b/src/renderer/src/components/native-chat/links/previews/NativeChatGitHubLinkPeek.tsx index e90c95efad8..31c0d9e56d1 100644 --- a/src/renderer/src/components/native-chat/links/previews/NativeChatGitHubLinkPeek.tsx +++ b/src/renderer/src/components/native-chat/links/previews/NativeChatGitHubLinkPeek.tsx @@ -1,4 +1,4 @@ -import type { ReactNode } from 'react' +import { useMemo, type ReactNode } from 'react' import { useAppStore } from '@/store/index' import { Button } from '@/components/ui/button' import { GitHubIcon } from '@/components/icons/GitHubIcon' @@ -105,10 +105,14 @@ export function NativeChatGitHubPeekBody({ * Null shows just the name until then. Undefined for the chat repo's own links. */ otherRepoFallback?: ReactNode }): ReactNode { - const key = useAppStore((state) => - source.kind === 'repo' - ? githubLinkPreviewKey(source.repo, link) - : routedGitHubLinkPreviewKey(source.authorityKey, link, state.repos) + // Select the catalog itself so unrelated store updates don't rescan it for a route. + const routeRepos = useAppStore((state) => (source.kind === 'routed' ? state.repos : null)) + const key = useMemo( + () => + source.kind === 'repo' + ? githubLinkPreviewKey(source.repo, link) + : routedGitHubLinkPreviewKey(source.authorityKey, link, routeRepos ?? []), + [source, link, routeRepos] ) return ( () - const stop = useAppStore.subscribe((state) => { + const stop = useAppStore.subscribe((state, previous) => { + if (state.repos === previous.repos) { + return + } const live = new Set( githubPreviewRouteCandidates(state.repos, routeAuthority).map(githubPreviewRepoIdentity) ) diff --git a/src/renderer/src/components/native-chat/links/previews/native-chat-github-routed-preview-store.test.tsx b/src/renderer/src/components/native-chat/links/previews/native-chat-github-routed-preview-store.test.tsx new file mode 100644 index 00000000000..79eeac21507 --- /dev/null +++ b/src/renderer/src/components/native-chat/links/previews/native-chat-github-routed-preview-store.test.tsx @@ -0,0 +1,115 @@ +// @vitest-environment happy-dom + +import { act } from 'react' +import { createRoot, type Root } from 'react-dom/client' +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import type { Repo } from '../../../../../../shared/repo-types' +import { parseGitHubIssueOrPRLink } from '../../../../../../shared/github/repositories/links' +import { githubRepoIdentityKey } from '../../../../../../shared/github/repositories/repository-identity-key' +import { + clearRepoSlugCacheValues, + rememberRepoSlug, + repoSlugCacheKey +} from '@/lib/workspace/catalog/repo-slug-cache' +import type * as RouteRepo from './native-chat-github-route-repo' + +const mocks = await vi.hoisted(async () => { + const { create } = await import('zustand') + return { + store: create<{ repos: Repo[]; tick: number }>(() => ({ repos: [], tick: 0 })), + endpoint: vi.fn(), + scans: 0 + } +}) +vi.mock('@/store/index', () => ({ useAppStore: mocks.store })) +vi.mock('@/lib/workspace/catalog/repo-slug-index', async () => { + const { readRepoSlugCache, repoSlugCacheKey } = + await import('@/lib/workspace/catalog/repo-slug-cache') + return { + resolveRepoSlug: async (repo: Repo) => { + const key = repoSlugCacheKey(repo) + const cached = key ? readRepoSlugCache(key) : null + return cached?.hit ? cached.value : null + } + } +}) +vi.mock('./native-chat-github-route-repo', async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + githubPreviewRouteCandidates: ( + ...args: Parameters + ) => { + mocks.scans += 1 + return actual.githubPreviewRouteCandidates(...args) + }, + readGitHubPreviewRouteRepo: (...args: Parameters) => { + mocks.scans += 1 + return actual.readGitHubPreviewRouteRepo(...args) + } + } +}) + +const { NativeChatGitHubPeekBody } = await import('./NativeChatGitHubLinkPeek') + +globalThis.IS_REACT_ACT_ENVIRONMENT = true + +function repo(index: number): Repo { + return { + id: `repo-${index}`, + path: `/repos/${index}`, + displayName: `Repo ${index}`, + badgeColor: '', + addedAt: 0 + } +} + +const repos = Array.from({ length: 200 }, (_, index) => repo(index)) +let container: HTMLDivElement +let root: Root + +beforeEach(() => { + clearRepoSlugCacheValues() + for (const row of repos) { + rememberRepoSlug( + repoSlugCacheKey(row)!, + githubRepoIdentityKey({ owner: 'acme', repo: row.id, host: 'github.com' }) + ) + } + mocks.store.setState({ repos, tick: 0 }) + mocks.endpoint.mockReset() + mocks.endpoint.mockReturnValue(new Promise(() => {})) + vi.stubGlobal('api', { gh: { workItemByOwnerRepo: mocks.endpoint } }) + container = document.createElement('div') + document.body.appendChild(container) + root = createRoot(container) +}) +afterEach(() => { + act(() => root.unmount()) + container.remove() + vi.unstubAllGlobals() +}) + +it('leaves a pending routed preview alone on store updates that do not touch repos', async () => { + const link = parseGitHubIssueOrPRLink('https://github.com/acme/repo-7/pull/42')! + await act(async () => { + root.render( + + ) + }) + expect(mocks.endpoint).toHaveBeenLastCalledWith(expect.objectContaining({ repoId: 'repo-7' })) + + mocks.scans = 0 + act(() => { + for (let tick = 1; tick <= 1000; tick += 1) { + mocks.store.setState({ tick }) + } + }) + expect(mocks.scans).toBe(0) + + // Removing the linked repo's clone still reroutes the card through another repo on its host. + await act(async () => { + mocks.store.setState({ repos: repos.filter((row) => row.id !== 'repo-7') }) + }) + expect(mocks.endpoint).toHaveBeenLastCalledWith(expect.objectContaining({ repoId: 'repo-0' })) +})