generated from nhcarrigan/template
fix: persist UI preferences across navigation and sessions #48
@@ -16,6 +16,31 @@ import type { Adventurer } from "@elysium/types";
|
||||
type BatchSize = 1 | 5 | 10 | 25 | 100 | "max";
|
||||
const batchOptions: Array<BatchSize> = [ 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<BatchSize>(1);
|
||||
const [ batchSize, setBatchSize ] = useState<BatchSize>(() => {
|
||||
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 (
|
||||
<button
|
||||
|
||||
Reference in New Issue
Block a user