Files
temetro/frontend/components/chat/analytics-card.tsx
T
Khalid Abdi 3aa699aefe feat: clinic-wide AI (analytics/earnings/inventory + invoice-from-file),
real Live card, persistent chat history

Analytics & earnings:
- Analytics now carries real money computed from invoices (billed/paid/
  outstanding + by-month); new Earnings section on the Analysis page drawn with
  the project's Bklit chart components (shared EarningsChart).

AI agent reaches the whole clinic:
- new read tools getClinicInfo / getAnalytics / listInventory render clinic,
  analytics (with a Bklit earnings chart) and inventory cards in chat
- proposeInvoice turns an uploaded purchase/medication list into an invoice
  (new "invoice" action-preview kind → createInvoice); invoices/appointments
  auto-create/link a patient (ensurePatient) so they hit the Patients page

Live card:
- plots real data — patients checked in today — via GET /api/analytics/live
  (polled); value pill clamped and margins widened so nothing spills the card

Persistent AI chat history (Claude-style):
- ai_chat_threads + ai_chat_messages (migration 0016); per-user, org-scoped
  thread CRUD under /api/chat/threads
- chat panel owns a thread id, loads /?thread=<id>, and auto-saves after each
  exchange; sidebar lists past chats (open/delete), "New chat" starts fresh

Verified with backend typecheck + frontend tsc + next build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-14 22:25:15 +03:00

73 lines
2.3 KiB
TypeScript

"use client";
import { BarChart3 } from "lucide-react";
import { useTranslation } from "react-i18next";
import { EarningsChart } from "@/components/analysis/earnings-chart";
import { Card } from "@/components/ui/card";
import type { Analytics } from "@/lib/analytics";
import { formatMoney } from "@/lib/invoices";
function Stat({ label, value }: { label: string; value: string }) {
return (
<div className="flex flex-col">
<span className="text-muted-foreground text-xs">{label}</span>
<span className="font-semibold text-foreground text-sm tabular-nums">
{value}
</span>
</div>
);
}
// The agent's analytics card: clinic KPIs + earnings, with a Bklit earnings
// chart (reused from the Analysis page).
export function AnalyticsCard({ data }: { data: Analytics }) {
const { t } = useTranslation();
return (
<Card className="w-full gap-3 p-4">
<div className="flex items-center gap-2">
<BarChart3 className="size-4 text-muted-foreground" />
<span className="font-medium text-sm">
{t("chat.analyticsCard.title")}
</span>
</div>
<div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
<Stat
label={t("chat.analyticsCard.patients")}
value={String(data.patients.total)}
/>
<Stat
label={t("chat.analyticsCard.appointmentsThisWeek")}
value={String(data.appointments.thisWeek)}
/>
<Stat
label={t("chat.analyticsCard.activePrescriptions")}
value={String(data.prescriptions.active)}
/>
<Stat
label={t("chat.analyticsCard.openTasks")}
value={String(data.tasks.open)}
/>
<Stat
label={t("chat.analyticsCard.billed")}
value={formatMoney(data.earnings.totalBilled)}
/>
<Stat
label={t("chat.analyticsCard.paid")}
value={formatMoney(data.earnings.totalPaid)}
/>
<Stat
label={t("chat.analyticsCard.outstanding")}
value={formatMoney(data.earnings.totalOutstanding)}
/>
</div>
<div className="flex flex-col gap-1.5">
<span className="text-muted-foreground text-xs">
{t("chat.analyticsCard.byMonth")}
</span>
<EarningsChart data={data.earnings.byMonth} />
</div>
</Card>
);
}