forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.ts
More file actions
109 lines (92 loc) · 2.08 KB
/
Copy pathstorage.ts
File metadata and controls
109 lines (92 loc) · 2.08 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
102
103
104
105
106
107
108
109
import {
WalletId,
WalletSessionSnapshot
} from './types';
const RECENT_WALLETS_KEY =
'txio_recent_wallets';
const ACTIVE_WALLET_KEY =
'txio_active_wallet';
const isBrowser = () =>
typeof window !== 'undefined';
export const readRecentWallets = () => {
if (!isBrowser()) {
return [] as WalletId[];
}
try {
const raw = localStorage.getItem(
RECENT_WALLETS_KEY
);
if (!raw) {
return [] as WalletId[];
}
const parsed = JSON.parse(raw);
return Array.isArray(parsed)
? (parsed.filter(
(value) =>
typeof value ===
'string'
) as WalletId[])
: [];
} catch {
return [] as WalletId[];
}
};
export const persistRecentWallet = (
walletId: WalletId
) => {
if (!isBrowser()) {
return;
}
const next = [
walletId,
...readRecentWallets().filter(
(value) =>
value !== walletId
)
].slice(0, 4);
localStorage.setItem(
RECENT_WALLETS_KEY,
JSON.stringify(next)
);
};
export const readActiveWalletSnapshot =
() => {
if (!isBrowser()) {
return null;
}
try {
const raw =
localStorage.getItem(
ACTIVE_WALLET_KEY
);
if (!raw) {
return null;
}
return JSON.parse(
raw
) as WalletSessionSnapshot;
} catch {
return null;
}
};
export const persistActiveWalletSnapshot =
(
snapshot: WalletSessionSnapshot
) => {
if (!isBrowser()) {
return;
}
localStorage.setItem(
ACTIVE_WALLET_KEY,
JSON.stringify(snapshot)
);
};
export const clearActiveWalletSnapshot =
() => {
if (!isBrowser()) {
return;
}
localStorage.removeItem(
ACTIVE_WALLET_KEY
);
};