{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "team-05",
  "description": "A grid of what each role can do, with permissions in rows and roles in columns, unsaved changes highlighted and a save bar that shows once something changes.",
  "registryDependencies": [
    "checkbox",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/forms.json",
    "https://www.dashboardblocks.com/r/radix/table.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/team/team-05.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateProvider,\n  BlockStateRows,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { SaveBar } from '@/registry/components/dashboardblocks/forms'\nimport {\n  DataTable,\n  DataTableBody,\n  DataTableCell,\n  DataTableGroupHead,\n  DataTableHead,\n  DataTableHeader,\n  DataTableRow,\n  DataTableRowHead,\n} from '@/registry/components/dashboardblocks/table'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { useState } from 'react'\n\nimport { Checkbox } from '@/components/ui/checkbox'\n\nimport { cn } from '@/lib/utils'\n\ninterface PermissionRole {\n  id: string\n  /** Has every permission and can't be edited, like a workspace owner. */\n  locked?: boolean\n  members: number\n  name: string\n}\n\ninterface Permission {\n  description?: string\n  id: string\n  label: string\n  requires?: string\n}\n\ninterface PermissionGroup {\n  id: string\n  label: string\n  permissions: Permission[]\n}\n\n/** `grants[roleId]` lists the permission ids that role has. */\ntype PermissionGrants = Record<string, string[]>\n\ninterface Team5Props extends BlockStateProps {\n  description: string\n  /** Leave out, with `roles`, to show the empty state. */\n  grants?: PermissionGrants\n  groups: PermissionGroup[]\n  onSave?: (grants: PermissionGrants) => void\n  /** Leave out, or pass none, to show the empty state. */\n  roles?: PermissionRole[]\n  title: string\n}\n\ninterface PermissionMatrixProps extends Omit<Team5Props, 'grants' | 'roles' | 'status'> {\n  grants: PermissionGrants\n  roles: PermissionRole[]\n}\n\nconst exampleProps: Team5Props = {\n  description: 'Choose what each role can do. Changes apply to everyone with the role.',\n  grants: {\n    admin: [\n      'dashboards.view',\n      'dashboards.edit',\n      'dashboards.delete',\n      'dashboards.share',\n      'data.view',\n      'data.connect',\n      'data.export',\n      'members.view',\n      'members.invite',\n      'members.roles',\n      'billing.view',\n    ],\n    analyst: [\n      'dashboards.view',\n      'dashboards.edit',\n      'dashboards.share',\n      'data.view',\n      'data.export',\n      'members.view',\n    ],\n    member: ['dashboards.view', 'dashboards.edit', 'data.view', 'members.view'],\n    viewer: ['dashboards.view'],\n  },\n  groups: [\n    {\n      id: 'dashboards',\n      label: 'Dashboards',\n      permissions: [\n        { id: 'dashboards.view', label: 'View dashboards' },\n        {\n          id: 'dashboards.edit',\n          label: 'Create and edit',\n          requires: 'dashboards.view',\n        },\n        { id: 'dashboards.delete', label: 'Delete', requires: 'dashboards.edit' },\n        {\n          description: 'Invite people and create public links',\n          id: 'dashboards.share',\n          label: 'Share',\n          requires: 'dashboards.view',\n        },\n      ],\n    },\n    {\n      id: 'data',\n      label: 'Data',\n      permissions: [\n        { id: 'data.view', label: 'View data sources' },\n        {\n          description: 'Add, edit and remove connections',\n          id: 'data.connect',\n          label: 'Manage connections',\n          requires: 'data.view',\n        },\n        { id: 'data.export', label: 'Export to CSV', requires: 'data.view' },\n      ],\n    },\n    {\n      id: 'members',\n      label: 'Members',\n      permissions: [\n        { id: 'members.view', label: 'View members' },\n        { id: 'members.invite', label: 'Invite and remove', requires: 'members.view' },\n        { id: 'members.roles', label: 'Change roles', requires: 'members.invite' },\n      ],\n    },\n    {\n      id: 'billing',\n      label: 'Billing',\n      permissions: [\n        { description: 'Plan, usage and invoices', id: 'billing.view', label: 'View' },\n        {\n          id: 'billing.manage',\n          label: 'Change plan and payment',\n          requires: 'billing.view',\n        },\n      ],\n    },\n  ],\n  roles: [\n    { id: 'owner', locked: true, members: 1, name: 'Owner' },\n    { id: 'admin', members: 2, name: 'Admin' },\n    { id: 'analyst', members: 4, name: 'Analyst' },\n    { id: 'member', members: 12, name: 'Member' },\n    { id: 'viewer', members: 7, name: 'Viewer' },\n  ],\n  title: 'Roles and permissions',\n}\n\nfunction grantWithRequirements(\n  granted: Set<string>,\n  id: string,\n  byId: Map<string, Permission>,\n) {\n  for (let next: string | undefined = id; next && !granted.has(next);) {\n    granted.add(next)\n    next = byId.get(next)?.requires\n  }\n}\n\nfunction revokeWithDependents(granted: Set<string>, id: string, all: Permission[]) {\n  granted.delete(id)\n  for (const permission of all) {\n    if (permission.requires === id && granted.has(permission.id))\n      revokeWithDependents(granted, permission.id, all)\n  }\n}\n\nfunction PermissionMatrix(props: PermissionMatrixProps) {\n  const { description, groups, onSave, roles, title } = props\n  const [saved, setSaved] = useState(props.grants)\n  const [grants, setGrants] = useState(props.grants)\n\n  const all = groups.flatMap((group) => group.permissions)\n  const byId = new Map(all.map((permission) => [permission.id, permission]))\n  const editable = roles.filter((role) => !role.locked)\n\n  const has = (source: PermissionGrants, roleId: string, permissionId: string) =>\n    source[roleId]?.includes(permissionId) ?? false\n\n  const changes = editable.reduce(\n    (count, role) =>\n      count +\n      all.filter(\n        (permission) =>\n          has(grants, role.id, permission.id) !== has(saved, role.id, permission.id),\n      ).length,\n    0,\n  )\n\n  const toggle = (roleId: string, permissionId: string, on: boolean) => {\n    setGrants((current) => {\n      const granted = new Set(current[roleId] ?? [])\n      if (on) grantWithRequirements(granted, permissionId, byId)\n      else revokeWithDependents(granted, permissionId, all)\n      return { ...current, [roleId]: all.map((p) => p.id).filter((p) => granted.has(p)) }\n    })\n  }\n\n  const locked = roles.filter((role) => role.locked).map((role) => role.name)\n\n  return (\n    <section className='@container flex flex-col gap-3'>\n      <header className='flex flex-col gap-0.5'>\n        <h2 className='font-semibold tracking-tight'>{title}</h2>\n        <p className='text-muted-foreground text-sm'>\n          {description}\n          {locked.length > 0 &&\n            ` ${locked.join(' and ')} ${locked.length === 1 ? 'can' : 'can each'} do everything.`}\n        </p>\n      </header>\n      <form\n        method='post'\n        onSubmit={(event) => {\n          event.preventDefault()\n          setSaved(grants)\n          onSave?.(grants)\n        }}\n      >\n        <DataTable className='border-t' caption={title}>\n          <DataTableHeader>\n            <DataTableRow>\n              <DataTableHead className='min-w-40'>\n                <span className='sr-only'>Permission</span>\n              </DataTableHead>\n              {roles.map((role) => (\n                <DataTableHead key={role.id} align='center' className='min-w-20 py-2.5'>\n                  <div className='flex flex-col items-center'>\n                    <span className='text-foreground text-sm'>{role.name}</span>\n                    <span className='inline-flex items-center gap-1 font-normal tabular-nums [&_svg]:size-3'>\n                      {role.locked && (\n                        <IconPlaceholder\n                          lucide='LockIcon'\n                          tabler='IconLock'\n                          hugeicons='SquareLock02Icon'\n                          phosphor='LockIcon'\n                          remixicon='RiLockLine'\n                          aria-hidden\n                        />\n                      )}\n                      {role.members} {role.members === 1 ? 'member' : 'members'}\n                    </span>\n                  </div>\n                </DataTableHead>\n              ))}\n            </DataTableRow>\n          </DataTableHeader>\n          {groups.map((group) => (\n            <DataTableBody key={group.id}>\n              <DataTableRow>\n                <DataTableGroupHead colSpan={roles.length + 1}>\n                  {group.label}\n                </DataTableGroupHead>\n              </DataTableRow>\n              {group.permissions.map((permission) => (\n                <DataTableRow key={permission.id}>\n                  <DataTableRowHead>\n                    <span className='block'>{permission.label}</span>\n                    {permission.description && (\n                      <span className='text-muted-foreground block text-xs'>\n                        {permission.description}\n                      </span>\n                    )}\n                  </DataTableRowHead>\n                  {roles.map((role) => {\n                    const on = role.locked || has(grants, role.id, permission.id)\n                    const changed =\n                      !role.locked && on !== has(saved, role.id, permission.id)\n                    return (\n                      <DataTableCell\n                        key={role.id}\n                        align='center'\n                        className={cn(changed && 'bg-primary/5')}\n                      >\n                        <span className='inline-flex'>\n                          <Checkbox\n                            aria-label={`${role.name}: ${group.label}, ${permission.label}`}\n                            checked={on}\n                            disabled={role.locked}\n                            onCheckedChange={(next) =>\n                              toggle(role.id, permission.id, next === true)\n                            }\n                          />\n                        </span>\n                      </DataTableCell>\n                    )\n                  })}\n                </DataTableRow>\n              ))}\n            </DataTableBody>\n          ))}\n        </DataTable>\n        <SaveBar\n          className='mt-4'\n          isDirty={changes > 0}\n          isSubmitting={false}\n          message={`${changes} unsaved ${changes === 1 ? 'change' : 'changes'}`}\n          onDiscard={() => setGrants(saved)}\n        />\n      </form>\n    </section>\n  )\n}\n\nconst Team5 = (props: Team5Props) => {\n  const { description, grants = {}, roles = [], errorMessage, status, title } = props\n  const state = getBlockState(status, roles)\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={state}>\n      {state ? (\n        <section className='@container flex flex-col gap-3'>\n          <header className='flex flex-col gap-0.5'>\n            <h2 className='font-semibold tracking-tight'>{title}</h2>\n            <p className='text-muted-foreground text-sm'>{description}</p>\n          </header>\n          <BlockStateRows className='border-t' rows={8} />\n        </section>\n      ) : (\n        <PermissionMatrix {...props} grants={grants} roles={roles} />\n      )}\n    </BlockStateProvider>\n  )\n}\n\nexport {\n  Team5,\n  exampleProps as team5ExampleProps,\n  type Permission,\n  type PermissionGrants,\n  type PermissionGroup,\n  type PermissionRole,\n  type Team5Props,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/team/team-05.tsx"
    }
  ],
  "meta": {
    "shows": "A grid of what each role can do, with permissions in rows and roles in columns, unsaved changes highlighted and a save bar that shows once something changes.",
    "size": "full",
    "usedWith": [
      "team-01",
      "settings-01",
      "security-02"
    ],
    "component": "Team5",
    "props": "{ description: string; grants?: {  }; groups: { id: string; label: string; permissions: { description?: string; id: string; label: string; requires?: string }[] }[]; onSave?: (grants: {  }) => void; roles?: { id: string; locked?: boolean; members: number; name: string }[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
