All files / src/components/document DocumentCard.tsx

94.73% Statements 18/19
93.02% Branches 40/43
100% Functions 6/6
100% Lines 17/17

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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                                                              23x   23x         25x 25x 25x   25x 25x 25x   25x 3x 3x 3x 3x     25x                                                                               30x                                                                             6x                                     3x                                              
import { EVIDENCE_CONFIDENCE, StatusBadge } from "@/components/detail";
import { DocumentSpec, documentImages } from "@/objects/Document";
import {
  ChevronDown,
  Download,
  FileText,
  Image as ImageIcon,
  ListChecks,
  Loader2,
  Quote,
} from "lucide-react";
import React, { useState } from "react";
import { useDocumentDownload } from "./DocumentDownload";
 
/**
 * A structured document in a reply or a run: what it says at a glance, the
 * full text on request, and a PDF download.
 *
 * The card shows the document as svc-ai validated it. The PDF is rendered
 * server-side from the stored record, so the file a user downloads is the
 * record, not this browser's copy of it.
 */
export interface DocumentCardProps {
  document: DocumentSpec;
  /** Position among the documents in the same content, as svc-core counts. */
  index: number;
  /** Renders the Markdown body when expanded — `RichMarkdown`, passed in so
   *  this component does not import the renderer that draws it. */
  renderMarkdown?: (markdown: string) => React.ReactNode;
}
 
const MAX_FINDINGS = 3;
 
export const DocumentCard: React.FC<DocumentCardProps> = ({
  document,
  index,
  renderMarkdown,
}) => {
  const download = useDocumentDownload();
  const [state, setState] = useState<"idle" | "busy" | "failed">("idle");
  const [expanded, setExpanded] = useState(false);
 
  const findings = document.key_findings ?? [];
  const sources = document.sources ?? [];
  const images = documentImages(document);
 
  const onDownload = async () => {
    Iif (!download) return;
    setState("busy");
    const ok = await download(index, document);
    setState(ok ? "idle" : "failed");
  };
 
  return (
    <div
      data-testid="document-card"
      className="not-prose my-3 rounded-xl border border-violet-200 dark:border-violet-800/70 bg-white dark:bg-gray-800 shadow-sm overflow-hidden"
    >
      <div className="flex items-start gap-3 px-4 pt-4">
        <div className="p-2 rounded-lg bg-violet-100 dark:bg-violet-900/30 shrink-0">
          <FileText className="size-5 text-violet-600 dark:text-violet-400" />
        </div>
        <div className="min-w-0 flex-1">
          <div className="flex items-start justify-between gap-2">
            <h4 className="text-sm font-semibold text-gray-900 dark:text-gray-100 leading-snug">
              {document.title}
            </h4>
            {document.confidence && (
              <StatusBadge
                status={document.confidence}
                scale={EVIDENCE_CONFIDENCE}
                label={`${document.confidence} confidence`}
                className="shrink-0"
              />
            )}
          </div>
          {document.subtitle && (
            <p className="text-xs text-gray-500 dark:text-gray-400 mt-0.5">
              {document.subtitle}
            </p>
          )}
        </div>
      </div>
 
      <div className="px-4 py-3 space-y-2.5">
        {document.summary && (
          <p className="text-sm text-gray-700 dark:text-gray-300 leading-relaxed">
            {document.summary}
          </p>
        )}
        {findings.length > 0 && (
          <ul className="space-y-1">
            {findings.slice(0, MAX_FINDINGS).map((finding, i) => (
              <li
                key={i}
                className="flex gap-2 text-xs text-gray-600 dark:text-gray-400"
              >
                <ListChecks className="size-3.5 shrink-0 mt-0.5 text-violet-500" />
                <span>{finding}</span>
              </li>
            ))}
            {findings.length > MAX_FINDINGS && (
              <li className="text-xs text-gray-400 dark:text-gray-500 pl-5">
                +{findings.length - MAX_FINDINGS} more in the document
              </li>
            )}
          </ul>
        )}
        {expanded && document.body && renderMarkdown && (
          <div className="pt-2 border-t border-gray-100 dark:border-gray-700">
            {renderMarkdown(document.body)}
          </div>
        )}
      </div>
 
      <div className="flex flex-wrap items-center justify-between gap-2 px-4 py-2.5 border-t border-gray-100 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/60">
        <div className="flex items-center gap-3 text-xs text-gray-400 dark:text-gray-500">
          {sources.length > 0 && (
            <span className="flex items-center gap-1">
              <Quote className="size-3" />
              {sources.length} source{sources.length !== 1 ? "s" : ""}
            </span>
          )}
          {images.length > 0 && (
            <span className="flex items-center gap-1">
              <ImageIcon className="size-3" />
              {images.length} image{images.length !== 1 ? "s" : ""}
            </span>
          )}
          {document.body && renderMarkdown && (
            <button
              type="button"
              onClick={() => setExpanded((v) => !v)}
              className="flex items-center gap-1 font-medium hover:text-gray-700 dark:hover:text-gray-200 transition-colors"
            >
              <ChevronDown
                className={`size-3 transition-transform ${expanded ? "rotate-180" : ""}`}
              />
              {expanded ? "Hide full text" : "Read full text"}
            </button>
          )}
        </div>
        {download ? (
          <div className="flex items-center gap-2">
            {state === "failed" && (
              <span role="alert" className="text-xs text-red-500">
                Download failed — try again.
              </span>
            )}
            <button
              type="button"
              onClick={() => void onDownload()}
              disabled={state === "busy"}
              className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-gray-900 dark:bg-white text-white dark:text-gray-900 text-xs font-medium hover:opacity-80 transition-opacity disabled:opacity-50"
            >
              {state === "busy" ? (
                <Loader2 className="size-3.5 animate-spin" />
              ) : (
                <Download className="size-3.5" />
              )}
              Download PDF
            </button>
          </div>
        ) : (
          <span className="text-xs text-gray-400 dark:text-gray-500 italic">
            The PDF is available once the reply is saved.
          </span>
        )}
      </div>
    </div>
  );
};
 
export default DocumentCard;