"use client"; import { useState } from "react"; import { toast } from "sonner"; import { Badge } from "@/components/ui/badge"; import { DropdownMenu, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { updateUserRole } from "@/lib/actions/admin"; import { Role } from "@/lib/generated/prisma/enums"; const ROLE_BADGE_VARIANT: Record = { ADMIN: "default", USER: "secondary", PENDING: "outline", }; const ROLE_LABEL: Record = { ADMIN: "Administrator", USER: "User", PENDING: "Pending", }; /** The role badge doubles as the trigger -- clicking it opens a menu to * change the user's role in place. */ export function UserRoleMenu({ userId, role }: { userId: string; role: Role }) { const [current, setCurrent] = useState(role); const [pending, setPending] = useState(false); async function handleChange(next: string) { if (next === current || pending) return; const prev = current; setCurrent(next as Role); setPending(true); const result = await updateUserRole(userId, next); setPending(false); if (result?.error) { setCurrent(prev); toast.error(result.error); } } return ( {ROLE_LABEL[current]} } /> {Object.values(Role).map((value) => ( {ROLE_LABEL[value]} ))} ); }