Skip to content

Refactor Gamification Load Logic to Pure Function - #296

Merged
albertoivo merged 1 commit into
mainfrom
refactor/extract-gamification-load-logic-14052783801156281986
Oct 12, 2026
Merged

albertoivo merged 1 commit into
mainfrom
refactor/extract-gamification-load-logic-14052783801156281986

Conversation

@albertoivo

Copy link
Copy Markdown
Owner

This PR extracts the heavy inline business logic for processing loaded gamification data out of the useGamificationStore hook and into a pure utility function.

Changes

  • Created src/utils/gamification/loadLogic.ts: Contains processLoadedGamificationData which handles:
    • Daily/Weekly Resets
    • Legacy Streak Migration
    • Streak Updates
    • Pet Status Checks
  • Updated src/hooks/gamification/useGamificationStore.ts: Replaced the large inline if/else logic block within the loadGamification callback with a clean call to processLoadedGamificationData.
  • Updated src/utils/gamification/streakLogic.ts: Adjusted migrateLegacyStreak slightly to handle missing fields correctly when spreading to new values.
  • Updated src/utils/gamificationState.ts: Exported the new logic.

Benefits

  • Single Responsibility Principle (SRP): The hook no longer manages both the side-effects/loading mechanisms and the deep gamification data logic.
  • Testability: processLoadedGamificationData is a pure function that can be easily unit-tested.
  • Readability: Reduces the size of the large useGamificationStore hook.

PR created automatically by Jules for task 14052783801156281986 started by @albertoivo

Extracted the complex inline data processing logic from `loadGamification` inside `src/hooks/gamification/useGamificationStore.ts` into a pure, testable function `processLoadedGamificationData` in `src/utils/gamification/loadLogic.ts`.

This adheres to the Single Responsibility Principle, making the `useGamificationStore` hook smaller and focused solely on React lifecycles and state/Firestore integration, while delegating the logic for daily resets, legacy migrations, streak updates, and pet status checks to the new pure utility.

- Created `src/utils/gamification/loadLogic.ts`.
- Updated `src/hooks/gamification/useGamificationStore.ts` to consume the new function.
- Updated `src/utils/gamificationState.ts` to export the new module.
- Resolved tests failures by handling edge cases (like `isInitial`) in the new logic and streak logic updates.

Co-authored-by: albertoivo <1276520+albertoivo@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@github-actions

Copy link
Copy Markdown

Visit the preview URL for this PR (updated for commit 37d4231):

https://pyexplorer-cd32d--pr296-refactor-extract-gam-a2br6nn5.web.app

(expires Mon, 19 Oct 2026 13:16:58 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 916b4905e0cdc420f3368c8eb19b2f2e90c3be61

@albertoivo
albertoivo merged commit 4410619 into main Oct 12, 2026
6 checks passed
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.

1 participant