forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

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