chore: add JmTxStore

This commit is contained in:
theborakompanioni 2026-02-08 13:02:16 +01:00
parent 468326a3f6
commit ce644df7bf
No known key found for this signature in database
GPG key ID: E8070AF0053AAC0D
5 changed files with 47 additions and 30 deletions

View file

@ -26,7 +26,7 @@ export const authStore = createStore<AuthStoreState>()(
clear: () => set({ state: undefined }),
}),
{
name: 'jam-auth',
name: 'jam-auth-store',
storage: createJSONStorage(() => sessionStorage),
},
),

View file

@ -31,7 +31,7 @@ export const jamSettingsStore = createStore<JamSettingsStoreState>()(
clear: () => set({ state: initial }),
}),
{
name: 'jam-settings',
name: 'jam-settings-store',
storage: createJSONStorage(() => localStorage),
},
),

View file

@ -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<JmConfigStoreState>()(
(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),
},
),

View file

@ -8,5 +8,5 @@ interface JmSessionStoreState {
export const jmSessionStore = createStore<JmSessionStoreState>()((set) => ({
state: undefined,
update: (val) => set((state) => ({ state: { ...state.state, ...val } })),
update: (val) => set(() => ({ state: val })),
}))

37
src/store/jmTxStore.ts Normal file
View file

@ -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<DirectSendResponse['txinfo']['txid']>
export type JmTxInfo = Omit<DirectSendResponse['txinfo'], 'txid'> & {
txid: TxId
}
type JmTxInfoMap = Map<TxId, JmTxInfo>
interface JmTxStoreState {
state: JmTxInfoMap
get: (txid: TxId) => JmTxInfo | undefined
getAll: () => JmTxInfoMap
add: (val: JmTxInfo) => void
clear: () => void
}
export const jmTxStore = createStore<JmTxStoreState>()(
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),
},
),
)