From ddbad7a13f6469ea09c4aef3e3aabfaa18c15885 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Sun, 22 Mar 2026 16:28:35 -0700 Subject: [PATCH] Fix PR status display: deduplicate concurrent requests and simplify cache logic (#38) --- .../src/components/sidebar/WorktreeCard.tsx | 14 +--- src/renderer/src/store/slices/github.ts | 81 ++++++++++++------- 2 files changed, 54 insertions(+), 41 deletions(-) diff --git a/src/renderer/src/components/sidebar/WorktreeCard.tsx b/src/renderer/src/components/sidebar/WorktreeCard.tsx index 63b7f99b6..ff87a49e8 100644 --- a/src/renderer/src/components/sidebar/WorktreeCard.tsx +++ b/src/renderer/src/components/sidebar/WorktreeCard.tsx @@ -114,20 +114,12 @@ const WorktreeCard = React.memo(function WorktreeCard({ return liveTabs.length > 0 ? 'active' : 'inactive' }, [hasTerminals, tabs]) - // Fetch PR data (debounced via ref guard) - const prFetchedRef = useRef(null) + // Fetch PR data on mount – the store's isFresh() check prevents redundant API calls useEffect(() => { - if ( - repo && - !worktree.isBare && - pr === undefined && - prCacheKey && - prCacheKey !== prFetchedRef.current - ) { - prFetchedRef.current = prCacheKey + if (repo && !worktree.isBare && prCacheKey) { fetchPRForBranch(repo.path, branch) } - }, [repo, worktree.isBare, pr, fetchPRForBranch, branch, prCacheKey]) + }, [repo, worktree.isBare, fetchPRForBranch, branch, prCacheKey]) // Fetch issue data (debounced via ref guard) const issueFetchedRef = useRef(null) diff --git a/src/renderer/src/store/slices/github.ts b/src/renderer/src/store/slices/github.ts index 57bc62b92..41f8f7dd6 100644 --- a/src/renderer/src/store/slices/github.ts +++ b/src/renderer/src/store/slices/github.ts @@ -9,6 +9,9 @@ export interface CacheEntry { const CACHE_TTL = 300_000 // 5 minutes (stale data shown instantly, then refreshed) +const inflightPRRequests = new Map>() +const inflightIssueRequests = new Map>() + function isFresh(entry: CacheEntry | undefined): entry is CacheEntry { return entry !== undefined && Date.now() - entry.fetchedAt < CACHE_TTL } @@ -57,43 +60,61 @@ export const createGitHubSlice: StateCreator = (s const cacheKey = `${repoPath}::${branch}` const cached = get().prCache[cacheKey] if (isFresh(cached)) return cached.data + const inflightRequest = inflightPRRequests.get(cacheKey) + if (inflightRequest) return inflightRequest - try { - const pr = await window.api.gh.prForBranch({ repoPath, branch }) - set((s) => ({ - prCache: { ...s.prCache, [cacheKey]: { data: pr, fetchedAt: Date.now() } } - })) - debouncedSaveCache(get()) - return pr - } catch (err) { - console.error('Failed to fetch PR:', err) - set((s) => ({ - prCache: { ...s.prCache, [cacheKey]: { data: null, fetchedAt: Date.now() } } - })) - debouncedSaveCache(get()) - return null - } + const request = (async () => { + try { + const pr = await window.api.gh.prForBranch({ repoPath, branch }) + set((s) => ({ + prCache: { ...s.prCache, [cacheKey]: { data: pr, fetchedAt: Date.now() } } + })) + debouncedSaveCache(get()) + return pr + } catch (err) { + console.error('Failed to fetch PR:', err) + set((s) => ({ + prCache: { ...s.prCache, [cacheKey]: { data: null, fetchedAt: Date.now() } } + })) + debouncedSaveCache(get()) + return null + } finally { + inflightPRRequests.delete(cacheKey) + } + })() + + inflightPRRequests.set(cacheKey, request) + return request }, fetchIssue: async (repoPath, number) => { const cacheKey = `${repoPath}::${number}` const cached = get().issueCache[cacheKey] if (isFresh(cached)) return cached.data + const inflightRequest = inflightIssueRequests.get(cacheKey) + if (inflightRequest) return inflightRequest - try { - const issue = await window.api.gh.issue({ repoPath, number }) - set((s) => ({ - issueCache: { ...s.issueCache, [cacheKey]: { data: issue, fetchedAt: Date.now() } } - })) - debouncedSaveCache(get()) - return issue - } catch (err) { - console.error('Failed to fetch issue:', err) - set((s) => ({ - issueCache: { ...s.issueCache, [cacheKey]: { data: null, fetchedAt: Date.now() } } - })) - debouncedSaveCache(get()) - return null - } + const request = (async () => { + try { + const issue = await window.api.gh.issue({ repoPath, number }) + set((s) => ({ + issueCache: { ...s.issueCache, [cacheKey]: { data: issue, fetchedAt: Date.now() } } + })) + debouncedSaveCache(get()) + return issue + } catch (err) { + console.error('Failed to fetch issue:', err) + set((s) => ({ + issueCache: { ...s.issueCache, [cacheKey]: { data: null, fetchedAt: Date.now() } } + })) + debouncedSaveCache(get()) + return null + } finally { + inflightIssueRequests.delete(cacheKey) + } + })() + + inflightIssueRequests.set(cacheKey, request) + return request } })