mirror of
https://github.com/temetro/temetro.git
synced 2026-08-09 18:20:00 +00:00
67cafdac3d
The "Enable AI assistant" toggle already disabled the AI for everyone (including owners/admins) at the policy layer, but the copy read as an employee-only control and the Analysis surface stayed visible. Clarify that the master switch covers admins too, and close the gaps: hide the Analysis nav/command entry and redirect /analysis (alongside the chat home) to /patients when AI is disabled. The employee-only toggle remains the secondary option that keeps AI for owners/admins. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
81 lines
2.8 KiB
TypeScript
81 lines
2.8 KiB
TypeScript
"use client";
|
|
|
|
import { usePathname, useRouter } from "next/navigation";
|
|
import { type ReactNode, useEffect, useRef } from "react";
|
|
|
|
import { useAiAccess } from "@/lib/ai-policy";
|
|
import { authClient } from "@/lib/auth-client";
|
|
import { canAccessRoute, defaultLandingFor, useActiveRole } from "@/lib/roles";
|
|
|
|
// Authoritative client-side gate for the app shell. Requires a session and an
|
|
// active clinic. If the user is signed in without an active clinic but already
|
|
// belongs to one, we select it automatically; onboarding is only for users
|
|
// with no clinics at all. The API enforces the same access rules server-side.
|
|
export function AppAuthGuard({ children }: { children: ReactNode }) {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const role = useActiveRole();
|
|
const { allowed: aiAllowed, loading: aiLoading } = useAiAccess();
|
|
const { data: session, isPending } = authClient.useSession();
|
|
const { data: orgs, isPending: orgsPending } =
|
|
authClient.useListOrganizations();
|
|
const settingActive = useRef(false);
|
|
|
|
const hasUser = Boolean(session?.user);
|
|
const activeOrgId = session?.session?.activeOrganizationId ?? null;
|
|
|
|
useEffect(() => {
|
|
if (isPending) return;
|
|
if (!hasUser) {
|
|
router.replace("/login");
|
|
return;
|
|
}
|
|
if (activeOrgId) return;
|
|
|
|
// Signed in but no active clinic selected yet.
|
|
if (orgsPending) return;
|
|
const first = orgs?.[0];
|
|
if (first) {
|
|
if (!settingActive.current) {
|
|
settingActive.current = true;
|
|
void authClient.organization.setActive({ organizationId: first.id });
|
|
}
|
|
} else {
|
|
router.replace("/onboarding");
|
|
}
|
|
}, [isPending, hasUser, activeOrgId, orgsPending, orgs, router]);
|
|
|
|
const ready = hasUser && Boolean(activeOrgId);
|
|
|
|
// Role-based route guard: keep non-clinical roles (reception) out of clinical
|
|
// pages — bounce them to their default landing. The backend enforces the same
|
|
// via per-route RBAC (403); this just avoids showing an empty/erroring page.
|
|
useEffect(() => {
|
|
if (!ready || role == null) return;
|
|
if (!canAccessRoute(pathname, role)) {
|
|
router.replace(defaultLandingFor(role));
|
|
return;
|
|
}
|
|
// AI kill-switch: the AI surfaces — chat home ("/") and Analysis — are off
|
|
// for this user (a full clinic disable also covers owners/admins). Send them
|
|
// to patients (clinical roles always have it; non-clinical never land here).
|
|
if (
|
|
!aiLoading &&
|
|
!aiAllowed &&
|
|
(pathname === "/" || pathname === "/analysis")
|
|
) {
|
|
router.replace("/patients");
|
|
}
|
|
}, [ready, role, pathname, router, aiAllowed, aiLoading]);
|
|
|
|
if (!ready) {
|
|
return (
|
|
<div className="flex h-dvh w-full items-center justify-center text-sm text-muted-foreground">
|
|
Loading…
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return <>{children}</>;
|
|
}
|