Multi-tenant platform (182 pages, 228 API routes, ~30 modules) built on a deterministic verification core: clinical-trial claims checked against ClinicalTrials.gov registered results and raw event counts (ARR/NNT/RR+CI recomputed in code, oracle-tested), code-enforced exact-span grounding, and multi-source cross-verification. Auth/RBAC/multi-tenancy, document processing at scale (unpdf + Docling), agentic workflows, Claude Science connector, compliance/audit, billing, public API, and more. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
import { getJson } from "@/components/billing/apiClient";
|
|
import type { Invoice, Subscription, UsageSummary } from "./types";
|
|
|
|
interface BillingState {
|
|
subscription: Subscription | null;
|
|
usage: UsageSummary | null;
|
|
invoices: Invoice[];
|
|
loading: boolean;
|
|
error: string | null;
|
|
reload: () => void;
|
|
}
|
|
|
|
// Loads the org's subscription, usage summary, and invoice history in parallel
|
|
// from the existing /api/billing/* endpoints. Shared by the billing overview
|
|
// and detail pages so their data-fetching stays identical.
|
|
export function useBilling(): BillingState {
|
|
const [subscription, setSubscription] = useState<Subscription | null>(null);
|
|
const [usage, setUsage] = useState<UsageSummary | null>(null);
|
|
const [invoices, setInvoices] = useState<Invoice[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const load = useCallback(async () => {
|
|
setLoading(true);
|
|
setError(null);
|
|
const [subRes, usageRes, invRes] = await Promise.all([
|
|
getJson<Subscription | null>("/api/billing/subscription"),
|
|
getJson<UsageSummary>("/api/billing/usage"),
|
|
getJson<Invoice[]>("/api/billing/invoices"),
|
|
]);
|
|
setLoading(false);
|
|
if (!subRes.success) {
|
|
setError(subRes.error ?? "Failed to load billing.");
|
|
return;
|
|
}
|
|
setSubscription(subRes.data ?? null);
|
|
if (usageRes.success && usageRes.data) setUsage(usageRes.data);
|
|
if (invRes.success && invRes.data) setInvoices(invRes.data);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
load();
|
|
}, [load]);
|
|
|
|
return { subscription, usage, invoices, loading, error, reload: load };
|
|
}
|