Skip to content

Fix useLocalStorage Hook to Sync State Across Tabs - #8998

Merged
JhaSourav07 merged 1 commit into
JhaSourav07:mainfrom
adrish-mage:fix/uselocalstorage-tab-sync
Aug 13, 2026
Merged

Fix useLocalStorage Hook to Sync State Across Tabs#8998
JhaSourav07 merged 1 commit into
JhaSourav07:mainfrom
adrish-mage:fix/uselocalstorage-tab-sync

Conversation

@adrish-mage

Copy link
Copy Markdown
Contributor

Description

Fixes #8996

Adds a storage event listener to the useLocalStorage hook so components using it (currently GoalTracker) stay in sync across browser tabs. Previously the hook only read from localStorage once on mount, so a change made in one tab (e.g. editing a goal) wasn't reflected in another open tab without a manual reload.

This mirrors the existing cross-tab sync pattern already used in context/TranslationContext.tsx for language preference — just applied generically to the reusable hook.

Changes:

  • Added a storage event listener inside the existing mount useEffect, filtered to only react to the hook's own key
  • Cleanup on unmount via removeEventListener
  • Added two tests: one confirming state updates on a matching-key storage event, one confirming it's ignored for a different key

Pillar

  • 🎨 Pillar 1 — New Theme Design
  • 📐 Pillar 2 — Geometric SVG Improvement
  • 🕐 Pillar 3 — Timezone Logic Optimization
  • 🛠️ Other (Bug fix, refactoring, docs)

Visual Preview

N/A — this is a hook-level logic fix with no visual/UI change. Behavior is covered by unit tests; can be manually verified by opening the dashboard in two tabs and editing a goal in one.

Checklist before requesting a review:

  • I have read the CONTRIBUTING.md file.
  • I have tested these changes locally (localhost:3000/api/streak?user=YOUR_USERNAME).
  • I have run npm run format and npm run lint locally and resolved all errors (CI will fail otherwise).
  • My commits follow the Conventional Commits format (e.g., feat(themes): ..., fix(calculate): ...).
  • I have updated README.md if I added a new theme or URL parameter.
  • I have started the repo.
  • I have made sure that i have only one commit to merge in this PR.
  • The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts).
  • (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.

@vercel

vercel Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

@adrish-mage is attempting to deploy a commit to the jhasourav07's projects Team on Vercel.

A member of the Team first needs to authorize it.

@retenta-bot

retenta-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

This pull request addresses the issue of state synchronization across tabs by implementing a storage event listener in the useLocalStorage hook. This is similar to the previous decision made regarding syncing contribution goals with MongoDB, which aimed to enhance user experience by ensuring data persistence across devices. While this PR focuses on local storage synchronization, it’s important to consider the broader implications of state management in our application. Thank you for your contribution, and I look forward to your updates!

@retenta-bot retenta-bot Bot changed the title fix(useLocalStorage): sync state across tabs via storage event Fix useLocalStorage Hook to Sync State Across Tabs Aug 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Next.js Bundle Size Report (Gzipped Sizes)

Asset PR Size Base Size Difference Status
static/chunks/app/(root)/dashboard/[username]/page-[hash].js 59.52 KB 59.46 KB +0.06 KB (+0.09%) 🔴 Regression

📊 Summary of Totals

Category PR Size Base Size Difference
Total JS 4999.01 KB 4998.89 KB +0.12 KB (+0.00%)
Total CSS 354.18 KB 354.18 KB 0 B

@github-actions github-actions Bot added the type:bug Something isn't working as expected label Aug 12, 2026
@adrish-mage

Copy link
Copy Markdown
Contributor Author

@JhaSourav07 lemme know if any changes are required !

@Aamod007 Aamod007 added mentor:Aamod007 level:intermediate Moderate complexity tasks quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. labels Aug 13, 2026

@Aamod007 Aamod007 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix! Adding a \storage\ event listener to \useLocalStorage.ts\ perfectly solves the synchronization issue across multiple tabs. It's great that you only react to the specific key matching the hook's invocation.

The tests you've added in \useLocalStorage.test.ts\ to dispatch simulated \StorageEvent\s with both matching and non-matching keys perfectly validate this behavior.

Labels applied:

  • type:bug: Fixes state desynchronization across tabs.
  • level:intermediate: Requires handling cross-tab \StorageEvent\ listeners within a React hook lifecycle.
  • quality:clean: Simple, effective logic backed by good unit tests.

@JhaSourav07 JhaSourav07 added the gssoc:approved PR has been reviewed and accepted for valid contribution points label Aug 13, 2026
@JhaSourav07
JhaSourav07 merged commit 032e8f7 into JhaSourav07:main Aug 13, 2026
10 of 11 checks passed
@github-actions github-actions Bot added this to the GSSoC 2026 milestone Aug 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🎉 Congratulations @adrish-mage! Your PR has been successfully merged. 🚀

Thank you for contributing to CommitPulse. Your work helps us build a better tool for the community.

⚠️ Important for GSSoC Contributors:
You are strictly advised to join our Discord Server as it is mandatory for all GSSoC participants. All important announcements, point claims, and community discussions happen there.

Keep building! 💻✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved PR has been reviewed and accepted for valid contribution points level:intermediate Moderate complexity tasks mentor:Aamod007 quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. type:bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: useLocalStorage hook doesn't listen for storage events - state goes stale across open tabs

3 participants