mirror of
https://github.com/temetro/temetro.git
synced 2026-08-12 11:36:52 +00:00
321a6298a4
- Pharmacy: the sidebar entry now expands into Pharmacy + a new Inventory page (searchable medication stock with derived in-stock/low/out availability, backed by /api/inventory). Fix the dispensing-queue "Expiring" badge so it only flags courses ending within the next 7 days, not ones already elapsed. - Lab "Add analysis result": the patient picker no longer lists patients until you type and supports arrow-key + Enter selection; the test field offers a catalog of common analyses with an Advanced option (custom analysis + ref range); submitted results now show immediately in a Recent results feed. - Analysis: add a Live panel (real-time line chart) and replace the flat trend sparklines with bklit-ui area + bar charts (installed from the @bklit shadcn registry; chart CSS variables wired into the theme for light and dark). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
143 lines
3.4 KiB
TypeScript
143 lines
3.4 KiB
TypeScript
"use client";
|
|
|
|
import { motion } from "motion/react";
|
|
import { memo, useEffect, useMemo, useState } from "react";
|
|
import { createPortal } from "react-dom";
|
|
import { cn } from "@/lib/utils";
|
|
import { useChart, useChartStable } from "./chart-context";
|
|
|
|
export interface BarYAxisProps {
|
|
/** Whether to show all labels or skip some for dense data. Default: true */
|
|
showAllLabels?: boolean;
|
|
/** Maximum number of labels to show. Default: 20 */
|
|
maxLabels?: number;
|
|
}
|
|
|
|
interface BarYAxisLabelProps {
|
|
label: string;
|
|
y: number;
|
|
bandHeight: number;
|
|
isHovered: boolean;
|
|
}
|
|
|
|
function BarYAxisLabel({
|
|
label,
|
|
y,
|
|
bandHeight,
|
|
isHovered,
|
|
}: BarYAxisLabelProps) {
|
|
return (
|
|
<div
|
|
className="absolute right-0 flex items-center justify-end pr-2"
|
|
style={{
|
|
top: y,
|
|
height: bandHeight,
|
|
}}
|
|
>
|
|
<motion.span
|
|
animate={{
|
|
opacity: isHovered ? 1 : 0.7,
|
|
color: isHovered
|
|
? "var(--foreground)"
|
|
: "var(--chart-label, var(--color-zinc-500))",
|
|
}}
|
|
className={cn("truncate whitespace-nowrap text-right text-xs")}
|
|
initial={{
|
|
opacity: 0.7,
|
|
color: "var(--chart-label, var(--color-zinc-500))",
|
|
}}
|
|
style={{ maxWidth: 70 }}
|
|
transition={{ duration: 0.15 }}
|
|
>
|
|
{label}
|
|
</motion.span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function BarYAxis(props: BarYAxisProps) {
|
|
const { containerRef, barScale } = useChartStable();
|
|
const [mounted, setMounted] = useState(false);
|
|
|
|
useEffect(() => {
|
|
setMounted(true);
|
|
}, []);
|
|
|
|
const container = containerRef.current;
|
|
if (!(mounted && container)) {
|
|
return null;
|
|
}
|
|
|
|
if (!barScale) {
|
|
return null;
|
|
}
|
|
|
|
return <BarYAxisInner {...props} container={container} />;
|
|
}
|
|
|
|
const BarYAxisInner = memo(function BarYAxisInner({
|
|
showAllLabels = true,
|
|
maxLabels = 20,
|
|
container,
|
|
}: BarYAxisProps & { container: HTMLDivElement }) {
|
|
const { margin, barScale, bandWidth, barXAccessor, data, hoveredBarIndex } =
|
|
useChart();
|
|
|
|
// Generate labels for each bar
|
|
const labelsToShow = useMemo(() => {
|
|
if (!(barScale && bandWidth && barXAccessor)) {
|
|
return [];
|
|
}
|
|
|
|
const allLabels = data.map((d, i) => {
|
|
const label = barXAccessor(d);
|
|
const bandY = barScale(label) ?? 0;
|
|
// Center the label vertically within the band
|
|
const y = bandY + margin.top;
|
|
return { label, y, bandHeight: bandWidth, index: i };
|
|
});
|
|
|
|
// If showAllLabels is true or we have fewer than maxLabels, show all
|
|
if (showAllLabels || allLabels.length <= maxLabels) {
|
|
return allLabels;
|
|
}
|
|
|
|
// Otherwise, skip some labels to avoid crowding
|
|
const step = Math.ceil(allLabels.length / maxLabels);
|
|
return allLabels.filter((_, i) => i % step === 0);
|
|
}, [
|
|
barScale,
|
|
bandWidth,
|
|
barXAccessor,
|
|
data,
|
|
margin.top,
|
|
showAllLabels,
|
|
maxLabels,
|
|
]);
|
|
|
|
return createPortal(
|
|
<div
|
|
className="pointer-events-none absolute top-0 bottom-0"
|
|
style={{
|
|
left: 0,
|
|
width: margin.left,
|
|
}}
|
|
>
|
|
{labelsToShow.map((item) => (
|
|
<BarYAxisLabel
|
|
bandHeight={item.bandHeight}
|
|
isHovered={hoveredBarIndex === item.index}
|
|
key={`${item.label}-${item.y}`}
|
|
label={item.label}
|
|
y={item.y}
|
|
/>
|
|
))}
|
|
</div>,
|
|
container
|
|
);
|
|
});
|
|
|
|
BarYAxis.displayName = "BarYAxis";
|
|
|
|
export default BarYAxis;
|