This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
/
Votes.tsx
executable file
·177 lines (159 loc) · 5.74 KB
/
Votes.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import {
DelegateMapper,
NetworkData,
Profile,
ReadOnlyWallet,
ReadWriteWallet,
} from "@arkecosystem/platform-sdk-profiles";
import { Address } from "app/components/Address";
import { Avatar } from "app/components/Avatar";
import { Circle } from "app/components/Circle";
import { Header } from "app/components/Header";
import { HeaderSearchBar } from "app/components/Header/HeaderSearchBar";
import { Icon } from "app/components/Icon";
import { Input } from "app/components/Input";
import { Page, Section } from "app/components/Layout";
import { TransactionDetail } from "app/components/TransactionDetail";
import { useEnvironmentContext } from "app/contexts";
import { useActiveProfile, useActiveWallet } from "app/hooks/env";
import { SelectNetwork } from "domains/network/components/SelectNetwork";
import { AddressTable } from "domains/vote/components/AddressTable";
import { DelegateTable } from "domains/vote/components/DelegateTable";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { useHistory, useParams } from "react-router-dom";
const InputAddress = ({ address, profile }: { address: string; profile: Profile }) => {
const { t } = useTranslation();
const walletName = profile.wallets().findByAddress(address)?.alias();
return (
<div className="relative flex items-center pb-24">
<Input type="text" disabled />
<div className="absolute flex items-center justify-between w-full ml-3">
<div className="flex items-center">
{address ? (
<>
<Avatar className="mr-3" address={address} size="sm" noShadow />
<Address address={address} walletName={walletName} />
</>
) : (
<>
<Circle className="mr-3 bg-theme-neutral-200 border-theme-neutral-200" size="sm" noShadow />
<span className="text-base font-semibold text-theme-neutral-light">
{t("COMMON.SELECT_OPTION", { option: t("COMMON.ADDRESS") })}
</span>
</>
)}
</div>
<Icon name="User" className="mr-6" width={20} height={20} />
</div>
</div>
);
};
export const Votes = () => {
const history = useHistory();
const { walletId: hasWalletId } = useParams();
const { env } = useEnvironmentContext();
const activeProfile = useActiveProfile();
const activeWallet = useActiveWallet();
const [network, setNetwork] = useState<NetworkData | null>(null);
const [wallets, setWallets] = useState<ReadWriteWallet[]>([]);
const [address, setAddress] = useState(hasWalletId ? activeWallet.address() : "");
const [delegates, setDelegates] = useState<ReadOnlyWallet[]>([]);
const { t } = useTranslation();
const crumbs = [
{
route: `/profiles/${activeProfile.id()}/dashboard`,
label: t("COMMON.GO_BACK_TO_PORTFOLIO"),
},
];
const networks = useMemo(() => env.availableNetworks(), [env]);
useEffect(() => {
if (hasWalletId) {
for (const network of networks) {
if (network.coin() === activeWallet.coinId() && network.id() === activeWallet.networkId()) {
setNetwork(network);
break;
}
}
}
}, [activeWallet, hasWalletId, networks]);
useEffect(() => {
if (network) {
setWallets(activeProfile.wallets().findByCoinWithNetwork(network.coin(), network.id()));
}
}, [activeProfile, network]);
const loadDelegates = useCallback(
async (wallet) => {
// TODO: move this to profile initialising and run it every X period
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
await env.coins().syncDelegates(wallet?.coinId()!, wallet?.networkId()!);
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
const delegates = env.coins().delegates(wallet?.coinId()!, wallet?.networkId()!);
const readOnlyDelegates = DelegateMapper.execute(
wallet,
delegates.map((delegate: ReadOnlyWallet) => delegate.publicKey),
);
setDelegates(readOnlyDelegates);
},
[env],
);
useEffect(() => {
if (hasWalletId) {
loadDelegates(activeWallet);
}
}, [activeWallet, loadDelegates, hasWalletId]);
const handleSelectNetwork = (network?: NetworkData | null) => {
setNetwork(network!);
};
const handleSelectAddress = async (address: string) => {
setAddress(address);
const wallet = activeProfile.wallets().findByAddress(address);
await loadDelegates(wallet);
};
return (
<Page profile={activeProfile} crumbs={crumbs}>
<Section>
<Header
title={t("VOTE.VOTES_PAGE.TITLE")}
subtitle={t("VOTE.VOTES_PAGE.SUBTITLE")}
extra={<HeaderSearchBar placeholder={t("VOTE.VOTES_PAGE.SEARCH_PLACEHOLDER")} />}
/>
</Section>
<div className="container mx-auto px-14">
<div className="grid grid-flow-col grid-cols-2 gap-6 -my-5">
<TransactionDetail border={false} label={t("COMMON.NETWORK")}>
<SelectNetwork
id="Votes__network"
networks={networks}
selected={network!}
placeholder={t("COMMON.SELECT_OPTION", { option: t("COMMON.NETWORK") })}
onSelect={handleSelectNetwork}
disabled={hasWalletId}
/>
</TransactionDetail>
<TransactionDetail border={false} label={t("COMMON.ADDRESS")} className="mt-2">
<InputAddress address={address} profile={activeProfile} />
</TransactionDetail>
</div>
</div>
<Section className="flex-1">
{address ? (
<DelegateTable
coin={network?.coin()}
delegates={delegates}
onContinue={(delegateAddress) => {
const walletId = hasWalletId
? activeWallet.id()
: activeProfile.wallets().findByAddress(address)?.id();
history.push(
`/profiles/${activeProfile.id()}/wallets/${walletId}/transactions/vote/${delegateAddress}/sender/${address}`,
);
}}
/>
) : (
<AddressTable wallets={wallets} onSelect={handleSelectAddress} />
)}
</Section>
</Page>
);
};