All files / src/apps/user/pages/knowledge/repository RepositoryTrustCard.tsx

48.14% Statements 13/27
20.83% Branches 5/24
36.36% Functions 4/11
50% Lines 12/24

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                                          3x   3x       41x 41x 41x   13x     41x   11x     41x 41x 44x   41x                                                                                                                                                                                                
import { SectionCard, SkeletonLines, StatusBadge } from "@/components/detail";
import type { Knowledge, RepositoryTrust, TrustSignalSpec, TrustState } from "@/objects/Knowledge";
import { resourceHref } from "@/routes/resourceHref";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import { ShieldCheck, TriangleAlert } from "lucide-react";
import React from "react";
import { Link } from "react-router-dom";
 
import { repositoryTrustKey, TRUST_SIGNALS_KEY } from "../figures";
import { TRUST_STATE, TRUST_STATE_LABEL } from "../scales";
 
/**
 * Trust across a repository's sources (svc-core `GET
 * /repositories/{id}/trust`): each source against each signal, its state
 * on the trust scale and its sentence on hover, with how many sources stand
 * where under each column. States, never a score — the same signals a
 * source's own page explains in full.
 */
 
/** The states a column's count names, in the order a reader scans them. */
const COUNTED: TrustState[] = ["conflict", "unmet", "in_progress", "partial", "met", "unknown"];
 
const RepositoryTrustCard: React.FC<{ repositoryId: string; members: Knowledge[] }> = ({
  repositoryId,
  members,
}) => {
  const { getApiHandler } = useApiHandler();
  const api = getApiHandler();
  const { data, isLoading } = useQuery<RepositoryTrust | null>({
    queryKey: repositoryTrustKey(repositoryId),
    queryFn: () => api.repository_router.trust(repositoryId),
    staleTime: 60_000,
  });
  const { data: catalogueData } = useQuery<TrustSignalSpec[]>({
    queryKey: TRUST_SIGNALS_KEY,
    queryFn: () => api.knowledge_router.trustSignals(),
    staleTime: Infinity,
  });
  const trust: RepositoryTrust | null | undefined = data;
  const signals: TrustSignalSpec[] = catalogueData ?? [];
  const names = new Map(members.map((m) => [m.id, m.name]));
 
  return (
    <SectionCard
      icon={ShieldCheck}
      title="Trust across its sources"
      description="What is on record about relying on each — open a source for the facts"
    >
      {isLoading ? (
        <SkeletonLines count={4} />
      ) : !trust ? (
        <p className="text-sm italic text-gray-400 dark:text-gray-500">
          Its trust signals could not be read.
        </p>
      ) : trust.sources.length === 0 ? (
        <p className="text-sm text-gray-500 dark:text-gray-400">No sources in it yet.</p>
      ) : (
        <div className="overflow-x-auto">
          <table className="w-full min-w-[36rem] text-left text-xs">
            <thead className="text-[11px] text-gray-400 dark:text-gray-500">
              <tr>
                <th scope="col" className="py-1.5 pr-3 font-medium">Source</th>
                {signals.map((signal) => (
                  <th key={signal.key} scope="col" className="py-1.5 pr-3 font-medium" title={signal.description}>
                    {signal.label}
                  </th>
                ))}
              </tr>
            </thead>
            <tbody className="divide-y divide-gray-100 dark:divide-gray-700">
              {trust.sources.map((source) => {
                const byKey = new Map(source.signals.map((s) => [s.key, s]));
                const attention = new Set(source.attention);
                const href = resourceHref("knowledge", source.knowledge_id);
                const name = names.get(source.knowledge_id) ?? "A source";
                return (
                  <tr key={source.knowledge_id}>
                    <th scope="row" className="max-w-[14rem] py-2 pr-3 font-medium text-gray-700 dark:text-gray-200">
                      <span className="flex items-center gap-1.5">
                        {attention.size > 0 && (
                          <TriangleAlert
                            className="size-3.5 shrink-0 text-amber-600 dark:text-amber-400"
                            aria-label="Needs attention"
                          />
                        )}
                        {href ? (
                          <Link to={href} className="truncate hover:underline">
                            {name}
                          </Link>
                        ) : (
                          <span className="truncate">{name}</span>
                        )}
                      </span>
                    </th>
                    {signals.map((signal) => {
                      const result = byKey.get(signal.key);
                      return (
                        <td key={signal.key} className="py-2 pr-3" title={result?.summary}>
                          {result ? (
                            <StatusBadge
                              status={result.state}
                              scale={TRUST_STATE}
                              label={TRUST_STATE_LABEL[result.state] ?? result.state}
                            />
                          ) : (
                            <span className="text-gray-300 dark:text-gray-600">—</span>
                          )}
                        </td>
                      );
                    })}
                  </tr>
                );
              })}
            </tbody>
            <tfoot className="text-[11px] text-gray-500 dark:text-gray-400">
              <tr className="border-t border-gray-200 dark:border-gray-600">
                <th scope="row" className="py-1.5 pr-3 font-medium">Across its sources</th>
                {signals.map((signal) => {
                  const counts = trust.summary[signal.key] ?? {};
                  const parts = COUNTED.filter((state) => counts[state]).map(
                    (state) => `${counts[state]} ${(TRUST_STATE_LABEL[state] ?? state).toLowerCase()}`,
                  );
                  return (
                    <td key={signal.key} className="py-1.5 pr-3">
                      {parts.length ? parts.join(" · ") : "—"}
                    </td>
                  );
                })}
              </tr>
            </tfoot>
          </table>
        </div>
      )}
    </SectionCard>
  );
};
 
export default RepositoryTrustCard;