From 993ac7111bfe21f6c57ec8aabb885aac5f6d0074 Mon Sep 17 00:00:00 2001 From: Victor Oliva Date: Mon, 10 Mar 2025 12:42:37 +0100 Subject: [PATCH] feat(storage): add pallet/entry to hash params --- src/lib/externalState.ts | 90 +++++++++++++++++++++++++++++++++++ src/pages/Storage/Storage.tsx | 9 ++-- 2 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/lib/externalState.ts diff --git a/src/lib/externalState.ts b/src/lib/externalState.ts new file mode 100644 index 0000000..c9ccccc --- /dev/null +++ b/src/lib/externalState.ts @@ -0,0 +1,90 @@ +import { useState } from "react" +import { getHashParams, setHashParams } from "../hashParams" + +interface Parser { + stringify: (value: T) => string | null + parse: (value: string) => T +} +interface ExternalState { + hasValue: (key: string) => boolean + getValue: (key: string) => string + setValue: (key: string, value: string) => void + deleteValue: (key: string) => void +} + +type ValueOrReducer = T | ((prev: T) => T) +const createExternalStateHook = (externalState: ExternalState) => { + function useExternalState( + key: string, + defaultValue: string, + ): [string, (newValue: ValueOrReducer) => void] + function useExternalState( + key: string, + defaultValue: T, + parser: Parser, + ): [T, (newValue: ValueOrReducer) => void] + function useExternalState( + key: string, + defaultValue: T, + parser: Parser = { + stringify: (value) => (value == null ? null : String(value)), + parse: (value) => value as T, + }, + ) { + const state = externalState.hasValue(key) + ? parser.parse(externalState.getValue(key)) + : defaultValue + const [_state, _setState] = useState(state) + + return [ + state, + (newValue: ValueOrReducer) => { + _setState((prev) => { + const value: T = + typeof newValue === "function" ? (newValue as any)(prev) : newValue + + const strValue = parser.stringify(value) + if (strValue != null) { + externalState.setValue(key, strValue) + } else { + externalState.deleteValue(key) + } + return value + }) + }, + ] + } + return useExternalState +} + +const sessionState: ExternalState = { + hasValue: (key: string) => sessionStorage.getItem(key) != null, + getValue: (key: string) => sessionStorage.getItem(key) ?? "", + setValue: (key: string, value: string) => sessionStorage.setItem(key, value), + deleteValue: (key: string) => sessionStorage.removeItem(key), +} +export const useSessionState = createExternalStateHook(sessionState) + +const hashState: ExternalState = { + hasValue: (key: string) => getHashParams().has(key), + getValue: (key: string) => getHashParams().get(key)!, + setValue: (key: string, value: string) => setHashParams({ [key]: value }), + deleteValue: (key: string) => setHashParams({ [key]: null }), +} +export const useHashState = createExternalStateHook(hashState) + +const sessionHashState: ExternalState = { + hasValue: (key: string) => + hashState.hasValue(key) || sessionState.hasValue(key), + getValue: (key: string) => + hashState.getValue(key) ?? sessionState.getValue(key), + setValue: (key: string, value: string) => { + hashState.setValue(key, value) + sessionState.setValue(key, value) + }, + deleteValue: (key: string) => { + hashState.deleteValue(key) + sessionState.deleteValue(key) + }, +} +export const useHashSessionState = createExternalStateHook(sessionHashState) diff --git a/src/pages/Storage/Storage.tsx b/src/pages/Storage/Storage.tsx index 7621f22..cd376f1 100644 --- a/src/pages/Storage/Storage.tsx +++ b/src/pages/Storage/Storage.tsx @@ -11,6 +11,7 @@ import { StorageQuery } from "./StorageQuery" import { StorageSubscriptions } from "./StorageSubscriptions" import { DocsRenderer } from "@/components/DocsRenderer" import { LoadingMetadata } from "@/components/Loading" +import { useHashState } from "@/lib/externalState" const metadataStorage$ = state( lookup$.pipe( @@ -33,8 +34,8 @@ const metadataStorage$ = state( export const Storage = withSubscribe( () => { const { lookup, entries } = useStateObservable(metadataStorage$) - const [pallet, setPallet] = useState("System") - const [entry, setEntry] = useState("Account") + const [pallet, setPallet] = useHashState("pallet", "System") + const [entry, setEntry] = useHashState("entry", "Account") const selectedEntry = useStateObservable(selectedEntry$) const selectedPallet = @@ -131,12 +132,12 @@ export const Storage = withSubscribe( )} - {selectedEntry?.docs.length && ( + {selectedEntry?.docs.length ? (
Docs
- )} + ) : null}