"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Loader2Icon, PencilIcon } from "lucide-react"; import { toast } from "sonner"; import { getLeadershipDisplayLabel, PERFORMANCE_LEVELS, isPerformanceLevelId, type PerformanceLevelId, } from "@/lib/evaluations/levels"; import type { RankDirection } from "@/lib/evaluations/ranks"; import { submitEvaluation } from "@/app/(frontend)/profile/[username]/actions"; import { Button } from "@/components/ui/button"; import { Combobox } from "@/components/ui/combobox"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import type { EvaluationMissionOption } from "./evaluation-types"; interface LeadershipEvaluationDialogProps { readonly username: string; readonly missions: readonly EvaluationMissionOption[]; /** Undefined for an equal-rank command review (chain link, no rank gap). */ readonly direction: RankDirection | undefined; /** True when the viewer is this mission's chain supervisor for the subject. */ readonly isCommandReview?: boolean; } function formatMissionDate(value: string): string { return new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric", year: "numeric", }).format(new Date(value)); } export function LeadershipEvaluationDialog({ username, missions, direction, isCommandReview = false, }: LeadershipEvaluationDialogProps) { const router = useRouter(); const isUpward = direction === "upward"; const triggerLabel = isCommandReview ? "Command review" : isUpward ? "Rate leadership" : "Rate subordinate"; const [open, setOpen] = useState(false); const [missionId, setMissionId] = useState(""); const [level, setLevel] = useState(""); const [comment, setComment] = useState(""); const [pending, setPending] = useState(false); const [error, setError] = useState(null); function reset() { setMissionId(""); setLevel(""); setComment(""); setPending(false); setError(null); } async function handleSubmit(event: React.FormEvent) { event.preventDefault(); const selectedMission = missions.find((mission) => String(mission.id) === missionId); if (!selectedMission) { setError("Select a mission to rate."); return; } if (!isPerformanceLevelId(level)) { setError("Select a performance level."); return; } setPending(true); setError(null); try { const trimmedComment = comment.trim(); const result = await submitEvaluation({ username, missionId: selectedMission.id, level, ...(trimmedComment ? { comment: trimmedComment } : {}), }); if (!result.success) { setError(result.error ?? "Could not save the rating."); return; } setOpen(false); reset(); toast.success(isUpward ? "Leadership rating saved" : "Subordinate rating saved"); router.refresh(); } catch (caught) { setError(caught instanceof Error ? caught.message : "Could not save the rating."); } finally { setPending(false); } } return ( { if (!nextOpen && pending) return; setOpen(nextOpen); if (!nextOpen) reset(); }} > {triggerLabel} {isCommandReview ? "You were in command of this operator for this mission. Your rating is recorded as a command review." : isUpward ? "Rate this senior operator for one completed mission you both attended. Your rating is included anonymously in the leadership aggregate." : "Rate this junior operator for one completed mission you both attended. Your rating is included anonymously in the leadership aggregate. If you are their direct supervisor, it is recorded as a command review instead."}
({ value: String(mission.id), label: `${mission.name} · ${formatMissionDate(mission.startDateTime)}`, }))} />