1
0
Fork 0
polaris-task-force/src/components/frontend/profile/LeadershipEvaluationDialog.tsx
Z8MB1E 79f790e02b feat(evaluations): allow equal-rank command-link reviews via mission chain
Resolve each evaluation mission against the command chain so a rater can rate a
ratee at equal rank when a supervisor->subordinate command link exists. Partition
graded docs into command vs spectrum buckets and surface command-link missions on
profiles that lack a strict rank gap.
2026-11-02 10:00:02 -05:00

235 lines
7.6 KiB
TypeScript

"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<PerformanceLevelId | "">("");
const [comment, setComment] = useState("");
const [pending, setPending] = useState(false);
const [error, setError] = useState<string | null>(null);
function reset() {
setMissionId("");
setLevel("");
setComment("");
setPending(false);
setError(null);
}
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
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 (
<Dialog
open={open}
onOpenChange={(nextOpen) => {
if (!nextOpen && pending) return;
setOpen(nextOpen);
if (!nextOpen) reset();
}}
>
<DialogTrigger asChild>
<Button type="button" size="sm" variant="outline">
<PencilIcon data-icon="inline-start" />
{triggerLabel}
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>{triggerLabel}</DialogTitle>
<DialogDescription>
{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."}
</DialogDescription>
</DialogHeader>
<form className="flex flex-col gap-4" onSubmit={handleSubmit} aria-busy={pending}>
<div className="flex flex-col gap-1.5">
<Label htmlFor="evaluation-mission">Mission</Label>
<Combobox
id="evaluation-mission"
aria-invalid={error !== null}
value={missionId}
onValueChange={setMissionId}
disabled={pending}
placeholder="Select a completed mission"
searchPlaceholder="Search missions..."
emptyMessage="No completed missions to evaluate."
options={missions.map((mission) => ({
value: String(mission.id),
label: `${mission.name} · ${formatMissionDate(mission.startDateTime)}`,
}))}
/>
</div>
<div className="flex flex-col gap-1.5">
<Label htmlFor="evaluation-level">Performance level</Label>
<Select
value={level}
onValueChange={(value) => {
if (isPerformanceLevelId(value)) setLevel(value);
}}
disabled={pending}
>
<SelectTrigger id="evaluation-level" aria-invalid={error !== null}>
<SelectValue placeholder="Select a level" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
{PERFORMANCE_LEVELS.map((performanceLevel) => (
<SelectItem
key={performanceLevel.id}
value={performanceLevel.id}
style={{ color: performanceLevel.color }}
>
{getLeadershipDisplayLabel(performanceLevel)} · {performanceLevel.score}
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
</div>
<div className="flex flex-col gap-1.5">
<Label htmlFor="evaluation-comment">Comment (optional)</Label>
<Textarea
id="evaluation-comment"
value={comment}
onChange={(event) => setComment(event.target.value)}
placeholder="Add context for the leadership team."
maxLength={1000}
rows={4}
disabled={pending}
/>
</div>
{error && (
<p role="alert" className="text-sm text-destructive">
{error}
</p>
)}
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => setOpen(false)}
disabled={pending}
>
Cancel
</Button>
<Button type="submit" disabled={pending}>
{pending && <Loader2Icon data-icon="inline-start" className="animate-spin" />}
{pending ? "Saving..." : "Save rating"}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}