86 lines
2.7 KiB
TypeScript
86 lines
2.7 KiB
TypeScript
import { useAuth, type AuthUser } from "~/context/AuthContext";
|
|
|
|
export type OperationsRole = "business" | "model" | "admin";
|
|
export type OperationsPageKey =
|
|
| "workbench"
|
|
| "usage"
|
|
| "models"
|
|
| "banks"
|
|
| "deployed-models"
|
|
| "monitoring"
|
|
| "reports"
|
|
| "report-summary"
|
|
| "workflows"
|
|
| "knowledge"
|
|
| "rules"
|
|
| "prompts"
|
|
| "settings";
|
|
|
|
export type OperationsAction =
|
|
| "workflow:create"
|
|
| "workflow:advance"
|
|
| "workflow:business-confirm"
|
|
| "monitor:initial-review"
|
|
| "monitor:final-review"
|
|
| "report:edit"
|
|
| "report:send"
|
|
| "rules:manage"
|
|
| "prompts:manage"
|
|
| "settings:manage";
|
|
|
|
const PAGE_ROLE_VISIBILITY: Record<OperationsPageKey, OperationsRole[]> = {
|
|
workbench: ["business", "model", "admin"],
|
|
usage: ["admin"],
|
|
models: ["business", "model", "admin"],
|
|
banks: ["business", "model", "admin"],
|
|
"deployed-models": ["model", "admin"],
|
|
monitoring: ["business", "model", "admin"],
|
|
reports: ["business", "model", "admin"],
|
|
"report-summary": ["business", "model", "admin"],
|
|
workflows: ["business", "model", "admin"],
|
|
knowledge: ["model", "admin"],
|
|
rules: ["model", "admin"],
|
|
prompts: ["model", "admin"],
|
|
settings: ["admin"],
|
|
};
|
|
|
|
const ACTION_ROLE_VISIBILITY: Record<OperationsAction, OperationsRole[]> = {
|
|
"workflow:create": ["business", "admin"],
|
|
"workflow:advance": ["model", "admin"],
|
|
"workflow:business-confirm": ["business", "admin"],
|
|
"monitor:initial-review": ["model", "admin"],
|
|
"monitor:final-review": ["business", "admin"],
|
|
"report:edit": ["model", "admin"],
|
|
"report:send": ["model", "admin"],
|
|
"rules:manage": ["admin"],
|
|
"prompts:manage": ["model", "admin"],
|
|
"settings:manage": ["admin"],
|
|
};
|
|
|
|
export function resolveOperationsRole(user: AuthUser | null): OperationsRole {
|
|
if (user?.is_system_admin || user?.role_code === "admin") return "admin";
|
|
const code = user?.role_code?.toLowerCase() ?? "";
|
|
if (["business", "business_team", "biz"].includes(code)) return "business";
|
|
return "model";
|
|
}
|
|
|
|
export function useOperationsRole(): OperationsRole {
|
|
return resolveOperationsRole(useAuth().user);
|
|
}
|
|
|
|
export function isOperationsPageVisibleForRole(role: OperationsRole, page: OperationsPageKey): boolean {
|
|
return PAGE_ROLE_VISIBILITY[page].includes(role);
|
|
}
|
|
|
|
export function isOperationsActionVisibleForRole(role: OperationsRole, action: OperationsAction): boolean {
|
|
return ACTION_ROLE_VISIBILITY[action].includes(role);
|
|
}
|
|
|
|
export function operationsPageFromPath(pathname: string): OperationsPageKey {
|
|
const path = pathname.replace(/^\/operations\/?/, "").split("/")[0] ?? "";
|
|
if (!path) return "workbench";
|
|
if (path === "monitoring") return "monitoring";
|
|
if (path in PAGE_ROLE_VISIBILITY) return path as OperationsPageKey;
|
|
return "workbench";
|
|
}
|