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 | 3x 4x 2x 2x 9x 3x 3x 11x 11x 7x 9x 9x 7x 7x 2x 2x 2x 2x 11x 1x 1x 11x 66x 66x 66x 1x 14x 14x 14x 14x 14x 1x | import { SparkBars } from "@/components/charts";
import { MeterBar, SectionCard, SkeletonLines, type MeterTone } from "@/components/detail";
import type { Knowledge, KnowledgeStats } from "@/objects/Knowledge";
import { resourceHref } from "@/routes/resourceHref";
import { ArrowDown, ArrowUp, BookCopy, X } from "lucide-react";
import React, { useMemo, useState } from "react";
import { Link } from "react-router-dom";
import { AddSourceMenu } from "../AddSourceMenu";
import { HealthBadge } from "../cardParts";
import { attentionReasons, citationTitles, formatAgo, indexedShare, sourceHealth } from "../figures";
import { SOURCE_TYPE_PILL, itemCount, sourceType } from "../sourceTypes";
/**
* A repository's sources as a table (svc-core `GET
* /repositories/{id}/stats`, each member's figures): what each holds, how
* much of it is searchable, how it stands, how current it is and how often
* answers cited it — sortable by any of them. Where the repository is the
* viewer's to change, sources are added here and removed from their row
* (each a version of the repository).
*/
type SortKey = "name" | "type" | "items" | "indexed" | "updated" | "cited";
interface Row {
knowledge: Knowledge;
stats: KnowledgeStats | null;
share: number | null;
}
const value = (row: Row, key: SortKey): number | string => {
switch (key) {
case "name":
return row.knowledge.name.toLowerCase();
case "type":
return sourceType(row.knowledge.source_type).label;
case "items":
return row.stats?.items ?? 0;
case "indexed":
return row.share ?? -1;
case "updated":
return row.stats?.last_updated_at ?? "";
case "cited":
return row.stats?.citations ?? 0;
}
};
/** A searchable share's meaning: all of it, some still in hand, or some
* that could not be indexed. */
const indexedTone = (stats: KnowledgeStats | null, share: number | null): MeterTone =>
(stats?.documents.by_state.failed ?? 0) > 0 ? "warning" : share === 1 ? "ok" : "info";
const COLUMNS: { key: SortKey; label: string; numeric?: boolean }[] = [
{ key: "name", label: "Source" },
{ key: "type", label: "Type" },
{ key: "items", label: "Holds", numeric: true },
{ key: "indexed", label: "Indexed", numeric: true },
{ key: "updated", label: "Updated" },
{ key: "cited", label: "Cited · 30 days", numeric: true },
];
export interface RepositorySourcesProps {
members: Knowledge[];
loading: boolean;
figures: Map<string, KnowledgeStats>;
/** What may join it here; absent when the viewer may not change it. */
available?: Knowledge[];
onAdd?: (knowledgeId: string) => void;
onRemove?: (knowledgeId: string) => void;
removing?: boolean;
}
const RepositorySources: React.FC<RepositorySourcesProps> = ({
members,
loading,
figures,
available,
onAdd,
onRemove,
removing,
}) => {
const [sort, setSort] = useState<{ key: SortKey; descending: boolean }>({
key: "cited",
descending: true,
});
const rows = useMemo(() => {
const built: Row[] = members.map((knowledge) => {
const stats = figures.get(knowledge.id) ?? null;
return {
knowledge,
stats,
share: stats ? indexedShare(stats.documents.by_state.indexed ?? 0, stats.documents.total) : null,
};
});
const sign = sort.descending ? -1 : 1;
return built.sort((a, b) => {
const x = value(a, sort.key);
const y = value(b, sort.key);
const order = x < y ? -1 : x > y ? 1 : 0;
return order * sign || a.knowledge.name.localeCompare(b.knowledge.name);
});
}, [members, figures, sort]);
const sortBy = (key: SortKey) =>
setSort((was) =>
was.key === key
? { key, descending: !was.descending }
: { key, descending: key !== "name" && key !== "type" },
);
return (
<SectionCard
icon={BookCopy}
title={`Sources${members.length ? ` (${members.length})` : ""}`}
description="Everything an agent granted it can retrieve from"
action={
onAdd && available ? <AddSourceMenu available={available} onAdd={onAdd} /> : undefined
}
>
{loading ? (
<SkeletonLines count={4} />
) : members.length === 0 ? (
<div className="flex flex-col items-center justify-center py-8 text-center">
<p className="text-sm font-medium text-gray-400 dark:text-gray-500">
No sources in this repository
</p>
{onAdd && (
<p className="mt-1 text-xs text-gray-400 dark:text-gray-500">
Add file, graph, web, database, or API sources to build the collection.
</p>
)}
</div>
) : (
<div className="overflow-x-auto">
<table className="w-full min-w-[44rem] text-left text-xs">
<thead className="text-[11px] text-gray-400 dark:text-gray-500">
<tr>
{COLUMNS.map((column) => {
const active = sort.key === column.key;
const Arrow = sort.descending ? ArrowDown : ArrowUp;
return (
<th
key={column.key}
scope="col"
aria-sort={active ? (sort.descending ? "descending" : "ascending") : "none"}
className={`py-1.5 pr-3 font-medium ${column.numeric ? "text-right" : ""}`}
>
<button
type="button"
onClick={() => sortBy(column.key)}
className={`inline-flex items-center gap-1 hover:text-gray-700 dark:hover:text-gray-200 ${
active ? "text-gray-700 dark:text-gray-200" : ""
}`}
>
{column.label}
{active && <Arrow className="size-3" aria-hidden="true" />}
</button>
</th>
);
})}
<th scope="col" className="py-1.5 font-medium">
<span className="sr-only">Health</span>
</th>
{onRemove && (
<th scope="col" className="py-1.5">
<span className="sr-only">Remove</span>
</th>
)}
</tr>
</thead>
<tbody className="divide-y divide-gray-100 dark:divide-gray-700">
{rows.map(({ knowledge, stats, share }) => {
const meta = sourceType(knowledge.source_type);
const Icon = meta.Icon;
const href = resourceHref("knowledge", knowledge.id);
const indexable = knowledge.source_type === "file" || knowledge.source_type === "web";
return (
<tr key={knowledge.id} className="align-middle">
<td className="max-w-[16rem] py-2 pr-3">
<Link
to={href ?? "#"}
className="flex min-w-0 items-center gap-2 font-medium text-gray-800 dark:text-gray-100 hover:underline"
>
<Icon className="size-3.5 shrink-0 text-gray-400 dark:text-gray-500" aria-hidden="true" />
<span className="truncate">{knowledge.name}</span>
</Link>
{knowledge.description && (
<p className="mt-0.5 truncate pl-5 text-[11px] text-gray-400 dark:text-gray-500">
{knowledge.description}
</p>
)}
</td>
<td className="py-2 pr-3">
<span className={SOURCE_TYPE_PILL}>{meta.label}</span>
</td>
<td className="whitespace-nowrap py-2 pr-3 text-right tabular-nums text-gray-600 dark:text-gray-300">
{stats ? itemCount(knowledge.source_type, stats.items) : "—"}
</td>
<td className="py-2 pr-3 text-right">
{indexable && stats && share !== null ? (
<div className="ml-auto w-24">
<div className="tabular-nums text-gray-600 dark:text-gray-300">
{Math.round(share * 100)}%
</div>
<MeterBar
share={share}
tone={indexedTone(stats, share)}
label={`${(stats.documents.by_state.indexed ?? 0).toLocaleString()} of ${stats.documents.total.toLocaleString()} indexed`}
className="mt-1"
/>
</div>
) : (
<span className="text-gray-300 dark:text-gray-600">—</span>
)}
</td>
<td className="whitespace-nowrap py-2 pr-3 text-gray-500 dark:text-gray-400">
{stats?.last_updated_at ? formatAgo(stats.last_updated_at) : "—"}
</td>
<td className="py-2 pr-3 text-right">
<div className="flex items-end justify-end gap-2">
{stats && stats.citations_daily.length > 0 && (
<SparkBars
values={stats.citations_daily}
titles={citationTitles(stats.citations_daily)}
label={`Answers citing ${knowledge.name}, per day over 30 days`}
height={14}
/>
)}
<span className="tabular-nums text-gray-700 dark:text-gray-200">
{(stats?.citations ?? 0).toLocaleString()}
</span>
</div>
</td>
<td className="py-2 pr-3">
<HealthBadge
health={sourceHealth(knowledge.source_type, stats)}
reasons={attentionReasons(stats)}
/>
</td>
{onRemove && (
<td className="py-2 text-right">
<button
type="button"
onClick={() => onRemove(knowledge.id)}
disabled={removing}
className="rounded p-1 text-gray-300 dark:text-gray-600 hover:text-red-500 dark:hover:text-red-400 disabled:opacity-40"
title="Remove from repository"
>
<X className="size-3.5" />
</button>
</td>
)}
</tr>
);
})}
</tbody>
</table>
</div>
)}
</SectionCard>
);
};
export default RepositorySources;
|