1
0
Fork 0
polaris-task-force/src/components/frontend/profile/LeadershipEvaluationSection.tsx
Z8MB1E 977f841056 feat(evaluations): gate leadership ratings by rank instead of squad assignments
Eligibility now requires a strict rank gap plus a yes-RSVP from at least
one side of the pair on an evaluable mission. Junior rates senior writes
leader-direct (feeds the anonymous aggregate); senior rates junior writes
subordinate; leader-indirect stays readable for legacy rows only.

- New src/lib/evaluations/ranks.ts ladder helpers shared by all call sites
- Profile page: eligible missions union viewer+subject attendance, dialog
  hidden without a rank gap, subordinate line visible to higher ranks
- Bot and in-app reminder planners pair co-attendees by rank, not squads
- isDirectLeaderOf/isAnyLeader removed; no schema change or migration
2026-10-25 02:53:49 -04:00

264 lines
12 KiB
TypeScript

"use client";
import { BadgeCheckIcon, ClipboardCheckIcon } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import {
Item,
ItemActions,
ItemContent,
ItemDescription,
ItemGroup,
ItemMedia,
ItemTitle,
} from "@/components/ui/item";
import { Separator } from "@/components/ui/separator";
import { getLeadershipDisplayLabel, PERFORMANCE_LEVELS } from "@/lib/evaluations/levels";
import { LeadershipEvaluationDialog } from "./LeadershipEvaluationDialog";
import type {
EvaluationMissionOption,
EvaluationSummary,
LatestMissionRow,
} from "./evaluation-types";
interface LeadershipEvaluationSectionProps {
readonly username: string;
readonly summary: EvaluationSummary | null;
readonly summaryError: string | null;
readonly eligibleMissions: readonly EvaluationMissionOption[];
readonly latestMissionRows: readonly LatestMissionRow[];
readonly canSubmitEvaluation: boolean;
readonly canViewSubordinate: boolean;
}
const PERFORMANCE_GRADIENT = `linear-gradient(to right, ${[...PERFORMANCE_LEVELS]
.reverse()
.map((level) => level.color)
.join(", ")})`;
function formatDate(value: string): string {
return new Intl.DateTimeFormat("en-US", {
month: "short",
day: "numeric",
year: "numeric",
}).format(new Date(value));
}
function formatPercentage(value: number): string {
return `${value.toFixed(value % 1 === 0 ? 0 : 1)}%`;
}
export function LeadershipEvaluationSection({
username,
summary,
summaryError,
eligibleMissions,
latestMissionRows,
canSubmitEvaluation,
canViewSubordinate,
}: LeadershipEvaluationSectionProps) {
return (
<section className="flex flex-col gap-3" aria-labelledby="leadership-evaluation-heading">
<div className="flex items-center gap-2">
<BadgeCheckIcon className="size-4 text-muted-foreground" />
<h2
id="leadership-evaluation-heading"
className="text-sm font-semibold uppercase tracking-wider text-muted-foreground"
>
Leadership Evaluation
</h2>
</div>
<Card>
<CardHeader>
<CardTitle className="flex flex-wrap items-center justify-between gap-3 text-base">
<span>Leadership performance</span>
{canSubmitEvaluation && eligibleMissions.length > 0 && (
<LeadershipEvaluationDialog username={username} missions={eligibleMissions} />
)}
</CardTitle>
<CardDescription>
Anonymous ratings from unit members ranked above or below this operator.
</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-5">
{summaryError ? (
<p role="alert" className="text-sm text-destructive">
{summaryError}
</p>
) : summary ? (
<>
{summary.leader.visible &&
summary.leader.averageScore !== null &&
summary.leader.levels !== null ? (
<div className="flex flex-col gap-5">
<div className="flex flex-col gap-3 rounded-md border border-border bg-muted/30 p-4">
<div className="flex items-end justify-between gap-3">
<div className="flex flex-col gap-1">
<span className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">
Average rating
</span>
<span className="font-mono text-4xl font-semibold tracking-tight">
{Math.round(summary.leader.averageScore)}%
</span>
</div>
<span className="font-mono text-xs text-muted-foreground">
{summary.leader.totalRaters} responses
</span>
</div>
<div className="flex flex-col gap-1.5">
<div
role="img"
aria-label={`Average leadership rating: ${Math.round(summary.leader.averageScore)} percent`}
className="relative h-5"
>
<div
className="absolute inset-x-0 top-1/2 h-3 -translate-y-1/2 overflow-hidden rounded-full"
style={{ background: PERFORMANCE_GRADIENT }}
>
<span
aria-hidden="true"
className="absolute inset-0"
style={{
background: PERFORMANCE_GRADIENT,
filter: "grayscale(1)",
maskImage: `linear-gradient(to right, black 0%, transparent ${Math.min(100, Math.max(0, Math.round(summary.leader.averageScore)))}%, transparent ${Math.min(100, Math.max(0, Math.round(summary.leader.averageScore)))}%, black 100%)`,
WebkitMaskImage: `linear-gradient(to right, black 0%, transparent ${Math.min(100, Math.max(0, Math.round(summary.leader.averageScore)))}%, transparent ${Math.min(100, Math.max(0, Math.round(summary.leader.averageScore)))}%, black 100%)`,
}}
/>
</div>
<span
aria-hidden="true"
className="absolute inset-y-0 w-1.5 -translate-x-1/2 rounded-full bg-foreground ring-2 ring-background"
style={{
left: `${Math.min(100, Math.max(0, Math.round(summary.leader.averageScore)))}%`,
}}
/>
</div>
<div className="flex justify-between text-xs text-muted-foreground">
<span>Needs work</span>
<span>Exceptional</span>
</div>
</div>
</div>
<div className="flex flex-col gap-3">
<div className="flex items-center justify-between gap-3">
<h3 className="text-sm font-semibold">Level distribution</h3>
<span className="text-xs text-muted-foreground">All submitted ratings</span>
</div>
<div className="flex flex-col gap-2.5">
{PERFORMANCE_LEVELS.map((performanceLevel) => {
const levelSummary = summary.leader.levels?.find(
(level) => level.id === performanceLevel.id,
);
const percentage = levelSummary?.percentage ?? 0;
const count = levelSummary?.count ?? 0;
return (
<div key={performanceLevel.id} className="flex items-center gap-3">
<span className="w-28 shrink-0 text-xs text-muted-foreground">
{getLeadershipDisplayLabel(performanceLevel)}
</span>
<div className="h-2 min-w-0 flex-1 overflow-hidden rounded-full bg-muted">
<div
className="h-full rounded-full"
style={{
width: `${percentage}%`,
backgroundColor: performanceLevel.color,
}}
/>
</div>
<span className="w-20 shrink-0 text-right font-mono text-xs text-muted-foreground">
{formatPercentage(percentage)} · {count}
</span>
</div>
);
})}
</div>
</div>
{latestMissionRows.length > 0 && (
<div className="flex flex-col gap-3">
<div className="flex items-center gap-2">
<ClipboardCheckIcon className="size-4 text-muted-foreground" />
<h3 className="text-sm font-semibold">Latest mission ratings</h3>
</div>
<ItemGroup className="gap-2">
{latestMissionRows.map((mission) => (
<Item key={mission.missionId} variant="muted" size="sm">
<ItemMedia variant="icon">
<ClipboardCheckIcon />
</ItemMedia>
<ItemContent className="min-w-0">
<ItemTitle className="max-w-full truncate">
{mission.missionName}
</ItemTitle>
<ItemDescription>{formatDate(mission.startDateTime)}</ItemDescription>
</ItemContent>
<ItemActions className="ml-auto">
<Badge
variant="outline"
className="font-mono text-xs"
style={{ color: mission.level.color }}
>
{getLeadershipDisplayLabel(mission.level)}
</Badge>
</ItemActions>
</Item>
))}
</ItemGroup>
</div>
)}
</div>
) : (
<div className="flex flex-col gap-2 rounded-md border border-border bg-muted/30 p-4">
<span className="text-sm font-semibold">Confidential baseline</span>
<p className="text-sm text-muted-foreground">
Leadership ratings remain private until at least three distinct operators have
responded.
</p>
</div>
)}
{canViewSubordinate && summary.subordinate && (
<>
<Separator />
<div className="flex flex-col gap-2">
<div className="flex flex-wrap items-center justify-between gap-3">
<div className="flex flex-col gap-1">
<span className="text-sm font-semibold">Subordinate performance</span>
<span className="text-xs text-muted-foreground">Latest command review</span>
</div>
<Badge
variant="outline"
className="font-mono text-xs"
style={{ color: summary.subordinate.level.color }}
>
{summary.subordinate.level.label}
</Badge>
</div>
<span className="text-xs text-muted-foreground">
Updated {formatDate(summary.subordinate.at)}
</span>
</div>
</>
)}
</>
) : (
<p role="alert" className="text-sm text-muted-foreground">
Evaluation data is unavailable right now.
</p>
)}
{canSubmitEvaluation && eligibleMissions.length === 0 && (
<p className="text-xs text-muted-foreground">
No completed missions with a Yes RSVP are available to rate yet.
</p>
)}
</CardContent>
</Card>
</section>
);
}