From bf3ddfc3f8b9890783d36d0ef5de710f84f2ee4d Mon Sep 17 00:00:00 2001 From: Sped0n Date: Sat, 4 Nov 2023 15:35:49 +0800 Subject: [PATCH] feat(state.ts): add sessionStorage support to store and restore thresholdsIndex value when navigating between pages --- assets/ts/state.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/assets/ts/state.ts b/assets/ts/state.ts index 1a4e3eb..e6341a7 100644 --- a/assets/ts/state.ts +++ b/assets/ts/state.ts @@ -21,8 +21,10 @@ const thresholds = [ const defaultState = { index: -1, length: 0, - threshold: thresholds[2].threshold, - trailLength: thresholds[2].trailLength + threshold: + thresholds[parseInt(sessionStorage.getItem('thresholdsIndex') || '2')].threshold, + trailLength: + thresholds[parseInt(sessionStorage.getItem('thresholdsIndex') || '2')].trailLength } export const state = new Watchable(defaultState) @@ -34,6 +36,7 @@ export const state = new Watchable(defaultState) export function initState(length: number): void { const s = state.get() s.length = length + updateThreshold(s, 0) state.set(s) } @@ -72,9 +75,11 @@ export function decThreshold(): void { */ function updateThreshold(state: State, inc: number): State { - const i = thresholds.findIndex((t) => state.threshold === t.threshold) + inc + let i = thresholds.findIndex((t) => state.threshold === t.threshold) + inc // out of bounds if (i < 0 || i >= thresholds.length) return state + // storage the index so we can restore it even if we go to another page + sessionStorage.setItem('thresholdsIndex', i.toString()) const newItems = thresholds[i] return { ...state, ...newItems } }