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-bar.tsx128 lines, 5.0 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 ChartBarItem = {
9 id: string;
10 label: string;
11 value: number;
12 max?: number;
13 formattedValue?: string;
14 colorClass?: string;
15};
16
17export type ChartBarProps = {
18 title?: string;
19 subtitle?: string;
20 items: ChartBarItem[];
21 maxScale?: number;
22 class?: string;
23 /** When set and items.length exceeds it, the list is paged with Prev/Next controls. */
24 itemsPerPage?: number;
25};
26
27export const ChartBar: Component<ChartBarProps> = (props) => {
28 const [pageIndex, setPageIndex] = createSignal(0);
29 const t = useT();
30 const { locale } = usePreferences();
31 const display = (item: ChartBarItem) => item.formattedValue ?? formatNumber(item.value, locale());
32
33 // The scale is computed off every item, not just the visible page, so bars
34 // don't rescale as the reader pages through them.
35 const maxValue = () => {
36 if (props.maxScale != null && props.maxScale > 0) return props.maxScale;
37 const itemMax = Math.max(...props.items.map((i) => i.value), 1);
38 return itemMax;
39 };
40
41 const pageCount = createMemo(() =>
42 props.itemsPerPage ? Math.max(1, Math.ceil(props.items.length / props.itemsPerPage)) : 1,
43 );
44 // A refetch can shrink the item count out from under the current page.
45 createEffect(() => {
46 if (pageIndex() >= pageCount()) setPageIndex(Math.max(0, pageCount() - 1));
47 });
48 const visibleItems = createMemo(() => {
49 if (!props.itemsPerPage) return props.items;
50 const start = pageIndex() * props.itemsPerPage;
51 return props.items.slice(start, start + props.itemsPerPage);
52 });
53
54 return (
55 <div class={cn("flex flex-col gap-4 rounded-xl border border-border-line bg-surface-base p-4", props.class)}>
56 <Show when={props.title}>
57 <div class="border-b border-border-hairline pb-3">
58 <div>
59 <h3 class="text-sm font-semibold tracking-tight text-foreground">{props.title}</h3>
60 <Show when={props.subtitle}>
61 <p class="text-xs text-muted-foreground mt-0.5">{props.subtitle}</p>
62 </Show>
63 </div>
64 </div>
65 </Show>
66
67 <Show
68 when={props.items.length > 0}
69 fallback={
70 <EmptyInline illustration="charts" title={t("dashboard.chartEmpty")} hint={t("dashboard.chartEmptyHint")} />
71 }
72 >
73 <div class="space-y-3 pt-1">
74 <For each={visibleItems()}>
75 {(item) => {
76 const pct = () => Math.min(100, Math.max(0, (item.value / maxValue()) * 100));
77
78 return (
79 // Tooltip stays permanently mounted and toggles via CSS group-hover
80 // opacity rather than a JS-driven Show — mounting/unmounting it on
81 // every hover was the source of a visible jump for whichever item
82 // sits at the bottom of the list, since that insert/remove briefly
83 // altered the card's content.
84 <div class="group relative">
85 <div class="space-y-1.5">
86 <div class="flex items-center justify-between text-xs">
87 <span class="font-medium text-foreground/90 truncate max-w-[70%]">
88 {item.label}
89 </span>
90 <span class="font-mono text-xs font-semibold tabular-nums text-foreground">
91 {display(item)}
92 </span>
93 </div>
94
95 <div class="relative h-2.5 w-full overflow-hidden rounded-full bg-surface-fill">
96 <div
97 class={cn(
98 "h-full rounded-full transition-all duration-500 ease-out",
99 item.colorClass ?? "bg-primary",
100 "group-hover:brightness-110 group-hover:shadow-xs",
101 )}
102 style={{ width: `${pct()}%` }}
103 />
104 </div>
105 </div>
106
107 <div class="pointer-events-none absolute -top-7 right-0 z-20 rounded-lg bg-popover px-2.5 py-1 text-[11px] font-medium text-popover-foreground opacity-0 shadow-md border border-border transition-opacity group-hover:opacity-100">
108 {item.label}: {display(item)} ({Math.round(pct())}%)
109 </div>
110 </div>
111 );
112 }}
113 </For>
114 </div>
115
116 <Show when={props.itemsPerPage && pageCount() > 1}>
117 <TablePagination
118 pageIndex={pageIndex()}
119 pageCount={pageCount()}
120 pageSize={props.itemsPerPage!}
121 total={props.items.length}
122 onPageChange={setPageIndex}
123 />
124 </Show>
125 </Show>
126 </div>
127 );
128};