From fc93efd245867e9fe0b02953bc732fd9350d8aaa Mon Sep 17 00:00:00 2001 From: Hikari Date: Mon, 9 Mar 2026 22:02:52 -0700 Subject: [PATCH] feat: persist adventure multiplier selection in localStorage Reads the saved batch-size preference on mount and writes it back to localStorage on every selection change, so the chosen multiplier survives page refreshes. Falls back to 1 when no stored value is found or the value is unrecognisable. Closes #35 --- .../src/components/game/adventurerPanel.tsx | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/apps/web/src/components/game/adventurerPanel.tsx b/apps/web/src/components/game/adventurerPanel.tsx index efa7790..c996374 100644 --- a/apps/web/src/components/game/adventurerPanel.tsx +++ b/apps/web/src/components/game/adventurerPanel.tsx @@ -16,6 +16,31 @@ import type { Adventurer } from "@elysium/types"; type BatchSize = 1 | 5 | 10 | 25 | 100 | "max"; const batchOptions: Array = [ 1, 5, 10, 25, 100, "max" ]; +/** + * Parses a localStorage string back into a valid BatchSize, defaulting to 1. + * @param stored - The raw string from localStorage (or null if absent). + * @returns A valid BatchSize value. + */ +const parseBatchSize = (stored: string | null): BatchSize => { + if (stored === "max") { + return "max"; + } + const numeric = Number(stored); + if (numeric === 5) { + return 5; + } + if (numeric === 10) { + return 10; + } + if (numeric === 25) { + return 25; + } + if (numeric === 100) { + return 100; + } + return 1; +}; + /** * Computes the total cost to buy a batch of adventurers. * @param adventurer - The adventurer to buy. @@ -148,7 +173,9 @@ const AdventurerCard = ({ const AdventurerPanel = (): JSX.Element => { const { state, formatNumber } = useGame(); const [ showLocked, setShowLocked ] = useState(true); - const [ batchSize, setBatchSize ] = useState(1); + const [ batchSize, setBatchSize ] = useState(() => { + return parseBatchSize(localStorage.getItem("elysium_batch_size")); + }); if (state === null) { return ( @@ -196,6 +223,7 @@ const AdventurerPanel = (): JSX.Element => { {batchOptions.map((option) => { function handleBatchSelect(): void { setBatchSize(option); + localStorage.setItem("elysium_batch_size", String(option)); } return (