diff --git a/src/store/authStore.ts b/src/store/authStore.ts index 750c857c..a00a431b 100644 --- a/src/store/authStore.ts +++ b/src/store/authStore.ts @@ -26,7 +26,7 @@ export const authStore = createStore()( clear: () => set({ state: undefined }), }), { - name: 'jam-auth', + name: 'jam-auth-store', storage: createJSONStorage(() => sessionStorage), }, ), diff --git a/src/store/jamSettingsStore.ts b/src/store/jamSettingsStore.ts index 960eba10..d659fe69 100644 --- a/src/store/jamSettingsStore.ts +++ b/src/store/jamSettingsStore.ts @@ -31,7 +31,7 @@ export const jamSettingsStore = createStore()( clear: () => set({ state: initial }), }), { - name: 'jam-settings', + name: 'jam-settings-store', storage: createJSONStorage(() => localStorage), }, ), diff --git a/src/store/jmConfigStore.ts b/src/store/jmConfigStore.ts index e0027bb1..6030335b 100644 --- a/src/store/jmConfigStore.ts +++ b/src/store/jmConfigStore.ts @@ -9,7 +9,6 @@ export type JmConfigs = { interface JmConfigStoreState { state: JmConfigs get: (val: ConfigKey) => ConfigValue | null - getAll: () => ConfigValue[] set: (val: ConfigValue) => void clear: () => void } @@ -21,39 +20,20 @@ export const jmConfigStore = createStore()( (set, get) => ({ state: initial, get: (key) => { - const fields = get().state[key.section] - if (fields === undefined) return null - - const value = fields[key.field] - if (value === undefined) return null - - return { - key, - value: value || null, - } - }, - getAll: () => { - return Object.entries(get().state).flatMap(([section, entries]) => { - return Object.entries(entries).map(([field, value]) => ({ - key: { - section, - field, - }, - value, - })) - }) + const value = get().state[key.section]?.[key.field] + return value !== undefined ? { key, value } : null }, set: (val) => set((state) => { - const copy = { state: { ...state.state } } - copy.state[val.key.section] = copy.state[val.key.section] || {} - copy.state[val.key.section][val.key.field] = val.value - return copy + const copy = { ...state.state } + copy[val.key.section] = copy[val.key.section] || {} + copy[val.key.section][val.key.field] = val.value + return { state: copy } }), clear: () => set({ state: initial }), }), { - name: 'jm-config', + name: 'jm-config-store', storage: createJSONStorage(() => sessionStorage), }, ), diff --git a/src/store/jmSessionStore.ts b/src/store/jmSessionStore.ts index a97eb05f..96223cef 100644 --- a/src/store/jmSessionStore.ts +++ b/src/store/jmSessionStore.ts @@ -8,5 +8,5 @@ interface JmSessionStoreState { export const jmSessionStore = createStore()((set) => ({ state: undefined, - update: (val) => set((state) => ({ state: { ...state.state, ...val } })), + update: (val) => set(() => ({ state: val })), })) diff --git a/src/store/jmTxStore.ts b/src/store/jmTxStore.ts new file mode 100644 index 00000000..de7ff677 --- /dev/null +++ b/src/store/jmTxStore.ts @@ -0,0 +1,37 @@ +import type { DirectSendResponse } from '@joinmarket-webui/joinmarket-api-ts/jm' +import { createStore } from 'zustand' +import { persist, createJSONStorage } from 'zustand/middleware' + +export type TxId = NonNullable +export type JmTxInfo = Omit & { + txid: TxId +} + +type JmTxInfoMap = Map + +interface JmTxStoreState { + state: JmTxInfoMap + get: (txid: TxId) => JmTxInfo | undefined + getAll: () => JmTxInfoMap + add: (val: JmTxInfo) => void + clear: () => void +} + +export const jmTxStore = createStore()( + persist( + (set, get) => ({ + state: new Map([] as [TxId, JmTxInfo][]), + get: (txid) => get().state.get(txid), + getAll: () => get().state, + add: (val) => + set((state) => ({ + state: new Map(state.state).set(val.txid, val), + })), + clear: () => set({ state: new Map() }), + }), + { + name: 'jm-tx-store', + storage: createJSONStorage(() => sessionStorage), + }, + ), +)