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