Elffuss BraidHub

kikocis/continue public

f0ea2b85847c · 7.1 KiBRawPermalink
1import { ConfigYaml } from "@continuedev/config-yaml";
2import { ArrowPathIcon, StopIcon } from "@heroicons/react/24/outline";
3import { SiteIndexingConfig } from "core";
4import { useContext, useEffect, useMemo, useState } from "react";
5import { IdeMessengerContext } from "../../../../../context/IdeMessenger";
6import { useAppDispatch, useAppSelector } from "../../../../../redux/hooks";
7import { updateIndexingStatus } from "../../../../../redux/slices/indexingSlice";
8import {
9 setDialogMessage,
10 setShowDialog,
11} from "../../../../../redux/slices/uiSlice";
12import { fontSize } from "../../../../../util";
13import ConfirmationDialog from "../../../../dialogs/ConfirmationDialog";
14import { ToolTip } from "../../../../gui/Tooltip";
15import EditBlockButton from "../../EditBlockButton";
16import { useLump } from "../../LumpContext";
17import { IndexedPagesTooltip } from "./IndexedPagesTooltip";
18import { StatusIndicator } from "./StatusIndicator";
19
20interface IndexingStatusViewerProps {
21 docConfig: SiteIndexingConfig;
22 docFromYaml?: NonNullable<ConfigYaml["docs"]>[number];
23}
24
25function DocsIndexingStatus({
26 docConfig,
27 docFromYaml,
28}: IndexingStatusViewerProps) {
29 const ideMessenger = useContext(IdeMessengerContext);
30 const dispatch = useAppDispatch();
31 const { hideLump } = useLump();
32 const [indexedPages, setIndexedPages] = useState<null | string[]>(null);
33 const [showTooltip, setShowTooltip] = useState(false);
34
35 const status = useAppSelector(
36 (store) => store.indexing.indexing.statuses[docConfig.startUrl],
37 );
38
39 const isComplete = status?.status === "complete";
40
41 const reIndex = () =>
42 ideMessenger.post("indexing/reindex", {
43 type: "docs",
44 id: docConfig.startUrl,
45 });
46
47 const abort = () => {
48 ideMessenger.post("indexing/abort", {
49 type: "docs",
50 id: docConfig.startUrl,
51 });
52 // Optimistic abort status
53 if (status) {
54 dispatch(
55 updateIndexingStatus({ ...status, status: "aborted", progress: 0 }),
56 );
57 }
58 };
59
60 const [hasDeleted, setHasDeleted] = useState(false); // simple alternative to optimistic redux update
61 const onDelete = () => {
62 // optimistic update
63 dispatch(
64 setDialogMessage(
65 <ConfirmationDialog
66 title={`Delete ${docConfig.title}`}
67 text={`Are you sure you want to remove ${docConfig.title} from your configuration?`}
68 onConfirm={() => {
69 ideMessenger.post("context/removeDocs", {
70 startUrl: docConfig.startUrl,
71 });
72 setHasDeleted(true);
73 }}
74 />,
75 ),
76 );
77 dispatch(setShowDialog(true));
78 };
79
80 const progressPercentage = useMemo(() => {
81 if (!status) {
82 return 0;
83 }
84 return Math.min(100, Math.max(0, status.progress * 100)).toFixed(0);
85 }, [status?.progress]);
86
87 // Fetch pages list when the status changes to complete
88 useEffect(() => {
89 async function getPagesList() {
90 try {
91 const response = await ideMessenger.request("docs/getIndexedPages", {
92 startUrl: docConfig.startUrl,
93 });
94 if (response.status === "error") {
95 throw new Error(response.error);
96 }
97 setIndexedPages(response.content.sort());
98 } catch (ex) {
99 console.error(
100 `Unable to fetch pages list for ${docConfig.startUrl}: ${ex}`,
101 );
102 }
103 }
104
105 if (isComplete) {
106 void getPagesList();
107 }
108 }, [isComplete, ideMessenger, docConfig.startUrl]);
109
110 const showPagesList = () => {
111 if (indexedPages) {
112 setShowTooltip(true);
113 }
114 };
115
116 if (hasDeleted) return null;
117
118 const startUrlSlug = docConfig.startUrl.replace(/[^a-zA-Z0-9_-]/g, "_");
119
120 return (
121 <div className="mt-1 flex w-full flex-col">
122 <div
123 className={`flex flex-row items-center justify-between gap-2 text-sm`}
124 >
125 <div
126 className={`flex flex-row items-center gap-1`}
127 onClick={() => {
128 if (status?.url) {
129 ideMessenger.post("openUrl", status.url);
130 hideLump();
131 }
132 }}
133 >
134 <p
135 style={{
136 fontSize: fontSize(-3),
137 }}
138 className={`m-0 line-clamp-1 p-0 text-left ${status?.url ? "cursor-pointer hover:underline" : ""}`}
139 >
140 {docConfig.title ?? docConfig.startUrl}
141 </p>
142 </div>
143
144 <div className="flex flex-row items-center gap-2">
145 <div className="flex flex-row items-center gap-2 text-gray-400">
146 {status?.status === "indexing" && (
147 <span
148 className="text-xs"
149 style={{
150 fontSize: fontSize(-3),
151 }}
152 >
153 {progressPercentage}%
154 </span>
155 )}
156
157 {status?.status === "indexing" && (
158 <StopIcon
159 className="h-3 w-3 cursor-pointer text-gray-400 hover:brightness-125"
160 onClick={abort}
161 data-testid="stop-indexing"
162 />
163 )}
164
165 <EditBlockButton
166 blockType="docs"
167 block={docFromYaml}
168 sourceFile={docConfig.sourceFile}
169 />
170
171 {["aborted", "complete", "failed"].includes(
172 status?.status ?? "",
173 ) && (
174 <ArrowPathIcon
175 className="h-3 w-3 cursor-pointer text-gray-400 hover:brightness-125"
176 onClick={reIndex}
177 data-testid="reindex-docs"
178 />
179 )}
180
181 <StatusIndicator
182 status={status?.status}
183 hoverMessage={
184 status?.status === "failed" ? status?.debugInfo : undefined
185 }
186 />
187 </div>
188 </div>
189 </div>
190
191 {status && (
192 <div
193 className={`flex flex-row items-center justify-between gap-2 text-sm`}
194 >
195 <p
196 style={{
197 fontSize: fontSize(-4),
198 }}
199 className={`m-0 line-clamp-1 p-0 text-left text-gray-400 ${isComplete ? "cursor-pointer hover:underline" : ""}`}
200 onClick={() => {
201 if (isComplete) {
202 showPagesList();
203 }
204 }}
205 data-tooltip-id={`docs-tooltip-${startUrlSlug}`}
206 >
207 {isComplete
208 ? indexedPages
209 ? `${indexedPages.length} page${indexedPages.length === 1 ? "" : "s"} indexed`
210 : "Loading site info..."
211 : status.description}
212 </p>
213 </div>
214 )}
215
216 {indexedPages && (
217 <ToolTip
218 id={`docs-tooltip-${startUrlSlug}`}
219 isOpen={showTooltip}
220 setIsOpen={setShowTooltip}
221 clickable
222 delayShow={0}
223 openEvents={{
224 mouseenter: false,
225 click: true,
226 }}
227 closeEvents={{
228 blur: true,
229 mouseleave: false,
230 click: true,
231 }}
232 >
233 <IndexedPagesTooltip
234 pages={indexedPages}
235 siteTitle={docConfig.title ?? docConfig.startUrl}
236 baseUrl={docConfig.startUrl}
237 />
238 </ToolTip>
239 )}
240 </div>
241 );
242}
243
244export default DocsIndexingStatus;