generated from overmind-xyz/pay-me-a-river-remastered
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreatedStreamList.tsx
307 lines (278 loc) · 11.3 KB
/
CreatedStreamList.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { Button } from "@/components/ui/button";
import { useWallet } from "@aptos-labs/wallet-adapter-react";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { ScrollArea } from "@/components/ui/scroll-area";
import CountUp from "react-countup";
import { useEffect, useState } from "react";
import { useToast } from "@/components/ui/use-toast";
import { ToastAction } from "@/components/ui/toast";
import { Skeleton } from "@/components/ui/skeleton";
export type Stream = {
sender: string;
recipient: string;
amountAptFloat: number;
durationMilliseconds: number;
startTimestampMilliseconds: number;
streamId: number;
};
export default function CreatedStreamList(props: {
isTxnInProgress: boolean;
setTxn: (isTxnInProgress: boolean) => void;
}) {
// Wallet state
const { connected, account, signAndSubmitTransaction } = useWallet();
// Toast state
const { toast } = useToast();
// Streams state
const [streams, setStreams] = useState<Stream[]>([]);
const [areStreamsLoading, setAreStreamsLoading] = useState(true);
/*
Retrieve the streams from the module and set the streams state.
*/
useEffect(() => {
if (connected) {
getSenderStreams().then((streams) => {
setStreams(streams);
setAreStreamsLoading(false);
});
}
}, [account, connected, props.isTxnInProgress]);
/*
Cancels a selected stream.
*/
const cancelStream = async (recipient: string) => {
/*
TODO #7: Validate the account is defined before continuing. If not, return.
*/
/*
TODO #8: Set the isTxnInProgress state to true. This will display the loading spinner.
*/
/*
TODO #9: Make a request to the entry function `cancel_stream` to cancel the stream.
HINT:
- In case of an error, set the isTxnInProgress state to false and return.
- In case of success, display a toast notification with the transaction hash.
-- Toast notification --
toast({
title: "Stream closed!",
description: `Closed stream for ${`${recipient.slice(
0,
6
)}...${recipient.slice(-4)}`}`,
action: (
<a
href={`PLACEHOLDER: Input the explorer link here with the transaction hash`}
target="_blank"
>
<ToastAction altText="View transaction">View txn</ToastAction>
</a>
),
});
*/
/*
TODO #10: Set the isTxnInProgress state to false. This will hide the loading spinner.
*/
};
/*
Retrieves the sender streams.
*/
const getSenderStreams = async () => {
/*
TODO #4: Validate the account is defined before continuing. If not, return.
*/
/*
TODO #5: Make a request to the view function `get_senders_streams` to retrieve the gifts sent by
the user.
*/
/*
TODO #6: Parse the response from the view request and create the gifts array using the given
data. Return the new gifts array.
HINT:
- Remember to convert the amount to floating point number
*/
return []; // PLACEHOLDER: Remove this line
};
return (
<ScrollArea className="rounded-lg bg-neutral-400 border border-neutral-200 w-full">
<div className="h-fit max-h-96 w-full">
<Table className="w-full">
<TableHeader className="bg-neutral-300">
<TableRow className="uppercase text-xs font-matter hover:bg-neutral-300">
<TableHead className="text-center">ID</TableHead>
<TableHead className="text-center">Recipient</TableHead>
<TableHead className="text-center">End date</TableHead>
<TableHead className="text-center">Remaining amount</TableHead>
<TableHead className="text-center">Cancel stream</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{
/*
TODO #1: Add a skeleton loader when the streams are loading. Use the provided Skeleton component.
HINT:
- Use the areStreamsLoading state to determine if the streams are loading.
-- Skeleton loader --
<TableRow>
<TableCell className="items-center">
<div className="flex flex-row justify-center items-center w-full">
<Skeleton className="h-4 w-4" />
</div>
</TableCell>
<TableCell className="items-center">
<div className="flex flex-row justify-center items-center w-full">
<Skeleton className="h-4 w-24" />
</div>
</TableCell>
<TableCell className="items-center">
<div className="flex flex-row justify-center items-center w-full">
<Skeleton className="h-4 w-24" />
</div>
</TableCell>
<TableCell className="items-center">
<div className="flex flex-row justify-center items-center w-full">
<Skeleton className="h-4 w-24" />
</div>
</TableCell>
<TableCell className="items-center">
<div className="flex flex-row justify-center items-center w-full">
<Skeleton className="h-8 w-12" />
</div>
</TableCell>
</TableRow>
*/
}
{
/*
TODO #2: Add a row to the table when there are no streams. Use the provided component
to display the message.
HINT:
- Use the areStreamsLoading state to determine if the streams are loading.
- Use the streams state to determine if there are any streams.
-- message component --
<TableRow className="hover:bg-neutral-400">
<TableCell colSpan={5}>
<p className="break-normal text-center font-matter py-4 text-neutral-100">
You don't have any outgoing payments.
</p>
</TableCell>
</TableRow>
*/
}
{
/*
TODO #3: Add a row to the table for each stream in the streams array. Use the provided
component to display the stream information.
HINT:
- Use the areStreamsLoading state to determine if the streams are loading. Don't display
the streams if they are loading.
- Use the streams state to determine if there are any streams.
-- stream component --
<TableRow
key={index}
className="font-matter hover:bg-neutral-400"
>
<TableCell className="text-center">
PLACEHOLDER: Input the stream id here {0}
</TableCell>
<TableCell className="text-center">
<TooltipProvider>
<Tooltip>
<TooltipTrigger>PLACEHOLDER: truncate recipient address here</TooltipTrigger>
<TooltipContent>
<p>PLACEHOLDER: full recipient address here</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
</TableCell>
<TableCell className="text-center">
{
TODO: Display the end date of the stream. If the stream has not started,
display a message saying "Stream has not started". Use the provided
component to display the date.
HINT:
- Use the startTimestampMilliseconds to determine if the stream has started.
- Use the durationMilliseconds and startTimestampMilliseconds to calculate
the end date.
-- date component --
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
{endDate.toLocaleDateString()}
</TooltipTrigger>
<TooltipContent>
<p>{endDate.toLocaleString()}</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
-- message component --
<p>
<i>Stream has not started</i>
</p>
}
</TableCell>
<TableCell className="font-mono text-center">
{
TODO: Display the remaining amount of the stream. If the stream has not started,
display the full amount. Use the provided component to display the amount.
HINT:
- Use the startTimestampMilliseconds to determine if the stream has started.
- Use the durationMilliseconds and startTimestampMilliseconds to determine if
the stream has finished.
-- amount component (show when stream is completed) --
<p>0.00 APT</p>
-- amount component (show when stream is not completed) --
<CountUp
start={amountRemaining}
end={0}
duration={stream.durationMilliseconds / 1000}
decimals={8}
decimal="."
suffix=" APT"
useEasing={false}
/>
-- amount component (show when stream has not started) --
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
PLACEHOLDER: Input the amount here (format to 2 decimal places)
</TooltipTrigger>
<TooltipContent>
<p>
PLACEHOLDER: Input the amount here (format to 8 decimal places)
</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
}
</TableCell>
<TableCell className="text-center">
<Button
size="sm"
className="bg-red-800 hover:bg-red-700 text-white"
onClick={() => console.log('PLACEHOLDER: cancel stream')}
>
Cancel
</Button>
</TableCell>
</TableRow>
*/
}
</TableBody>
</Table>
</div>
</ScrollArea>
);
}