|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -import { itemVariants, listVariants } from '@/constants/animation'; |
4 | | -import { motion } from 'framer-motion'; |
5 | | -import { useEffect, useState, useMemo, useRef, useCallback } from 'react'; |
6 | | -import BigNumber from 'bignumber.js'; |
7 | | -import AnimatedNumber from '@/components/shared/animated-number'; |
8 | | -import AssetList from './_components/asset-list'; |
9 | | -import { delay, getMoneymarketTokens, getUniqueTokenSymbols, calculateAPY, formatBalance } from '@/lib/utils'; |
10 | | -import DepositOverview from './_components/deposit-overview'; |
11 | | -import TotalSaveAssets from './_components/total-save-assets'; |
12 | | -import { useSaveActions, useSaveState } from './_stores/save-store-provider'; |
13 | | -import { useReservesUsdFormat } from '@sodax/dapp-kit'; |
14 | | -import { useTokenSupplyBalances } from '@/hooks/useTokenSupplyBalances'; |
15 | | -import { useAllTokenPrices } from '@/hooks/useAllTokenPrices'; |
16 | | -import type { XToken } from '@sodax/types'; |
17 | | -import type { CarouselApi } from '@/components/ui/carousel'; |
18 | | -import CurrencySearchPanel from './_components/currency-search-panel'; |
19 | | -import type { DepositItemData, NetworkBalance } from '@/constants/save'; |
20 | | - |
21 | | -export default function SavingsPage() { |
22 | | - const [isOpen, setIsOpen] = useState(false); |
23 | | - const { setDepositValue, setTotalDepositedUsdValue, reset } = useSaveActions(); |
24 | | - const { activeAsset, isSwitchingChain } = useSaveState(); |
25 | | - const carouselApiRef = useRef<CarouselApi | undefined>(undefined); |
26 | | - const [searchQuery, setSearchQuery] = useState(''); |
27 | | - const { data: formattedReserves } = useReservesUsdFormat(); |
28 | | - const allTokens = useMemo(() => getMoneymarketTokens(), []); |
29 | | - const allAssets = useMemo(() => getUniqueTokenSymbols(allTokens), [allTokens]); |
30 | | - const originalTokensWithSupplyBalances = useTokenSupplyBalances(allTokens, formattedReserves || []); |
31 | | - const [selectedChain, setSelectedChain] = useState<string | null>(null); |
32 | | - const cachedTokensWithSupplyBalancesRef = useRef<typeof originalTokensWithSupplyBalances>( |
33 | | - originalTokensWithSupplyBalances, |
34 | | - ); |
35 | | - |
36 | | - const tokensWithSupplyBalances = useMemo(() => { |
37 | | - if (isSwitchingChain) { |
38 | | - return cachedTokensWithSupplyBalancesRef.current; |
39 | | - } |
40 | | - cachedTokensWithSupplyBalancesRef.current = originalTokensWithSupplyBalances; |
41 | | - return originalTokensWithSupplyBalances; |
42 | | - }, [originalTokensWithSupplyBalances, isSwitchingChain]); |
43 | | - |
44 | | - const { data: tokenPrices } = useAllTokenPrices(allTokens); |
45 | | - |
46 | | - const highestAPY = useMemo((): number => { |
47 | | - if (!formattedReserves || allTokens.length === 0) { |
48 | | - return 0; |
49 | | - } |
50 | | - |
51 | | - let maxAPY = 0; |
52 | | - |
53 | | - allTokens.forEach(token => { |
54 | | - const apyString = calculateAPY(formattedReserves, token); |
55 | | - if (apyString !== '-') { |
56 | | - const apyValue = Number.parseFloat(apyString.replace('%', '')); |
57 | | - if (!Number.isNaN(apyValue) && apyValue > maxAPY) { |
58 | | - maxAPY = apyValue; |
59 | | - } |
60 | | - } |
61 | | - }); |
62 | | - |
63 | | - return maxAPY; |
64 | | - }, [allTokens, formattedReserves]); |
65 | | - |
66 | | - const suppliedAssets = useMemo((): DepositItemData[] => { |
67 | | - const items: DepositItemData[] = []; |
68 | | - |
69 | | - allAssets.forEach(asset => { |
70 | | - const tokensWithBalance = tokensWithSupplyBalances.filter( |
71 | | - token => token.symbol === asset.symbol && Number(token.supplyBalance) > 0, |
72 | | - ); |
73 | | - |
74 | | - if (tokensWithBalance.length === 0) { |
75 | | - return; |
76 | | - } |
77 | | - |
78 | | - const totalBalance = tokensWithBalance.reduce((sum, token) => { |
79 | | - return sum + Number(token.supplyBalance || '0'); |
80 | | - }, 0); |
81 | | - |
82 | | - const networksWithFunds: NetworkBalance[] = tokensWithBalance |
83 | | - .map(token => ({ |
84 | | - networkId: token.xChainId, |
85 | | - balance: token.supplyBalance || '0', |
86 | | - token, |
87 | | - })) |
88 | | - .filter(network => network.networkId); |
89 | | - |
90 | | - const firstToken = tokensWithBalance[0]; |
91 | | - if (!firstToken) { |
92 | | - return; |
93 | | - } |
94 | | - |
95 | | - let fiatValue = '$0.00'; |
96 | | - if (tokenPrices && Number(totalBalance) > 0) { |
97 | | - const priceKey = `${firstToken.symbol}-${firstToken.xChainId}`; |
98 | | - const tokenPrice = tokenPrices[priceKey] || 0; |
99 | | - const usdValue = new BigNumber(totalBalance).multipliedBy(tokenPrice).toString(); |
100 | | - fiatValue = `$${formatBalance(usdValue, tokenPrice)}`; |
101 | | - } |
102 | | - |
103 | | - const apy = calculateAPY(formattedReserves, firstToken); |
104 | | - |
105 | | - items.push({ |
106 | | - asset: firstToken, |
107 | | - totalBalance: totalBalance.toFixed(6), |
108 | | - fiatValue, |
109 | | - networksWithFunds, |
110 | | - apy, |
111 | | - }); |
112 | | - }); |
113 | | - |
114 | | - return items; |
115 | | - }, [allAssets, tokensWithSupplyBalances, tokenPrices, formattedReserves]); |
116 | | - |
117 | | - const totalUsdValue = useMemo((): string => { |
118 | | - if (suppliedAssets.length === 0) { |
119 | | - return '$0.00'; |
120 | | - } |
121 | | - |
122 | | - let total = new BigNumber(0); |
123 | | - |
124 | | - suppliedAssets.forEach(item => { |
125 | | - const numericValue = item.fiatValue.replace(/[$,]/g, ''); |
126 | | - const value = Number(numericValue); |
127 | | - if (!Number.isNaN(value) && value > 0) { |
128 | | - total = total.plus(value); |
129 | | - } |
130 | | - }); |
131 | | - setTotalDepositedUsdValue(Number(total.toString())); |
132 | | - return formatBalance(total.toString(), 0); |
133 | | - }, [suppliedAssets, setTotalDepositedUsdValue]); |
134 | | - |
135 | | - const hasDeposits = suppliedAssets.length > 0; |
136 | | - |
137 | | - useEffect(() => { |
138 | | - delay(500).then(() => { |
139 | | - setIsOpen(true); |
140 | | - }); |
141 | | - }, []); |
142 | | - |
143 | | - useEffect(() => { |
144 | | - if (activeAsset !== '') { |
145 | | - setDepositValue(0); |
146 | | - } |
147 | | - }, [activeAsset, setDepositValue]); |
148 | | - |
149 | | - useEffect(() => { |
150 | | - return () => { |
151 | | - reset(); |
152 | | - }; |
153 | | - }, [reset]); |
154 | | - |
155 | | - const navigateToAsset = useCallback( |
156 | | - (asset: XToken): void => { |
157 | | - if (!carouselApiRef.current) { |
158 | | - return; |
159 | | - } |
160 | | - |
161 | | - const assetIndex = suppliedAssets.findIndex(item => item.asset.symbol === asset.symbol); |
162 | | - if (assetIndex !== -1) { |
163 | | - carouselApiRef.current.scrollTo(assetIndex); |
164 | | - } |
165 | | - }, |
166 | | - [suppliedAssets], |
167 | | - ); |
168 | | - |
169 | | - const handleCarouselApiReady = useCallback((api: CarouselApi | undefined): void => { |
170 | | - carouselApiRef.current = api; |
171 | | - }, []); |
172 | | - |
| 3 | +export default function SavePage() { |
173 | 4 | return ( |
174 | | - <motion.div |
175 | | - className="w-full flex flex-col gap-(--layout-space-comfortable)" |
176 | | - variants={listVariants} |
177 | | - initial={false} |
178 | | - animate={isOpen ? 'open' : 'closed'} |
179 | | - > |
180 | | - {hasDeposits ? ( |
181 | | - <motion.div className="w-full flex flex-col gap-4" variants={itemVariants}> |
182 | | - <DepositOverview |
183 | | - suppliedAssets={suppliedAssets} |
184 | | - tokenPrices={tokenPrices} |
185 | | - onApiReady={handleCarouselApiReady} |
186 | | - /> |
187 | | - <TotalSaveAssets |
188 | | - suppliedAssets={suppliedAssets} |
189 | | - onAssetClick={navigateToAsset} |
190 | | - totalUsdValue={totalUsdValue} |
191 | | - /> |
192 | | - </motion.div> |
193 | | - ) : ( |
194 | | - <motion.div className="inline-flex flex-col justify-start items-start gap-4" variants={itemVariants}> |
195 | | - <div className="self-stretch mix-blend-multiply justify-end"> |
196 | | - <div className="text-yellow-dark text-(length:--app-title) font-bold font-['InterRegular'] leading-9"> |
197 | | - Deposit and earn{' '} |
198 | | - </div> |
199 | | - <div className="text-yellow-dark text-(length:--app-title) font-normal font-['Shrikhand'] leading-9"> |
200 | | - instantly |
201 | | - </div> |
202 | | - </div> |
203 | | - <div className="mix-blend-multiply justify-start text-clay-light font-normal font-['InterRegular'] leading-snug !text-(length:--subtitle) flex"> |
204 | | - Up to |
205 | | - <AnimatedNumber |
206 | | - to={highestAPY} |
207 | | - decimalPlaces={2} |
208 | | - className="text-clay-light font-normal font-['InterRegular'] leading-snug !text-(length:--subtitle) min-w-6 ml-1" |
209 | | - /> |
210 | | - % with no lockups. |
211 | | - </div> |
212 | | - </motion.div> |
213 | | - )} |
214 | | - |
215 | | - <motion.div className="w-full flex-grow-1" variants={itemVariants}> |
216 | | - <CurrencySearchPanel |
217 | | - searchQuery={searchQuery} |
218 | | - onSearchChange={setSearchQuery} |
219 | | - selectedChain={selectedChain} |
220 | | - setSelectedChain={setSelectedChain} |
221 | | - /> |
222 | | - </motion.div> |
223 | | - |
224 | | - <motion.div className="w-full flex-grow-1 relative" variants={itemVariants}> |
225 | | - <AssetList searchQuery={searchQuery} selectedChain={selectedChain} /> |
226 | | - </motion.div> |
227 | | - </motion.div> |
| 5 | + <div className="inline-flex flex-col justify-start items-start gap-4"> |
| 6 | + <div>SavePage</div> |
| 7 | + </div> |
228 | 8 | ); |
229 | 9 | } |
0 commit comments