-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathPingPongComponent.tsx
More file actions
227 lines (192 loc) · 6.67 KB
/
PingPongComponent.tsx
File metadata and controls
227 lines (192 loc) · 6.67 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { faArrowDown, faArrowUp } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { TokenLoginType } from '@multiversx/sdk-dapp/out/types/login.types';
import { Duration } from 'luxon';
import { useEffect, useState } from 'react';
import { contractAddress } from 'config';
import { getCountdownSeconds, setTimeRemaining } from 'helpers';
import {
MvxButton,
MvxDataWithExplorerLink,
useGetNetworkConfig,
useGetPendingTransactionsSessions
} from 'lib';
import { ACCOUNTS_ENDPOINT, Transaction, useGetPendingTransactions } from 'lib';
import { ItemsIdentifiersEnum } from 'pages/Dashboard/dashboard.types';
import { Label } from '../Label';
import { MissingNativeAuthError } from '../MissingNativeAuthError';
import { OutputContainer } from '../OutputContainer';
import { PingPongOutput } from '../OutputContainer/components';
const styles = {
pingPongContainer: 'ping-pong-container flex flex-col gap-6',
infosContainer: 'infos-container flex flex-col gap-2',
addressComponent: 'address-component flex w-full justify-between',
timeRemaining: 'time-remaining text-red-600',
buttonsContainer: 'buttons-container flex flex-col gap-2',
buttons: 'buttons flex justify-start gap-2',
buttonContent: 'button-content text-sm font-normal'
} satisfies Record<string, string>;
export interface PingTransactionPayloadType {
amount?: string;
transactions?: Transaction[];
}
interface PingPongComponentPropsType {
identifier: `${ItemsIdentifiersEnum}`;
sendPingTransaction: (
payload: PingTransactionPayloadType
) => Promise<string | undefined>;
sendPongTransaction: (
transactions?: Transaction[]
) => Promise<string | undefined>;
getTimeToPong: () => Promise<number | null | undefined>;
pingAmount?: string;
getPingTransaction?: () => Promise<Transaction | null>;
getPongTransaction?: () => Promise<Transaction | null>;
tokenLogin?: TokenLoginType | null;
}
export const PingPongComponent = ({
identifier,
sendPingTransaction,
sendPongTransaction,
getTimeToPong,
pingAmount,
getPingTransaction,
getPongTransaction,
tokenLogin
}: PingPongComponentPropsType) => {
const { network } = useGetNetworkConfig();
const [currentSessionId, setCurrentSessionId] = useState<string>();
const transactions = useGetPendingTransactions();
const pendingSession = useGetPendingTransactionsSessions();
const [sessionId] = Object.keys(pendingSession);
const pingPongTransactions =
currentSessionId === sessionId ? transactions : [];
const hasPendingTransactions = pingPongTransactions.length > 0;
const explorerAddress = network.explorerAddress;
const [hasPing, setHasPing] = useState(true);
const [secondsLeft, setSecondsLeft] = useState(0);
const setSecondsRemaining = async () => {
if (tokenLogin && !tokenLogin?.nativeAuthToken) {
return;
}
const msRemaining = await getTimeToPong();
// If backend now returns milliseconds, convert to whole seconds
const secondsRemaining =
msRemaining == null
? msRemaining
: Math.max(0, Math.floor(msRemaining / 1000));
const { canPing, timeRemaining } = setTimeRemaining(secondsRemaining);
setHasPing(canPing);
if (timeRemaining && timeRemaining >= 0) {
setSecondsLeft(timeRemaining);
}
};
const onSendPingTransaction = async () => {
if (pingAmount) {
const sessionId = await sendPingTransaction({ amount: pingAmount });
setCurrentSessionId(sessionId);
return;
}
if (!getPingTransaction) {
return;
}
const pingTransaction = await getPingTransaction();
if (!pingTransaction) {
return;
}
const sessionId = await sendPingTransaction({
transactions: [pingTransaction]
});
setCurrentSessionId(sessionId);
};
const onSendPongTransaction = async () => {
if (pingAmount) {
const sessionId = await sendPongTransaction();
setCurrentSessionId(sessionId);
return;
}
if (!getPongTransaction) {
return;
}
const pongTransaction = await getPongTransaction();
if (!pongTransaction) {
return;
}
const sessionId = await sendPongTransaction([pongTransaction]);
setCurrentSessionId(sessionId);
};
const timeRemaining = Duration.fromObject({
seconds: secondsLeft ?? 0
}).toFormat('mm:ss');
const pongAllowed = secondsLeft === 0;
useEffect(() => {
getCountdownSeconds({ secondsLeft, setSecondsLeft });
}, [hasPing]);
useEffect(() => {
setSecondsRemaining();
}, [hasPendingTransactions]);
if (tokenLogin && !tokenLogin?.nativeAuthToken) {
return <MissingNativeAuthError />;
}
const isPingDisabled = !hasPing || hasPendingTransactions;
const isPongDisabled = !pongAllowed || hasPing || hasPendingTransactions;
return (
<div id={identifier} className={styles.pingPongContainer}>
<div className={styles.infosContainer}>
<Label>Contract: </Label>
<OutputContainer>
{!hasPendingTransactions && (
<>
<MvxDataWithExplorerLink
withTooltip={true}
data={contractAddress}
className={styles.addressComponent}
explorerLink={`${explorerAddress}/${ACCOUNTS_ENDPOINT}/${contractAddress}`}
/>
{!pongAllowed && (
<p>
<Label>Time remaining: </Label>
<span className={styles.timeRemaining}>{timeRemaining}</span>
<span> until able to pong</span>
</p>
)}
</>
)}
<PingPongOutput
transactions={pingPongTransactions}
pongAllowed={pongAllowed}
timeRemaining={timeRemaining}
/>
</OutputContainer>
</div>
<div className={styles.buttonsContainer}>
<div className={styles.buttons}>
<MvxButton
data-testid='btnPing'
disabled={isPingDisabled}
onClick={isPingDisabled ? undefined : onSendPingTransaction}
size='small'
>
<FontAwesomeIcon
icon={faArrowUp}
className={styles.buttonContent}
/>
<span className={styles.buttonContent}>Ping</span>
</MvxButton>
<MvxButton
data-testid='btnPong'
disabled={isPongDisabled}
onClick={isPongDisabled ? undefined : onSendPongTransaction}
size='small'
>
<FontAwesomeIcon
icon={faArrowDown}
className={styles.buttonContent}
/>
<span className={styles.buttonContent}>Pong</span>
</MvxButton>
</div>
</div>
</div>
);
};