-
Notifications
You must be signed in to change notification settings - Fork 294
/
Body.tsx
185 lines (183 loc) · 6.02 KB
/
Body.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
178
179
180
181
182
183
184
185
import { UserRefusedOnDevice } from "@ledgerhq/errors";
import { addPendingOperation } from "@ledgerhq/live-common/account/index";
import { getAccountBridge } from "@ledgerhq/live-common/bridge/index";
import { SyncSkipUnderPriority } from "@ledgerhq/live-common/bridge/react/index";
import useBridgeTransaction from "@ledgerhq/live-common/bridge/useBridgeTransaction";
import { SolanaAccount, Transaction } from "@ledgerhq/live-common/families/solana/types";
import { AccountBridge, Operation, Account } from "@ledgerhq/types-live";
import invariant from "invariant";
import React, { useCallback, useState } from "react";
import { Trans, withTranslation } from "react-i18next";
import { TFunction } from "i18next";
import { connect, useDispatch } from "react-redux";
import { compose } from "redux";
import { createStructuredSelector } from "reselect";
import logger from "~/renderer/logger";
import { updateAccountWithUpdater } from "~/renderer/actions/accounts";
import { OpenModal, openModal } from "~/renderer/actions/modals";
import Track from "~/renderer/analytics/Track";
import Stepper from "~/renderer/components/Stepper";
import GenericStepConnectDevice from "~/renderer/modals/Send/steps/GenericStepConnectDevice";
import { getCurrentDevice } from "~/renderer/reducers/devices";
import StepAmount, { StepAmountFooter } from "./steps/StepAmount";
import StepConfirmation, { StepConfirmationFooter } from "./steps/StepConfirmation";
import StepValidator, { StepValidatorFooter } from "./steps/StepValidator";
import { St, StepProps, StepId } from "./types";
import { Device } from "@ledgerhq/live-common/hw/actions/types";
import { defaultVoteAccAddrByCurrencyId } from "@ledgerhq/live-common/families/solana/logic";
export type Data = {
account: SolanaAccount;
source?: string;
};
type OwnProps = {
stepId: StepId;
onClose: () => void;
onChangeStepId: (a: StepId) => void;
params: Data;
};
type StateProps = {
t: TFunction;
device: Device | undefined | null;
accounts: Account[];
openModal: OpenModal;
};
type Props = OwnProps & StateProps;
const steps: Array<St> = [
{
id: "validator",
label: <Trans i18nKey="solana.delegation.flow.steps.validator.title" />,
component: StepValidator,
noScroll: true,
footer: StepValidatorFooter,
},
{
id: "amount",
label: <Trans i18nKey="solana.delegation.flow.steps.amount.title" />,
component: StepAmount,
onBack: ({ transitionTo }: StepProps) => transitionTo("validator"),
noScroll: true,
footer: StepAmountFooter,
},
{
id: "connectDevice",
label: <Trans i18nKey="solana.common.connectDevice.title" />,
component: GenericStepConnectDevice,
onBack: ({ transitionTo }: StepProps) => transitionTo("amount"),
},
{
id: "confirmation",
label: <Trans i18nKey="solana.common.confirmation.title" />,
component: StepConfirmation,
footer: StepConfirmationFooter,
},
];
const mapStateToProps = createStructuredSelector({
device: getCurrentDevice,
});
const mapDispatchToProps = {
openModal,
};
const Body = ({ t, stepId, device, onClose, openModal, onChangeStepId, params }: Props) => {
const [optimisticOperation, setOptimisticOperation] = useState<Operation | null>(null);
const [transactionError, setTransactionError] = useState<Error | null>(null);
const [signed, setSigned] = useState(false);
const dispatch = useDispatch();
const { account, source = "Account Page" } = params;
const {
transaction,
setTransaction,
updateTransaction,
parentAccount,
status,
bridgeError,
bridgePending,
} = useBridgeTransaction(() => {
invariant(account && account.solanaResources, "solana: account and solana resources required");
const bridge: AccountBridge<Transaction> = getAccountBridge(account, undefined);
const transaction = bridge.updateTransaction(bridge.createTransaction(account), {
model: {
kind: "stake.createAccount",
uiState: {
delegate: {
voteAccAddress: defaultVoteAccAddrByCurrencyId(account.currency.id) ?? "",
},
},
},
});
return {
account,
parentAccount: undefined,
transaction,
};
});
const handleStepChange = useCallback((e: St) => onChangeStepId(e.id), [onChangeStepId]);
const handleRetry = useCallback(() => {
setTransactionError(null);
onChangeStepId("connectDevice");
}, [onChangeStepId]);
const handleTransactionError = useCallback((error: Error) => {
if (!(error instanceof UserRefusedOnDevice)) {
logger.critical(error);
}
setTransactionError(error);
}, []);
const handleOperationBroadcasted = useCallback(
(optimisticOperation: Operation) => {
if (!account) return;
dispatch(
updateAccountWithUpdater(account.id, account =>
addPendingOperation(account, optimisticOperation),
),
);
setOptimisticOperation(optimisticOperation);
setTransactionError(null);
},
[account, dispatch],
);
const error = transactionError || bridgeError;
const errorSteps = [];
if (transactionError) {
errorSteps.push(2);
} else if (bridgeError) {
errorSteps.push(0);
}
const stepperProps = {
title: t("solana.delegation.flow.title"),
device,
account,
parentAccount,
transaction,
signed,
stepId,
steps,
errorSteps,
disabledSteps: [],
hideBreadcrumb: !!error && ["validator"].includes(stepId),
onRetry: handleRetry,
onStepChange: handleStepChange,
onClose,
error,
status,
optimisticOperation,
openModal,
setSigned,
onChangeTransaction: setTransaction,
onUpdateTransaction: updateTransaction,
onOperationBroadcasted: handleOperationBroadcasted,
onTransactionError: handleTransactionError,
t,
bridgePending,
source,
};
return (
<Stepper {...stepperProps}>
<SyncSkipUnderPriority priority={100} />
<Track onUnmount event="CloseModalDelegation" />
</Stepper>
);
};
const C = compose<React.ComponentType<OwnProps>>(
connect(mapStateToProps, mapDispatchToProps),
withTranslation(),
)(Body);
export default C;