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