Elffuss BraidHub

kikocis/continue public

8cf316c34504 · 4.5 KiBRawPermalink
1import { ConfigYaml } from "@continuedev/config-yaml";
2import { ArrowPathIcon, StopIcon } from "@heroicons/react/24/outline";
3import { SiteIndexingConfig } from "core";
4import { useContext, 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 EditBlockButton from "../../EditBlockButton";
15import { useLump } from "../../LumpContext";
16import { StatusIndicator } from "./StatusIndicator";
17interface IndexingStatusViewerProps {
18 docConfig: SiteIndexingConfig;
19 docFromYaml?: NonNullable<ConfigYaml["docs"]>[number];
20}
21
22function DocsIndexingStatus({
23 docConfig,
24 docFromYaml,
25}: IndexingStatusViewerProps) {
26 const ideMessenger = useContext(IdeMessengerContext);
27 const dispatch = useAppDispatch();
28 const { hideLump } = useLump();
29
30 const status = useAppSelector(
31 (store) => store.indexing.indexing.statuses[docConfig.startUrl],
32 );
33
34 const reIndex = () =>
35 ideMessenger.post("indexing/reindex", {
36 type: "docs",
37 id: docConfig.startUrl,
38 });
39
40 const abort = () => {
41 ideMessenger.post("indexing/abort", {
42 type: "docs",
43 id: docConfig.startUrl,
44 });
45 // Optimistic abort status
46 if (status) {
47 dispatch(
48 updateIndexingStatus({ ...status, status: "aborted", progress: 0 }),
49 );
50 }
51 };
52
53 const [hasDeleted, setHasDeleted] = useState(false); // simple alternative to optimistic redux update
54 const onDelete = () => {
55 // optimistic update
56 dispatch(
57 setDialogMessage(
58 <ConfirmationDialog
59 title={`Delete ${docConfig.title}`}
60 text={`Are you sure you want to remove ${docConfig.title} from your configuration?`}
61 onConfirm={() => {
62 ideMessenger.post("context/removeDocs", {
63 startUrl: docConfig.startUrl,
64 });
65 setHasDeleted(true);
66 }}
67 />,
68 ),
69 );
70 dispatch(setShowDialog(true));
71 };
72
73 const progressPercentage = useMemo(() => {
74 if (!status) {
75 return 0;
76 }
77 return Math.min(100, Math.max(0, status.progress * 100)).toFixed(0);
78 }, [status?.progress]);
79
80 if (hasDeleted) return null;
81
82 return (
83 <div className="mt-1 flex w-full flex-col">
84 <div
85 className={`flex flex-row items-center justify-between gap-2 text-sm`}
86 >
87 <div
88 className={`flex flex-row items-center gap-1`}
89 onClick={() => {
90 if (status?.url) {
91 ideMessenger.post("openUrl", status.url);
92 hideLump();
93 }
94 }}
95 >
96 <p
97 style={{
98 fontSize: fontSize(-3),
99 }}
100 className={`m-0 line-clamp-1 p-0 text-left ${status?.url ? "cursor-pointer hover:underline" : ""}`}
101 >
102 {docConfig.title ?? docConfig.startUrl}
103 </p>
104 </div>
105
106 <div className="flex flex-row items-center gap-2">
107 <div className="flex flex-row items-center gap-2 text-gray-400">
108 {status?.status === "indexing" && (
109 <span
110 className="text-xs"
111 style={{
112 fontSize: fontSize(-3),
113 }}
114 >
115 {progressPercentage}%
116 </span>
117 )}
118
119 {status?.status === "indexing" && (
120 <StopIcon
121 className="h-3 w-3 cursor-pointer text-gray-400 hover:brightness-125"
122 onClick={abort}
123 />
124 )}
125
126 <EditBlockButton
127 blockType="docs"
128 block={docFromYaml}
129 sourceFile={docConfig.sourceFile}
130 />
131
132 {["aborted", "complete", "failed"].includes(
133 status?.status ?? "",
134 ) && (
135 <ArrowPathIcon
136 className="h-3 w-3 cursor-pointer text-gray-400 hover:brightness-125"
137 onClick={reIndex}
138 />
139 )}
140
141 <StatusIndicator
142 status={status?.status}
143 hoverMessage={
144 status?.status === "failed" ? status?.debugInfo : undefined
145 }
146 />
147 </div>
148 </div>
149 </div>
150 </div>
151 );
152}
153
154export default DocsIndexingStatus;