import Decimal from "decimal.js"; import { PriceMapSchema } from "@/src/features/models/validation"; import { getMaxDecimals } from "@/src/features/models/utils"; export function PricePreview({ prices, }: { prices: Record; }) { const parsedPrices = PriceMapSchema.safeParse(prices); const getMaxDecimalsForPriceGroup = ( price: number | undefined, multiplier: number, ) => { return price != null ? Math.max( ...Object.values(prices).map((price) => { return getMaxDecimals(price, multiplier); }), ) : 0; }; return (

Price Preview

{parsedPrices.success ? (
Usage Type per unit per 1K per 1M
{Object.entries(parsedPrices.data) .filter((entry): entry is [string, number] => Boolean(entry[1])) .map(([usageType, price]) => (
{usageType} $ {new Decimal(price).toFixed( getMaxDecimalsForPriceGroup(price, 1), )} $ {new Decimal(price) .mul(1000) .toFixed(getMaxDecimalsForPriceGroup(price, 1000))} $ {new Decimal(price) .mul(1000000) .toFixed(getMaxDecimalsForPriceGroup(price, 1000000))}
))}
) : (
Invalid price entries. Please check your input format.
)}
); }