Skip to content

Feat/consolidate localstorage stores - #880

Open
Oluwasuyi-Oluwatimilehin-Daniel wants to merge 5 commits into
Iris-IV:mainfrom
Oluwasuyi-Oluwatimilehin-Daniel:feat/consolidate-localstorage-stores
Open

Feat/consolidate localstorage stores#880
Oluwasuyi-Oluwatimilehin-Daniel wants to merge 5 commits into
Iris-IV:mainfrom
Oluwasuyi-Oluwatimilehin-Daniel:feat/consolidate-localstorage-stores

Conversation

@Oluwasuyi-Oluwatimilehin-Daniel

Copy link
Copy Markdown

#closes #835

PR: Consolidate localStorage-backed store modules into shared helper

Issue

#835 [Code Quality] Merge overlapping localStorage-backed store modules (commentStore, reportStore, adminLog) into a shared helper

Summary

This PR consolidates duplicate localStorage read/write/JSON-parse boilerplate across multiple modules into a single typed helper (localStorageStore.ts). This makes future backend migration a single-point change instead of requiring changes across multiple files.

Changes Made

New File

  • src/lib/localStorageStore.ts: Generic typed localStorage store helper with:
    • canUseStorage(): Checks if localStorage is available
    • getItem<T>(): Reads and parses JSON from localStorage with error handling
    • setItem<T>(): Writes values to localStorage as JSON with error handling
    • getArray<T>(): Specialized array reader with validation
    • setArray<T>(): Specialized array writer with optional max entries limit
    • removeItem(): Removes items from localStorage with error handling

Refactored Files

  • src/lib/adminLog.ts: Replaced custom canUseStorage(), readAllEntries(), and writeAllEntries() with helper functions
  • src/lib/transactionLog.ts: Replaced custom localStorage functions with helper functions
  • src/lib/notifications.ts: Replaced custom readNotificationIds() and writeNotificationIds() with helper functions
  • src/lib/preferences.ts: Refactored notification preferences, theme, and locale functions to use helper
  • src/lib/contributorLeaderboard.ts: Refactored anonymity preference functions to use helper
  • src/lib/analytics.ts: Refactored analytics opt-in/opt-out functions to use helper
  • src/lib/eventSubscriber.ts: Refactored cursor persistence to use helper

Files Not Modified (as expected)

  • src/lib/commentStore.ts: Uses in-memory Map (not localStorage)
  • src/lib/reportStore.ts: Uses in-memory array (not localStorage)

Testing

  • TypeScript compilation passes (npx tsc --noEmit)
  • Prettier formatting passes (npm run format:check)
  • Note: Some pre-existing test failures were observed (unrelated to this refactoring):
    • window.matchMedia errors in component tests
    • Sitemap generation test failures
    • Language switcher test failures
      These failures existed before the changes and are not caused by the localStorage refactoring.

Benefits

Breaking Changes

None. This is a pure refactoring with no API changes.

Out of Scope

@sshdopey

Copy link
Copy Markdown
Contributor

Auto-review failed (API error). Leaving PR for human review.

@Oluwasuyi-Oluwatimilehin-Daniel
Oluwasuyi-Oluwatimilehin-Daniel force-pushed the feat/consolidate-localstorage-stores branch from 9567233 to 4b62a1a Compare July 30, 2026 21:43
@sshdopey

Copy link
Copy Markdown
Contributor

Auto-review failed (API error). Leaving PR for human review.

@sshdopey

Copy link
Copy Markdown
Contributor

Auto-review failed (API error). Leaving PR for human review.

@sshdopey

Copy link
Copy Markdown
Contributor

Auto-review failed (API error). Leaving PR for human review.

@sshdopey

Copy link
Copy Markdown
Contributor

Auto-review failed (API error). Leaving PR for human review.

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.

[Code Quality] Merge overlapping localStorage-backed store modules (commentStore, reportStore, adminLog) into a shared helper

2 participants