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