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