Elffuss BraidHub

kikocis/continue public

590460f7d20a · 5.4 KiBRawPermalink
1import { InformationCircleIcon } from "@heroicons/react/24/outline";
2import { IndexingStatus, SiteIndexingConfig } from "core";
3import { usePostHog } from "posthog-js/react";
4import { useContext, useLayoutEffect, useMemo, useRef, useState } from "react";
5import { useDispatch } from "react-redux";
6import { Input, SecondaryButton } from "..";
7import { IdeMessengerContext } from "../../context/IdeMessenger";
8import { useAppSelector } from "../../redux/hooks";
9import { updateIndexingStatus } from "../../redux/slices/indexingSlice";
10import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice";
11import { ToolTip } from "../gui/Tooltip";
12import DocsIndexingPeeks from "../mainInput/Lump/sections/docs/DocsIndexingPeeks";
13
14function 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 <InformationCircleIcon
98 data-tooltip-id={"add-docs-form-title"}
99 className="text-lightgray h-3.5 w-3.5 select-none"
100 />
101 <ToolTip id={"add-docs-form-title"} place="top">
102 The title that will be displayed to users in the `@docs`
103 submenu
104 </ToolTip>
105 </div>
106 </div>
107
108 <Input
109 type="text"
110 placeholder="Title"
111 value={title}
112 ref={titleRef}
113 onChange={(e) => setTitle(e.target.value)}
114 />
115 </label>
116
117 <label className="flex w-full flex-col gap-1">
118 <div className="flex flex-row items-center gap-1">
119 <span className="line-clamp-1 whitespace-nowrap">
120 Start URL
121 </span>
122 <div>
123 <InformationCircleIcon
124 data-tooltip-id={"add-docs-form-url"}
125 className="text-lightgray h-3.5 w-3.5 select-none"
126 />
127 <ToolTip id={"add-docs-form-url"} place="top">
128 The starting location to begin crawling the documentation
129 site
130 </ToolTip>
131 </div>
132 </div>
133 <Input
134 ref={urlRef}
135 type="url"
136 placeholder="Start URL"
137 value={startUrl}
138 onChange={(e) => {
139 setStartUrl(e.target.value);
140 }}
141 />
142 </label>
143 </div>
144 <div className="flex flex-row justify-end gap-2">
145 <SecondaryButton
146 className="min-w-16"
147 disabled={!isFormValid}
148 type="submit"
149 >
150 Add
151 </SecondaryButton>
152 </div>
153 </form>
154 </div>
155 </div>
156
157 {docsIndexingStatuses.length > 0 && (
158 <>
159 <DocsIndexingPeeks statuses={docsIndexingStatuses} />
160 <p className="mt-2 flex flex-row items-center gap-1 p-0 px-1 text-center text-xs text-stone-500">
161 Closing this dialog will not affect indexing progress
162 </p>
163 </>
164 )}
165 </div>
166 );
167}
168
169export default AddDocsDialog;