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 | 2x 2x 2x 63x 63x 63x 63x 63x 63x 63x 63x 19x 63x 63x 63x 63x 4x 63x 63x 1x 1x 1x 2x 1x 63x 63x 63x 63x 63x 63x 63x 1x 1x 63x 1x 41x 1x | import { BuiltInBadge } from "@/components/badge/BuiltInBadge";
import {
ConfirmDialog,
DetailPage,
DetailTabs,
encodeResourceId,
Strong,
useDetailTab,
useResourceId,
} from "@/components/detail";
import { MoveToShared } from "@/components/owner";
import { isSystemManaged } from "@/objects/Doc";
import type { Knowledge, KnowledgeDocument } from "@/objects/Knowledge";
import { APP_ROUTES, ROUTE_PARAMS } from "@/routes/const";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { errorDetail } from "@/util/apiError";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { BarChart3, LayoutDashboard, Settings, Trash2 } from "lucide-react";
import React, { useState } from "react";
import { Link, useNavigate } from "react-router-dom";
import { documentsKey, sourceKey } from "./figures";
import SourceContent from "./source/SourceContent";
import SourceOverview, { useSourceStats } from "./source/SourceOverview";
import UsagePanel from "./source/UsagePanel";
import { sourceType } from "./sourceTypes";
/**
* A knowledge source's page, on the detail kit: its figures, trust and
* where it is used (Overview); what it holds, with everything that changes
* it (Content); and what answers drew on it (Usage). The tab is in the
* address (`?tab=`), so each is a link; the edit history sits under every
* one.
*/
const TABS = ["overview", "content", "usage"] as const;
/** What the Content tab is called for each kind of source. */
const CONTENT_LABEL: Record<string, string> = {
file: "Documents",
web: "Pages",
graph: "Graph",
};
const KnowledgeViewer: React.FC = () => {
const { getApiHandler } = useApiHandler();
const api = getApiHandler().knowledge_router;
const queryClient = useQueryClient();
const navigate = useNavigate();
const [confirmingDelete, setConfirmingDelete] = useState(false);
const [tab, setTab] = useDetailTab(TABS);
// Null for a missing *or* malformed id: a bare `atob` throws on the latter
// and blanks the page.
const knowledgeId = useResourceId("knowledgeId");
const { data, error, isLoading } = useQuery<Knowledge | null>({
queryKey: sourceKey(knowledgeId ?? ""),
queryFn: () => api.id(knowledgeId as string),
enabled: !!knowledgeId,
});
const knowledge: Knowledge | null | undefined = data;
const { stats } = useSourceStats(knowledge ? knowledge.id : null);
// A file source's documents: the same query its Documents section reads,
// so whether it is empty costs no second request.
const isFile = knowledge?.source_type === "file";
const { data: documentsData } = useQuery<KnowledgeDocument[] | null>({
queryKey: documentsKey(knowledgeId ?? ""),
queryFn: () => api.listDocuments(knowledgeId as string),
enabled: !!knowledgeId && isFile,
});
const documents: KnowledgeDocument[] | null | undefined = documentsData;
const deleteMutation = useMutation({
mutationFn: () => api.delete(knowledgeId as string),
onSuccess: async () => {
setConfirmingDelete(false);
await Promise.all(
[QUERY_KEYS.GET.KNOWLEDGE, QUERY_KEYS.GET.REPOSITORIES].map((key) =>
queryClient.invalidateQueries({ queryKey: [key] }),
),
);
void navigate(APP_ROUTES.USER.KNOWLEDGE.sources);
},
});
// A built-in source (the CHAIN-R guide, a News feed) is read and granted,
// never edited — the API refuses every change, so the page offers none.
const builtIn = isSystemManaged(knowledge);
// Nor one the viewer may not change here: what a project inherits, or a
// level that does not allow it (svc-core says, `can_change`).
const mayChange = !!knowledge && !builtIn && knowledge.can_change !== false;
// An empty website or file source can find its own material.
const empty =
knowledge?.source_type === "web"
? !((knowledge.source_config?.urls as unknown[] | undefined)?.length ?? 0)
: isFile && documents != null && documents.length === 0;
const meta = sourceType(knowledge?.source_type ?? "");
const SourceIcon = meta.Icon;
const editHref = knowledgeId
? APP_ROUTES.USER.KNOWLEDGE.edit.replace(
ROUTE_PARAMS.KNOWLEDGE.id,
encodeResourceId(knowledgeId),
)
: APP_ROUTES.USER.KNOWLEDGE.sources;
const actions = mayChange
? [
<MoveToShared key="move" kind="knowledge" resource={knowledge as Knowledge} />,
<Link
key="edit"
to={editHref}
className="flex items-center gap-1.5 text-xs px-2.5 py-1.5 rounded-lg border border-gray-200 dark:border-gray-600 text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
>
<Settings className="size-3.5" />
Edit
</Link>,
<button
key="delete"
type="button"
onClick={() => {
deleteMutation.reset();
setConfirmingDelete(true);
}}
className="flex items-center gap-1.5 text-xs px-2.5 py-1.5 rounded-lg border border-red-200 dark:border-red-800 text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-950/40 transition-colors"
>
<Trash2 className="size-3.5" />
Delete
</button>,
]
: [];
return (
<DetailPage<Knowledge>
resource={knowledge}
loading={!!knowledgeId && isLoading}
error={error}
title={
knowledge ? (
<>
{knowledge.name}
{builtIn && <BuiltInBadge className="ml-2 align-middle" />}
</>
) : undefined
}
icon={<SourceIcon className="size-12 text-gray-400 dark:text-gray-500" />}
description={knowledge?.description || undefined}
back={APP_ROUTES.USER.KNOWLEDGE.sources}
label="knowledge source"
actions={actions}
resourceId={knowledgeId}
history={{
source: api,
queryKey: QUERY_KEYS.GET.KNOWLEDGE_HISTORY,
fieldLabels: {
name: "Name",
description: "Description",
source_type: "Source type",
source_config: "Source configuration",
},
}}
overlay={
confirmingDelete &&
knowledge && (
<ConfirmDialog
title="Delete this source?"
icon={Trash2}
destructive
body={
<>
<Strong>{knowledge.name}</Strong> and everything it holds — its
documents, pages and their versions — are deleted, and it
leaves every repository it is in, so agents no longer read it.
Its edit history is kept.
</>
}
confirmLabel="Delete source"
loadingLabel="Deleting…"
loading={deleteMutation.isPending}
error={
deleteMutation.error
? errorDetail(deleteMutation.error, "Could not delete the source.")
: null
}
onConfirm={() => deleteMutation.mutate()}
onCancel={() => setConfirmingDelete(false)}
/>
)
}
>
{(source) => (
<>
<DetailTabs
tabs={[
{ key: "overview", label: "Overview", Icon: LayoutDashboard },
{
key: "content",
label: CONTENT_LABEL[source.source_type] ?? "Configuration",
Icon: SourceIcon,
count:
source.source_type === "file" || source.source_type === "web"
? stats?.items
: undefined,
},
{ key: "usage", label: "Usage", Icon: BarChart3 },
]}
active={tab}
onChange={setTab}
/>
{tab === "overview" && (
<SourceOverview
knowledge={source}
builtIn={builtIn}
emptyHere={mayChange && empty}
onOpenContent={() => setTab("content")}
/>
)}
{tab === "content" && (
<SourceContent
knowledge={source}
stats={stats}
mayChange={mayChange}
empty={empty}
editHref={editHref}
/>
)}
{tab === "usage" && (
<UsagePanel
queryKey={[...sourceKey(source.id), "usage"]}
fetch={(days) => api.usage(source.id, days)}
costSource="knowledge"
sourceId={source.id}
noun="source"
/>
)}
</>
)}
</DetailPage>
);
};
export default KnowledgeViewer;
|