import { For, Show, createEffect, createMemo, createSignal, type Component } from "solid-js"; import { cn } from "@/lib/cn"; import { EmptyInline } from "@/components/ui/empty-inline"; import { TablePagination } from "@/components/ui/table-pagination"; import { useT } from "@/stores/preferences-context"; export type ChartBarItem = { id: string; label: string; value: number; max?: number; formattedValue?: string; colorClass?: string; }; export type ChartBarProps = { title?: string; subtitle?: string; items: ChartBarItem[]; maxScale?: number; class?: string; /** When set and items.length exceeds it, the list is paged with Prev/Next controls. */ itemsPerPage?: number; }; export const ChartBar: Component = (props) => { const [pageIndex, setPageIndex] = createSignal(0); const t = useT(); // The scale is computed off every item, not just the visible page, so bars // don't rescale as the reader pages through them. const maxValue = () => { if (props.maxScale != null && props.maxScale > 0) return props.maxScale; const itemMax = Math.max(...props.items.map((i) => i.value), 1); return itemMax; }; const pageCount = createMemo(() => props.itemsPerPage ? Math.max(1, Math.ceil(props.items.length / props.itemsPerPage)) : 1, ); // A refetch can shrink the item count out from under the current page. createEffect(() => { if (pageIndex() >= pageCount()) setPageIndex(Math.max(0, pageCount() - 1)); }); const visibleItems = createMemo(() => { if (!props.itemsPerPage) return props.items; const start = pageIndex() * props.itemsPerPage; return props.items.slice(start, start + props.itemsPerPage); }); return (

{props.title}

{props.subtitle}

0} fallback={ } >
{(item) => { const pct = () => Math.min(100, Math.max(0, (item.value / maxValue()) * 100)); return ( // Tooltip stays permanently mounted and toggles via CSS group-hover // opacity rather than a JS-driven Show — mounting/unmounting it on // every hover was the source of a visible jump for whichever item // sits at the bottom of the list, since that insert/remove briefly // altered the card's content.
{item.label} {item.formattedValue ?? item.value}
{item.label}: {item.formattedValue ?? item.value} ({Math.round(pct())}%)
); }}
1}>
); };