Files
temetro/frontend/components/charts/chart-config-context.tsx
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

93 lines
2.3 KiB
TypeScript

"use client";
import { createContext, type ReactNode, useContext, useMemo } from "react";
export interface SpringConfig {
stiffness: number;
damping: number;
}
export interface ChartConfigValue {
/** Crosshair indicator, tooltip dot, date pill. */
tooltipSpring: SpringConfig;
/** Floating tooltip panel. */
tooltipBoxSpring: SpringConfig;
/** Line/area hover-highlight band (x + width). */
highlightSpring: SpringConfig;
}
export const DEFAULT_CHART_CONFIG: ChartConfigValue = {
tooltipSpring: { stiffness: 300, damping: 30 },
tooltipBoxSpring: { stiffness: 100, damping: 20 },
highlightSpring: { stiffness: 180, damping: 28 },
};
const ChartConfigContext = createContext<ChartConfigValue | null>(null);
export interface ChartConfigProviderProps {
value?: Partial<ChartConfigValue>;
children: ReactNode;
}
export function ChartConfigProvider({
value,
children,
}: ChartConfigProviderProps) {
const merged = useMemo<ChartConfigValue>(
() => ({
...DEFAULT_CHART_CONFIG,
...value,
}),
[value]
);
return (
<ChartConfigContext.Provider value={merged}>
{children}
</ChartConfigContext.Provider>
);
}
export function useChartConfig(): ChartConfigValue {
return useContext(ChartConfigContext) ?? DEFAULT_CHART_CONFIG;
}
const DEFAULT_TOOLTIP_BOX_DAMPING =
DEFAULT_CHART_CONFIG.tooltipBoxSpring.damping;
/** Maps a damping slider to the floating tooltip panel follow spring. `0` = instant. */
export function resolveTooltipBoxMotion(damping?: number): {
animate: boolean;
springConfig: SpringConfig;
} {
if (damping === 0) {
return {
animate: false,
springConfig: DEFAULT_CHART_CONFIG.tooltipBoxSpring,
};
}
const effectiveDamping = damping ?? DEFAULT_TOOLTIP_BOX_DAMPING;
let stiffness = DEFAULT_CHART_CONFIG.tooltipBoxSpring.stiffness;
if (effectiveDamping < DEFAULT_TOOLTIP_BOX_DAMPING) {
const t =
(DEFAULT_TOOLTIP_BOX_DAMPING - effectiveDamping) /
DEFAULT_TOOLTIP_BOX_DAMPING;
stiffness += t * 400;
} else if (effectiveDamping > DEFAULT_TOOLTIP_BOX_DAMPING) {
const t =
(effectiveDamping - DEFAULT_TOOLTIP_BOX_DAMPING) /
(100 - DEFAULT_TOOLTIP_BOX_DAMPING);
stiffness -= t * 85;
}
return {
animate: true,
springConfig: {
stiffness: Math.max(12, Math.round(stiffness)),
damping: effectiveDamping,
},
};
}