The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { formatPercent } from "@t3tools/shared/usageFormat"; |
| 2 | import { useMessages } from "gt-react"; |
| 3 | import type { ReactNode } from "react"; |
| 4 | |
| 5 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 6 | |
| 7 | export interface ShareSegment { |
| 8 | readonly label: string; |
| 9 | readonly value: number; |
| 10 | readonly color: string; |
| 11 | } |
| 12 | |
| 13 | export function UsageShareBar({ |
| 14 | label, |
| 15 | segments, |
| 16 | format, |
| 17 | aside, |
| 18 | }: { |
| 19 | readonly label: string; |
| 20 | readonly segments: readonly ShareSegment[]; |
| 21 | readonly format: (value: number) => string; |
| 22 | readonly aside?: ReactNode; |
| 23 | }) { |
| 24 | const m = useMessages(); |
| 25 | const visible = segments.filter((segment) => segment.value > 0); |
| 26 | const total = visible.reduce((sum, segment) => sum + segment.value, 0); |
| 27 | if (total <= 0) return null; |
| 28 | |
| 29 | return ( |
| 30 | <div className="flex min-w-0 flex-col gap-2.5"> |
| 31 | <div className="flex items-baseline justify-between gap-3"> |
| 32 | <h3 className="text-sm font-medium text-foreground">{label}</h3> |
| 33 | {aside} |
| 34 | </div> |
| 35 | {/* The legend below carries every figure; the bar's tooltips are pointer-only. */} |
| 36 | <div aria-hidden className="flex h-2 gap-0.5"> |
| 37 | {visible.map((segment) => ( |
| 38 | <Tooltip key={segment.label}> |
| 39 | <TooltipTrigger |
| 40 | render={ |
| 41 | <div |
| 42 | className="h-full min-w-1 rounded-xs first:rounded-l-full last:rounded-r-full" |
| 43 | style={{ flex: `${segment.value} 1 0`, backgroundColor: segment.color }} |
| 44 | /> |
| 45 | } |
| 46 | /> |
| 47 | <TooltipPopup> |
| 48 | {m(segment.label)} · {format(segment.value)} · {formatPercent(segment.value / total)} |
| 49 | </TooltipPopup> |
| 50 | </Tooltip> |
| 51 | ))} |
| 52 | </div> |
| 53 | <ul aria-label={label} className="flex flex-wrap gap-x-4 gap-y-1 text-xs"> |
| 54 | {visible.map((segment) => ( |
| 55 | <li key={segment.label} className="flex items-center gap-1.5"> |
| 56 | <span |
| 57 | aria-hidden |
| 58 | className="size-2 rounded-xs" |
| 59 | style={{ backgroundColor: segment.color }} |
| 60 | /> |
| 61 | <span className="text-muted-foreground">{m(segment.label)}</span>{" "} |
| 62 | <span className="text-foreground tabular-nums">{format(segment.value)}</span> |
| 63 | <span className="sr-only">, {formatPercent(segment.value / total)}</span> |
| 64 | </li> |
| 65 | ))} |
| 66 | </ul> |
| 67 | </div> |
| 68 | ); |
| 69 | } |