-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(earn): Market summary component (#61)
- Loading branch information
Showing
13 changed files
with
212 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { cn } from "@repo/ui/lib"; | ||
|
||
export const Card = ({ | ||
className, | ||
...props | ||
}: React.HTMLAttributes<HTMLDivElement>) => { | ||
return ( | ||
<div className={cn("rounded-lg bg-semi-white", className)} {...props} /> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Card } from "./Card"; |
49 changes: 49 additions & 0 deletions
49
apps/flame-defi/app/earn/components/MarketSummary/MarketSummary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useEffect, useState } from "react"; | ||
import { MarketSummaryCard } from "./MarketSummaryCard"; | ||
|
||
// TODO: Use fetched values. | ||
const VALUE_DEPOSIT = 1000000; | ||
const VALUE_BORROW = 75000; | ||
|
||
export const MarketSummary = () => { | ||
const [countDeposit, setCountDeposit] = useState<number | null>(null); | ||
const [countBorrow, setCountBorrow] = useState<number | null>(null); | ||
|
||
const [isAnimating, setIsAnimating] = useState(false); | ||
|
||
useEffect(() => { | ||
// Prevent counter animation when number is too low. | ||
// May need to adjust based on the expected maximum value. | ||
const multiplier = 0.1; | ||
const countDepositMinimum = VALUE_DEPOSIT * multiplier; | ||
const countBorrowMinimum = VALUE_BORROW * multiplier; | ||
|
||
if ( | ||
countDeposit !== null && | ||
countBorrow !== null && | ||
countDeposit > countDepositMinimum && | ||
countBorrow > countBorrowMinimum | ||
) { | ||
setIsAnimating(true); | ||
} | ||
}, [countDeposit, countBorrow]); | ||
|
||
return ( | ||
<div className="flex flex-col gap-2 md:flex-row"> | ||
<MarketSummaryCard | ||
label="Total Deposits" | ||
value={VALUE_DEPOSIT} | ||
count={countDeposit} | ||
setCount={setCountDeposit} | ||
isAnimating={isAnimating} | ||
/> | ||
<MarketSummaryCard | ||
label="Total Borrow" | ||
value={VALUE_BORROW} | ||
count={countBorrow} | ||
setCount={setCountBorrow} | ||
isAnimating={isAnimating} | ||
/> | ||
</div> | ||
); | ||
}; |
93 changes: 93 additions & 0 deletions
93
apps/flame-defi/app/earn/components/MarketSummary/MarketSummaryCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Skeleton } from "@repo/ui/shadcn-primitives"; | ||
import Big from "big.js"; | ||
import React, { useEffect } from "react"; | ||
import { FormattedNumber } from "react-intl"; | ||
import { Card } from "../Card"; | ||
|
||
interface MarketSummaryCardProps { | ||
label: React.ReactNode; | ||
value: number; | ||
count: number | null; | ||
setCount: (value: number) => void; | ||
/** | ||
* Sync animation state between multiple cards. | ||
*/ | ||
isAnimating: boolean; | ||
} | ||
|
||
export const MarketSummaryCard = ({ | ||
label, | ||
value, | ||
count, | ||
setCount, | ||
isAnimating, | ||
}: MarketSummaryCardProps) => { | ||
useEffect(() => { | ||
let isMounted = true; | ||
|
||
const counter = (minimum: number, maximum: number) => { | ||
let i = minimum; | ||
|
||
const updateCount = () => { | ||
if (isMounted && i <= maximum) { | ||
setCount(i); | ||
|
||
// Increment counter based on proximity to maximum so multiple cards have synced animations. | ||
const range = maximum - minimum; | ||
const progress = (i - minimum) / range; | ||
const step = Math.max(1, (Math.log10(1 + 9 * progress) * range) / 10); | ||
|
||
i += Math.ceil(step); | ||
|
||
setTimeout(updateCount, 10); | ||
} | ||
|
||
if (isMounted && i > maximum) { | ||
setCount(maximum); | ||
} | ||
}; | ||
|
||
updateCount(); | ||
|
||
return () => { | ||
isMounted = false; | ||
}; | ||
}; | ||
|
||
counter(0, value); | ||
|
||
return () => { | ||
isMounted = false; | ||
}; | ||
}, [value, setCount]); | ||
|
||
return ( | ||
<Card className="flex flex-col rounded-xl p-5 space-y-1"> | ||
<span className="text-xs/3 text-grey-light">{label}</span> | ||
|
||
<div className="relative"> | ||
{/* Reserve space for animation to prevent card size increasing with counter value. */} | ||
<span className="text-3xl/8 font-dot opacity-0"> | ||
<FormattedNumber | ||
value={+new Big(value).toFixed()} | ||
style="currency" | ||
currency="USD" | ||
maximumFractionDigits={0} | ||
/> | ||
</span> | ||
<span className="text-3xl/8 font-dot w-full absolute top-0 left-0"> | ||
<Skeleton isLoading={!count || !isAnimating}> | ||
<span> | ||
<FormattedNumber | ||
value={+new Big(count ?? 0).toFixed()} | ||
style="currency" | ||
currency="USD" | ||
maximumFractionDigits={0} | ||
/> | ||
</span> | ||
</Skeleton> | ||
</span> | ||
</div> | ||
</Card> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { MarketSummary } from "./MarketSummary"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export { Table } from "./Table"; | ||
export { TableCard } from "./TableCard"; | ||
export { TablePagination } from "./TablePagination"; | ||
export { TableSearch } from "./TableSearch"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 15 additions & 13 deletions
28
apps/flame-defi/app/earn/sections/TableSection/TableSection.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters