mirror of
https://github.com/Sped0n/bridget.git
synced 2026-04-18 12:09:29 -07:00
feat(state.ts): add sessionStorage support to store and restore thresholdsIndex value when navigating between pages
This commit is contained in:
@@ -21,8 +21,10 @@ const thresholds = [
|
|||||||
const defaultState = {
|
const defaultState = {
|
||||||
index: -1,
|
index: -1,
|
||||||
length: 0,
|
length: 0,
|
||||||
threshold: thresholds[2].threshold,
|
threshold:
|
||||||
trailLength: thresholds[2].trailLength
|
thresholds[parseInt(sessionStorage.getItem('thresholdsIndex') || '2')].threshold,
|
||||||
|
trailLength:
|
||||||
|
thresholds[parseInt(sessionStorage.getItem('thresholdsIndex') || '2')].trailLength
|
||||||
}
|
}
|
||||||
|
|
||||||
export const state = new Watchable<State>(defaultState)
|
export const state = new Watchable<State>(defaultState)
|
||||||
@@ -34,6 +36,7 @@ export const state = new Watchable<State>(defaultState)
|
|||||||
export function initState(length: number): void {
|
export function initState(length: number): void {
|
||||||
const s = state.get()
|
const s = state.get()
|
||||||
s.length = length
|
s.length = length
|
||||||
|
updateThreshold(s, 0)
|
||||||
state.set(s)
|
state.set(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,9 +75,11 @@ export function decThreshold(): void {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function updateThreshold(state: State, inc: number): State {
|
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
|
// out of bounds
|
||||||
if (i < 0 || i >= thresholds.length) return state
|
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]
|
const newItems = thresholds[i]
|
||||||
return { ...state, ...newItems }
|
return { ...state, ...newItems }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user