import { cn } from "~/utils/cn"; import { formatCurrency } from "~/utils/numberFormatter"; import { Paragraph } from "../primitives/Paragraph"; import { SimpleTooltip } from "../primitives/Tooltip"; import { motion } from "framer-motion"; type UsageBarProps = { current: number; billingLimit?: number; tierLimit?: number; isPaying: boolean; }; const startFactor = 4; export function UsageBar({ current, billingLimit, tierLimit, isPaying }: UsageBarProps) { const getLargestNumber = Math.max(current, tierLimit ?? -Infinity, billingLimit ?? -Infinity, 5); //creates a maximum range for the progress bar, add 10% to the largest number so the bar doesn't reach the end const maxRange = Math.round(getLargestNumber * 1.1); const tierRunLimitPercentage = tierLimit ? Math.round((tierLimit / maxRange) * 100) : 0; const billingLimitPercentage = billingLimit !== undefined ? Math.round((billingLimit / maxRange) * 100) : 0; const usagePercentage = Math.round((current / maxRange) * 100); //cap the usagePercentage to the freeRunLimitPercentage const usageCappedToLimitPercentage = Math.min(usagePercentage, tierRunLimitPercentage); return (
{billingLimit !== undefined && ( )} tierLimit ? "bg-green-700" : "bg-green-600" )} > {tierLimit !== undefined && ( )}
); } const positions = { topRow1: "bottom-0 h-9", topRow2: "bottom-0 h-14", bottomRow1: "top-0 h-9 items-end", bottomRow2: "top-0 h-14 items-end", }; type LegendProps = { text: string; value: number | string; percentage: number; position: keyof typeof positions; tooltipContent?: string; }; function Legend({ text, value, position, percentage, tooltipContent }: LegendProps) { const flipLegendPositionValue = 80; const flipLegendPosition = percentage > flipLegendPositionValue ? true : false; return (
{tooltipContent ? ( {text} {value} } side="top" content={tooltipContent} className="z-50 h-fit" /> ) : ( {text} {value} )}
); }