| 1 | import { DocsIndexingDetails } from "core"; |
| 2 | import { usePostHog } from "posthog-js/react"; |
| 3 | import { useCallback, useContext, useEffect, useState } from "react"; |
| 4 | import { useDispatch } from "react-redux"; |
| 5 | import { Tooltip } from "react-tooltip"; |
| 6 | import { SecondaryButton } from "../../../.."; |
| 7 | import { IdeMessengerContext } from "../../../../../context/IdeMessenger"; |
| 8 | import { useAppSelector } from "../../../../../redux/hooks"; |
| 9 | import { |
| 10 | setDialogMessage, |
| 11 | setShowDialog, |
| 12 | } from "../../../../../redux/slices/uiSlice"; |
| 13 | interface DocsDetailsDialogProps { |
| 14 | startUrl: string; |
| 15 | } |
| 16 | function DocsDetailsDialog({ startUrl }: DocsDetailsDialogProps) { |
| 17 | const config = useAppSelector((store) => store.config.config); |
| 18 | const posthog = usePostHog(); |
| 19 | const dispatch = useDispatch(); |
| 20 | |
| 21 | const ideMessenger = useContext(IdeMessengerContext); |
| 22 | |
| 23 | const closeDialog = () => { |
| 24 | dispatch(setShowDialog(false)); |
| 25 | dispatch(setDialogMessage(undefined)); |
| 26 | }; |
| 27 | |
| 28 | const [isLoading, setIsLoading] = useState(false); |
| 29 | const [isError, setIsError] = useState(false); |
| 30 | const [data, setData] = useState<DocsIndexingDetails | undefined>(undefined); |
| 31 | |
| 32 | const fetchData = useCallback(async () => { |
| 33 | setIsLoading(true); |
| 34 | setIsError(false); |
| 35 | |
| 36 | try { |
| 37 | const response = await ideMessenger.request("docs/getDetails", { |
| 38 | startUrl, |
| 39 | }); |
| 40 | if (response.status === "error") { |
| 41 | throw new Error(response.error); |
| 42 | } |
| 43 | setData(response.content); |
| 44 | } catch (error) { |
| 45 | setIsError(true); |
| 46 | } finally { |
| 47 | setIsLoading(false); |
| 48 | } |
| 49 | }, []); |
| 50 | |
| 51 | useEffect(() => { |
| 52 | fetchData(); |
| 53 | }, []); |
| 54 | |
| 55 | let comp = <div>Loading...</div>; |
| 56 | if (!isLoading) { |
| 57 | if (isError) { |
| 58 | comp = ( |
| 59 | <div> |
| 60 | <div>Error fetching docs details</div> |
| 61 | <SecondaryButton onClick={fetchData}>Try again</SecondaryButton> |
| 62 | </div> |
| 63 | ); |
| 64 | } |
| 65 | if (data) { |
| 66 | comp = ( |
| 67 | <div className="flex flex-col gap-0.5"> |
| 68 | <p className="m-0 mt-2 p-0 text-stone-500">{`Title: ${data.config.title}`}</p> |
| 69 | {!data.chunks?.length ? ( |
| 70 | <div>No article chunks</div> |
| 71 | ) : ( |
| 72 | <div className="relative mt-2 h-[300px] overflow-auto"> |
| 73 | <table className="w-full border-collapse"> |
| 74 | <thead className="bg-vsc-background sticky top-0 text-left"> |
| 75 | <tr> |
| 76 | <th className="py-1">Filepath</th> |
| 77 | <th>Content</th> |
| 78 | </tr> |
| 79 | </thead> |
| 80 | <tbody className="h-20 overflow-y-scroll"> |
| 81 | {data.chunks.map((chunk, i) => { |
| 82 | const contentToolTipId = `docs-content-peek-${i}`; |
| 83 | const urlToolTipId = `docs-url-peek-${i}`; |
| 84 | return ( |
| 85 | <> |
| 86 | <tr key={i} className=""> |
| 87 | <td |
| 88 | className="cursor-pointer px-1" |
| 89 | data-tooltip-id={urlToolTipId} |
| 90 | data-tooltip-delay-show={500} |
| 91 | > |
| 92 | <span className="truncate-start max-w-[200px]"> |
| 93 | {chunk.filepath} |
| 94 | </span> |
| 95 | </td> |
| 96 | <td |
| 97 | className="cursor-pointer px-1" |
| 98 | data-tooltip-id={contentToolTipId} |
| 99 | data-tooltip-delay-show={500} |
| 100 | > |
| 101 | <span className="line-clamp-1"> |
| 102 | {chunk.content} |
| 103 | </span> |
| 104 | </td> |
| 105 | </tr> |
| 106 | </> |
| 107 | ); |
| 108 | })} |
| 109 | </tbody> |
| 110 | </table> |
| 111 | {/* Rending tooltips here because div can't be child of tbody apparently */} |
| 112 | {data.chunks.map((chunk, i) => { |
| 113 | const contentToolTipId = `docs-content-peek-${i}`; |
| 114 | const urlToolTipId = `docs-url-peek-${i}`; |
| 115 | return ( |
| 116 | <> |
| 117 | <Tooltip |
| 118 | key={urlToolTipId} |
| 119 | id={urlToolTipId} |
| 120 | place="top" |
| 121 | className="max-w-full" |
| 122 | > |
| 123 | {chunk.filepath} |
| 124 | </Tooltip> |
| 125 | <Tooltip |
| 126 | key={contentToolTipId} |
| 127 | id={contentToolTipId} |
| 128 | place="top" |
| 129 | className="max-h-[300px] max-w-[170px] overflow-y-auto" |
| 130 | > |
| 131 | {chunk.content} |
| 132 | </Tooltip> |
| 133 | </> |
| 134 | ); |
| 135 | })} |
| 136 | </div> |
| 137 | )} |
| 138 | </div> |
| 139 | ); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return ( |
| 144 | <div className="px-2 py-4 sm:px-4"> |
| 145 | <h3>Docs index</h3> |
| 146 | <p |
| 147 | className="text-lightgray m-0 mt-1 cursor-pointer p-0 hover:underline" |
| 148 | onClick={(e) => { |
| 149 | e.stopPropagation(); |
| 150 | ideMessenger.post("openUrl", startUrl); |
| 151 | }} |
| 152 | > |
| 153 | {startUrl} |
| 154 | </p> |
| 155 | {comp} |
| 156 | <div className="flex flex-row justify-end"> |
| 157 | <SecondaryButton onClick={closeDialog}>Close</SecondaryButton> |
| 158 | </div> |
| 159 | </div> |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | export default DocsDetailsDialog; |