mirror of
https://github.com/joinmarket-webui/jam.git
synced 2026-08-19 13:18:30 +02:00
chore: add JmTxStore
This commit is contained in:
parent
468326a3f6
commit
ce644df7bf
5 changed files with 47 additions and 30 deletions
|
|
@ -26,7 +26,7 @@ export const authStore = createStore<AuthStoreState>()(
|
|||
clear: () => set({ state: undefined }),
|
||||
}),
|
||||
{
|
||||
name: 'jam-auth',
|
||||
name: 'jam-auth-store',
|
||||
storage: createJSONStorage(() => sessionStorage),
|
||||
},
|
||||
),
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ export const jamSettingsStore = createStore<JamSettingsStoreState>()(
|
|||
clear: () => set({ state: initial }),
|
||||
}),
|
||||
{
|
||||
name: 'jam-settings',
|
||||
name: 'jam-settings-store',
|
||||
storage: createJSONStorage(() => localStorage),
|
||||
},
|
||||
),
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
},
|
||||
),
|
||||
|
|
|
|||
|
|
@ -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
37
src/store/jmTxStore.ts
Normal 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),
|
||||
},
|
||||
),
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue