-
Notifications
You must be signed in to change notification settings - Fork 0
Implement score saving with D1 database #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
goofmint
merged 4 commits into
main
from
claude/implement-d1-score-saving-01Na71XTJYW9e9Yum5fGiB7k
Nov 22, 2025
+459
−3
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d6acfc5
feat: Implement D1 score saving with useFetcher
claude 7e16c5d
fix: Navigate to result page after score submission
claude 3d64aff
fix: Add result routes to routes.ts and debug logging
claude e139141
refactor: Improve error handling and ID generation
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Guard score-saving effect to avoid endless resubmits on failure
When
/result/createreturns{ success: false, error: ... }(or any falsysuccess),fetcher.stategoes back to'idle'withfetcher.dataset, so:scoreSavedisfalse, andif (gameEnded && !scoreSaved && fetcher.state === 'idle')becomes true again,causing the effect to immediately re-call
fetcher.submit. This will loop indefinitely on persistent failures, spamming your D1 endpoint and re-triggering the error UI.You already surface errors nicely in the second effect and in the game-over UI, so it’s better to auto-submit only once and let the user decide if they want to retry.
One simple fix is to treat “has we ever tried to save?” as a separate guard derived from
fetcher.state/fetcher.data, and only auto-submit when there has been no prior attempt:// Save score when game ends useEffect(() => { - const scoreSaved = fetcher.state === 'idle' && fetcher.data?.success; - - if (gameEnded && !scoreSaved && fetcher.state === 'idle') { + const scoreSaved = !!fetcher.data?.success; + const hasAttemptedSave = fetcher.state !== 'idle' || fetcher.data != null; + + if (gameEnded && !scoreSaved && !hasAttemptedSave) { // Calculate total issues across all problems const allProblems = getProblems(codeLanguage, 1) .concat(getProblems(codeLanguage, 2)) .concat(getProblems(codeLanguage, 3)); @@ // Submit to result/create action fetcher.submit( { payload: JSON.stringify(resultData) }, { method: 'post', action: '/result/create' } ); } - }, [gameEnded, fetcher.state, fetcher.data, gameState, codeLanguage, lang, fetcher]); + }, [gameEnded, fetcher.state, fetcher.data, gameState, codeLanguage, lang, fetcher]);This keeps your existing success navigation and error handling intact, but prevents uncontrolled retries. If you later want a manual “Retry saving score” button, it can call
fetcher.submitdirectly without changing this effect.Also applies to: 216-229, 344-380
I'll verify the review comment by examining the actual codebase. Let me start by inspecting the file and understanding the complete context.
Now let me examine the actual file and related code sections.
🏁 Script executed:
Length of output: 2451
Let me examine the other locations mentioned in the review.
🏁 Script executed:
Length of output: 1651
Let me check the
/result/createaction handler to confirm the failure response format:🏁 Script executed:
Length of output: 288
🏁 Script executed:
Length of output: 2232
Fix the infinite loop in score submission on API failure
The infinite loop bug is confirmed and real. When
/result/createfails and throws an error,fetcher.databecomes{ error: '...' }andfetcher.statebecomes'idle'. Since the current logic checks onlyfetcher.state === 'idle' && fetcher.data?.success, the condition remains true when success is missing/falsy, triggering an immediate resubmit. This cycles indefinitely, spamming the D1 endpoint.The proposed fix correctly guards against this by tracking whether a save attempt has ever been made, allowing the action to submit once and letting error handling (via the second effect) take over.
Apply the suggested diff to lines 176-215 as shown in the review comment.
🤖 Prompt for AI Agents