forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

chart-progress-ring.tsx124 lines, 4.8 KiBHistoryRawDownload
1import { For, Show, createEffect, createMemo, createSignal, type Component } from "solid-js";
2import { cn } from "@/lib/cn";
3import { EmptyInline } from "@/components/ui/empty-inline";
4import { TablePagination } from "@/components/ui/table-pagination";
5import { formatNumber } from "@/lib/format";
6import { usePreferences, useT } from "@/stores/preferences-context";
7
8export type ProgressRingSegment = {
9 id: string;
10 label: string;
11 value: number;
12 colorClass: string;
13};
14
15export type ChartProgressRingProps = {
16 title?: string;
17 subtitle?: string;
18 valueText?: string;
19 subtext?: string;
20 segments: ProgressRingSegment[];
21 total?: number;
22 class?: string;
23 /** When set and segments.length exceeds it, the legend is paged with Prev/Next controls. */
24 itemsPerPage?: number;
25};
26
27export const ChartProgressRing: Component<ChartProgressRingProps> = (props) => {
28 const t = useT();
29 const { locale } = usePreferences();
30 const num = (value: number) => formatNumber(value, locale());
31 const [pageIndex, setPageIndex] = createSignal(0);
32 // The gauge bar above always renders every segment's share of the total —
33 // only the legend cards below it are paged.
34 const calculatedTotal = () => props.total ?? props.segments.reduce((acc, s) => acc + s.value, 0);
35
36 const pageCount = createMemo(() =>
37 props.itemsPerPage ? Math.max(1, Math.ceil(props.segments.length / props.itemsPerPage)) : 1,
38 );
39 createEffect(() => {
40 if (pageIndex() >= pageCount()) setPageIndex(Math.max(0, pageCount() - 1));
41 });
42 const visibleSegments = createMemo(() => {
43 if (!props.itemsPerPage) return props.segments;
44 const start = pageIndex() * props.itemsPerPage;
45 return props.segments.slice(start, start + props.itemsPerPage);
46 });
47
48 return (
49 <div class={cn("flex flex-col gap-4 rounded-xl border border-border-line bg-surface-base p-4", props.class)}>
50 <Show when={props.title}>
51 <div class="border-b border-border-hairline pb-3">
52 <div>
53 <h3 class="text-sm font-semibold tracking-tight text-foreground">{props.title}</h3>
54 <Show when={props.subtitle}>
55 <p class="text-xs text-muted-foreground mt-0.5">{props.subtitle}</p>
56 </Show>
57 </div>
58 </div>
59 </Show>
60
61 <Show
62 when={calculatedTotal() > 0}
63 fallback={
64 <EmptyInline illustration="charts" title={t("dashboard.chartEmpty")} hint={t("dashboard.chartEmptyHint")} />
65 }
66 >
67 <div class="flex flex-col justify-between gap-4 py-1">
68 {/* Total Stat Highlight */}
69 <div class="flex items-baseline gap-2">
70 <span class="font-mono text-3xl font-bold tracking-tight text-foreground">
71 {props.valueText ?? num(calculatedTotal())}
72 </span>
73 <Show when={props.subtext}>
74 <span class="text-xs font-medium text-muted-foreground">{props.subtext}</span>
75 </Show>
76 </div>
77
78 {/* Multi-Segmented Progress Gauge Bar */}
79 <div class="flex h-3.5 w-full overflow-hidden rounded-full border border-border-hairline bg-surface-fill p-0.5">
80 <For each={props.segments}>
81 {(segment) => {
82 const pct = () => (calculatedTotal() > 0 ? (segment.value / calculatedTotal()) * 100 : 0);
83 return (
84 <Show when={pct() > 0}>
85 <div
86 class={cn("h-full rounded-full transition-all duration-500 ease-out", segment.colorClass)}
87 style={{ width: `${pct()}%` }}
88 title={`${segment.label}: ${num(segment.value)} (${Math.round(pct())}%)`}
89 />
90 </Show>
91 );
92 }}
93 </For>
94 </div>
95
96 {/* Legend Grid */}
97 <div class="grid grid-cols-2 gap-2 pt-1 sm:grid-cols-2">
98 <For each={visibleSegments()}>
99 {(segment) => (
100 <div class="flex items-center justify-between rounded-lg border border-border-hairline bg-surface-overlay px-3 py-2 text-xs">
101 <div class="flex items-center gap-2 truncate">
102 <span class={cn("h-2.5 w-2.5 shrink-0 rounded-full", segment.colorClass)} />
103 <span class="truncate font-medium text-muted-foreground">{segment.label}</span>
104 </div>
105 <span class="font-mono font-semibold text-foreground ml-2">{num(segment.value)}</span>
106 </div>
107 )}
108 </For>
109 </div>
110
111 <Show when={props.itemsPerPage && pageCount() > 1}>
112 <TablePagination
113 pageIndex={pageIndex()}
114 pageCount={pageCount()}
115 pageSize={props.itemsPerPage!}
116 total={props.segments.length}
117 onPageChange={setPageIndex}
118 />
119 </Show>
120 </div>
121 </Show>
122 </div>
123 );
124};