Files
temetro/frontend/components/charts/use-scheduled-tooltip.ts
T
Khalid Abdi 321a6298a4 frontend: pharmacy inventory, lab add-result & analysis charts
- 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>
2026-06-12 19:22:07 +03:00

98 lines
2.5 KiB
TypeScript

"use client";
import { useCallback, useEffect, useRef, useState } from "react";
export interface ScheduledTooltipControls<T> {
tooltipData: T | null;
setTooltipData: React.Dispatch<React.SetStateAction<T | null>>;
scheduleTooltip: (tooltip: T, dedupeKey?: string) => void;
clearTooltip: () => void;
resetTooltipDedupe: () => void;
}
function defaultDedupeKey<T>(tooltip: T): string {
if (
typeof tooltip === "object" &&
tooltip !== null &&
"index" in tooltip &&
typeof (tooltip as { index: unknown }).index === "number"
) {
const { index, x } = tooltip as { index: number; x?: number };
if (typeof x === "number") {
return `${index}:${Math.round(x)}`;
}
return String(index);
}
return JSON.stringify(tooltip);
}
export function useScheduledTooltip<T>(): ScheduledTooltipControls<T> {
const [tooltipData, setTooltipData] = useState<T | null>(null);
const lastKeyRef = useRef<string | null>(null);
const pendingRef = useRef<T | null>(null);
const rafRef = useRef<number | null>(null);
const pendingKeyRef = useRef<string | null>(null);
useEffect(() => {
return () => {
if (rafRef.current !== null) {
cancelAnimationFrame(rafRef.current);
}
};
}, []);
const commitTooltip = useCallback((tooltip: T, dedupeKey: string) => {
if (dedupeKey === lastKeyRef.current) {
return;
}
lastKeyRef.current = dedupeKey;
setTooltipData(tooltip);
}, []);
const scheduleTooltip = useCallback(
(tooltip: T, dedupeKey?: string) => {
const key = dedupeKey ?? defaultDedupeKey(tooltip);
pendingRef.current = tooltip;
pendingKeyRef.current = key;
if (key === lastKeyRef.current) {
return;
}
if (rafRef.current !== null) {
return;
}
rafRef.current = requestAnimationFrame(() => {
rafRef.current = null;
const next = pendingRef.current;
const nextKey = pendingKeyRef.current;
if (next && nextKey) {
commitTooltip(next, nextKey);
}
});
},
[commitTooltip]
);
const clearTooltip = useCallback(() => {
if (rafRef.current !== null) {
cancelAnimationFrame(rafRef.current);
rafRef.current = null;
}
pendingRef.current = null;
pendingKeyRef.current = null;
lastKeyRef.current = null;
setTooltipData(null);
}, []);
const resetTooltipDedupe = useCallback(() => {
lastKeyRef.current = null;
}, []);
return {
tooltipData,
setTooltipData,
scheduleTooltip,
clearTooltip,
resetTooltipDedupe,
};
}