-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAssetSelector.tsx
More file actions
159 lines (144 loc) Β· 4.78 KB
/
AssetSelector.tsx
File metadata and controls
159 lines (144 loc) Β· 4.78 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
import { useState, useEffect, useRef } from "react";
import { motion } from "framer-motion";
import { cn, useWindowWidth } from "@utils";
import { TMarketReserve, MarketSectionTypes } from "@types";
type TProps = {
assets: TMarketReserve[];
activeSection: MarketSectionTypes;
activeAsset: TMarketReserve | undefined;
handleAssetChange: (asset: TMarketReserve) => void;
};
const AssetSelector: React.FC<TProps> = ({
assets,
activeSection,
activeAsset,
handleAssetChange,
}) => {
const carouselRef = useRef<HTMLDivElement>(null);
const [width, setWidth] = useState<number>(0);
const [showAll, setShowAll] = useState<boolean>(false);
const windowWidth = useWindowWidth();
useEffect(() => {
if (carouselRef.current) {
const scrollWidth = carouselRef.current.scrollWidth;
const offsetWidth = carouselRef.current.offsetWidth;
setWidth(scrollWidth - offsetWidth);
}
}, []);
const isHidden =
[MarketSectionTypes.Users, MarketSectionTypes.Liquidations].includes(
activeSection
) ||
(assets.length === 2 &&
windowWidth >= 1024 &&
activeSection === MarketSectionTypes.Information);
return (
<div
className={cn(
"w-full md:w-auto overflow-hidden md:overflow-visible",
isHidden && "opacity-0 pointer-events-none"
)}
>
<motion.div
ref={carouselRef}
className="block md:hidden cursor-grab overflow-hidden"
>
<motion.div
className="flex items-center gap-2"
drag="x"
dragConstraints={{ right: 0, left: -width }}
whileTap={{ cursor: "grabbing" }}
>
{assets.map((asset) => {
if (
!asset?.isBorrowable &&
[MarketSectionTypes.Borrow, MarketSectionTypes.Repay].includes(
activeSection
)
) {
return null;
}
return (
<motion.div
key={asset?.address}
className={cn(
"min-w-[80px] flex-shrink-0 flex items-center gap-2 py-2 px-3 rounded-lg border",
asset?.address === activeAsset?.address
? "bg-[#232429] border-[#35363B]"
: " bg-transparent border-[#232429]"
)}
onClick={() => handleAssetChange(asset)}
>
<img
src={asset?.assetData?.logoURI}
alt={asset?.assetData?.symbol}
className="w-5 h-5 rounded-full pointer-events-none select-none"
/>
<span
className={cn(
"text-[14px] leading-5 font-medium pointer-events-none select-none",
asset?.address === activeAsset?.address
? "text-white"
: "text-[#7C7E81]"
)}
>
{asset?.assetData?.symbol}
</span>
</motion.div>
);
})}
</motion.div>
</motion.div>
<div className="hidden md:flex items-center gap-2 flex-wrap">
{assets.map((asset, index) => {
if (
!asset?.isBorrowable &&
[MarketSectionTypes.Borrow, MarketSectionTypes.Repay].includes(
activeSection
)
) {
return null;
}
return (
<div
key={asset?.address}
className={cn(
"flex items-center gap-2 py-2 px-3 rounded-lg border cursor-pointer",
asset?.address === activeAsset?.address
? "bg-[#232429] border-[#35363B]"
: " bg-transparent border-[#232429]",
index > 9 && !showAll && "hidden"
)}
onClick={() => handleAssetChange(asset)}
>
<img
src={asset?.assetData?.logoURI}
alt={asset?.assetData?.symbol}
className="w-5 h-5 rounded-full pointer-events-none select-none"
/>
<span
className={cn(
"text-[14px] leading-5 font-medium pointer-events-none select-none",
asset?.address === activeAsset?.address
? "text-white"
: "text-[#7C7E81]"
)}
>
{asset?.assetData?.symbol}
</span>
</div>
);
})}
{assets?.length > 10 ? (
<p
className="font-medium text-[14px] text-[#9180F4] cursor-pointer"
onClick={() => setShowAll((prev) => !prev)}
>
{showAll ? "Hide" : `Show all ${assets?.length}`}
</p>
) : null}
</div>
</div>
);
};
export { AssetSelector };