Skip to content

Conversation

Copy link

Copilot AI commented Oct 8, 2025

Problem

Users were experiencing duplicate confirmation dialogs when leaving the game using the Leave button (❌) in the top-right corner:

  1. First dialog: In-game confirmation ("Are you sure you want to exit?")
  2. Second dialog: Browser's beforeunload confirmation

This created a poor user experience with redundant prompts. Additionally, the beforeunload confirmation was incorrectly showing when users intentionally left the game, rather than only when closing the browser window/tab.

Solution

This PR introduces a minimal flag-based solution to track intentional navigation. When users click any exit/leave button in the game and confirm their intent, the flag is set to prevent the beforeunload handler from showing a second confirmation.

Changes Made

  1. Main.ts: Added isIntentionalNavigation flag and exported setIntentionalNavigation() function for use by UI components
  2. OptionsMenu.ts: Calls setIntentionalNavigation() before redirecting when user clicks Leave
  3. WinModal.ts: Calls setIntentionalNavigation() before redirecting when user clicks Exit
  4. GameRightSidebar.ts: Calls setIntentionalNavigation() before redirecting when user clicks Exit
  5. SettingsModal.ts: Calls setIntentionalNavigation() before redirecting when user clicks Exit

The beforeunload event handler now checks the flag and skips showing the confirmation if navigation is intentional.

Behavior After Fix

  • Clicking Leave button (player alive): Shows only the in-game confirmation → User confirms → Navigates to home (no second dialog) ✅
  • Clicking Leave button (player dead): Navigates to home immediately (no confirmation needed) ✅
  • Closing browser window/tab (player alive): Shows beforeunload confirmation dialog ✅
  • No duplicate dialogs

Testing

  • ✅ All existing tests pass (38 test suites, 298 tests)
  • ✅ Build succeeds with no errors
  • ✅ Linter passes with no warnings
  • ✅ Minimal code changes (5 files, +35 lines, -2 lines)

This fix ensures the window close confirmation only appears when users are actually closing the browser window/tab, not when they're intentionally leaving the game through UI controls.

Original prompt

In the smallest/cleanest/best way possible, only show the window close confirmation dialog when ACTUALLY CLOSING THE WINDOW. When the user exists the game with the Leave button top-right, this alert window should not be shown.

Also the dialog is sometimes observed twice immediately after each other.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 14 commits October 8, 2025 19:15
Co-authored-by: Foorack <5008081+Foorack@users.noreply.github.com>
Co-authored-by: Foorack <5008081+Foorack@users.noreply.github.com>
Co-authored-by: Foorack <5008081+Foorack@users.noreply.github.com>
Refine comment to clarify when the confirmation dialog appears.
Co-authored-by: Foorack <5008081+Foorack@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Changed joinLobby to return a LobbyControl object with cleanup and shouldPreventUnload methods
- Moved currentGameRunner from module-level to local closure within joinLobby
- Updated Main.ts to use the new LobbyControl API instead of separate gameStop and shouldPreventUnload functions
- All tests pass and build is successful

Co-authored-by: Foorack <5008081+Foorack@users.noreply.github.com>
Instead of LobbyControl interface, joinLobby now returns a function that:
- Checks if it should prevent closing (player alive in game)
- Returns false if close was prevented, true if successful
- Eliminates the need for separate cleanup and shouldPreventUnload methods

This is a simpler approach as suggested in the code review.

Co-authored-by: Foorack <5008081+Foorack@users.noreply.github.com>
- Renamed property from leaveLobby to gameStop to minimize diff from original
- Removed verbose comments to keep only essential ones
- Maintains same functionality with cleaner code

Co-authored-by: Foorack <5008081+Foorack@users.noreply.github.com>
Co-authored-by: Foorack <5008081+Foorack@users.noreply.github.com>
…-function

Add JSDoc documentation to shouldPreventWindowClose method
Co-authored-by: Foorack <5008081+Foorack@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix window close confirmation dialog behavior Fix duplicate window close confirmation dialogs when leaving game intentionally Oct 8, 2025
Copilot AI requested a review from Foorack October 8, 2025 21:16
Copilot finished work on behalf of Foorack October 8, 2025 21:16
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.

2 participants