Skip to content

perf(desktop): only query diff stats for the active sidebar workspace item - #6098

Merged
Kitenite merged 1 commit into
mainfrom
remove-sidebar-line-check
Aug 1, 2026
Merged

Kitenite merged 1 commit into
mainfrom
remove-sidebar-line-check

Conversation

@Kitenite

@Kitenite Kitenite commented Aug 1, 2026 •

Copy link
Copy Markdown
Member

Summary

Every DashboardSidebarWorkspaceItem called useDiffStats(id), firing a background git.getStatus request per sidebar workspace — even though the expanded row only renders the +/− line-count badge for the active item. On a sidebar with N workspaces, N−1 of those git status reads were pure waste.

  • useDiffStats now takes an optional { enabled } option (defaults to true; hover-card overlay and other callers unaffected) gating the git.getStatus query.
  • The git:changed subscription intentionally stays on while disabled: it's a cheap listener on the shared per-host event bus, and its invalidation marks the cached stats stale so counts refetch when the item becomes active again (with staleTime: Infinity, gating it would freeze counts at their last-active values).
  • DashboardSidebarWorkspaceItem passes { enabled: isActive }.

The v1 WorkspaceSidebar already gates its fetch on hover, so no changes there.

Test plan

  • bun run lint clean, tsc --noEmit passes in apps/desktop
  • Active workspace row still shows +/− diff stats and updates on git changes
  • Switching to a workspace whose files changed while it was inactive refetches (invalidation marks the disabled query stale)

Summary by cubic

Fetch diff stats only for the active sidebar workspace to eliminate redundant git.getStatus calls and reduce background work. useDiffStats now accepts { enabled }, used by DashboardSidebarWorkspaceItem, while the git:changed subscription remains to invalidate stale data so counts refresh when the item becomes active again.

Written for commit aefca5f. Summary will update on new commits.

Review in cubic

@coderabbitai

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 7f86219e-ccbe-487d-91dd-dcb00d353a41

📥 Commits

Reviewing files that changed from the base of the PR and between 7e97ded and aefca5f.

📒 Files selected for processing (2)
  • apps/desktop/src/renderer/hooks/host-service/useDiffStats/useDiffStats.ts
  • apps/desktop/src/renderer/routes/_authenticated/_dashboard/components/DashboardSidebar/components/DashboardSidebarWorkspaceItem/DashboardSidebarWorkspaceItem.tsx

📝 Walkthrough

Walkthrough

useDiffStats now supports conditional querying. DashboardSidebarWorkspaceItem enables diff statistics only for the active workspace while preserving event-based cache invalidation.

Changes

Diff statistics query flow

Layer / File(s) Summary
Conditional diff statistics query
apps/desktop/src/renderer/hooks/host-service/useDiffStats/useDiffStats.ts
useDiffStats accepts an optional enabled flag. The query requires this flag, workspace availability, and host availability. Event subscriptions remain active while disabled.
Active workspace sidebar wiring
apps/desktop/src/renderer/routes/_authenticated/_dashboard/components/DashboardSidebar/components/DashboardSidebarWorkspaceItem/DashboardSidebarWorkspaceItem.tsx
The sidebar enables diff statistics only for the active workspace row. Expanded rows still receive the statistics result.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant DashboardSidebarWorkspaceItem
  participant useDiffStats
  participant WorkspaceEvents
  participant DiffStatsQuery

  DashboardSidebarWorkspaceItem->>useDiffStats: Pass active-row enable state
  useDiffStats->>DiffStatsQuery: Run query when enabled
  WorkspaceEvents->>useDiffStats: Notify workspace changes
  useDiffStats->>DiffStatsQuery: Invalidate cached statistics
Loading

Possibly related PRs

Suggested reviewers: saddlepaddle

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the performance change and follows the required conventional commit format.
Description check ✅ Passed The description explains the change, rationale, implementation details, and test plan, but it omits the template checklist.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch remove-sidebar-line-check

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/desktop/src/renderer/routes/_authenticated/_dashboard/components/DashboardSidebar/components/DashboardSidebarWorkspaceItem/DashboardSidebarWorkspaceItem.tsx">

<violation number="1" location="apps/desktop/src/renderer/routes/_authenticated/_dashboard/components/DashboardSidebar/components/DashboardSidebarWorkspaceItem/DashboardSidebarWorkspaceItem.tsx:82">
P3: The optimization skips the git status query for inactive rows, but the active row still fires the `git.getStatus` query even when the sidebar is collapsed, where the row only renders the collapsed button and never shows the +/− counts. So there's still one wasted background read in a collapsed sidebar. Consider gating on the collapsed state as well so the fetch only happens when the expanded active row can actually render the badge.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant Item as "DashboardSidebarWorkspaceItem"
    participant Hook as "useDiffStats"
    participant Query as "QueryClient (React Query)"
    participant Git as "Host Service (git.getStatus)"
    participant Bus as "Shared git:changed Event Bus"

    Note over Item,Bus: Default enabled: true (hover-card and other callers unaffected)
    Note over Item,Bus: sidebar item passes enabled: isActive
    Item->>Hook: useDiffStats(id, { enabled: isActive })
    Hook->>Bus: subscribe("git:changed", invalidate)
    Hook->>Query: useQuery({ queryKey, enabled })

    alt isActive = true
        Query->>Git: git.getStatus.query({ workspaceId })
        Git-->>Query: DiffStats { additions, deletions }
        Query-->>Hook: diff stats (cached)
        Hook-->>Item: render +N/-M badge
    else isActive = false
        Note over Query,Git: query disabled - no git.getStatus call fired
    end

    opt git:changed event arrives for this workspace
        Bus->>Hook: invalidation callback (subscription kept while disabled)
        Hook->>Query: invalidateQueries({ queryKey })
        alt item is currently active
            Query->>Git: refetch and update stats
        else item is inactive
            Note over Query: stats marked stale (staleTime: Infinity)
            Note over Query: refetched when item becomes active
        end
    end
Loading

Reply with feedback, questions, or to request a fix.

Re-trigger cubic


// Only the active workspace row shows line counts, so skip the per-item
// git status query everywhere else.
const diffStats = useDiffStats(id, { enabled: isActive });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The optimization skips the git status query for inactive rows, but the active row still fires the git.getStatus query even when the sidebar is collapsed, where the row only renders the collapsed button and never shows the +/− counts. So there's still one wasted background read in a collapsed sidebar. Consider gating on the collapsed state as well so the fetch only happens when the expanded active row can actually render the badge.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/desktop/src/renderer/routes/_authenticated/_dashboard/components/DashboardSidebar/components/DashboardSidebarWorkspaceItem/DashboardSidebarWorkspaceItem.tsx, line 82:

<comment>The optimization skips the git status query for inactive rows, but the active row still fires the `git.getStatus` query even when the sidebar is collapsed, where the row only renders the collapsed button and never shows the +/− counts. So there's still one wasted background read in a collapsed sidebar. Consider gating on the collapsed state as well so the fetch only happens when the expanded active row can actually render the badge.</comment>

<file context>
@@ -78,6 +77,10 @@ export function DashboardSidebarWorkspaceItem({
 
+	// Only the active workspace row shows line counts, so skip the per-item
+	// git status query everywhere else.
+	const diffStats = useDiffStats(id, { enabled: isActive });
+
 	const { v2Workspaces: v2WorkspaceActions } = useOptimisticCollectionActions();
</file context>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional: the collapsed active item's query shares its cache key with the hover-card overlay and keeps the badge instant when the sidebar expands, so it's one warm read rather than waste. The N-per-sidebar reads were the problem this PR targets.

@Kitenite
Kitenite merged commit f0b5f31 into main Aug 1, 2026
12 checks passed
@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

🧹 Preview Cleanup Complete

The following preview resources have been cleaned up:

  • ✅ Neon database branch

Thank you for your contribution! 🎉

@Kitenite
Kitenite deleted the remove-sidebar-line-check branch August 1, 2026 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant