-
Notifications
You must be signed in to change notification settings - Fork 10
/
ActivityFilter.tsx
161 lines (155 loc) · 4.8 KB
/
ActivityFilter.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
import { Activity } from 'libs/queries/extApi/activity';
import { Token } from 'libs/tokens';
import { activityActionName } from './utils';
import { toPairSlug } from 'utils/pairSearch';
import { FC, useId } from 'react';
import { Combobox, Option } from 'components/common/combobox';
import { cn, getLowestBits } from 'utils/helpers';
import { ReactComponent as IconSearch } from 'assets/icons/search.svg';
import { ReactComponent as IconPair } from 'assets/icons/token-pair.svg';
import { TokensOverlap } from 'components/common/tokensOverlap';
import {
DateRangePicker,
datePickerPresets,
} from 'components/common/datePicker/DateRangePicker';
import { useActivity } from './ActivityProvider';
interface DisplayID {
id: string;
base: Token;
quote: Token;
}
interface DisplayPair {
pair: string;
base: Token;
quote: Token;
}
const getAllIds = (activities: Activity[]) => {
const map = new Map<string, DisplayID>();
for (const activity of activities) {
const { base, quote } = activity.strategy;
const id = getLowestBits(activity.strategy.id);
map.set(id, { id, base, quote });
}
return Array.from(map.values());
};
const getAllPairs = (activities: Activity[]) => {
const map = new Map<string, DisplayPair>();
for (const activity of activities) {
const { base, quote } = activity.strategy;
const pair = toPairSlug(base, quote);
map.set(pair, { pair, base, quote });
}
return Array.from(map.values());
};
export interface ActivityFilterProps {
filters?: ('ids' | 'pairs')[];
className?: string;
}
export const ActivityFilter: FC<ActivityFilterProps> = (props) => {
const { filters = [], className } = props;
const formId = useId();
const { all: activities, searchParams, setSearchParams } = useActivity();
const allIds = getAllIds(activities);
const allPairs = getAllPairs(activities);
const allActions = Array.from(
new Set(activities.map((a) => a.action))
).sort();
const { pairs, ids, actions, start, end } = searchParams;
const updateParams = () => {
const selector = `input[form="${formId}"]`;
const inputs = document.querySelectorAll<HTMLInputElement>(selector);
const params: Record<string, string[] | string> = {};
for (const input of inputs) {
const name = input.name;
if (input.type === 'checkbox') {
params[name] ||= [];
if (input.checked) (params[name] as string[]).push(input.value);
} else {
params[name] = input.value;
}
}
setSearchParams(params);
};
return (
<form
id={formId}
className={cn(
'flex flex-wrap items-center justify-start gap-8 md:justify-end md:gap-16',
className
)}
role="search"
onChange={updateParams}
>
{filters.includes('ids') && (
<Combobox
form={formId}
name="ids"
value={ids}
icon={<IconSearch className="text-primary w-14" />}
label={
ids.length
? `${ids.length} Strategies Selected`
: 'Filter Strategies'
}
filterLabel="Search by ID or Symbol"
options={allIds.map(({ id, base, quote }) => (
<Option key={id} value={id}>
<span>{id}</span>
<svg width="4" height="4">
<circle cx="2" cy="2" r="2" fill="white" fillOpacity="0.4" />
</svg>
<span className="text-white/40">
{base.symbol}/{quote.symbol}
</span>
</Option>
))}
/>
)}
{filters.includes('pairs') && (
<Combobox
form={formId}
name="pairs"
value={pairs}
icon={<IconPair className="text-primary w-14" />}
label={
pairs.length ? `${pairs.length} Pairs Selected` : 'Filter Pairs'
}
filterLabel="Search by Pair"
options={allPairs.map(({ pair, base, quote }) => (
<Option key={pair} value={pair}>
<TokensOverlap tokens={[base, quote]} size={14} />
{base.symbol}/{quote.symbol}
</Option>
))}
/>
)}
<Combobox
form={formId}
name="actions"
value={actions}
icon={<IconSearch className="text-primary w-14" />}
label={
actions.length
? `${actions.length} Actions Selected`
: 'Filter Actions'
}
filterLabel="Search by Action"
options={allActions.map((action) => (
<Option key={action} value={action}>
{activityActionName[action]}
</Option>
))}
/>
<DateRangePicker
form={formId}
presets={datePickerPresets}
onConfirm={updateParams}
start={start}
end={end}
options={{
disabled: { after: new Date() },
}}
/>
</form>
);
};