Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/game/IslandExplorer.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { StatsHUD } from './ui/StatsHUD'
import { GameMachineProvider } from './machines/GameMachineProvider'
import { ProgressionSync } from './machines/ProgressionSync'
import { BadgeOverlay } from './ui/BadgeOverlay'
import { useGamePersistence } from './hooks/useGameStore'

const LOADING_MESSAGES = [
['Convincing the waves to behave...', 'They never listen'],
Expand Down Expand Up @@ -58,6 +59,7 @@ function LoadingOverlay() {

export default function IslandExplorer() {
const [isLoading, setIsLoading] = useState(true)
useGamePersistence()

return (
<GameMachineProvider>
Expand Down
31 changes: 18 additions & 13 deletions src/components/game/hooks/useGameStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect } from 'react'
import { create } from 'zustand'
import type { IslandData } from '../utils/islandGenerator'
import {
Expand Down Expand Up @@ -997,20 +998,24 @@ export const useGameStore = create<GameState>()((set, get) => ({
},
}))

// Periodic auto-save and beforeunload handler (client-side only)
if (typeof window !== 'undefined') {
// Auto-save every 5 seconds
setInterval(() => {
const state = useGameStore.getState()
// Only save if game is active
if (state.phase === 'playing' || state.phase === 'gameover') {
export function useGamePersistence() {
useEffect(() => {
const persist = () => {
const state = useGameStore.getState()
saveToLocalStorage(extractPersistedState(state))
}
}, 5000)
const interval = window.setInterval(() => {
const state = useGameStore.getState()
if (state.phase === 'playing' || state.phase === 'gameover') {
saveToLocalStorage(extractPersistedState(state))
}
}, 5000)

// Also save when page is about to unload
window.addEventListener('beforeunload', () => {
const state = useGameStore.getState()
saveToLocalStorage(extractPersistedState(state))
})
window.addEventListener('beforeunload', persist)
return () => {
window.clearInterval(interval)
window.removeEventListener('beforeunload', persist)
persist()
Comment on lines +1015 to +1018

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

Preserve the storage clear during unmount.

startOver removes STORAGE_KEY at Line [887]. When IslandExplorer unmounts, this cleanup calls persist() at Line [1018] and recreates the save key with the reset state. Track the explicit clear and skip the final save until a new game starts. Add a regression test for startOver followed by unmount.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/game/hooks/useGameStore.ts` around lines 1015 - 1018, Update
the cleanup around the useGameStore persistence effect so startOver’s explicit
STORAGE_KEY removal is tracked and the unmount persist() call is skipped after
that reset. Clear the tracking state when a new game starts, preserve normal
unmount persistence otherwise, and add a regression test covering startOver
followed by unmount without recreating the save key.

}
}, [])
}
Loading