"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { promoteMember } from "@/app/(frontend)/profile/[username]/actions"; import { ArrowUpIcon, Loader2Icon, MedalIcon } from "lucide-react"; import { toast } from "sonner"; interface PromoteButtonProps { username: string; currentRankName: string; targetRanks: readonly { id: number; name: string }[]; } /** * Profile-page section that lets a viewer with promotion authority promote the * profile owner. Authority and the allowed target ranks are computed server-side * by the page; this component only provides the entry point and submits the * chosen rank id. */ export function PromoteButton({ username, currentRankName, targetRanks }: PromoteButtonProps) { const [open, setOpen] = useState(false); const [rankId, setRankId] = useState(null); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const router = useRouter(); const selectedRankId = rankId ?? targetRanks[0]?.id ?? null; const selectedRank = targetRanks.find((r) => r.id === selectedRankId); async function handlePromote() { if (selectedRankId == null || submitting) return; setSubmitting(true); setError(null); const res = await promoteMember(username, selectedRankId); if (res.success) { toast.success("Promotion applied", { description: `${username} promoted to ${selectedRank?.name ?? "new rank"}.`, }); setOpen(false); router.refresh(); } else { setError(res.error ?? "Failed to promote member."); setSubmitting(false); } } return (

Promotion

{ if (!submitting) setOpen(o); }} > Promote {username} Current rank: {currentRankName}. Select the new rank, then confirm.
{error &&

{error}

}
); }