Skip to content

fix(theme-toggle): use functional setIsDark in toggleTheme (#1247)#1272

Merged
MODSetter merged 1 commit intoMODSetter:devfrom
mvanhorn:osc/1247-functional-setIsDark
Apr 21, 2026
Merged

fix(theme-toggle): use functional setIsDark in toggleTheme (#1247)#1272
MODSetter merged 1 commit intoMODSetter:devfrom
mvanhorn:osc/1247-functional-setIsDark

Conversation

@mvanhorn
Copy link
Copy Markdown
Contributor

@mvanhorn mvanhorn commented Apr 20, 2026

Description

Swap setIsDark(!isDark)setIsDark((prev) => !prev) in toggleTheme and drop isDark from the useCallback dependency list.

Motivation and Context

FIX #1247

The previous implementation read isDark from the closure, which forced it into the useCallback deps. Every theme click therefore produced a new toggleTheme reference, invalidating any downstream memoization.

Functional updaters are the React-recommended pattern when a state transition is derived from the previous value (a boolean flip qualifies), and they're also safer under concurrent updates.

The sibling setCrazyLightTheme / setCrazyDarkTheme callbacks already follow this pattern — they pass concrete values to setIsDark and don't list isDark in deps. This PR brings toggleTheme in line with its two siblings.

Screenshots

Not applicable — no visual change. Clicking the theme toggle still flips isDark the same way.

API Changes

  • This PR includes API changes

Change Type

  • Bug fix
  • New feature
  • Performance improvement
  • Refactoring
  • Documentation
  • Dependency/Build system
  • Breaking change
  • Other (specify):

Testing Performed

  • Tested locally (2-line diff; behavior unchanged)
  • Manual/QA verification

Checklist

  • Follows project coding standards and conventions
  • Documentation updated as needed
  • Dependencies updated as needed
  • No lint/build errors or new warnings
  • All relevant tests are passing

High-level PR Summary

This PR refactors the toggleTheme function to use a functional state updater pattern (setIsDark((prev) => !prev)) instead of reading isDark directly from the closure. This removes isDark from the useCallback dependency array, preventing unnecessary re-creation of the toggleTheme callback on every theme change and improving memoization stability. The change aligns toggleTheme with the existing pattern used by its sibling functions setCrazyLightTheme and setCrazyDarkTheme.

⏱️ Estimated Review Time: 5-15 minutes

💡 Review Order Suggestion
Order File Path
1 surfsense_web/components/theme/theme-toggle.tsx

Need help? Join our Discord

Analyze latest changes

…#1247)

Closes MODSetter#1247.

toggleTheme's previous implementation read isDark from the closure via
setIsDark(!isDark), which forced isDark into the useCallback dependency
array. As a result toggleTheme's reference changed on every click,
invalidating any downstream memoization.

Switched to the functional updater setIsDark((prev) => !prev) and
dropped isDark from the dependency list. The sibling setCrazyLightTheme
and setCrazyDarkTheme callbacks already use this pattern (they pass
concrete values to setIsDark without listing isDark in deps), so this
keeps the three theme callbacks consistent.

No observable behavior change — clicking the theme toggle still flips
isDark. The callback reference is now stable between clicks, which is
also safer under concurrent updates per React's standard guidance.
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 20, 2026

@mvanhorn is attempting to deploy a commit to the Rohan Verma's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 20, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b574d606-ceb4-4b97-8566-d47d7d7e72a7

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Copy link
Copy Markdown

@recurseml recurseml Bot left a comment

Choose a reason for hiding this comment

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

Review by RecurseML

🔍 Review performed on 2b2453e..8cf957b

✨ No bugs found, your code is sparkling clean

✅ Files analyzed, no issues (1)

surfsense_web/components/theme/theme-toggle.tsx

@MODSetter MODSetter merged commit 517c40f into MODSetter:dev Apr 21, 2026
8 of 11 checks passed
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