import { useState } from "react"; import { Button } from "../ui/Button"; import { Card, CardBody, CardHeader } from "../ui/Card"; import { Field, Textarea } from "../ui/Field"; import { maxLength, minLength, required } from "../../lib/validation"; import type { Dcr, Role } from "../../types"; export type ReviewAction = "return" | "approve"; /** Decision box for the manager whose turn it is. Renders nothing for anyone else. */ export function ReviewPanel({ dcr, role, busy, onDecide }: { dcr: Dcr; role: Role; busy: boolean; onDecide: (action: ReviewAction, comment: string) => Promise; }) { const [comment, setComment] = useState(""); const [problem, setProblem] = useState(null); // The Area Manager is the final approver: approve and lock, or return for correction. const canDecide = role === "AM" && dcr.status === "PENDING_AM"; if (!canDecide) return null; const check = (action: ReviewAction, text: string): string | null => maxLength(2000, "Comment")(text) ?? (action === "return" ? required("A reason")(text) ?? minLength(10, "The reason")(text) : null); const decide = async (action: ReviewAction) => { const message = check(action, comment); setProblem(message); if (message) return; if (await onDecide(action, comment.trim())) setComment(""); }; return (