All files / src/components/decisions VerdictBar.tsx

92% Statements 23/25
80% Branches 12/15
90% Functions 9/10
92% Lines 23/25

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                              14x               14x           83x 83x 83x 83x 83x     83x 24x 24x     83x   3x   3x 3x 3x 3x           83x       83x                   1x                     1x                 1x                 1x                               1x                        
import { DECISION_VERDICT, StatusBadge } from "@/components/detail";
import {
  DecisionVerdict,
  OutcomeCreate,
  VERDICT_LABELS,
} from "@/objects/Decision";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { errorDetail } from "@/util/apiError";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { Check, PencilLine, X } from "lucide-react";
import React, { useEffect, useState } from "react";
import VerdictModal from "./VerdictModal";
 
const BUTTON =
  "inline-flex items-center gap-1 px-2 py-1 rounded-md text-[11px] font-medium text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors disabled:opacity-50";
 
/**
 * Under an answer: was it right? Accept and Reject are one click — a verdict
 * has to cost less than ignoring the answer, or nobody gives one — and
 * Correct opens the answer to say what it should have been. Once given, the
 * verdict shows, and can be changed (a new verdict; the record keeps both).
 */
export const VerdictBar: React.FC<{
  invocationId: string;
  /** The answer as it stands: where a correction starts. */
  answer: string;
  verdict?: DecisionVerdict | null;
}> = ({ invocationId, answer, verdict: given }) => {
  const { getApiHandler } = useApiHandler();
  const queryClient = useQueryClient();
  const [verdict, setVerdict] = useState<DecisionVerdict | null>(given ?? null);
  const [changing, setChanging] = useState(false);
  const [correcting, setCorrecting] = useState(false);
  // The answer's verdict as stored follows the answer: another answer, or
  // this one read back after a verdict given elsewhere, shows its own.
  useEffect(() => {
    setVerdict(given ?? null);
    setChanging(false);
  }, [invocationId, given]);
 
  const record = useMutation({
    mutationFn: (body: OutcomeCreate) =>
      getApiHandler().decision_router.recordOutcome(invocationId, body),
    onSuccess: (row) => {
      setVerdict(row.verdict);
      setChanging(false);
      setCorrecting(false);
      void queryClient.invalidateQueries({
        queryKey: [QUERY_KEYS.GET.DECISION],
      });
    },
  });
 
  const error = record.isError
    ? errorDetail(record.error, "Could not record that verdict.")
    : null;
 
  return (
    <div className="mt-1.5 flex items-center gap-1 flex-wrap text-[11px] text-gray-400 dark:text-gray-500">
      {verdict && !changing ? (
        <>
          <StatusBadge
            status={verdict}
            scale={DECISION_VERDICT}
            label={VERDICT_LABELS[verdict]}
            size="xs"
          />
          <button type="button" className={BUTTON} onClick={() => setChanging(true)}>
            Change
          </button>
        </>
      ) : (
        <>
          <span className="mr-1">Was this right?</span>
          <button
            type="button"
            className={BUTTON}
            disabled={record.isPending}
            onClick={() => record.mutate({ verdict: "accepted" })}
          >
            <Check className="size-3" />
            Accept
          </button>
          <button
            type="button"
            className={BUTTON}
            disabled={record.isPending}
            onClick={() => setCorrecting(true)}
          >
            <PencilLine className="size-3" />
            Correct
          </button>
          <button
            type="button"
            className={BUTTON}
            disabled={record.isPending}
            onClick={() => record.mutate({ verdict: "rejected" })}
          >
            <X className="size-3" />
            Reject
          </button>
        </>
      )}
      {error && !correcting && (
        <span className="text-red-600 dark:text-red-400">{error}</span>
      )}
      {correcting && (
        <VerdictModal
          initial="edited"
          answer={answer}
          busy={record.isPending}
          error={error}
          onSubmit={(body) => record.mutate(body)}
          onClose={() => {
            setCorrecting(false);
            record.reset();
          }}
        />
      )}
    </div>
  );
};
 
export default VerdictBar;