-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an easier to read
NominalPriceDisplay
re-usable component to vi…
…sually mute non-significant figures Fix imports everywhere it was used
- Loading branch information
Showing
6 changed files
with
57 additions
and
23 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
41 changes: 41 additions & 0 deletions
41
src/typescript/frontend/src/components/misc/NominalPriceDisplay.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,41 @@ | ||
import { type AnyNumber } from "@sdk/emojicoin_dot_fun/types"; | ||
import { toNominalPrice } from "@sdk/utils"; | ||
import { cn } from "lib/utils/class-name"; | ||
import React, { type HTMLAttributes } from "react"; | ||
|
||
type PriceColors = "neutral" | "buy" | "sell"; | ||
export const PriceColors: { [key in PriceColors]: string } = { | ||
neutral: "text-lighter-gray", | ||
buy: "text-green", | ||
sell: "text-pink", | ||
}; | ||
|
||
interface NominalPriceDisplayProps extends HTMLAttributes<HTMLSpanElement> { | ||
priceQ64: AnyNumber; | ||
decimals?: number; | ||
colorFor?: PriceColors; | ||
} | ||
|
||
/** | ||
* Splits a Q64 price into two parts, the significant figures, and the insignificant figures. | ||
* | ||
* Visually mutes the insignificant figures and highlights the significant ones. | ||
*/ | ||
export const NominalPriceDisplay = ({ | ||
priceQ64, | ||
decimals = 9, | ||
colorFor = "neutral", | ||
className, | ||
}: NominalPriceDisplayProps) => { | ||
const price = toNominalPrice(priceQ64); | ||
const fixed = price.toFixed(decimals); | ||
const firstSigFigOnwards = fixed.match(/[1-9].*/)?.at(0) ?? ""; | ||
const beforeSigFig = fixed.slice(0, fixed.length - firstSigFigOnwards.length); | ||
const color = PriceColors[colorFor]; | ||
return ( | ||
<> | ||
<span className={cn(color, "brightness-[0.4]", className)}>{beforeSigFig}</span> | ||
<span className={cn(color, "brightness-125", className)}>{firstSigFigOnwards}</span> | ||
</> | ||
); | ||
}; |
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
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