"use client";
import { useCallback, useState } from "react";
import {
type Grants,
type MatrixModule,
PermissionMatrix,
} from "@/components/ui/permission-matrix";
const settings = {
readOnly: false,
/** None, Own and All; off: None and All only. */
scopes: true,
title: "Team leads",
};
const MODULES: MatrixModule[] = [
{ group: "Work", id: "requests", label: "Requests" },
{ group: "Work", id: "approvals", label: "Approvals" },
{ group: "Work", id: "support", label: "Support" },
{ group: "Operations", id: "monitoring", label: "Monitoring" },
{ group: "Operations", id: "alerts", label: "Alerts" },
{ group: "Operations", id: "audit", label: "Audit log" },
{ group: "Admin", id: "access", label: "Access" },
];
const TEAM_LEADS: Grants = {
alerts: { view: "all" },
approvals: { edit: "own", view: "all" },
audit: { view: "own" },
monitoring: { view: "all" },
requests: { edit: "all", manage: "own", view: "all" },
support: { edit: "own", view: "all" },
};
const SAVE_MS = 700;
const wait = (ms: number) =>
new Promise<void>((resolve) => {
setTimeout(resolve, ms);
});
/** One group's permissions: press cells to change them, then Save or Reset. */
export default function Demo(props: Partial<typeof settings>) {
const { readOnly, scopes, title } = { ...settings, ...props };
const [grants, setGrants] = useState(TEAM_LEADS);
const save = useCallback(async (next: Grants) => {
await wait(SAVE_MS);
setGrants(next);
}, []);
return (
<div className="flex min-h-[600px] w-full items-center justify-center bg-background p-6">
<div className="w-full max-w-3xl">
<PermissionMatrix
description={`What members of ${title} may do in each module.`}
modules={MODULES}
onSave={readOnly ? undefined : save}
scopes={scopes}
title={title}
value={grants}
/>
</div>
</div>
);
}