refactor: extract a generic useStorageValue hook for localStorage state#163
Conversation
📝 WalkthroughWalkthroughThree React hooks that independently managed localStorage state with identical boilerplate patterns are consolidated into thin wrappers around a new shared ChangeslocalStorage State Consolidation via useStorageValue
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
src/lib/settings.tsESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox. src/lib/tracked-miners.tsESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox. src/lib/tracked-repos.tsESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.
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 |
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 `@src/lib/settings.ts`:
- Around line 43-52: The parse() function must guard against non-object JSON
(e.g., JSON null or primitives) before treating parsed as an AppSettings object:
after JSON.parse(raw) (referenced as parsed), check that parsed is a non-null
object (typeof parsed === 'object' && parsed !== null) and only then spread its
properties and read parsed.layout; otherwise fall back to returning
DEFAULT_SETTINGS (or merging only safe defaults). Update parse to validate
parsed before accessing parsed.layout and only cast to Partial<AppSettings>
after the object check so corrupt/legacy storage values use default settings
instead of causing runtime errors.
🪄 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: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 2720c91f-4a56-4d35-bc4a-19c7f4ed126d
📒 Files selected for processing (4)
src/lib/settings.tssrc/lib/tracked-miners.tssrc/lib/tracked-repos.tssrc/lib/use-storage-value.ts
Summary
Adds
src/lib/use-storage-value.ts, a generic hook that encapsulates the localStorage read, write, parse, serialize, and cross tab event subscription pattern that was previously duplicated across three files.useTrackedRepos,useTrackedMiners, anduseSettingsare now thin wrappers around it.The public hook APIs and the custom event names (
tracked-repos-changed,tracked-miners-changed,settings-changed) are unchanged, so no call sites needed to be touched. The wrappers preserve the original behavior of always reflecting the parsed disk value after a write (toggle and update still re read from localStorage so rapid successive writes do not race).Related Issues
Closes #162
Type of Change
Testing
pnpm buildpassesChecklist
Summary by CodeRabbit