Update cached branch list immediately after checkout #48
Summary#
- add
projectGitBranchesToCurrentto project cached branch data to the newly checked-out branch - add
setGitBranchesCurrentBranchto update React Query cache forgit.listBranchesbefore invalidation - wire checkout mutation success handlers to optimistically set the current branch in cache
- add unit tests for branch reordering, missing-branch insertion, and undefined-cache handling
Testing#
apps/web/src/lib/gitReactQuery.test.ts: verifies checked-out branch is moved to top and marked currentapps/web/src/lib/gitReactQuery.test.ts: verifies missing checked-out branch is inserted as currentapps/web/src/lib/gitReactQuery.test.ts: verifies undefined cache input returnsundefined- Not run: project lint/test scripts
Summary by CodeRabbit#
- Tests: Added comprehensive unit tests for Git branch management, covering proper reordering of the current branch to the top of the list, preservation of branch metadata, and handling of new branches.
- Improvements: Git operations (branch checkout and creation) now properly synchronize cached branch information in real-time, ensuring the currently active branch is immediately visible in the UI.
- 1
Update React Query branches cache in apps/web after checkout to mark the checked-out branch current and move it to the top#
Add
projectGitBranchesToCurrentto reorder and flag the current branch, addsetGitBranchesCurrentBranchto update thegitQueryKeys.branches(cwd)cache, and call it fromgitCheckoutMutationOptions.onSuccessandgitCreateBranchAndCheckoutMutationOptions.onSuccess; add tests in gitReactQuery.test.ts.📍Where to Start#
Start with
gitCheckoutMutationOptions.onSuccessin gitReactQuery.ts, then reviewsetGitBranchesCurrentBranchandprojectGitBranchesToCurrent.
Macroscope summarized ee56d5b.Greptile Summary
This PR implements optimistic cache updates for git branch checkout operations to improve UI responsiveness. When a user checks out a branch, the React Query cache is immediately updated to reflect the new current branch before the invalidation triggers a refetch. This eliminates the brief moment where the UI would show stale branch data.
The implementation adds two new exported functions:
projectGitBranchesToCurrentperforms the cache transformation (reordering branches and marking the correct one as current), andsetGitBranchesCurrentBranchapplies this transformation to the query cache. BothgitCheckoutMutationOptionsandgitCreateBranchAndCheckoutMutationOptionsnow call the cache update function in theironSuccesshandlers.Unit tests cover the core scenarios: moving an existing branch to the top and marking it current, inserting a missing branch as current, and gracefully handling undefined cache state.
Confidence Score: 5/5
- This PR is safe to merge with minimal risk
- The changes are well-implemented with comprehensive unit tests covering all edge cases. The optimistic update logic is sound - it properly handles existing branches, missing branches, and undefined cache states. The implementation follows React Query best practices by updating cache before invalidation. No breaking changes or security concerns.
- No files require special attention
Important Files Changed
Filename Overview apps/web/src/lib/gitReactQuery.ts Added optimistic cache update functions to immediately reflect branch checkout before invalidation apps/web/src/lib/gitReactQuery.test.ts New unit tests verify branch reordering, missing branch insertion, and undefined cache handling Sequence Diagram
Last reviewed commit: ee56d5bsequenceDiagram participant User participant BranchToolbar participant Mutation participant Cache participant API User->>BranchToolbar: Select branch to checkout BranchToolbar->>Mutation: gitCheckoutMutation.mutate(branch) Mutation->>API: api.git.checkout({cwd, branch}) API-->>Mutation: checkout success Mutation->>Cache: setGitBranchesCurrentBranch(queryClient, cwd, branch) Note over Cache: Optimistically update cache:<br/>1. Mark new branch as current<br/>2. Move to top of list<br/>3. Unmark old current branch Mutation->>Cache: invalidateGitQueries(queryClient) Note over Cache: Trigger refetch of all git queries Cache-->>BranchToolbar: Updated branch list BranchToolbar->>BranchToolbar: UI reflects new current branch immediatelyjuliusmarminge closed this
Sign in to join this conversation.
Walkthrough#
This PR introduces helpers to manage Git branch state, ensuring the currently checked-out branch appears first in cached branch lists. New functions
projectGitBranchesToCurrentandsetGitBranchesCurrentBranchare added, and mutation success handlers for git checkout and branch creation are updated to synchronize cache state with current branch information.Changes#
apps/web/src/lib/gitReactQuery.tsprojectGitBranchesToCurrentto reorder branches with current branch first, andsetGitBranchesCurrentBranchto update cached branches. UpdatedgitCheckoutMutationOptionsandgitCreateBranchAndCheckoutMutationOptionsmutation success handlers to call the new cache update function. AddedGitListBranchesResultto imports.apps/web/src/lib/gitReactQuery.test.tsprojectGitBranchesToCurrentcovering branch reordering, insertion of missing branches, and undefined input handling.Estimated code review effort#
🎯 2 (Simple) | ⏱️ ~10 minutes
🚥 Pre-merge checks | ✅ 3 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
main✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉
Comment `@coderabbitai help` to get the list of available commands and usage tips.