@@ -108,30 +108,31 @@ export function useGameService(): UseGameServiceReturn {
108108 try {
109109 // Delete from API
110110 await apiClient . deleteGame ( playerAlias , gameId ) ;
111+ } catch ( err ) {
112+ if ( ! ( err instanceof Error ) || ! err . message . startsWith ( 'HTTP 404' ) ) {
113+ const errorMessage = err instanceof Error ? err . message : 'Failed to delete game' ;
114+ setError ( errorMessage ) ;
115+ console . error ( 'Failed to delete game:' , err ) ;
116+ throw err ;
117+ }
118+ }
111119
112- // Update local state
113- setSavedGames ( games => games . filter ( g => g . id !== gameId ) ) ;
114-
115- // Clear current game if it's the one being deleted
116- setCurrentGame ( current => current ?. id === gameId ? null : current ) ;
117-
118- // Update localStorage cache
119- const cachedGames = localStorage . getItem ( 'savedGames' ) ;
120- if ( cachedGames ) {
121- try {
122- const parsedGames : GameModel [ ] = JSON . parse ( cachedGames ) ;
123- const updatedGames = parsedGames . filter ( g => g . id !== gameId ) ;
124- localStorage . setItem ( 'savedGames' , JSON . stringify ( updatedGames ) ) ;
125- } catch {
126- // If cache is corrupted, just remove it
127- localStorage . removeItem ( 'savedGames' ) ;
128- }
120+ // Update local state (runs on success or 404 — game is gone either way)
121+ setSavedGames ( games => games . filter ( g => g . id !== gameId ) ) ;
122+
123+ // Clear current game if it's the one being deleted
124+ setCurrentGame ( current => current ?. id === gameId ? null : current ) ;
125+
126+ // Update localStorage cache
127+ const cachedGames = localStorage . getItem ( 'savedGames' ) ;
128+ if ( cachedGames ) {
129+ try {
130+ const parsedGames : GameModel [ ] = JSON . parse ( cachedGames ) ;
131+ const updatedGames = parsedGames . filter ( g => g . id !== gameId ) ;
132+ localStorage . setItem ( 'savedGames' , JSON . stringify ( updatedGames ) ) ;
133+ } catch {
134+ localStorage . removeItem ( 'savedGames' ) ;
129135 }
130- } catch ( err ) {
131- const errorMessage = err instanceof Error ? err . message : 'Failed to delete game' ;
132- setError ( errorMessage ) ;
133- console . error ( 'Failed to delete game:' , err ) ;
134- throw err ; // Re-throw so calling component can handle it
135136 }
136137 } , [ isLoading ] ) ;
137138
0 commit comments