Repository navigation
Expand file tree
/
Copy pathwallets.tsx
More file actions
101 lines (93 loc) · 2.52 KB
/
Copy pathwallets.tsx
File metadata and controls
101 lines (93 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as C from '.'
import * as T from './types'
import * as Z from '@/util/zustand'
import invert from 'lodash/invert'
export const statusSimplifiedToString = invert(T.RPCStellar.PaymentStatus) as {
[K in T.RPCStellar.PaymentStatus]: keyof typeof T.RPCStellar.PaymentStatus
}
export const balanceDeltaToString = invert(T.RPCStellar.BalanceDelta) as {
[K in T.RPCStellar.BalanceDelta]: keyof typeof T.RPCStellar.BalanceDelta
}
export const makeAssetDescription = (
a?: Partial<T.Wallets.AssetDescription>
): T.Wallets.AssetDescription => ({
code: '',
depositButtonText: '',
infoUrl: '',
infoUrlText: '',
issuerAccountID: T.Wallets.noAccountID,
issuerName: '',
issuerVerifiedDomain: '',
showDepositButton: false,
showWithdrawButton: false,
withdrawButtonText: '',
...a,
})
export const emptyAssetDescription = makeAssetDescription()
export type Account = {
accountID: string
balanceDescription: string
deviceReadOnly: boolean
isDefault: boolean
name: string
}
type Store = T.Immutable<{
accountMap: Map<string, Account>
}>
const initialStore: Store = {
accountMap: new Map(),
}
interface State extends Store {
dispatch: {
load: () => void
removeAccount: (accountID: string) => void
resetState: 'default'
}
}
export const loadAccountsWaitingKey = 'wallets:loadAccounts'
export const _useState = Z.createZustand<State>((set, get) => {
const dispatch: State['dispatch'] = {
load: () => {
const f = async () => {
if (!C.useConfigState.getState().loggedIn) {
return
}
const res = await T.RPCStellar.localGetWalletAccountsLocalRpcPromise(undefined, [
loadAccountsWaitingKey,
])
set(s => {
s.accountMap = new Map(
res?.map(a => {
return [
a.accountID,
{
accountID: a.accountID,
balanceDescription: a.balanceDescription,
deviceReadOnly: a.deviceReadOnly,
isDefault: a.isDefault,
name: a.name,
},
]
})
)
})
}
C.ignorePromise(f())
},
removeAccount: accountID => {
const f = async () => {
await T.RPCStellar.localDeleteWalletAccountLocalRpcPromise(
{accountID, userAcknowledged: 'yes'},
loadAccountsWaitingKey
)
get().dispatch.load()
}
C.ignorePromise(f())
},
resetState: 'default',
}
return {
...initialStore,
dispatch,
}
})