From 1852d81580db90a53171c9c33b428f54c3e5451f Mon Sep 17 00:00:00 2001 From: Andy Aylward Date: Mon, 13 Apr 2026 00:15:29 -0400 Subject: [PATCH] Wire up leaveRoom through plugin/adapter/hook and add Leave Room button MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The backend already handles leaveRoom but the frontend never sent it. Adds the message through the same plugin → adapter → hook chain as leaveGame, resets all room/game state locally, and navigates back to the lobby. --- src/apps/golf/components/GolfGame.tsx | 5 +++++ src/hooks/useGolfGame.ts | 17 +++++++++++++++++ src/plugins/golfNetworkPlugin.ts | 11 ++++++++++- src/utils/networkAdapter.ts | 4 ++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/apps/golf/components/GolfGame.tsx b/src/apps/golf/components/GolfGame.tsx index f352195..f37c06b 100644 --- a/src/apps/golf/components/GolfGame.tsx +++ b/src/apps/golf/components/GolfGame.tsx @@ -63,6 +63,7 @@ const GolfGame = ({ onGameIdChange, onPlayerIdChange, onPlayerNameChange, onConn handleCardClick, setRoomCode, leaveGame, + leaveRoom, dismissNewGameNotification, joinNewGame, permalinkJoinAttempt @@ -344,6 +345,10 @@ const GolfGame = ({ onGameIdChange, onPlayerIdChange, onPlayerNameChange, onConn )} + + {notification && ( diff --git a/src/hooks/useGolfGame.ts b/src/hooks/useGolfGame.ts index bdddddf..2bf0f4c 100644 --- a/src/hooks/useGolfGame.ts +++ b/src/hooks/useGolfGame.ts @@ -73,6 +73,7 @@ interface UseGolfGameReturn { setRoomCode: (code: string) => void clearGameState: () => void leaveGame: () => void + leaveRoom: () => void // Computed currentPlayer: Player | undefined @@ -294,6 +295,21 @@ export const useGolfGame = ({ setSelectedCardIndex(null) }, []) + const leaveRoom = useCallback(() => { + if (roomState?.id) { + networkAdapterRef.current?.leaveRoom(roomState.id) + } + setRoomState(null) + setGameState(null) + setIsInRoom(false) + setIsInLobby(true) + setWinner(null) + setFinalScores(null) + setSelectedCardIndex(null) + setNewGameNotifications([]) + navigate('/golf', { replace: true }) + }, [roomState?.id, navigate]) + // Navigation helper functions const navigateToRoom = useCallback((roomId: string) => { const roomUrl = generateRoomPermalink(roomId) @@ -827,6 +843,7 @@ export const useGolfGame = ({ setRoomCode, clearGameState, leaveGame, + leaveRoom, // Navigation helpers navigateToRoom, diff --git a/src/plugins/golfNetworkPlugin.ts b/src/plugins/golfNetworkPlugin.ts index f126e4f..5a346a7 100644 --- a/src/plugins/golfNetworkPlugin.ts +++ b/src/plugins/golfNetworkPlugin.ts @@ -12,7 +12,7 @@ interface GolfMessage extends BaseNetworkMessage { type: 'authenticate' | 'authenticated' | 'createRoom' | 'joinRoom' | 'createGame' | 'joinGame' | 'roomJoined' | 'roomStateUpdate' | 'gameState' | 'error' | 'gameStarted' | 'turnChanged' | 'playerKnocked' | 'gameEnded' | 'newGameStarted' | 'startGame' | 'peekCard' | 'drawCard' | 'takeFromDiscard' | - 'swapCard' | 'discardDrawn' | 'knock' | 'hideCards' | 'startNewGame' | 'leaveGame' + 'swapCard' | 'discardDrawn' | 'knock' | 'hideCards' | 'startNewGame' | 'leaveGame' | 'leaveRoom' // Request fields roomId?: string gameId?: string @@ -412,6 +412,15 @@ export class GolfNetworkPlugin implements GameNetworkPlugin { console.log('📤 Sent leave game request') } + leaveRoom(roomId: string, context: NetworkContext): void { + context.send({ + type: 'leaveRoom', + roomId: roomId, + timestamp: Date.now() + }) + console.log(`📤 Sent leave room request for room ${roomId}`) + } + // Session token management private storeSessionToken(token: string): void { try { diff --git a/src/utils/networkAdapter.ts b/src/utils/networkAdapter.ts index 7b7c009..17bb0a4 100644 --- a/src/utils/networkAdapter.ts +++ b/src/utils/networkAdapter.ts @@ -259,6 +259,10 @@ export class GolfNetworkAdapter { this.plugin.leaveGame(this.getContext()) } + leaveRoom(roomId: string): void { + this.plugin.leaveRoom(roomId, this.getContext()) + } + isMyTurn(): boolean { return this.plugin.isMyTurn(this.getContext()) }