fix(theme): tolerate blocked web storage#2094
Conversation
📝 WalkthroughWalkthroughThe theme initialization logic in ChangesTheme Storage Safety
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
Superagent didn't find any vulnerabilities or security issues in this PR. |
|
Important Gittensory found maintainer review notesPublic GitHub metadata was checked for review readiness. Gittensor-specific context appears only when confirmed. Readiness score: 93/100
Signal definitions
Review context
Maintainer notes
Contributor next steps
Checked by Gittensory, a quiet PR intelligence layer for OSS maintainers. Learn more about Gittensor contribution workflows. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@apps/web/src/lib/theme.tsx`:
- Around line 47-49: The optional chaining currently only protects the
matchMedia call but not the .matches access, causing a TypeError when
window.matchMedia is undefined; update the reduced computation to fully guard
both call and property access by checking that window.matchMedia exists and that
the result of window.matchMedia("(prefers-reduced-motion: reduce)") is truthy
before reading .matches (i.e., use a complete optional chain or intermediate
variable), ensuring the variable reduced safely falls back to false in
sandboxed/restricted environments where matchMedia is unavailable; locate and
change the reduced assignment in apps/web/src/lib/theme.tsx.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 620fcb5e-b7e7-40de-beb8-92f8be74bec7
📒 Files selected for processing (1)
apps/web/src/lib/theme.tsx
| const reduced = | ||
| typeof window !== "undefined" && | ||
| window.matchMedia("(prefers-reduced-motion: reduce)").matches; | ||
| window.matchMedia?.("(prefers-reduced-motion: reduce)").matches; |
There was a problem hiding this comment.
Incomplete optional chaining will throw when matchMedia is unavailable.
The optional chaining on line 49 only guards the function call, not the .matches property access. If window.matchMedia is undefined, the expression window.matchMedia?.(...) evaluates to undefined, and then .matches attempts to read a property on undefined, throwing a TypeError.
This defeats the PR's goal to tolerate missing APIs in sandboxed or restricted environments, and will crash the provider (which wraps the entire app per the root component).
🔒 Proposed fix to complete the optional chain
const reduced =
typeof window !== "undefined" &&
- window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
+ window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const reduced = | |
| typeof window !== "undefined" && | |
| window.matchMedia("(prefers-reduced-motion: reduce)").matches; | |
| window.matchMedia?.("(prefers-reduced-motion: reduce)").matches; | |
| const reduced = | |
| typeof window !== "undefined" && | |
| window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches; |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@apps/web/src/lib/theme.tsx` around lines 47 - 49, The optional chaining
currently only protects the matchMedia call but not the .matches access, causing
a TypeError when window.matchMedia is undefined; update the reduced computation
to fully guard both call and property access by checking that window.matchMedia
exists and that the result of window.matchMedia("(prefers-reduced-motion:
reduce)") is truthy before reading .matches (i.e., use a complete optional chain
or intermediate variable), ensuring the variable reduced safely falls back to
false in sandboxed/restricted environments where matchMedia is unavailable;
locate and change the reduced assignment in apps/web/src/lib/theme.tsx.
🔍 Maintainer advisory reviewReviewed 1 changed file(s) — two independent AI reviewers. This is an advisory review — it does not merge or close the PR. Suggested action: 🛠️ Request changes. (reviewers split: request changes / merge) Address the items below before merging. Reviewer A · Suggestions
Worth double-checking
Reviewer B · Suggestions
Worth double-checking
|
Motivation
localStorage.getItem('hc-theme')without handlingDOMExceptioncases which can occur when Web Storage is blocked, causing client-side crashes because the provider is mounted globally.Description
isThemetype guard and areadStoredThemehelper that wrapswindow.localStorage.getItemin atry/catchand returnsnullon failure.localStorageread withreadStoredTheme()and preserve existing best-effort writes usingwindow.localStorage.setIteminside the existing guardedtry/catch.window.matchMediato avoid errors in environments wherematchMediais unavailable.ThemeProvideranduseTheme) and the default theme ("light") behavior.Testing
pnpm type-checkand the TypeScript checks completed successfully.git diff --checkand no diff-check issues were reported.Codex Task
Summary by CodeRabbit