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