Skip to content

Conversation

@guitavano
Copy link
Contributor

@guitavano guitavano commented Nov 11, 2025

WIP

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 11, 2025

Walkthrough

Optimization of the usage settings component to conditionally fetch integrations and thread audit events based on the active usage type tab, reducing unnecessary API calls when specific tabs aren't displayed.

Changes

Cohort / File(s) Summary
Usage settings optimization
apps/web/src/components/settings/usage/filters.tsx, apps/web/src/components/settings/usage/usage.tsx
Conditional data fetching based on active tab: integrations now only fetch when usageType === "contract", thread audit events only when usageType === "thread". Updates thread data source reference from threads.data?.threads to threadsData?.threads in usage.tsx.

Sequence Diagram

sequenceDiagram
    actor User
    participant Component
    participant useIntegrations
    participant useAuditEvents
    participant API

    User->>Component: Switch to "contract" tab
    Component->>useIntegrations: shouldFetch: true
    useIntegrations->>API: Fetch integrations
    API-->>useIntegrations: Return data
    useIntegrations-->>Component: integrations loaded

    User->>Component: Switch to "thread" tab
    Component->>useAuditEvents: enabled: true
    useAuditEvents->>API: Fetch thread audit events
    API-->>useAuditEvents: Return data
    useAuditEvents-->>Component: threadsData loaded

    User->>Component: Switch to other tab
    Component->>useIntegrations: shouldFetch: false
    useIntegrations-->>Component: skip fetch
    Component->>useAuditEvents: enabled: false
    useAuditEvents-->>Component: skip fetch
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10–12 minutes

  • Consistent optimization pattern applied across both files
  • Straightforward conditional logic using shouldFetch and enabled parameters
  • Single prop reference update (threads.data?.threadsthreadsData?.threads)

Poem

🐰 Two tabs, two hooks, one clever dance,
We fetch only what the tab demands at a glance!
No wasted API calls in the night,
Your settings load swift and light! ✨

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description check ⚠️ Warning The pull request description is completely empty, missing all required sections from the template including 'What is this contribution about?', 'Screenshots/Demonstration', and 'Review Checklist'. Add a complete description following the template: explain why these optimizations are needed, include any relevant UI screenshots, and complete the review checklist.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title 'improve usage screen loadtime' accurately reflects the main objective of the changeset, which optimizes the usage screen by conditionally fetching integrations and deferring expensive data loads.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch tavano/fix-usage-loadtime

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 and usage tips.

@cloudflare-workers-and-pages
Copy link

Deploying decocms-admin-frontend with  Cloudflare Pages  Cloudflare Pages

Latest commit: 5170544
Status: ✅  Deploy successful!
Preview URL: https://149b3680.admin-decocms-frontend.pages.dev
Branch Preview URL: https://tavano-fix-usage-loadtime.admin-decocms-frontend.pages.dev

View logs

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f8bb115 and 5170544.

📒 Files selected for processing (2)
  • apps/web/src/components/settings/usage/filters.tsx (1 hunks)
  • apps/web/src/components/settings/usage/usage.tsx (2 hunks)
🧰 Additional context used
📓 Path-based instructions (7)
apps/web/**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (apps/web/.cursor/rules/posthog-integration.mdc)

apps/web/**/*.{js,jsx,ts,tsx}: Use each feature flag in as few places as possible; if a flag must appear at multiple callsites, explicitly flag this for careful developer review
Gate any flag-dependent code behind checks that verify the flag’s values are valid and expected
If a custom person or event property is referenced in two or more files or at two or more callsites in the same file, centralize the keys in an enum (TS) or const object (JS)

Files:

  • apps/web/src/components/settings/usage/usage.tsx
  • apps/web/src/components/settings/usage/filters.tsx
apps/web/**/*.{ts,tsx}

📄 CodeRabbit inference engine (apps/web/.cursor/rules/posthog-integration.mdc)

In TypeScript, store feature flag names in an enum with members written UPPERCASE_WITH_UNDERSCORE and use a consistent naming convention

Files:

  • apps/web/src/components/settings/usage/usage.tsx
  • apps/web/src/components/settings/usage/filters.tsx
apps/web/**

📄 CodeRabbit inference engine (AGENTS.md)

Place the Vite/React web client in apps/web

Files:

  • apps/web/src/components/settings/usage/usage.tsx
  • apps/web/src/components/settings/usage/filters.tsx
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.{ts,tsx,js,jsx}: Biome formatting: use two-space indentation and double quotes
Keep imports sorted
Name hooks and utility functions using camelCase

Files:

  • apps/web/src/components/settings/usage/usage.tsx
  • apps/web/src/components/settings/usage/filters.tsx
**/*.{tsx,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

Name React components and classes using PascalCase

Files:

  • apps/web/src/components/settings/usage/usage.tsx
  • apps/web/src/components/settings/usage/filters.tsx
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Prefer specific TypeScript types over any

Files:

  • apps/web/src/components/settings/usage/usage.tsx
  • apps/web/src/components/settings/usage/filters.tsx
{apps/web,packages}/**/*.{tsx,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

Keep Tailwind design tokens consistent with the design system

Files:

  • apps/web/src/components/settings/usage/usage.tsx
  • apps/web/src/components/settings/usage/filters.tsx
🧬 Code graph analysis (2)
apps/web/src/components/settings/usage/usage.tsx (5)
packages/sdk/src/mcp/schema.ts (1)
  • integrations (351-363)
packages/sdk/src/hooks/mcp.ts (1)
  • useIntegrations (141-175)
packages/sdk/src/hooks/wallet.ts (3)
  • useUsagePerAgent (29-42)
  • useUsagePerThread (47-59)
  • useContractsCommits (82-94)
apps/web/src/components/settings/usage/members.ts (1)
  • useMembersWithUnknownUsers (59-85)
packages/sdk/src/hooks/audit.ts (1)
  • useAuditEvents (6-15)
apps/web/src/components/settings/usage/filters.tsx (1)
packages/sdk/src/hooks/mcp.ts (1)
  • useIntegrations (141-175)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (3)
apps/web/src/components/settings/usage/filters.tsx (1)

34-37: LGTM! Optimization aligns fetch with usage.

The conditional fetch correctly ensures integrations are only loaded when displaying the contract tab. Since the contract dropdown (lines 63-81) only renders when usageType === "contract", this optimization eliminates unnecessary API calls without affecting functionality.

apps/web/src/components/settings/usage/usage.tsx (2)

73-78: LGTM! Well-documented optimization.

The conditional fetch is correctly aligned with usage (line 146 in chartData), and the detailed comment explains the performance rationale. React Query will deduplicate this request with the identical call in filters.tsx.


299-299: LGTM! Safe handling of conditionally-fetched data.

The variable rename improves clarity, and the optional chaining with || [] fallback safely handles the case when threadsData is undefined (when usageType !== "thread"). Since ThreadsTable only renders when usageType === "thread" (line 293), the data will be available when needed.

Comment on lines +98 to 104
// ONLY fetch audit events when actually viewing thread tab
// This is the most expensive query (100 records) - defer it!
const { data: threadsData } = useAuditEvents({
orderBy: "updatedAt_desc",
limit: 100,
enabled: usageType === "thread",
});
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Verify that enabled option is properly forwarded to React Query.

The useAuditEvents hook receives enabled: usageType === "thread" but the implementation shown in packages/sdk/src/hooks/audit.ts (lines 5-14) doesn't destructure or forward enabled to the useQuery config. If enabled is being passed as part of the generic options object, it will:

  1. Pollute the query key (causing unnecessary cache variations)
  2. Potentially be passed to listThreads (which shouldn't receive React Query options)
  3. Not control query execution (defeating this optimization)

For conditional queries, enabled must be passed directly to useQuery's config, not through the options object.


🏁 Script executed:

#!/bin/bash
# Verify that useAuditEvents properly forwards the enabled option to useQuery

echo "=== Current useAuditEvents implementation ==="
rg -A 15 "export const useAuditEvents" packages/sdk/src/hooks/audit.ts

echo -e "\n=== Check if ThreadFilterOptions type includes 'enabled' ==="
rg -B 5 -A 10 "ThreadFilterOptions" packages/sdk/src/

echo -e "\n=== Search for any recent changes to useAuditEvents that handle 'enabled' ==="
git log --all --oneline --grep="enabled" --grep="useAuditEvents" -- packages/sdk/src/hooks/audit.ts | head -20

echo -e "\n=== Check if there are other patterns for conditional queries in the SDK ==="
rg -A 10 "enabled.*usageType|shouldFetch" packages/sdk/src/hooks/

Length of output: 12625


Fix enabled handling in useAuditEvents hook to properly implement conditional fetching.

The code passes enabled: usageType === "thread" to useAuditEvents, but:

  1. ThreadFilterOptions (packages/sdk/src/crud/thread.ts) does not include an enabled property
  2. The hook doesn't extract or forward enabled to React Query's config, so the query executes regardless of the condition
  3. Other hooks in the codebase (e.g., useThreadMessages, useMCPIntegrations) use a separate shouldFetch parameter extracted and checked within queryFn

Required fix: Either extract enabled and pass it to useQuery's config object, or adopt the shouldFetch pattern used consistently elsewhere in packages/sdk/src/hooks/.

🤖 Prompt for AI Agents
In apps/web/src/components/settings/usage/usage.tsx around lines 98 to 104, the
call to useAuditEvents uses enabled: usageType === "thread" but the hook does
not accept or forward an enabled flag so the query always runs; update the hook
call and/or the hook implementation to support conditional fetching: either (A)
change the hook signature to accept an enabled boolean and forward it to the
underlying useQuery config (useQuery(..., { enabled })) or (B) follow the
project's shouldFetch pattern — accept a shouldFetch boolean, check it at the
top of the hook/queryFn (return early/skip) and only execute the query when true
— then update this call to pass usageType === "thread" as the new parameter.

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.

2 participants