| 1 | import { IndexingProgressUpdate } from "core"; |
| 2 | import { useContext, useEffect, useState } from "react"; |
| 3 | import { useDispatch } from "react-redux"; |
| 4 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 5 | import { isJetBrains } from "../../../util"; |
| 6 | import { useWebviewListener } from "../../../hooks/useWebviewListener"; |
| 7 | import IndexingProgressBar from "./IndexingProgressBar"; |
| 8 | import IndexingProgressIndicator from "./IndexingProgressIndicator"; |
| 9 | import IndexingProgressTitleText from "./IndexingProgressTitleText"; |
| 10 | import IndexingProgressSubtext from "./IndexingProgressSubtext"; |
| 11 | import { usePostHog } from "posthog-js/react"; |
| 12 | import ConfirmationDialog from "../../../components/dialogs/ConfirmationDialog"; |
| 13 | import { setShowDialog, setDialogMessage } from "../../../redux/slices/uiSlice"; |
| 14 | import IndexingProgressErrorText from "./IndexingProgressErrorText"; |
| 15 | |
| 16 | export function getProgressPercentage( |
| 17 | progress: IndexingProgressUpdate["progress"], |
| 18 | ) { |
| 19 | return Math.min(100, Math.max(0, progress * 100)); |
| 20 | } |
| 21 | |
| 22 | function IndexingProgress() { |
| 23 | const ideMessenger = useContext(IdeMessengerContext); |
| 24 | const posthog = usePostHog(); |
| 25 | const dispatch = useDispatch(); |
| 26 | const [paused, setPaused] = useState<boolean | undefined>(undefined); |
| 27 | const [update, setUpdate] = useState<IndexingProgressUpdate>({ |
| 28 | desc: "Loading indexing config", |
| 29 | progress: 0.0, |
| 30 | status: "loading", |
| 31 | }); |
| 32 | |
| 33 | // If sidebar is opened after extension initializes, retrieve saved states. |
| 34 | let initialized = false; |
| 35 | |
| 36 | useWebviewListener("indexProgress", async (data) => { |
| 37 | setUpdate(data); |
| 38 | }); |
| 39 | |
| 40 | useEffect(() => { |
| 41 | if (!initialized) { |
| 42 | // Triggers retrieval for possible non-default states set prior to IndexingProgressBar initialization |
| 43 | ideMessenger.post("index/indexingProgressBarInitialized", undefined); |
| 44 | initialized = true; |
| 45 | } |
| 46 | }, []); |
| 47 | |
| 48 | useEffect(() => { |
| 49 | if (paused === undefined) return; |
| 50 | ideMessenger.post("index/setPaused", paused); |
| 51 | }, [paused]); |
| 52 | |
| 53 | function onClickRetry() { |
| 54 | // For now, we don't show in JetBrains since the re-index command |
| 55 | // is not yet implemented |
| 56 | if (update.shouldClearIndexes && !isJetBrains()) { |
| 57 | dispatch(setShowDialog(true)); |
| 58 | dispatch( |
| 59 | setDialogMessage( |
| 60 | <ConfirmationDialog |
| 61 | title="Rebuild codebase index" |
| 62 | confirmText="Rebuild" |
| 63 | text={ |
| 64 | "Your index appears corrupted. We recommend clearing and rebuilding it, " + |
| 65 | "which may take time for large codebases.\n\n" + |
| 66 | "For a faster rebuild without clearing data, press 'Shift + Command + P' to open " + |
| 67 | "the Command Palette, and type out 'Continue: Force Codebase Re-Indexing'" |
| 68 | } |
| 69 | onConfirm={() => { |
| 70 | posthog.capture("rebuild_index_clicked"); |
| 71 | ideMessenger.post("index/forceReIndex", { |
| 72 | shouldClearIndexes: true, |
| 73 | }); |
| 74 | }} |
| 75 | />, |
| 76 | ), |
| 77 | ); |
| 78 | } else { |
| 79 | ideMessenger.post("index/forceReIndex", undefined); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | function onClick() { |
| 84 | switch (update.status) { |
| 85 | case "failed": |
| 86 | onClickRetry(); |
| 87 | break; |
| 88 | case "indexing": |
| 89 | case "loading": |
| 90 | case "paused": |
| 91 | if (update.progress < 1 && update.progress >= 0) { |
| 92 | setPaused((prev) => !prev); |
| 93 | } else { |
| 94 | ideMessenger.post("index/forceReIndex", undefined); |
| 95 | } |
| 96 | break; |
| 97 | case "disabled": |
| 98 | ideMessenger.post("config/openProfile", { |
| 99 | profileId: undefined, |
| 100 | }); |
| 101 | break; |
| 102 | case "done": |
| 103 | ideMessenger.post("index/forceReIndex", undefined); |
| 104 | default: |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return ( |
| 110 | <div className="mt-4 flex flex-col"> |
| 111 | <div className="mb-0 flex justify-between text-sm"> |
| 112 | <IndexingProgressTitleText update={update} /> |
| 113 | {update.status !== "loading" && ( |
| 114 | <IndexingProgressIndicator update={update} /> |
| 115 | )} |
| 116 | </div> |
| 117 | |
| 118 | <IndexingProgressBar update={update} /> |
| 119 | |
| 120 | <IndexingProgressSubtext update={update} onClick={onClick} /> |
| 121 | |
| 122 | {update.status === "failed" && ( |
| 123 | <div className="mt-4"> |
| 124 | <IndexingProgressErrorText update={update} /> |
| 125 | </div> |
| 126 | )} |
| 127 | </div> |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | export default IndexingProgress; |