Open History to see the last commit
1import { useGT } from "gt-react";
2import { memo } from "react";
3import { cn } from "~/lib/utils";
4
5export function formatCompactDiffCount(value: number): string {
6 if (value < 1000) return String(value);
7 if (value < 999_500) {
8 const k = value / 1000;
9 return `${k < 10 ? k.toFixed(1).replace(/\.0$/, "") : Math.round(k)}k`;
10 }
11 if (value < 999_500_000) {
12 const m = value / 1_000_000;
13 return `${m < 10 ? m.toFixed(1).replace(/\.0$/, "") : Math.round(m)}m`;
14 }
15 const b = value / 1_000_000_000;
16 return `${b < 10 ? b.toFixed(1).replace(/\.0$/, "") : Math.round(b)}b`;
17}
18
19export const DiffStatLabel = memo(function DiffStatLabel(props: {
20 additions: number;
21 deletions: number;
22 className?: string;
23 layout?: "aligned" | "inline";
24}) {
25 const { additions, deletions, className, layout = "aligned" } = props;
26 const gt = useGT();
27 return (
28 <span
29 role="group"
30 aria-label={gt("{additions} additions, {deletions} deletions", { additions, deletions })}
31 className={cn(
32 layout === "inline"
33 ? "inline-flex items-center gap-1 tabular-nums align-middle"
34 : "inline-grid grid-cols-[4ch_4ch] gap-2 text-right tabular-nums align-middle",
35 className,
36 )}
37 >
38 <span aria-hidden="true" className="font-mono text-diff-addition-foreground">
39 +{formatCompactDiffCount(additions)}
40 </span>
41 <span aria-hidden="true" className="font-mono text-diff-deletion-foreground">
42 -{formatCompactDiffCount(deletions)}
43 </span>
44 </span>
45 );
46});