Skip to content

Replace discarded isExportingKB useState with a ref (or wire it to UI) in DocumentsSidebar #1250

@MODSetter

Description

@MODSetter

Description

surfsense_web/components/layout/ui/sidebar/DocumentsSidebar.tsx declares a boolean state where the value is destructured into _ (discarded) and only the setter is used. Every call to that setter triggers a re-render of this sidebar without changing any visible UI.

Evidence

File: surfsense_web/components/layout/ui/sidebar/DocumentsSidebar.tsx

// Line 481 — value intentionally discarded
const [, setIsExportingKB] = useState(false);

rg "setIsExportingKB|isExportingKB" on this file confirms:

  • Line 481: declaration (tuple position 0 discarded)
  • Line 511: setIsExportingKB(true);
  • Line 527: setIsExportingKB(false); (finally block)
  • Line 563: setIsExportingKB(true);
  • Line 579: setIsExportingKB(false); (finally block)

No other occurrences. There is no consumer of the value (no loading spinner, no disabled-button, no UI reading this flag).

What to do

Pick one path — prefer Option A unless you intend to ship loading UX in the same PR:

Option A (preferred): Convert to a useRef

Replaces state updates (which schedule re-renders) with ref mutations (which don't), preserving the existing runtime tracking while eliminating wasted renders.

  1. Replace line 481:
const isExportingKBRef = useRef(false);

(Add useRef to the React import if not already present.)

  1. Replace the four setter calls:
isExportingKBRef.current = true;   // replaces setIsExportingKB(true);
isExportingKBRef.current = false;  // replaces setIsExportingKB(false);

(lines 511, 527, 563, 579)

Option B: Wire it to UI (larger scope)

If the intent was to show a spinner / disable the export button while the KB is exporting, keep useState, destructure the value, and reflect it in the affected button/menu item (e.g. disabled={isExportingKB}, show a spinner). Only take this route if you want to actually ship the loading UX in this PR.

Verify

  1. Run the app, open the documents sidebar
  2. Trigger the KB export flow — it completes successfully (both code paths at ~511 and ~563)
  3. On error paths, finally still runs and isExportingKBRef.current returns to false
  4. pnpm build succeeds

Acceptance criteria

  • No useState in this file whose value is discarded
  • Export flow still works identically
  • Build passes

Notes

If you go with Option A and later decide to add loading UX, migrating back from useRef to useState is trivial — but for now, a ref matches the actual usage.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions