"use client" import { useState } from "react" import { ChevronRight } from "lucide-react" import { Checkbox } from "@/components/ui/checkbox" import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "@/components/ui/collapsible" import { Label } from "@/components/ui/label" import { API_TOKEN_PERMISSION_GROUPS, API_TOKEN_PERMISSIONS, type ApiTokenPermissionGrant, type ApiTokenPermissionGroup, } from "@/lib/mail-automation/api-token-permissions" import { cn } from "@/lib/utils" function updateGrant( grants: ApiTokenPermissionGrant[], resource: string, patch: Partial> ): ApiTokenPermissionGrant[] { return grants.map((grant) => grant.resource === resource ? { ...grant, ...patch } : grant ) } function countSelectedInGroup( group: ApiTokenPermissionGroup, grants: ApiTokenPermissionGrant[] ) { const defs = API_TOKEN_PERMISSIONS.filter((def) => def.group === group) return defs.filter((def) => { const grant = grants.find((g) => g.resource === def.id) return grant?.read || grant?.write }).length } export function ApiTokenPermissionEditor({ grants, onChange, className, }: { grants: ApiTokenPermissionGrant[] onChange: (grants: ApiTokenPermissionGrant[]) => void className?: string }) { return (

Développez chaque domaine pour choisir lecture et écriture.

{API_TOKEN_PERMISSION_GROUPS.map((group) => ( ))}
) } function PermissionGroupSection({ group, title, description, grants, onChange, selectedCount, }: { group: ApiTokenPermissionGroup title: string description: string grants: ApiTokenPermissionGrant[] onChange: (grants: ApiTokenPermissionGrant[]) => void selectedCount: number }) { const [open, setOpen] = useState(false) const defs = API_TOKEN_PERMISSIONS.filter((def) => def.group === group) if (defs.length === 0) return null return (
{title} {description} {selectedCount > 0 ? `${selectedCount} active(s)` : "Aucune"}
Permission Lecture Écriture
{defs.map((def) => { const grant = grants.find((g) => g.resource === def.id) ?? { resource: def.id, read: false, write: false, } return (

{def.description}

{def.supportsRead ? ( onChange(updateGrant(grants, def.id, { read: checked === true })) } aria-label={`${def.label} — lecture`} /> ) : ( )}
{def.supportsWrite ? ( onChange(updateGrant(grants, def.id, { write: checked === true })) } aria-label={`${def.label} — écriture`} /> ) : ( )}
) })}
) }