File size: 4,660 Bytes
9e15897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useEffect, useState } from "react";
import { CREDENTIALS } from "../../apiContract";

type Evidence = { label: string; value: string };
type Draft = { status: "draft" | "fallback"; caseText: string; reason: string; evidence: Evidence[] };
type Load = { phase: "loading" } | { phase: "error"; message: string } | { phase: "ready"; value: Draft };
const text = (value: unknown) => typeof value === "string" ? value : "";
const record = (value: unknown): Record<string, unknown> =>
  value && typeof value === "object" ? value as Record<string, unknown> : {};

export default function CaseText({ itemId, url, onDenied }: {
  itemId: string; url: (path: string) => string; onDenied: () => void;
}) {
  const [state, setState] = useState<Load>({ phase: "loading" });
  const [attempt, setAttempt] = useState(0);
  const [copied, setCopied] = useState<"" | "done" | "failed">("");
  useEffect(() => {
    const controller = new AbortController();
    setState({ phase: "loading" });
    setCopied("");
    void fetch(url(`/items/${encodeURIComponent(itemId)}/case-text`),
      { credentials: CREDENTIALS, signal: controller.signal })
      .then(async (response) => {
        if ((response.status === 403 || response.status === 404) && !controller.signal.aborted) onDenied();
        const body = record(await response.json().catch(() => null));
        if (!response.ok) {
          const error = record(body.error ?? record(body.detail).error);
          throw new Error(text(error.message) || "Case text could not be loaded.");
        }
        if ((body.status !== "draft" && body.status !== "fallback") || !text(body.caseText)
          || !Array.isArray(body.evidence) || !body.evidence.every((row: unknown) =>
            text(record(row).label) && text(record(row).value))) {
          throw new Error("Case text could not be read.");
        }
        const value: Draft = { status: body.status, caseText: text(body.caseText), reason: text(body.reason),
          evidence: body.evidence.map((row: unknown) => ({ label: text(record(row).label), value: text(record(row).value) })) };
        if (!controller.signal.aborted) setState({ phase: "ready", value });
      }).catch((reason: unknown) => {
        if (!controller.signal.aborted) setState({ phase: "error",
          message: reason instanceof Error ? reason.message : "Case text could not be loaded." });
      });
    return () => controller.abort();
  }, [itemId, url, onDenied, attempt]);

  const copy = async () => {
    if (state.phase !== "ready") return;
    try {
      await navigator.clipboard.writeText(state.value.caseText);
      setCopied("done");
    } catch {
      setCopied("failed");
    }
  };
  return <section className="po-section" aria-label="Case text" data-proof="payouts-case-text"
    data-state={state.phase === "ready" ? state.value.status : state.phase} aria-busy={state.phase === "loading"}>
    <div className="po-toolbar">
      <h3 className="po-heading">Case text</h3>
      <button type="button" className="po-more" disabled={state.phase !== "ready"} onClick={() => void copy()}>Copy</button>
      {copied === "done" && <span className="po-note" role="status">Copied</span>}
    </div>
    {state.phase === "loading" && <div className="po-skeleton-rows" role="status" aria-label="Drafting case text">
      <div className="po-skeleton" /><div className="po-skeleton" /><div className="po-skeleton" />
    </div>}
    {state.phase === "error" && <p className="po-error" role="alert">{state.message}{" "}
      <button type="button" className="po-link" onClick={() => setAttempt((count) => count + 1)}>Retry</button></p>}
    {state.phase === "ready" && <>
      {state.value.status === "fallback" && <p className="po-note" role="status">Template</p>}
      {state.value.reason && <p className="po-note" role="status">{state.value.reason}</p>}
      <div className="po-section po-part-label" data-proof="payouts-case-copy">
        {state.value.caseText.split(/\n\s*\n/).map((paragraph, index) => <p className="po-note" key={index}>{paragraph}</p>)}
      </div>
      <section className="po-section" aria-label="Case evidence" data-proof="payouts-case-evidence">
        <h3 className="po-heading">Evidence</h3>
        <dl className="po-tie-rows">{state.value.evidence.map((row, index) =>
          <div className="po-field" key={index}>
            <dt className="po-field-label">{row.label}</dt>
            <dd className="po-note po-part-label">{row.value}</dd>
          </div>)}</dl>
      </section>
    </>}
    {copied === "failed" && <p className="po-error" role="alert">Copy did not work. Select the text to copy it.</p>}
  </section>;
}