-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathColumnSort.tsx
More file actions
107 lines (95 loc) · 2.93 KB
/
ColumnSort.tsx
File metadata and controls
107 lines (95 loc) · 2.93 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
import { cn } from "@utils";
import type { TTableColumn } from "@types";
type TProps = {
index: number;
value: string;
table: TTableColumn[];
sort: (table: TTableColumn[]) => void;
};
const ColumnSort: React.FC<TProps> = ({ index, value, table, sort }) => {
const styles: Record<string, string> = {
Market:
"sticky left-0 z-10 lg:relative w-[100px] md:w-[20%] bg-[#151618] lg:bg-transparent",
Asset: "w-[100px] md:w-[15%]",
"Supply APR": "w-[100px] md:w-[13%] justify-end",
"Borrow APR": "w-[100px] md:w-[13%] justify-end",
"Supply TVL": "w-[100px] md:w-[13%] justify-end",
Utilization: "w-[150px] md:w-[13%] text-center justify-end",
"maxLTV / LT": "w-[150px] md:w-[13%] justify-end",
};
const tabController = () => {
if (table[index].unsortable) return;
const newUrl = new URL(window.location.href);
const params = new URLSearchParams(newUrl.search);
let nextCase: string = "";
switch (table[index].sortType) {
case "none":
nextCase = "descendentic";
break;
case "ascendentic":
nextCase = "descendentic";
break;
case "descendentic":
nextCase = "ascendentic";
break;
default:
break;
}
const updatedTable: TTableColumn[] = table.map(
(column: TTableColumn, i: number) => {
if (index === i) {
const URLSortCase = nextCase === "descendentic" ? "desc" : "asc";
const sortParam = `${column.name.toLowerCase()}-${URLSortCase}`;
if (sortParam === "tvl-desc") {
params.delete("sort");
} else {
params.set("sort", sortParam);
}
newUrl.search = `?${params.toString()}`;
window.history.pushState({}, "", newUrl.toString());
return { ...column, sortType: nextCase };
} else {
return { ...column, sortType: "none" };
}
}
);
sort(updatedTable);
};
return (
<div
onClick={tabController}
className={cn(
"whitespace-nowrap flex items-center text-[12px] font-manrope font-semibold px-2 md:px-4 py-2",
!table[index].unsortable && "cursor-pointer",
styles[value] || "text-center"
)}
data-testid="sort"
>
<p
className={cn(
table[index].sortType !== "none" ? "text-white" : "text-[#97979A]"
)}
>
{value}
</p>
{!table[index].unsortable && (
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
className="flex-shrink-0"
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M5.8335 5.8995V7.00048H10.1668V5.8995L8.00016 3.56543L5.8335 5.8995ZM10.1668 10.1013V9.00032H5.8335V10.1013L8.00016 12.4354L10.1668 10.1013Z"
fill="#97979A"
/>
</svg>
)}
</div>
);
};
export { ColumnSort };