diff --git a/src/Connections/ConnectionsPlay.tsx b/src/Connections/ConnectionsPlay.tsx index 56fda3f1..32a8260a 100644 --- a/src/Connections/ConnectionsPlay.tsx +++ b/src/Connections/ConnectionsPlay.tsx @@ -102,7 +102,7 @@ function ConnectionsPlay({ game, backTo, debug }: { game: ConnectionsGame, backT // Save puzzleState to localStorage when categoriesState or guesses change useEffect(() => { if (id) { - setPuzzleState(id, guesses, categoriesState); + setPuzzleState(id, normalizeGame(game, true), guesses, categoriesState); } }, [categoriesState, guesses]); diff --git a/src/Connections/ConnectionsProvider.tsx b/src/Connections/ConnectionsProvider.tsx index 1ac295ca..ceb34732 100644 --- a/src/Connections/ConnectionsProvider.tsx +++ b/src/Connections/ConnectionsProvider.tsx @@ -2,7 +2,7 @@ import { useEffect, useState, useMemo } from 'react'; import axios from 'axios'; import { Helmet } from 'react-helmet'; import { ConnectionsProviderProps, ConnectionsContext } from './ConnectionsContext'; -import { ConnectionsGame } from './utils'; +import { ConnectionsGame, getPuzzleState } from './utils'; import './connections.scss'; function ConnectionsProvider({ children }: ConnectionsProviderProps) { @@ -30,6 +30,11 @@ function ConnectionsProvider({ children }: ConnectionsProviderProps) { }, [loadedConnections]); const getGame = async (id: number): Promise => { + const cachedPuzzle = getPuzzleState(id.toString()); + if (cachedPuzzle?.game) { + return cachedPuzzle.game; + } + if (id <= nytConnections.length) { return nytConnections[id - 1]; } diff --git a/src/Connections/utils.ts b/src/Connections/utils.ts index 012caacb..ee387894 100644 --- a/src/Connections/utils.ts +++ b/src/Connections/utils.ts @@ -155,6 +155,7 @@ export const BODIED_TEXTS = ['Damn bruh 💀', 'Down bad 😔', 'Try harder', 'S const PUZZLE_STATES_KEY = 'puzzle_states'; type PuzzleState = { + game?: ConnectionsGame, categoriesState?: CategoriesState, guesses?: RecordedGuess[], }; @@ -177,24 +178,25 @@ export const getPuzzleState = (id: string): PuzzleState => { return {}; }; -export const isSolved = (categoriesState?: CategoriesState): boolean => { - if (!categoriesState) { - return false; - } - - return Object.values(categoriesState).every((category) => category.solved); -}; - -export const setPuzzleState = (id: string, guesses?: RecordedGuess[], categoriesState?: CategoriesState): void => { +export const setPuzzleState = (id: string, game: ConnectionsGame, guesses?: RecordedGuess[], categoriesState?: CategoriesState): void => { const puzzleStates = getPuzzleStates(); const puzzleState = puzzleStates[id]; puzzleStates[id] = { + game, guesses: guesses || puzzleState.guesses, categoriesState: categoriesState || puzzleState.categoriesState, }; window.localStorage.setItem(PUZZLE_STATES_KEY, JSON.stringify(puzzleStates)); }; +export const isSolved = (categoriesState?: CategoriesState): boolean => { + if (!categoriesState) { + return false; + } + + return Object.values(categoriesState).every((category) => category.solved); +}; + const FIRST_DAY = new Date(2023, 5, 12); export const daysBetween = (now: Date, then: Date): number => {