All files / src/apps/user/pages/knowledge/map EvidencePanel.tsx

92.85% Statements 39/42
69.49% Branches 41/59
100% Functions 14/14
94.59% Lines 35/37

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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286                                                        4x 4x                                   4x             8x         2x                     1x                       4x 2x 2x                   4x       9x   11x 11x 11x                                       8x                                               4x                     90x 117x   27x 13x 9x 9x                                                               18x 27x   2x                                   2x 27x 27x 27x 27x                                                                     3x 3x       1x                                 4x 16x              
import { MeterBar, StatusBadge } from "@/components/detail";
import type { SourceMap, SourceMapKind, SourceMapNode } from "@/objects/Knowledge";
import { resourceHref } from "@/routes/resourceHref";
import { ArrowUpRight, EyeOff, ExternalLink, Loader2, Plus } from "lucide-react";
import React from "react";
import { Link } from "react-router-dom";
 
import { SOURCE_HEALTH, SOURCE_HEALTH_LABEL } from "../scales";
import { itemCount, sourceType } from "../sourceTypes";
import type { MapSelection } from "./SourceMapCanvas";
import {
  connectionRows,
  countPhrase,
  edgeId,
  percent,
  strengthWord,
  tiesOf,
  type ConnectionRow,
  type Visible,
} from "./mapModel";
 
/**
 * Why two sources are connected, or what one is tied to: beside the map,
 * and the same for a row of its Connections table. Every connection says
 * what it rests on — each kind's count, what it adds to the weight, and up
 * to three examples — so a line on the map is never an unexplained claim.
 */
 
const heading = "text-sm font-semibold text-gray-900 dark:text-gray-100";
const muted = "text-xs text-gray-500 dark:text-gray-400";
 
export interface EvidencePanelProps {
  map: SourceMap;
  visible: Visible;
  selection: MapSelection | null;
  healthOf: (node: SourceMapNode) => string;
  onSelect: (selection: MapSelection | null) => void;
  /** Adds a suggested source to the repository; absent when the viewer may
   *  not change it. */
  onAdd?: (knowledgeId: string) => void;
  adding?: boolean;
  /** Sets a suggestion aside while its evidence holds; absent when the
   *  viewer may not. */
  onSetAside?: (node: SourceMapNode) => void;
  settingAside?: boolean;
}
 
const AddButton: React.FC<{
  node: SourceMapNode;
  onAdd?: (knowledgeId: string) => void;
  adding?: boolean;
  onSetAside?: (node: SourceMapNode) => void;
  settingAside?: boolean;
}> = ({ node, onAdd, adding, onSetAside, settingAside }) =>
  onAdd || onSetAside ? (
    <div className="mt-3 flex flex-wrap items-center gap-2">
      {onAdd && (
        <button
          type="button"
          onClick={() => onAdd(node.ref_id)}
          disabled={adding}
          className="inline-flex items-center gap-1.5 rounded-lg bg-gray-900 dark:bg-white px-3 py-1.5 text-xs font-medium text-white dark:text-gray-900 hover:opacity-80 disabled:opacity-40"
        >
          {adding ? <Loader2 className="size-3.5 animate-spin" /> : <Plus className="size-3.5" />}
          Add to repository
        </button>
      )}
      {onSetAside && typeof node.meta.fingerprint === "string" && (
        <button
          type="button"
          onClick={() => onSetAside(node)}
          disabled={settingAside}
          title="Stop suggesting it while what ties it here stays the same"
          className="inline-flex items-center gap-1.5 rounded-lg border border-gray-200 dark:border-gray-600 px-3 py-1.5 text-xs font-medium text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700 disabled:opacity-40"
        >
          <EyeOff className="size-3.5" />
          Set aside
        </button>
      )}
    </div>
  ) : null;
 
const SourceLink: React.FC<{ node: SourceMapNode }> = ({ node }) => {
  const href = resourceHref(node.kind === "agent" ? "agent" : "knowledge", node.ref_id);
  return href ? (
    <Link
      to={href}
      className="inline-flex items-center gap-0.5 text-xs font-medium text-blue-600 dark:text-blue-400 hover:underline"
    >
      Open <ArrowUpRight className="size-3" />
    </Link>
  ) : null;
};
 
const Evidence: React.FC<{ row: ConnectionRow; kinds: Map<string, SourceMapKind> }> = ({
  row,
  kinds,
}) => (
  <ul className="mt-3 space-y-3">
    {row.edge.evidence.map((part) => {
      const kind = kinds.get(part.kind);
      const counted = countPhrase(part, kind);
      return (
        <li key={part.kind}>
          <div className="flex items-baseline justify-between gap-2">
            <span className="text-xs font-medium text-gray-800 dark:text-gray-100" title={kind?.description}>
              {kind?.label ?? part.kind}
            </span>
            <span className="shrink-0 text-[11px] tabular-nums text-gray-500 dark:text-gray-400">
              adds {percent(part.contribution)}
            </span>
          </div>
          <MeterBar
            share={kind?.strength ? part.contribution / kind.strength : part.contribution}
            tone="info"
            label={`${kind?.label ?? part.kind}: ${percent(part.score)} of its full weight`}
            className="mt-1"
          />
          {counted && <p className={`mt-1 ${muted}`}>{counted}</p>}
          {part.examples.length > 0 && (
            <ul className="mt-1 space-y-0.5">
              {part.examples.map((example, i) => (
                <li key={`${example.label}-${i}`} className="min-w-0 truncate text-[11px] text-gray-600 dark:text-gray-300">
                  {example.url ? (
                    <a
                      href={example.url}
                      target="_blank"
                      rel="noopener noreferrer"
                      className="inline-flex items-center gap-1 hover:underline"
                    >
                      <span className="truncate">{example.label}</span>
                      <ExternalLink className="size-3 shrink-0" aria-hidden="true" />
                    </a>
                  ) : (
                    example.label
                  )}
                </li>
              ))}
            </ul>
          )}
        </li>
      );
    })}
  </ul>
);
 
const EvidencePanel: React.FC<EvidencePanelProps> = ({
  map,
  visible,
  selection,
  healthOf,
  onSelect,
  onAdd,
  adding,
  onSetAside,
  settingAside,
}) => {
  const kinds = new Map(map.kinds.map((k) => [k.key, k]));
  const byId = new Map(map.nodes.map((n) => [n.id, n]));
 
  if (selection?.kind === "edge") {
    const row = connectionRows(map, visible).find((r) => r.id === selection.id);
    Iif (!row) return <Hint />;
    return (
      <div aria-live="polite">
        <p className={muted}>Connection</p>
        <h4 className={`${heading} mt-0.5`}>
          {row.a.label} <span className="text-gray-400">↔</span> {row.b.label}
        </h4>
        <p className={`mt-1 ${muted}`}>
          {strengthWord(row.edge.weight)} · {percent(row.edge.weight)} — its kinds
          of evidence together
        </p>
        <Evidence row={row} kinds={kinds} />
        {row.suggestion && (
          <div className="mt-4 border-t border-gray-100 dark:border-gray-700 pt-3">
            <p className={muted}>
              <span className="font-medium text-gray-700 dark:text-gray-200">
                {row.suggestion.label}
              </span>{" "}
              is not in this repository.
            </p>
            <AddButton
              node={row.suggestion}
              onAdd={onAdd}
              adding={adding}
              onSetAside={onSetAside}
              settingAside={settingAside}
            />
          </div>
        )}
      </div>
    );
  }
 
  const node = selection?.kind === "node" ? byId.get(selection.id) : undefined;
  if (!node) return <Hint />;
 
  Iif (node.kind === "agent") {
    const cited = typeof node.meta.cited === "number" ? node.meta.cited : 0;
    return (
      <div aria-live="polite">
        <p className={muted}>Agent granted this repository</p>
        <h4 className={`${heading} mt-0.5`}>{node.label}</h4>
        <p className={`mt-1 ${muted}`}>
          {cited
            ? `Cited its sources ${cited.toLocaleString()} ${cited === 1 ? "time" : "times"} in 90 days; each line is that source's share.`
            : "Has not cited its sources in 90 days."}
        </p>
        <div className="mt-2">
          <SourceLink node={node} />
        </div>
      </div>
    );
  }
 
  const type = String(node.meta.source_type ?? "");
  const health = healthOf(node);
  const items = typeof node.meta.items === "number" ? node.meta.items : 0;
  const ties = tiesOf(map, node.id, visible);
  return (
    <div aria-live="polite">
      <p className={muted}>
        {node.kind === "suggestion" ? "Suggested: not in this repository" : sourceType(type).label}
      </p>
      <div className="mt-0.5 flex items-center gap-2">
        <h4 className={`${heading} min-w-0 truncate`}>{node.label}</h4>
        <StatusBadge
          status={health}
          scale={SOURCE_HEALTH}
          label={SOURCE_HEALTH_LABEL[health] ?? health}
          className="shrink-0"
        />
      </div>
      <p className={`mt-1 ${muted}`}>{itemCount(type, items)}</p>
      <div className="mt-2">
        <SourceLink node={node} />
      </div>
      {node.kind === "suggestion" && (
        <AddButton
          node={node}
          onAdd={onAdd}
          adding={adding}
          onSetAside={onSetAside}
          settingAside={settingAside}
        />
      )}
      <h5 className="mt-4 text-[11px] font-semibold uppercase tracking-wide text-gray-400 dark:text-gray-500">
        Connected to
      </h5>
      {ties.length === 0 ? (
        <p className={`mt-1 ${muted}`}>Nothing on record ties it to another source here.</p>
      ) : (
        <ul className="mt-1 divide-y divide-gray-100 dark:divide-gray-700">
          {ties.map((row) => {
            const other = row.a.id === node.id ? row.b : row.a;
            return (
              <li key={row.id}>
                <button
                  type="button"
                  onClick={() => onSelect({ kind: "edge", id: edgeId(row.edge) })}
                  className="flex w-full items-center justify-between gap-2 py-1.5 text-left text-xs text-gray-700 dark:text-gray-200 hover:underline"
                >
                  <span className="min-w-0 truncate">{other.label}</span>
                  <span className="shrink-0 tabular-nums text-gray-500 dark:text-gray-400">
                    {strengthWord(row.edge.weight)} · {percent(row.edge.weight)}
                  </span>
                </button>
              </li>
            );
          })}
        </ul>
      )}
    </div>
  );
};
 
const Hint: React.FC = () => (
  <p className="text-xs italic text-gray-400 dark:text-gray-500">
    Select a connection to see the evidence behind it, or a source to see what
    it is tied to.
  </p>
);
 
export default EvidencePanel;