Refresh PR state on worktree activation

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinwoo-H
2026-05-14 02:49:38 -04:00
co-authored by Orca
parent 169ab144fb
commit 20319c277c
4 changed files with 60 additions and 12 deletions
@@ -390,7 +390,7 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
const virtualItems = virtualizer.getVirtualItems()
useEffect(() => {
if (!cardProps.includes('pr') && !cardProps.includes('ci')) {
if (groupBy !== 'pr-status' && !cardProps.includes('pr') && !cardProps.includes('ci')) {
if (lastVisibleRefreshKeyRef.current !== '__hidden__') {
lastVisibleRefreshKeyRef.current = '__hidden__'
reportVisibleGitHubPRRefreshCandidates([], Date.now())
@@ -420,6 +420,7 @@ const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewp
reportVisibleGitHubPRRefreshCandidates(visibleWorktreeIds, Date.now())
}, [
cardProps,
groupBy,
reportVisibleGitHubPRRefreshCandidates,
rows,
sshConnectedGeneration,
@@ -10,6 +10,7 @@ import type { PRInfo } from '../../../../shared/types'
const mockApi = {
gh: {
prForBranch: vi.fn().mockResolvedValue(null),
enqueuePRRefresh: vi.fn().mockResolvedValue(undefined),
issue: vi.fn().mockResolvedValue(null),
prChecks: vi.fn().mockResolvedValue([]),
listWorkItems: vi.fn()
@@ -262,6 +263,56 @@ describe('createGitHubSlice.fetchPRForBranch', () => {
})
})
describe('createGitHubSlice.refreshGitHubForWorktreeIfStale', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('enqueues active PR refresh even when the cached PR is fresh', () => {
const store = createTestStore()
const repoPath = '/repo'
const branch = 'feature/test'
const worktreeId = 'wt-1'
store.setState({
repos: [{ id: 'repo-1', path: repoPath, name: 'repo', kind: 'git' }],
worktreesByRepo: {
'repo-1': [
{
id: worktreeId,
repoId: 'repo-1',
path: '/repo/worktrees/test',
branch,
displayName: 'test',
isMainWorktree: false,
isBare: false,
isArchived: false
}
]
},
prCache: {
[`${repoPath}::${branch}`]: {
data: makePR({ state: 'open' }),
fetchedAt: Date.now()
}
}
} as unknown as Partial<AppState>)
store.getState().refreshGitHubForWorktreeIfStale(worktreeId)
expect(mockApi.gh.enqueuePRRefresh).toHaveBeenCalledWith({
candidate: expect.objectContaining({
repoPath,
branch,
cacheKey: `${repoPath}::${branch}`,
cachedPRState: 'open'
}),
reason: 'active',
priority: 80
})
})
})
describe('createGitHubSlice.fetchWorkItems source/error envelope', () => {
beforeEach(() => {
vi.clearAllMocks()
+5 -8
View File
@@ -1802,9 +1802,9 @@ export const createGitHubSlice: StateCreator<AppState, [], [], GitHubSlice> = (s
})
},
// Why: worktree switches previously force-refreshed GitHub data on every
// click, bypassing the 5-min TTL. This variant only fetches when stale,
// avoiding unnecessary API calls and latency during rapid switching.
// Why: activation is the user's strongest freshness signal. A PR can merge
// seconds after the last sidebar poll; enqueue through the coordinator so
// clicks revalidate PR state without bypassing coalescing/rate-limit guards.
refreshGitHubForWorktreeIfStale: (worktreeId) => {
const state = get()
let worktree: Worktree | undefined
@@ -1823,13 +1823,9 @@ export const createGitHubSlice: StateCreator<AppState, [], [], GitHubSlice> = (s
return
}
const now = Date.now()
const branch = worktree.branch.replace(/^refs\/heads\//, '')
const prKey = `${repo.path}::${branch}`
const prEntry = state.prCache[prKey]
const prStale = !prEntry || now - prEntry.fetchedAt >= CACHE_TTL
if (!worktree.isBare && branch && prStale) {
if (!worktree.isBare && branch) {
const candidate = buildPRRefreshCandidate(state, worktree)
if (candidate) {
void window.api.gh.enqueuePRRefresh?.({ candidate, reason: 'active', priority: 80 })
@@ -1837,6 +1833,7 @@ export const createGitHubSlice: StateCreator<AppState, [], [], GitHubSlice> = (s
}
if (worktree.linkedIssue) {
const now = Date.now()
const issueKey = `${repo.path}::${worktree.linkedIssue}`
const issueEntry = state.issueCache[issueKey]
if (!issueEntry || now - issueEntry.fetchedAt >= CACHE_TTL) {
+2 -3
View File
@@ -926,9 +926,8 @@ export const createWorktreeSlice: StateCreator<AppState, [], [], WorktreeSlice>
}
})
// Why: force-refreshing GitHub data on every switch burned API rate limit
// quota and added 200-800ms latency. Only refresh when cache is actually
// stale (>5 min old). Users can still force-refresh via the sidebar button.
// Why: activation is explicit enough to revalidate PR state immediately;
// the GitHub coordinator still coalesces requests and applies rate guards.
if (worktreeId) {
get().refreshGitHubForWorktreeIfStale(worktreeId)
}