49 lines
1.6 KiB
JavaScript
49 lines
1.6 KiB
JavaScript
|
|
import { INITIAL_EXERCISE_LIST_FILTERS, mergeExerciseListPrefsFromApi } from '../constants/exerciseListFilters'
|
||
|
|
|
||
|
|
const STORAGE_KEY = 'shinkan.exerciseList.session.v1'
|
||
|
|
|
||
|
|
function safeParse(raw) {
|
||
|
|
if (!raw) return null
|
||
|
|
try {
|
||
|
|
return JSON.parse(raw)
|
||
|
|
} catch {
|
||
|
|
return null
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @returns {{ filters: object, searchInput: string, aiSearchInput: string, mineOnly: boolean } | null} */
|
||
|
|
export function readExerciseListSessionState() {
|
||
|
|
if (typeof sessionStorage === 'undefined') return null
|
||
|
|
const parsed = safeParse(sessionStorage.getItem(STORAGE_KEY))
|
||
|
|
if (!parsed || typeof parsed !== 'object') return null
|
||
|
|
|
||
|
|
const filters =
|
||
|
|
parsed.filters && typeof parsed.filters === 'object'
|
||
|
|
? mergeExerciseListPrefsFromApi(parsed.filters)
|
||
|
|
: { ...INITIAL_EXERCISE_LIST_FILTERS }
|
||
|
|
|
||
|
|
return {
|
||
|
|
filters,
|
||
|
|
searchInput: typeof parsed.searchInput === 'string' ? parsed.searchInput : '',
|
||
|
|
aiSearchInput: typeof parsed.aiSearchInput === 'string' ? parsed.aiSearchInput : '',
|
||
|
|
mineOnly: !!parsed.mineOnly,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function writeExerciseListSessionState(state) {
|
||
|
|
if (typeof sessionStorage === 'undefined' || !state || typeof state !== 'object') return
|
||
|
|
try {
|
||
|
|
sessionStorage.setItem(
|
||
|
|
STORAGE_KEY,
|
||
|
|
JSON.stringify({
|
||
|
|
filters: state.filters ?? { ...INITIAL_EXERCISE_LIST_FILTERS },
|
||
|
|
searchInput: typeof state.searchInput === 'string' ? state.searchInput : '',
|
||
|
|
aiSearchInput: typeof state.aiSearchInput === 'string' ? state.aiSearchInput : '',
|
||
|
|
mineOnly: !!state.mineOnly,
|
||
|
|
})
|
||
|
|
)
|
||
|
|
} catch {
|
||
|
|
/* quota / private mode */
|
||
|
|
}
|
||
|
|
}
|