mirror of
https://github.com/temetro/temetro.git
synced 2026-08-09 18:20:00 +00:00
3eb5687f4d
- Messages: search the inbox and the compose member picker. - Care Team: clickable member rows open an employee dialog showing role + permissions, with change-role (updateMemberRole) and remove. - Patients: Primary Care is now a provider dropdown (defaults to self for a doctor); add a Transfer action + dialog wired to the transfer API. - Activity: entries are clickable, opening a detail dialog. - Analytics: Section takes a columns prop so each row fills evenly (no orphan card in Appointments). - Add lib/staff.ts (listProviders), transferPatient client, rolePermissionSummary helper, and i18n keys for all new strings. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
143 lines
4.7 KiB
TypeScript
143 lines
4.7 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { type roles } from "@/lib/access";
|
|
import { authClient } from "@/lib/auth-client";
|
|
import { type NavItem, navItems } from "@/lib/nav";
|
|
|
|
export type RoleKey = keyof typeof roles;
|
|
|
|
// Roles an admin can assign when provisioning staff (owner is excluded — the
|
|
// clinic creator is the sole owner). Mirrors the backend's PROVISIONABLE_ROLES.
|
|
export const PROVISIONABLE_ROLES: RoleKey[] = [
|
|
"admin",
|
|
"doctor",
|
|
"reception",
|
|
"viewer",
|
|
];
|
|
|
|
// Departments a task can be assigned to (member roles). Mirrors the backend's
|
|
// TASK_DEPARTMENTS in lib/task-validation.ts.
|
|
export const DEPARTMENTS = ["admin", "doctor", "reception"] as const;
|
|
|
|
// The current user's role in the active clinic (null while loading or if they
|
|
// aren't a member). Re-fetches when the active organization changes.
|
|
export function useActiveRole(): string | null {
|
|
const { data: activeOrg } = authClient.useActiveOrganization();
|
|
const [role, setRole] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
authClient.organization
|
|
.getActiveMember()
|
|
.then(({ data }) => {
|
|
if (!cancelled) setRole(data?.role ?? null);
|
|
})
|
|
.catch(() => {
|
|
if (!cancelled) setRole(null);
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [activeOrg?.id]);
|
|
|
|
return role;
|
|
}
|
|
|
|
// The clinical resources + actions we surface in the Care Team permissions
|
|
// summary. Mirrors the statements in lib/access.ts.
|
|
export const CLINICAL_RESOURCES = [
|
|
"patient",
|
|
"appointment",
|
|
"prescription",
|
|
"task",
|
|
] as const;
|
|
const RESOURCE_ACTIONS = ["read", "write", "delete"] as const;
|
|
|
|
type PermissionArg = Parameters<
|
|
typeof authClient.organization.checkRolePermission
|
|
>[0]["permissions"];
|
|
|
|
// For a given role, the allowed actions on each clinical resource — computed
|
|
// from Better Auth so it stays in lock-step with lib/access.ts. Used by the
|
|
// Care Team employee dialog to show what a role can do.
|
|
export function rolePermissionSummary(
|
|
role: string | null | undefined,
|
|
): { resource: (typeof CLINICAL_RESOURCES)[number]; actions: string[] }[] {
|
|
if (!role) return [];
|
|
return CLINICAL_RESOURCES.map((resource) => {
|
|
const actions = RESOURCE_ACTIONS.filter((action) => {
|
|
try {
|
|
return authClient.organization.checkRolePermission({
|
|
role: role as RoleKey,
|
|
permissions: { [resource]: [action] } as PermissionArg,
|
|
});
|
|
} catch {
|
|
return false;
|
|
}
|
|
});
|
|
return { resource, actions };
|
|
});
|
|
}
|
|
|
|
// Whether a role may see clinical records (AI lookup, prescriptions, notes,
|
|
// analysis). Driven by Better Auth permissions so it stays in lock-step with
|
|
// lib/access.ts: the `reception` role has no `prescription` statement, so this
|
|
// is false for them and true for every clinical role.
|
|
export function hasClinicalAccess(role: string | null | undefined): boolean {
|
|
if (!role) return false;
|
|
try {
|
|
return authClient.organization.checkRolePermission({
|
|
role: role as RoleKey,
|
|
permissions: { prescription: ["read"] },
|
|
});
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Where a role lands after sign-in. Reception has no AI chat, so they start on
|
|
// the appointments board; clinical roles start on the chat home.
|
|
export function defaultLandingFor(role: string | null | undefined): string {
|
|
return hasClinicalAccess(role) ? "/" : "/appointments";
|
|
}
|
|
|
|
// Clinical-only routes — a non-clinical role (reception) is redirected away.
|
|
// Keyed by path; "/" matches exactly, others match themselves + nested paths.
|
|
const CLINICAL_ROUTES = [
|
|
"/",
|
|
"/prescriptions",
|
|
"/analysis",
|
|
"/notes",
|
|
"/activity",
|
|
];
|
|
|
|
// Whether `path` is reachable by `role`. Returns true while the role is still
|
|
// loading to avoid redirect flicker; the authoritative check is the backend's
|
|
// per-route RBAC (which returns 403 regardless).
|
|
export function canAccessRoute(
|
|
path: string,
|
|
role: string | null | undefined,
|
|
): boolean {
|
|
if (role == null) return true;
|
|
if (hasClinicalAccess(role)) return true;
|
|
return !CLINICAL_ROUTES.some((r) =>
|
|
r === "/" ? path === "/" : path === r || path.startsWith(`${r}/`),
|
|
);
|
|
}
|
|
|
|
// Nav items visible to a role, with clinical-only items (and sub-items) removed
|
|
// for non-clinical roles. While the role is loading we optimistically show
|
|
// everything (clinical users are the common case) — the flash is sub-second.
|
|
export function visibleNavItems(role: string | null | undefined): NavItem[] {
|
|
if (role == null) return navItems;
|
|
const clinical = hasClinicalAccess(role);
|
|
return navItems
|
|
.filter((item) => !item.requiresClinical || clinical)
|
|
.map((item) => ({
|
|
...item,
|
|
subs: item.subs?.filter((sub) => !sub.requiresClinical || clinical),
|
|
}));
|
|
}
|