-
Notifications
You must be signed in to change notification settings - Fork 10
/
ActivityTable.tsx
341 lines (328 loc) · 10.8 KB
/
ActivityTable.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { ChangeEvent, FC } from 'react';
import { TokensOverlap } from 'components/common/tokensOverlap';
import { Activity, ActivityAction } from 'libs/queries/extApi/activity';
import { Link, NewTabLink } from 'libs/routing';
import { cn, getLowestBits, shortenString, tokenAmount } from 'utils/helpers';
import { getExplorerLink } from 'utils/blockExplorer';
import { ReactComponent as IconCheck } from 'assets/icons/check.svg';
import { ReactComponent as IconPause } from 'assets/icons/pause.svg';
import { ReactComponent as IconEdit } from 'assets/icons/edit.svg';
import { ReactComponent as IconArrowDown } from 'assets/icons/arrowDown.svg';
import { ReactComponent as IconWithdraw } from 'assets/icons/withdraw.svg';
import { ReactComponent as IconDeposit } from 'assets/icons/deposit.svg';
import { ReactComponent as IconDelete } from 'assets/icons/delete.svg';
import { ReactComponent as IconTransfer } from 'assets/icons/transfer.svg';
import { ReactComponent as IconLink } from 'assets/icons/link.svg';
import { ReactComponent as IconChevronLeft } from 'assets/icons/chevron-left.svg';
import {
activityActionName,
activityDateFormatter,
activityDescription,
activityKey,
budgetColor,
} from './utils';
import { usePagination } from 'hooks/useList';
import { SafeDecimal } from 'libs/safedecimal';
import { Token } from 'libs/tokens';
import { ActivityListProps } from './ActivityList';
import { NotFound } from 'components/common/NotFound';
import { useActivity } from './ActivityProvider';
import style from './ActivityTable.module.css';
const thStyle = cn(
'text-start font-weight-400 py-16 pl-8 whitespace-nowrap',
'first:pl-24',
'last:pr-24 last:text-end'
);
const tdFirstLine = cn(
'pt-12 align-bottom whitespace-nowrap pl-8',
'first:pl-24',
'last:pr-24 last:text-end'
);
const tdSecondLine = cn(
'pb-12 align-top whitespace-nowrap pl-8',
'last:pr-24 last:text-end'
);
export const ActivityTable: FC<ActivityListProps> = (props) => {
const { activities, hideIds = false } = props;
return (
<table className={cn('w-full border-collapse', style.table)}>
<thead>
<tr className="border-background-800 text-14 border-y font-mono text-white/60">
{!hideIds && <th className={thStyle}>ID</th>}
<th className={thStyle} colSpan={2}>
Action
</th>
<th className={thStyle}>Buy Budget</th>
<th className={thStyle}>Sell Budget</th>
<th className={thStyle}>Date</th>
</tr>
</thead>
<tbody>
{activities.length === 0 ? (
<ActivityEmpty />
) : (
activities.map((activity, i) => (
<ActivityRow
key={activityKey(activity, i)}
activity={activity}
hideIds={hideIds}
index={i}
/>
))
)}
</tbody>
<tfoot>
<ActivityPaginator />
</tfoot>
</table>
);
};
const ActivityEmpty = () => (
<tr>
<td className="py-24 text-center" colSpan={6}>
<NotFound
variant="error"
title="There are no results for your filter"
text="Please reset your filters or try a different date range."
/>
</td>
</tr>
);
interface ActivityRowProps {
activity: Activity;
hideIds: boolean;
index: number;
}
const ActivityRow: FC<ActivityRowProps> = ({ activity, hideIds, index }) => {
const { searchParams, setSearchParams } = useActivity();
const { strategy, changes } = activity;
const { base, quote } = strategy;
const setAction = () => {
const actions = searchParams.actions.includes(activity.action)
? []
: [activity.action];
setSearchParams({ actions });
};
return (
<>
<tr className="text-14" style={{ animationDelay: `${index * 50}ms` }}>
{!hideIds && (
<td rowSpan={2} className="py-12 pl-8 first:pl-24">
<ActivityId activity={activity} size={14} />
</td>
)}
<td rowSpan={2} className="py-12 pl-8 first:pl-24">
<button onClick={setAction}>
<ActivityIcon activity={activity} size={32} />
</button>
</td>
<td className={cn(tdFirstLine, 'font-weight-500')}>
<button onClick={setAction} className="w-full text-start">
{activityActionName[activity.action]}
</button>
</td>
<td className={tdFirstLine}>
{tokenAmount(strategy.buy.budget, quote)}
</td>
<td className={tdFirstLine}>
{tokenAmount(strategy.sell.budget, base)}
</td>
<td className={tdFirstLine}>
{activityDateFormatter.format(activity.date)}
</td>
</tr>
<tr
className="text-12 font-mono text-white/60"
style={{ animationDelay: `${index * 50}ms` }}
>
{/* ID */}
{/* Action Icon */}
<td className={cn(tdSecondLine, 'w-full')}>
<button onClick={setAction} className="w-full text-start">
<p className="whitespace-normal">{activityDescription(activity)}</p>
</button>
</td>
<td className={tdSecondLine}>
<BudgetChange budget={changes?.buy?.budget} token={quote} />
</td>
<td className={tdSecondLine}>
<BudgetChange budget={changes?.sell?.budget} token={base} />
</td>
<td className={tdSecondLine}>
<p className="flex justify-end gap-8 align-bottom">
{shortenString(activity.txHash)}
<TransactionLink txHash={activity.txHash} className="h-14" />
</p>
</td>
</tr>
</>
);
};
interface ActivityIdProps {
activity: Activity;
size: number;
}
export const ActivityId: FC<ActivityIdProps> = ({ activity, size }) => {
const { id, base, quote } = activity.strategy;
return (
<Link
to="/strategy/$id"
params={{ id: id }}
className="bg-background-800 inline-flex items-center gap-4 rounded-full px-8 py-4"
>
<span className={`text-${size}`}>{getLowestBits(id)}</span>
<TokensOverlap tokens={[base, quote]} size={size + 2} />
</Link>
);
};
interface ActivityIconProps {
activity: Activity;
size: number;
className?: string;
}
export const ActivityIcon: FC<ActivityIconProps> = (props) => {
const { activity, className, size } = props;
const classes = cn(
'grid place-items-center rounded-full',
iconColor(activity.action),
`size-${size}`,
className
);
return (
<div className={classes}>
<ActionIcon action={activity.action} size={size - 16} />
</div>
);
};
interface TransactionLinkProps {
txHash: string;
className: string;
}
export const TransactionLink: FC<TransactionLinkProps> = (props) => {
const { txHash, className } = props;
return (
<NewTabLink
aria-label="See transaction on block explorer"
to={getExplorerLink('tx', txHash)}
>
<IconLink className={cn('text-white', className)} />
</NewTabLink>
);
};
interface BudgetChangeProps {
budget?: string;
token: Token;
}
export const BudgetChange: FC<BudgetChangeProps> = ({ budget, token }) => {
if (!budget) return '...';
const value = new SafeDecimal(budget);
const text = value.isNegative()
? tokenAmount(budget, token)
: `+${tokenAmount(budget, token)}`;
return <p className={budgetColor(budget)}>{text}</p>;
};
const ActivityPaginator = () => {
const {
limit,
offset,
currentPage,
maxPage,
setLimit,
firstPage,
lastPage,
previousPage,
nextPage,
} = usePagination();
const changeLimit = (e: ChangeEvent<HTMLSelectElement>) => {
setLimit(Number(e.target.value));
};
return (
<tr className="border-background-800 text-14 border-t text-white/80">
<td className="px-24 py-16" colSpan={3}>
<div className="flex items-center gap-8">
<label>Show results</label>
<select
className="border-background-800 bg-background-900 rounded-full border-2 px-12 py-8"
name="limit"
onChange={changeLimit}
value={limit}
>
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
<option value="50">50</option>
</select>
</div>
</td>
<td className="px-24 py-16 text-end" colSpan={3}>
<div role="group" className="flex justify-end gap-8 font-mono">
<button
onClick={firstPage}
disabled={!offset}
aria-label="First page"
className="disabled:opacity-50"
>
First
</button>
<button
onClick={previousPage}
disabled={!offset}
aria-label="Previous page"
className="p-8 disabled:opacity-50"
>
<IconChevronLeft className="h-12" />
</button>
<p
className="border-background-800 flex gap-8 rounded-full border-2 px-12 py-8"
aria-label="page position"
>
<span className="text-white">{currentPage}</span>
<span role="separator">/</span>
<span className="text-white">{maxPage}</span>
</p>
<button
onClick={nextPage}
disabled={currentPage === maxPage}
aria-label="Next page"
className="p-8 disabled:opacity-50"
>
<IconChevronLeft className="h-12 rotate-180" />
</button>
<button
onClick={lastPage}
disabled={currentPage === maxPage}
aria-label="Last page"
className="disabled:opacity-50"
>
Last
</button>
</div>
</td>
</tr>
);
};
interface ActionIconProps {
action: ActivityAction;
size: string | number;
}
const iconColor = (action: ActivityAction) => {
if (action === 'buy') return `bg-buy/10 text-buy`;
if (action === 'sell') return `bg-sell/10 text-sell`;
if (action === 'create') return `bg-success/10 text-success`;
if (action === 'delete') return `bg-error/10 text-error`;
return `bg-white/10 text-white`;
};
const ActionIcon: FC<ActionIconProps> = ({ action, size }) => {
const className = `size-${size}`;
if (action === 'create') return <IconCheck className={className} />;
if (action === 'transfer') return <IconTransfer className={className} />;
if (action === 'edit') return <IconEdit className={className} />;
if (action === 'delete') return <IconDelete className={className} />;
if (action === 'pause') return <IconPause className={className} />;
if (action === 'deposit') return <IconDeposit className={className} />;
if (action === 'withdraw') return <IconWithdraw className={className} />;
if (action === 'buy')
return <IconArrowDown className={cn('rotate-[-60deg]', className)} />;
return <IconArrowDown className={cn('rotate-[-120deg]', className)} />;
};