| 1 | import { ConfigYaml } from "@continuedev/config-yaml"; |
| 2 | import { ArrowPathIcon, StopIcon } from "@heroicons/react/24/outline"; |
| 3 | import { SiteIndexingConfig } from "core"; |
| 4 | import { useContext, useEffect, useMemo, useState } from "react"; |
| 5 | import { IdeMessengerContext } from "../../../../../context/IdeMessenger"; |
| 6 | import { useAppDispatch, useAppSelector } from "../../../../../redux/hooks"; |
| 7 | import { updateIndexingStatus } from "../../../../../redux/slices/indexingSlice"; |
| 8 | import { |
| 9 | setDialogMessage, |
| 10 | setShowDialog, |
| 11 | } from "../../../../../redux/slices/uiSlice"; |
| 12 | import { fontSize } from "../../../../../util"; |
| 13 | import ConfirmationDialog from "../../../../dialogs/ConfirmationDialog"; |
| 14 | import { ToolTip } from "../../../../gui/Tooltip"; |
| 15 | import EditBlockButton from "../../EditBlockButton"; |
| 16 | import { useLump } from "../../LumpContext"; |
| 17 | import { IndexedPagesTooltip } from "./IndexedPagesTooltip"; |
| 18 | import { StatusIndicator } from "./StatusIndicator"; |
| 19 | |
| 20 | interface IndexingStatusViewerProps { |
| 21 | docConfig: SiteIndexingConfig; |
| 22 | docFromYaml?: NonNullable<ConfigYaml["docs"]>[number]; |
| 23 | } |
| 24 | |
| 25 | function 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 | isOpen={showTooltip} |
| 219 | setIsOpen={setShowTooltip} |
| 220 | clickable |
| 221 | delayShow={0} |
| 222 | openEvents={{ |
| 223 | mouseenter: false, |
| 224 | click: true, |
| 225 | }} |
| 226 | closeEvents={{ |
| 227 | blur: true, |
| 228 | mouseleave: false, |
| 229 | click: true, |
| 230 | }} |
| 231 | content={ |
| 232 | <IndexedPagesTooltip |
| 233 | pages={indexedPages} |
| 234 | siteTitle={docConfig.title ?? docConfig.startUrl} |
| 235 | baseUrl={docConfig.startUrl} |
| 236 | /> |
| 237 | } |
| 238 | > |
| 239 | <div data-tooltip-id={`docs-tooltip-${startUrlSlug}`} /> |
| 240 | </ToolTip> |
| 241 | )} |
| 242 | </div> |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | export default DocsIndexingStatus; |