Repository navigation
feat(preferences): sync the dark mode choice across devices - #2342
Open
officialasishkumar wants to merge 1 commit into
Open
officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
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
|
@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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 clearslocalStorage.Change
Server
Themestring field tocontracts_Users.updatepreferencesacceptsThemeasdarkorlightand rejects anything else withInvalid theme..Themecan be the only preference in a request, which needed a change to the guard at the top of the function.getUserDetailsalready returns the wholecontracts_Usersrecord, so it needed no change.Frontend
ThemeTogglesaves the choice for signed-in users. If saving fails, the theme still applies on the device and the error is logged.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.jsholds the shared helpers.Behaviour to review
Tests
Neither app has a test script at the moment, though
jasmineandvitestare already dev dependencies. I added atestscript to each.spec/updatePreferences.spec.js: valid values, invalid values,Themeas 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.jsxhave 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:
lightbeats a device set todarkThemeunchangedvite buildalso succeeds.