"use client"; import "./components/ui/tidal.css"; import { useLayoutEffect, useMemo, useState } from "react"; import { TidalRolesPermissions, type TidalPermissionMember, type TidalPermissionRole } from "./components/ui/roles-permissions"; import "./components/ui/roles-permissions.css"; function tidalExampleAvatarDataUri(seed: unknown) { const text = String(seed).replace(/[^a-z0-9]/gi, "").slice(-2) || "UI"; const palette = ["#333333", "#555555", "#777777", "#999999", "#bbbbbb"]; const color = palette[Array.from(text).reduce((sum, character) => sum + character.charCodeAt(0), 0) % palette.length]; const svg = ``; return `data:image/svg+xml,${encodeURIComponent(svg)}`; } function tidalExampleImageDataUri(label: string) { const safeLabel = label.replace(/[<>&]/g, "").slice(0, 28) || "Sample image"; const svg = `${safeLabel}`; return `data:image/svg+xml,${encodeURIComponent(svg)}`; } const tidalExampleStyles = ".tidal-scope .roles-permissions-demo,\n.tidal-scope .roles-permissions-demo * {\n box-sizing: border-box;\n}\n\n.tidal-scope .roles-permissions-demo {\n display: grid;\n width: min(100%, 920px);\n min-width: 0;\n gap: 12px;\n}\n\n.tidal-scope .roles-permissions-demo-actions {\n display: flex;\n min-width: 0;\n align-items: center;\n justify-content: space-between;\n gap: 12px;\n}\n\n.tidal-scope .roles-permissions-demo-actions output {\n min-width: 0;\n color: var(--t-muted);\n font-size: 11px;\n line-height: 1.4;\n overflow-wrap: anywhere;\n}\n\n.tidal-scope .roles-permissions-demo-actions button {\n display: inline-flex;\n min-width: 44px;\n min-height: 44px;\n flex: 0 0 auto;\n align-items: center;\n justify-content: center;\n padding: 8px 13px;\n border: 1px solid var(--t-border);\n border-radius: var(--t-radius-control);\n color: var(--t-ink);\n background: var(--t-surface);\n font: inherit;\n font-size: 11px;\n font-weight: 720;\n cursor: pointer;\n transition:\n color 160ms,\n background-color 160ms,\n transform 140ms var(--t-ease-out);\n}\n\n.tidal-scope .roles-permissions-demo-actions button:hover {\n color: var(--t-surface);\n background: var(--t-ink);\n}\n\n.tidal-scope .roles-permissions-demo-actions button:active {\n transform: scale(0.97);\n}\n\n.tidal-scope .roles-permissions-demo-actions button:focus-visible {\n outline: 2px solid var(--t-ink);\n outline-offset: 3px;\n}\n\n@media (max-width: 520px) {\n .tidal-scope .roles-permissions-demo-actions {\n display: grid;\n }\n\n .tidal-scope .roles-permissions-demo-actions button {\n width: 100%;\n }\n}\n\n@media (prefers-reduced-motion: reduce) {\n .tidal-scope .roles-permissions-demo-actions button {\n transition: none;\n }\n\n .tidal-scope .roles-permissions-demo-actions button:active {\n transform: none;\n }\n}"; const ROLES: readonly TidalPermissionRole[] = [ { id: "admin", label: "Admin", description: "Workspace controls and publishing.", }, { id: "editor", label: "Editor", description: "Creates and reviews project work.", }, { id: "viewer", label: "Viewer", description: "Reads shared project work.", }, ]; const PERMISSIONS = [ { id: "view-projects", groupId: "content", groupLabel: "Project work", label: "View projects", description: "Open shared project records.", }, { id: "edit-projects", groupId: "content", groupLabel: "Project work", label: "Edit projects", description: "Change notes and milestones.", }, { id: "publish-projects", groupId: "content", groupLabel: "Project work", label: "Publish projects", description: "Move approved work to published.", }, { id: "manage-members", groupId: "workspace", groupLabel: "Workspace", label: "Manage members", description: "Change sample member assignments.", }, { id: "review-billing", groupId: "workspace", groupLabel: "Workspace", label: "Review billing", description: "Read the sample billing summary.", }, ] as const; const INITIAL_GRANTS: Readonly> = { admin: [ "view-projects", "edit-projects", "publish-projects", "manage-members", "review-billing", ], editor: ["edit-projects", "publish-projects"], viewer: [], }; const INITIAL_MEMBERS: readonly TidalPermissionMember[] = [ { id: "ava", name: "Ava Morgan", description: "Project lead", roleId: "admin", avatar: ( Ava Morgan ), }, { id: "noah", name: "Noah Park", description: "Design reviewer", roleId: "editor", avatar: ( Noah Park ), }, { id: "mila", name: "Mila Santos", description: "Guest reviewer", roleId: "viewer", avatar: ( Mila Santos ), }, ]; function copyGrants(source: Readonly>) { return Object.fromEntries( Object.entries(source).map(([roleId, permissionIds]) => [ roleId, [...permissionIds], ]), ); } export function RolesPermissionsDemo() { const [view, setView] = useState<"matrix" | "list">("matrix"); const [grants, setGrants] = useState>(() => copyGrants(INITIAL_GRANTS), ); const [members, setMembers] = useState(() => INITIAL_MEMBERS.map((member) => ({ ...member })), ); const [status, setStatus] = useState("Review the current sample access map."); const roles = useMemo( () => ROLES.map((role) => ({ ...role, memberCount: `${members.filter((member) => member.roleId === role.id).length} assigned`, })), [members], ); useLayoutEffect(() => { const media = window.matchMedia("(max-width: 599px)"); const syncView = () => setView(media.matches ? "list" : "matrix"); syncView(); media.addEventListener("change", syncView); return () => media.removeEventListener("change", syncView); }, []); const setPermission = ( roleId: string, permissionId: string, granted: boolean, ) => { setGrants((current) => { const next = new Set(current[roleId] ?? []); if (granted) next.add(permissionId); else next.delete(permissionId); return { ...current, [roleId]: [...next] }; }); const role = ROLES.find((candidate) => candidate.id === roleId); const permission = PERMISSIONS.find( (candidate) => candidate.id === permissionId, ); setStatus( `${String(permission?.label ?? permissionId)} ${granted ? "enabled" : "disabled"} for ${String(role?.label ?? roleId)}.`, ); }; return (
{ setView(next); setStatus( `${next === "matrix" ? "Matrix" : "Role list"} view selected.`, ); }} onPermissionChange={(roleId, permissionId, granted) => setPermission(roleId, permissionId, granted) } onGroupChange={(roleId, _groupId, granted, permissionIds) => { setGrants((current) => { const next = new Set(current[roleId] ?? []); permissionIds.forEach((permissionId) => { if (granted) next.add(permissionId); else next.delete(permissionId); }); return { ...current, [roleId]: [...next] }; }); const role = ROLES.find((candidate) => candidate.id === roleId); setStatus( `${permissionIds.length} editable permissions ${granted ? "enabled" : "disabled"} for ${String(role?.label ?? roleId)}.`, ); }} onMemberRoleChange={(memberId, roleId) => { setMembers((current) => current.map((member) => member.id === memberId ? { ...member, roleId } : member, ), ); const member = members.find((candidate) => candidate.id === memberId); const role = ROLES.find((candidate) => candidate.id === roleId); setStatus( `${String(member?.name ?? memberId)} assigned to ${String(role?.label ?? roleId)}.`, ); }} footer={ Sample permissions. Changes stay in this preview and do not enforce access. } />
{status}
); } export default function AgentExample() { return (
); }