Skip to content

Conversation

@kevalyq
Copy link
Contributor

@kevalyq kevalyq commented Nov 8, 2025

🐛 Bug Fix

Fixes #103

Problem

The NotificationPreferences component had a potential infinite loop risk due to useEffect depending on defaultPreferences, which depends on the Lingui _ function. Since _ function reference can change frequently during re-renders, this could trigger excessive re-renders.

Solution

Changed useEffect dependency from defaultPreferences to i18n.locale. This ensures translations only update when the locale actually changes, not on every _ function reference change.

Changes

  • Add i18n to useLingui() destructuring to access locale
  • Update useEffect dependency array: [i18n.locale, defaultPreferences]
  • Add explanatory comments about infinite loop prevention
  • Add 2 new tests to verify:
    1. Render count stays reasonable during locale changes
    2. Enabled state is preserved when translations update

Testing

  • ✅ All 134 tests passing (2 new tests added)
  • ✅ TypeScript: 0 errors
  • ✅ ESLint: 0 warnings
  • ✅ Prettier: compliant
  • ✅ REUSE: compliant (3.3)
  • ✅ Preflight checks: passed

Self-Review Checklist

Phase 1: Functional ✅

  • All tests pass locally (134/134)
  • TypeScript passes (0 errors)
  • ESLint passes (0 warnings)
  • Prettier passes
  • REUSE compliance passes

Phase 2: Pattern Review ✅

  • Follows React best practices (explicit dependencies)
  • Comments explain the "why" (infinite loop prevention)
  • Test patterns match existing tests
  • TDD followed (tests first, then implementation)

Phase 3: Cleanup ✅

  • No debug code
  • No unused imports
  • Git diff reviewed

Phase 4: Consistency ✅

  • Follows Lingui best practices
  • Dependency array is correct and complete
  • Tests are descriptive and comprehensive

Related

- Change useEffect dependency from defaultPreferences to i18n.locale
- Prevents potential infinite loop from frequent _ function reference changes
- Add i18n to useLingui destructuring to access locale
- Add 2 new tests to verify locale change behavior and render count
- Update comments to explain why locale dependency prevents infinite loop

Fixes #103

Tests:
- All 134 tests passing (2 new tests added for translation updates)
- TypeScript: 0 errors
- ESLint: 0 warnings
- Prettier: compliant
- REUSE: compliant (3.3)
Copilot AI review requested due to automatic review settings November 8, 2025 08:23
@github-actions
Copy link

github-actions bot commented Nov 8, 2025

💡 Tip: Consider Using Draft PRs

Benefits of opening PRs as drafts initially:

  • 💰 Saves CI runtime and Copilot review credits
  • 🎯 Automatically sets linked issues to "🚧 In Progress" status
  • 🚀 Mark "Ready for review" when done to trigger full CI pipeline

How to convert:

  1. Click "Still in progress? Convert to draft" in the sidebar, OR
  2. Use gh pr ready when ready for review

This is just a friendly reminder - feel free to continue as is! 😊

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses a potential infinite loop issue in the NotificationPreferences component by changing the useEffect dependency from relying on defaultPreferences (which depends on the frequently-changing _ function reference) to i18n.locale (which only changes when the locale actually changes).

  • Extracts i18n from useLingui() hook to access the stable locale property
  • Updates useEffect dependencies to use i18n.locale instead of relying solely on defaultPreferences
  • Adds test coverage for translation updates and render behavior

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/components/NotificationPreferences.tsx Adds i18n extraction, updates useEffect dependencies to include i18n.locale, and adds explanatory comments
src/components/NotificationPreferences.test.tsx Adds test suite for translation updates to verify no excessive re-renders and state preservation

Addresses Copilot review comment - removed defaultPreferences from
dependency array and compute translations directly using i18n._ and
msg to avoid dependency on the _ function which changes frequently.

This fully prevents the infinite loop issue by only depending on the
stable i18n object.
@kevalyq kevalyq requested a review from Copilot November 8, 2025 08:48
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

…e dependency

Addresses Copilot review comments:
- Extract getTranslationsForCategory helper to avoid duplicating translation strings
- Use i18n.locale instead of i18n object in useEffect dependency
- Simplify useEffect logic using helper function
@kevalyq kevalyq requested a review from Copilot November 8, 2025 08:58
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

- Update comment to reflect current implementation (no longer using _ function)
- Remove redundant i18n from dependency array, only keep i18n.locale
- Add eslint-disable comment with explanation for intentional dependency choice
@kevalyq kevalyq merged commit 51e3930 into main Nov 8, 2025
14 checks passed
@kevalyq kevalyq deleted the fix/issue-103-notification-preferences-locale branch November 8, 2025 09:18
@github-actions
Copy link

github-actions bot commented Nov 8, 2025

💡 Tip: Consider Using Draft PRs

Benefits of opening PRs as drafts initially:

  • 💰 Saves CI runtime and Copilot review credits
  • 🎯 Automatically sets linked issues to "🚧 In Progress" status
  • 🚀 Mark "Ready for review" when done to trigger full CI pipeline

How to convert:

  1. Click "Still in progress? Convert to draft" in the sidebar, OR
  2. Use gh pr ready when ready for review

This is just a friendly reminder - feel free to continue as is! 😊

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.

PWA Phase 3: Fix infinite loop risk in NotificationPreferences with Lingui translations

2 participants