Skip to content

feat(preferences): sync the dark mode choice across devices - #2342

Open
officialasishkumar wants to merge 1 commit into
OpenSignLabs:stagingfrom
officialasishkumar:feat/2045-sync-theme-preference
Open

officialasishkumar wants to merge 1 commit into
OpenSignLabs:stagingfrom
officialasishkumar:feat/2045-sync-theme-preference

Conversation

@officialasishkumar

Copy link
Copy Markdown

Fixes #2045

Problem

The dark mode choice is only kept in localStorage. It's lost on a new browser or device and after clearing browser data. It's also lost on logout, because logout clears localStorage.

Change

Server

  • A migration adds a Theme string field to contracts_Users.
  • updatepreferences accepts Theme as dark or light and rejects anything else with Invalid theme.. Theme can be the only preference in a request, which needed a change to the guard at the top of the function.
  • getUserDetails already returns the whole contracts_Users record, so it needed no change.

Frontend

  • ThemeToggle saves the choice for signed-in users. If saving fails, the theme still applies on the device and the error is logged.
  • All three login paths in Login.jsx (password, third-party and existing session) apply the saved theme before redirecting, so the header toggle starts in the right state.
  • src/utils/theme.js holds the shared helpers.

Behaviour to review

  • Saved theme wins at login: this is what makes the choice follow the user.
  • No saved theme: users who never used the toggle keep whatever their device has, so nobody's theme changes because of this upgrade. The local value isn't uploaded until they next use the toggle.
  • Sessions already open: another device that's already signed in picks up a change the next time it goes through the login page, not live.
  • Rapid toggling: each click sends its own request and they aren't queued, so if requests arrive out of order the saved value could differ from the last click.

Tests

Neither app has a test script at the moment, though jasmine and vitest are already dev dependencies. I added a test script to each.

  • spec/updatePreferences.spec.js: valid values, invalid values, Theme as the only field, other preferences left alone, and unauthenticated requests. 7 of the 10 specs fail against the unmodified function.
  • src/components/ThemeToggle.test.jsx: saving when signed in, no call when signed out, and keeping the theme when saving fails.
  • src/utils/theme.test.js: the saved theme overriding the device theme, and missing or invalid saved values being ignored.

The call sites in Login.jsx have no unit test. The browser run below covers the password and existing-session paths, but not third-party login.

Manual verification

I ran the real stack (MongoDB, Parse Server with migrations, Vite dev server) and drove it with headless Chromium, using a separate browser context for each "device". All 20 checks passed, including:

  • the migration adds the field
  • choosing dark on one device is stored in the database, and survives a reload
  • a new device logs in dark, with the toggle on
  • switching to light on that device is stored, and a saved light beats a device set to dark
  • clearing storage on a device, as logout does, and logging in again restores the saved theme
  • a user with no saved theme keeps the device's dark theme
  • the API rejects invalid values and anonymous calls
  • saving another preference leaves Theme unchanged

vite build also succeeds.

The dark mode choice only lived in localStorage, so it was lost on a new
browser or device, after clearing browser data, and on logout, which
clears localStorage.

Store the choice as a new Theme field on contracts_Users. updatepreferences
accepts "dark" or "light", rejects anything else, and accepts Theme as the
only preference in a request. ThemeToggle saves the choice for signed in
users, and still applies it on the device if saving fails. Every login
path in Login.jsx then applies the saved theme before redirecting, so the
header toggle starts in the right state. Users who never chose a theme
have no saved value and keep the theme of their device.

Add jasmine and vitest coverage and a "test" script for each app, using
the dev dependencies that are already declared.

Fixes OpenSignLabs#2045
@vercel

vercel Bot commented Nov 7, 2026

Copy link
Copy Markdown

@officialasishkumar is attempting to deploy a commit to the prafull 's projects Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
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.

Feature Request: Sync Dark Mode Theme Preference to Server/Database

1 participant