import { useState } from 'react' import { toast } from 'sonner' import { Button, cn, Collapsible, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, Form, Toggle, Tooltip, TooltipContent, TooltipTrigger, } from 'ui' import { useDatabaseRoleUpdateMutation } from 'data/database-roles/database-role-update-mutation' import { PgRole } from 'data/database-roles/database-roles-query' import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject' import { ChevronUp, HelpCircle, MoreVertical, Trash } from 'lucide-react' import { ROLE_PERMISSIONS } from './Roles.constants' interface RoleRowProps { role: PgRole disabled?: boolean onSelectDelete: (role: string) => void } export const RoleRow = ({ role, disabled = false, onSelectDelete }: RoleRowProps) => { const { data: project } = useSelectedProjectQuery() const [isExpanded, setIsExpanded] = useState(false) const { mutate: updateDatabaseRole, isPending: isUpdating } = useDatabaseRoleUpdateMutation() const { isSuperuser, canLogin, canCreateRole, canCreateDb, isReplicationRole, canBypassRls } = role const onSaveChanges = async (values: Partial, { resetForm }: any) => { if (!project) return console.error('Project is required') const changed = Object.fromEntries( Object.entries(values).filter(([k, v]) => v !== (role as any)[k]) ) updateDatabaseRole( { projectRef: project.ref, connectionString: project.connectionString, id: role.id, payload: changed, }, { onSuccess: () => { toast.success(`Successfully updated role "${role.name}"`) resetForm({ values: { ...values }, initialValues: { ...values } }) }, } ) } return (
{({ values, initialValues, handleReset }: any) => { const hasChanges = JSON.stringify(values) !== JSON.stringify(initialValues) return (
{(Object.keys(ROLE_PERMISSIONS) as (keyof typeof ROLE_PERMISSIONS)[]).map( (permission) => ( div>button]:opacity-30 [&>div>label]:text-foreground-lighter' : '', ].join(' ')} afterLabel={ !disabled && ROLE_PERMISSIONS[permission].disabled && ( This privilege cannot be updated via the dashboard ) } /> ) )}
{!disabled && (
)}
) }}
) }