| 1 | import { InformationCircleIcon } from "@heroicons/react/24/outline"; |
| 2 | import { IndexingStatus, SiteIndexingConfig } from "core"; |
| 3 | import { usePostHog } from "posthog-js/react"; |
| 4 | import { useContext, useLayoutEffect, useMemo, useRef, useState } from "react"; |
| 5 | import { useDispatch } from "react-redux"; |
| 6 | import { Input, SecondaryButton } from ".."; |
| 7 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 8 | import { useAppSelector } from "../../redux/hooks"; |
| 9 | import { updateIndexingStatus } from "../../redux/slices/indexingSlice"; |
| 10 | import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; |
| 11 | import { ToolTip } from "../gui/Tooltip"; |
| 12 | import DocsIndexingPeeks from "../../pages/config/sections/docs/DocsIndexingPeeks"; |
| 13 | |
| 14 | function AddDocsDialog() { |
| 15 | const posthog = usePostHog(); |
| 16 | const dispatch = useDispatch(); |
| 17 | |
| 18 | const titleRef = useRef<HTMLInputElement>(null); |
| 19 | const urlRef = useRef<HTMLInputElement>(null); |
| 20 | |
| 21 | const [title, setTitle] = useState(""); |
| 22 | const [startUrl, setStartUrl] = useState(""); |
| 23 | const [faviconUrl, setFaviconUrl] = useState(""); |
| 24 | |
| 25 | const ideMessenger = useContext(IdeMessengerContext); |
| 26 | const indexingStatuses = useAppSelector( |
| 27 | (store) => store.indexing.indexing.statuses, |
| 28 | ); |
| 29 | |
| 30 | const docsIndexingStatuses: IndexingStatus[] = useMemo(() => { |
| 31 | return Object.values(indexingStatuses).filter( |
| 32 | (status) => status.type === "docs" && status.status === "indexing", |
| 33 | ); |
| 34 | }, [indexingStatuses]); |
| 35 | |
| 36 | const isFormValid = startUrl && title; |
| 37 | |
| 38 | useLayoutEffect(() => { |
| 39 | setTimeout(() => { |
| 40 | if (titleRef.current) { |
| 41 | titleRef.current.focus(); |
| 42 | } |
| 43 | }, 100); |
| 44 | }, [titleRef]); |
| 45 | |
| 46 | const closeDialog = () => { |
| 47 | dispatch(setShowDialog(false)); |
| 48 | dispatch(setDialogMessage(undefined)); |
| 49 | }; |
| 50 | |
| 51 | function onSubmit(e: any) { |
| 52 | e.preventDefault(); |
| 53 | |
| 54 | const siteIndexingConfig: SiteIndexingConfig = { |
| 55 | startUrl, |
| 56 | title, |
| 57 | faviconUrl, |
| 58 | }; |
| 59 | |
| 60 | ideMessenger.post("context/addDocs", siteIndexingConfig); |
| 61 | |
| 62 | setTitle(""); |
| 63 | setStartUrl(""); |
| 64 | setFaviconUrl(""); |
| 65 | |
| 66 | posthog.capture("add_docs_gui", { url: startUrl }); |
| 67 | |
| 68 | // Optimistic status update |
| 69 | dispatch( |
| 70 | updateIndexingStatus({ |
| 71 | type: "docs", |
| 72 | description: "Initializing", |
| 73 | id: startUrl, |
| 74 | progress: 0, |
| 75 | status: "indexing", |
| 76 | title, |
| 77 | url: startUrl, |
| 78 | }), |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | return ( |
| 83 | <div className="px-2 pt-4 sm:px-4"> |
| 84 | <div className=""> |
| 85 | <h1 className="mb-0 hidden sm:block">Add documentation</h1> |
| 86 | <h1 className="sm:hidden">Add docs</h1> |
| 87 | <p className="m-0 mt-2 p-0 text-stone-500"> |
| 88 | Common documentation sites are cached for faster loading |
| 89 | </p> |
| 90 | <div className="mt-3"> |
| 91 | <form onSubmit={onSubmit} className="flex flex-col gap-1"> |
| 92 | <div className="flex flex-col gap-2"> |
| 93 | <label className="flex w-full flex-col gap-1"> |
| 94 | <div className="flex flex-row items-center gap-1"> |
| 95 | <span>Title</span> |
| 96 | <div> |
| 97 | <ToolTip |
| 98 | place="top" |
| 99 | content="The title that will be displayed to users in the `@docs` submenu" |
| 100 | > |
| 101 | <InformationCircleIcon className="text-lightgray h-3.5 w-3.5 select-none" /> |
| 102 | </ToolTip> |
| 103 | </div> |
| 104 | </div> |
| 105 | |
| 106 | <Input |
| 107 | type="text" |
| 108 | placeholder="Title" |
| 109 | value={title} |
| 110 | ref={titleRef} |
| 111 | onChange={(e) => setTitle(e.target.value)} |
| 112 | /> |
| 113 | </label> |
| 114 | |
| 115 | <label className="flex w-full flex-col gap-1"> |
| 116 | <div className="flex flex-row items-center gap-1"> |
| 117 | <span className="line-clamp-1 whitespace-nowrap"> |
| 118 | Start URL |
| 119 | </span> |
| 120 | <div> |
| 121 | <ToolTip |
| 122 | place="top" |
| 123 | content="The starting location to begin crawling the documentation site" |
| 124 | > |
| 125 | <InformationCircleIcon className="text-lightgray h-3.5 w-3.5 select-none" /> |
| 126 | </ToolTip> |
| 127 | </div> |
| 128 | </div> |
| 129 | <Input |
| 130 | ref={urlRef} |
| 131 | type="url" |
| 132 | placeholder="Start URL" |
| 133 | value={startUrl} |
| 134 | onChange={(e) => { |
| 135 | setStartUrl(e.target.value); |
| 136 | }} |
| 137 | /> |
| 138 | </label> |
| 139 | </div> |
| 140 | <div className="flex flex-row justify-end gap-2"> |
| 141 | <SecondaryButton |
| 142 | className="min-w-16" |
| 143 | disabled={!isFormValid} |
| 144 | type="submit" |
| 145 | > |
| 146 | Add |
| 147 | </SecondaryButton> |
| 148 | </div> |
| 149 | </form> |
| 150 | </div> |
| 151 | </div> |
| 152 | |
| 153 | {docsIndexingStatuses.length > 0 && ( |
| 154 | <> |
| 155 | <DocsIndexingPeeks statuses={docsIndexingStatuses} /> |
| 156 | <p className="mt-2 flex flex-row items-center gap-1 p-0 px-1 text-center text-xs text-stone-500"> |
| 157 | Closing this dialog will not affect indexing progress |
| 158 | </p> |
| 159 | </> |
| 160 | )} |
| 161 | </div> |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | export default AddDocsDialog; |