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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | 4x 165x 4x 165x 4x 4x 102x 27x 27x 27x 27x 27x 27x 4x 75x 75x 23x 75x 75x 75x 75x 75x 75x 75x 75x 75x 1x 1x 1x 1x 1x 1x 1x 75x 42x 50x 75x 38x 10x 10x 75x 42x 75x 52x 23x 9x 10x 7x 75x 10x 75x 75x 2x 2x 2x 2x 2x 75x 117x 75x 94x 75x 21x 90x 43x 6x | import { neighboursOf } from "@/components/dashboard/widgets/network/layout";
import { SectionCard, SkeletonLines } from "@/components/detail";
import type { KnowledgeStats, SourceMap as SourceMapData, SourceMapNode } from "@/objects/Knowledge";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { Info, Network } from "lucide-react";
import React, { useCallback, useMemo, useState } from "react";
import { repositoryMapKey } from "../figures";
import EvidencePanel from "./EvidencePanel";
import { forceLayout } from "./forceLayout";
import {
allKinds,
connectionRows,
edgeId,
layoutInput,
nodeHealth,
percent,
strengthWord,
visibleIn,
type MapFilters,
} from "./mapModel";
import SourceMapCanvas, { type MapSelection } from "./SourceMapCanvas";
import SourceMapLegend from "./SourceMapLegend";
/**
* A repository as a web of its sources (svc-core `GET
* /repositories/{id}/map`). Nobody draws a connection: CHAIN-R draws one
* where the record shows two sources belong together — the same documents,
* answers citing both, links between them, a shared site or publisher —
* and each says what it rests on. The workspace's sources the evidence ties
* to it are suggestions, added in one step by whoever may change it.
*
* The layout is computed once per map; the filters only hide, so nothing
* moves under the reader. The Connections table under the canvas is the
* same map for a screen reader, or a map too dense to read.
*/
const chip = (on: boolean) =>
on
? "border-gray-900 dark:border-gray-100 bg-gray-900 dark:bg-gray-100 text-white dark:text-gray-900"
: "border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700";
const Toggle: React.FC<{ on: boolean; onChange: (on: boolean) => void; children: React.ReactNode }> = ({
on,
onChange,
children,
}) => (
<button
type="button"
aria-pressed={on}
onClick={() => onChange(!on)}
className={`rounded-full border px-2.5 py-1 text-[11px] font-medium transition-colors ${chip(on)}`}
>
{children}
</button>
);
const plural = (n: number, one: string, many = `${one}s`) =>
`${n.toLocaleString()} ${n === 1 ? one : many}`;
/** What the map found, in a line: its connections and what to look at. */
function findings(map: SourceMapData): string[] {
const s = map.summary;
const parts = [plural(s.sources, "source"), plural(s.connections, "connection")];
if (s.duplicates) parts.push(`${plural(s.duplicates, "pair")} holding the same documents`);
if (s.isolated && s.sources > 1) parts.push(`${s.isolated} tied to no other`);
Eif (s.suggestions) parts.push(plural(s.suggestions, "suggestion"));
return parts;
}
export interface SourceMapProps {
repositoryId: string;
/** The members' full figures, for their health. */
figures?: Map<string, KnowledgeStats>;
/** Adds a suggested source; absent when the viewer may not change the
* repository, and then nothing offers to. */
onAdd?: (knowledgeId: string) => void;
adding?: boolean;
}
const SourceMap: React.FC<SourceMapProps> = ({ repositoryId, figures, onAdd, adding }) => {
const { getApiHandler } = useApiHandler();
const { data, isLoading } = useQuery<SourceMapData | null>({
queryKey: repositoryMapKey(repositoryId),
queryFn: () => getApiHandler().repository_router.map(repositoryId),
staleTime: 5 * 60_000,
});
const map: SourceMapData | null | undefined = data;
const queryClient = useQueryClient();
const [off, setOff] = useState<ReadonlySet<string>>(new Set());
const [agents, setAgents] = useState(true);
const [suggestions, setSuggestions] = useState(true);
const [strongOnly, setStrongOnly] = useState(false);
const [selection, setSelection] = useState<MapSelection | null>(null);
// A suggestion set aside stays aside, for everyone here, while what ties
// it to the repository stays the same; the map is read again without it.
const [settingAside, setSettingAside] = useState(false);
const setAside = useCallback(
async (node: SourceMapNode) => {
Iif (typeof node.meta.fingerprint !== "string") return;
setSettingAside(true);
try {
await getApiHandler().knowledge_router.dismissRecommendation({
kind: "map_suggestion",
subject: `${repositoryId}|${node.ref_id}`,
fingerprint: node.meta.fingerprint,
});
setSelection(null);
await queryClient.invalidateQueries({ queryKey: repositoryMapKey(repositoryId) });
} finally {
setSettingAside(false);
}
},
[getApiHandler, queryClient, repositoryId],
);
const filters: MapFilters = useMemo(
() => ({
kinds: new Set([...allKinds(map)].filter((k) => !off.has(k))),
agents,
suggestions,
strongOnly,
}),
[map, off, agents, suggestions, strongOnly],
);
// Once per map: a filter must never move what the reader is looking at.
const positions = useMemo(() => {
if (!map) return {};
const { nodes, links } = layoutInput(map);
return forceLayout(nodes, links);
}, [map]);
const visible = useMemo(
() => (map ? visibleIn(map, filters) : { nodes: new Set<string>(), edges: new Set<string>() }),
[map, filters],
);
const lit = useMemo(() => {
if (!map || !selection) return null;
const shown = map.edges.filter((e) => visible.edges.has(edgeId(e)));
if (selection.kind === "node") return neighboursOf(selection.id, shown);
const edge = shown.find((e) => edgeId(e) === selection.id);
return edge ? new Set([edge.source, edge.target]) : null;
}, [map, selection, visible]);
const healthOf = useCallback(
(node: SourceMapNode) => nodeHealth(node, node.kind === "source" ? figures?.get(node.ref_id) : null),
[figures],
);
const rows = useMemo(() => (map ? connectionRows(map, visible) : []), [map, visible]);
const toggleKind = (key: string) =>
setOff((was) => {
const next = new Set(was);
Iif (next.has(key)) next.delete(key);
else next.add(key);
return next;
});
const hasSuggestions = (map?.summary.suggestions ?? 0) > 0;
const hasAgents = (map?.nodes ?? []).some((n) => n.kind === "agent");
const unavailable = Object.entries(map?.summary.unavailable ?? {});
const kindLabel = new Map((map?.kinds ?? []).map((k) => [k.key, k.label]));
return (
<SectionCard
icon={Network}
title="Source map"
description="Its sources, connected by what is on record about them"
>
{isLoading ? (
<SkeletonLines count={6} />
) : !map ? (
<p className="text-sm italic text-gray-400 dark:text-gray-500">
The map could not be read.
</p>
) : map.summary.sources === 0 ? (
<p className="text-sm text-gray-500 dark:text-gray-400">
No sources in it yet, so there is nothing to map.
</p>
) : (
<div className="space-y-4">
<p className="text-xs text-gray-600 dark:text-gray-300">{findings(map).join(" · ")}</p>
{(unavailable.length > 0 || map.truncated) && (
<div
role="status"
className="flex gap-2 rounded-lg border border-amber-200 dark:border-amber-800 bg-amber-50/70 dark:bg-amber-950/30 px-3 py-2 text-xs text-amber-800 dark:text-amber-200"
>
<Info className="mt-0.5 size-3.5 shrink-0" aria-hidden="true" />
<div className="space-y-0.5">
{unavailable.length > 0 && (
<p>
Some evidence could not be read, so the map shows less than
there is:{" "}
{unavailable
.map(([key, why]) => `${kindLabel.get(key) ?? key} (${why})`)
.join(", ")}
.
</p>
)}
{map.truncated && (
<p>
Too much to draw at once: each source shows its strongest
connections, and the table lists what is drawn.
</p>
)}
</div>
</div>
)}
{map.summary.connections === 0 && !hasSuggestions && (
<p className="text-xs text-gray-500 dark:text-gray-400">
Nothing on record connects its sources yet. A connection appears
when two hold the same documents, answers cite both, one links to
the other's site, or they share a site or publisher.
</p>
)}
<div className="flex flex-wrap items-center gap-1.5" role="group" aria-label="Show connections by">
{map.kinds.map((kind) => (
<Toggle key={kind.key} on={!off.has(kind.key)} onChange={() => toggleKind(kind.key)}>
{kind.label}
</Toggle>
))}
<span className="mx-1 h-4 w-px bg-gray-200 dark:bg-gray-700" aria-hidden="true" />
<Toggle on={strongOnly} onChange={setStrongOnly}>
Strong only
</Toggle>
{hasSuggestions && (
<Toggle on={suggestions} onChange={setSuggestions}>
Suggestions
</Toggle>
)}
{hasAgents && (
<Toggle on={agents} onChange={setAgents}>
Agents
</Toggle>
)}
</div>
<div className="grid gap-4 lg:grid-cols-[minmax(0,1fr)_17rem]">
<SourceMapCanvas
map={map}
positions={positions}
visible={visible}
selection={selection}
lit={lit}
healthOf={healthOf}
onSelect={setSelection}
/>
<div className="rounded-lg border border-gray-100 dark:border-gray-700 p-3 lg:max-h-[28rem] lg:overflow-y-auto">
<EvidencePanel
map={map}
visible={visible}
selection={selection}
healthOf={healthOf}
onSelect={setSelection}
onAdd={onAdd}
adding={adding}
onSetAside={onAdd ? setAside : undefined}
settingAside={settingAside}
/>
</div>
</div>
<SourceMapLegend kinds={map.kinds} />
{rows.length > 0 && (
<div className="overflow-x-auto">
<table className="w-full text-left text-xs">
<caption className="pb-2 text-left text-[11px] font-semibold uppercase tracking-wide text-gray-400 dark:text-gray-500">
Connections
</caption>
<thead className="text-[11px] text-gray-400 dark:text-gray-500">
<tr>
<th scope="col" className="py-1 pr-3 font-medium">Between</th>
<th scope="col" className="py-1 pr-3 font-medium">Strength</th>
<th scope="col" className="py-1 pr-3 font-medium">Rests on</th>
<th scope="col" className="py-1 font-medium">
<span className="sr-only">Evidence</span>
</th>
</tr>
</thead>
<tbody className="divide-y divide-gray-100 dark:divide-gray-700">
{rows.map((row) => (
<tr
key={row.id}
className={selection?.id === row.id ? "bg-gray-50 dark:bg-gray-700/40" : undefined}
>
<td className="py-1.5 pr-3 text-gray-700 dark:text-gray-200">
{row.a.label} ↔ {row.b.label}
{row.suggestion && (
<span className="ml-1.5 rounded-full border border-dashed border-gray-300 dark:border-gray-600 px-1.5 text-[10px] text-gray-500 dark:text-gray-400">
suggestion
</span>
)}
</td>
<td className="whitespace-nowrap py-1.5 pr-3 tabular-nums text-gray-600 dark:text-gray-300">
{strengthWord(row.edge.weight)} · {percent(row.edge.weight)}
</td>
<td className="py-1.5 pr-3 text-gray-500 dark:text-gray-400">{row.kinds.join(", ")}</td>
<td className="py-1.5 text-right">
<button
type="button"
onClick={() => setSelection({ kind: "edge", id: row.id })}
className="font-medium text-blue-600 dark:text-blue-400 hover:underline"
aria-label={`Evidence for ${row.a.label} and ${row.b.label}`}
>
Evidence
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
)}
</SectionCard>
);
};
export default SourceMap;
|