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>
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
// Resolves the current user's role in the active org (from the session
|
|
// endpoint) so pages can gate management actions to owner/admin. Falls back to
|
|
// null while loading or if the org can't be resolved.
|
|
|
|
const ORG_STORAGE_KEY = "pt_active_org";
|
|
const RANK: Record<string, number> = { owner: 4, admin: 3, editor: 2, viewer: 1 };
|
|
|
|
interface SessionOrg {
|
|
id: string;
|
|
role: string;
|
|
}
|
|
|
|
export interface CurrentRoleState {
|
|
role: string | null;
|
|
loading: boolean;
|
|
canManage: boolean;
|
|
}
|
|
|
|
export function useCurrentRole(): CurrentRoleState {
|
|
const [role, setRole] = useState<string | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
(async () => {
|
|
try {
|
|
const res = await fetch("/api/auth/session");
|
|
const body = await res.json().catch(() => null);
|
|
if (cancelled || !body?.success) return;
|
|
const orgs: SessionOrg[] = body.data?.orgs ?? [];
|
|
const stored =
|
|
typeof window !== "undefined"
|
|
? window.localStorage.getItem(ORG_STORAGE_KEY)
|
|
: null;
|
|
const active = orgs.find((o) => o.id === stored) ?? orgs[0];
|
|
setRole(active?.role ?? null);
|
|
} catch {
|
|
if (!cancelled) setRole(null);
|
|
} finally {
|
|
if (!cancelled) setLoading(false);
|
|
}
|
|
})();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, []);
|
|
|
|
const canManage = role ? (RANK[role] ?? 0) >= RANK.admin : false;
|
|
return { role, loading, canManage };
|
|
}
|