-
Notifications
You must be signed in to change notification settings - Fork 28
/
ProviderWallets.tsx
70 lines (60 loc) · 1.78 KB
/
ProviderWallets.tsx
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
import { invoke } from "@tauri-apps/api/tauri";
import { useRegisterActions } from "kbar";
import { ReactNode, createContext } from "react";
import { useInvoke } from "../hooks/tauri";
import { useRefreshWallets } from "../hooks/useRefreshWallets";
import { Wallet } from "../types";
interface Value {
wallets?: Wallet[];
currentWallet?: Wallet;
setCurrentWallet: (idx: number) => Promise<unknown>;
setCurrentAddress: (key: string) => Promise<unknown>;
}
export const WalletsContext = createContext<Value>({} as Value);
const actionId = "wallet";
export function ProviderWallets({ children }: { children: ReactNode }) {
const { data: wallets, mutate: mutateWallets } =
useInvoke<Wallet[]>("wallets_get_all");
const { data: currentWallet, mutate: mutateCurrentWallet } =
useInvoke<Wallet>("wallets_get_current");
const value = {
wallets,
currentWallet,
setCurrentWallet: async (idx: number) => {
if (
!wallets ||
!currentWallet ||
wallets[idx].name === currentWallet.name
)
return;
await invoke("wallets_set_current_wallet", { idx });
mutateCurrentWallet();
},
setCurrentAddress: async (key: string) => {
await invoke("wallets_set_current_path", { key });
mutateCurrentWallet();
},
};
useRefreshWallets(() => {
mutateWallets();
mutateCurrentWallet();
});
useRegisterActions(
[
{
id: actionId,
name: "Change wallet",
},
...(wallets || []).map(({ name }, i) => ({
id: `${actionId}/${name}`,
name,
parent: actionId,
perform: () => value.setCurrentWallet(i),
})),
],
[wallets, value.setCurrentWallet]
);
return (
<WalletsContext.Provider value={value}>{children}</WalletsContext.Provider>
);
}