All files / src/components/decisions VerdictModal.tsx

85.71% Statements 12/14
66.66% Branches 10/15
60% Functions 3/5
85.71% Lines 12/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                      15x   15x                       15x                 2x 2x 2x 2x 2x 2x   2x                       1x                                                     1x                                          
import { SegmentedFilter } from "@/components/collection";
import { Modal, ModalActions } from "@/components/detail";
import {
  DecisionVerdict,
  OutcomeCreate,
  VERDICT_LABELS,
} from "@/objects/Decision";
import { Gavel } from "lucide-react";
import React, { useState } from "react";
 
const FIELD =
  "mt-1 w-full rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 px-3 py-2 text-sm text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-1 focus:ring-gray-400";
 
const OPTIONS: { value: DecisionVerdict; label: string }[] = [
  { value: "accepted", label: VERDICT_LABELS.accepted },
  { value: "edited", label: VERDICT_LABELS.edited },
  { value: "rejected", label: VERDICT_LABELS.rejected },
];
 
/**
 * Give an answer a verdict, with what goes with it: the corrected answer
 * for a correction (starting from the answer as it stands), and an optional
 * business-outcome label and note. The chat opens it on "Correct"; the
 * decision record opens it for any verdict.
 */
export const VerdictModal: React.FC<{
  initial: DecisionVerdict;
  /** The answer as it stands: where a correction starts. */
  answer: string;
  busy?: boolean;
  error?: string | null;
  onSubmit: (body: OutcomeCreate) => void;
  onClose: () => void;
}> = ({ initial, answer, busy = false, error, onSubmit, onClose }) => {
  const [verdict, setVerdict] = useState<DecisionVerdict>(initial);
  const [corrected, setCorrected] = useState(answer);
  const [label, setLabel] = useState("");
  const [note, setNote] = useState("");
  const editing = verdict === "edited";
  const missing = editing && !corrected.trim();
 
  return (
    <Modal
      title="Give a verdict"
      icon={Gavel}
      size="lg"
      busy={busy}
      onClose={onClose}
      description="Was this answer right? A correction keeps what it should have said, so it can become an evaluation case."
      footer={
        <ModalActions
          onCancel={onClose}
          onConfirm={() =>
            onSubmit({
              verdict,
              ...(editing ? { corrected_text: corrected } : {}),
              ...(label.trim() ? { label: label.trim() } : {}),
              ...(note.trim() ? { note: note.trim() } : {}),
            })
          }
          confirmLabel="Record verdict"
          loadingLabel="Recording…"
          loading={busy}
          disabled={missing}
        >
          {error && <span className="text-xs text-red-600 dark:text-red-400">{error}</span>}
        </ModalActions>
      }
    >
      <div className="space-y-4">
        <SegmentedFilter<DecisionVerdict>
          value={verdict}
          onChange={setVerdict}
          options={OPTIONS}
        />
        {editing && (
          <label className="block">
            <span className="text-xs text-gray-500 dark:text-gray-400">
              What the answer should have said
            </span>
            <textarea rows={8} value={corrected} onChange={(e) => setCorrected(e.target.value)} className={FIELD} />
          </label>
        )}
        <label className="block">
          <span className="text-xs text-gray-500 dark:text-gray-400">
            Business outcome (optional), e.g. “refund issued”
          </span>
          <input value={label} maxLength={120} onChange={(e) => setLabel(e.target.value)} className={FIELD} />
        </label>
        <label className="block">
          <span className="text-xs text-gray-500 dark:text-gray-400">
            Note (optional){verdict === "rejected" ? " — what was wrong with it" : ""}
          </span>
          <textarea rows={3} value={note} onChange={(e) => setNote(e.target.value)} className={FIELD} />
        </label>
      </div>
    </Modal>
  );
};
 
export default VerdictModal;