| 1 | import { ArrowPathIcon, EyeSlashIcon } from "@heroicons/react/24/outline"; |
| 2 | import { IndexingStatus } from "core"; |
| 3 | import { useMemo } from "react"; |
| 4 | import { useDispatch } from "react-redux"; |
| 5 | import { useNavigate } from "react-router-dom"; |
| 6 | import { useAppSelector } from "../../redux/hooks"; |
| 7 | import { setIndexingChatPeekHidden } from "../../redux/slices/indexingSlice"; |
| 8 | |
| 9 | interface MergedIndexingState { |
| 10 | displayName: string; |
| 11 | type: IndexingStatus["type"]; |
| 12 | titles: IndexingStatus["title"][]; |
| 13 | progressPercentage: number; |
| 14 | } |
| 15 | |
| 16 | const mergeProgress = (states: IndexingStatus[]): number => { |
| 17 | const progress = |
| 18 | states.reduce((acc, state) => acc + state.progress, 0) / states.length; |
| 19 | return Math.min(100, Math.max(0, progress * 100)); |
| 20 | }; |
| 21 | |
| 22 | export interface ChatIndexingPeekProps { |
| 23 | state: MergedIndexingState; |
| 24 | } |
| 25 | |
| 26 | function ChatIndexingPeek({ state }: ChatIndexingPeekProps) { |
| 27 | const navigate = useNavigate(); |
| 28 | const dispatch = useDispatch(); |
| 29 | const hiddenPeeks = useAppSelector( |
| 30 | (store) => store.indexing.indexing.hiddenChatPeekTypes, |
| 31 | ); |
| 32 | |
| 33 | if (hiddenPeeks[state.type]) return null; |
| 34 | return ( |
| 35 | <div className="flex flex-row items-center border-0 border-t border-solid border-t-zinc-700 px-3 py-0.5"> |
| 36 | <div className="group flex flex-1 cursor-pointer flex-row items-center gap-2"> |
| 37 | <div className={`flex flex-row items-center gap-2`}> |
| 38 | <p className="text-lightgray m-0 p-0 text-xs group-hover:underline"> |
| 39 | {state.displayName} |
| 40 | </p> |
| 41 | </div> |
| 42 | <div className="my-2 h-1.5 flex-1 rounded-md border border-solid border-gray-400"> |
| 43 | <div |
| 44 | className={`h-full rounded-lg bg-stone-500 transition-all duration-200 ease-in-out`} |
| 45 | style={{ |
| 46 | width: `${state.progressPercentage}%`, |
| 47 | }} |
| 48 | /> |
| 49 | </div> |
| 50 | <div className="xs:flex text-lightgray hidden flex-row items-center gap-1"> |
| 51 | <span className="text-xs no-underline"> |
| 52 | {state.progressPercentage.toFixed(0)}% |
| 53 | </span> |
| 54 | <ArrowPathIcon |
| 55 | className={`animate-spin-slow text-lightgray inline-block h-4 w-4`} |
| 56 | ></ArrowPathIcon> |
| 57 | </div> |
| 58 | </div> |
| 59 | <EyeSlashIcon |
| 60 | className="text-lightgray ml-6 h-4 w-4 cursor-pointer hover:opacity-80" |
| 61 | onClick={(e) => { |
| 62 | dispatch( |
| 63 | setIndexingChatPeekHidden({ type: state.type, hidden: true }), |
| 64 | ); |
| 65 | e.stopPropagation(); |
| 66 | }} |
| 67 | /> |
| 68 | </div> |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | function ChatIndexingPeeks() { |
| 73 | const config = useAppSelector((store) => store.config.config); |
| 74 | const indexingStatuses = useAppSelector( |
| 75 | (store) => store.indexing.indexing.statuses, |
| 76 | ); |
| 77 | const mergedIndexingStates: MergedIndexingState[] = useMemo(() => { |
| 78 | const mergedStates: MergedIndexingState[] = []; |
| 79 | |
| 80 | const configDocs = config?.docs ?? []; |
| 81 | const docsIndexing: IndexingStatus[] = []; |
| 82 | configDocs.forEach((doc) => { |
| 83 | const status = indexingStatuses[doc.startUrl]; |
| 84 | if (status && status.status === "indexing") { |
| 85 | docsIndexing.push(status); |
| 86 | } |
| 87 | }); |
| 88 | if (docsIndexing.length > 0) { |
| 89 | mergedStates.push({ |
| 90 | displayName: "Docs indexing", |
| 91 | type: "docs", |
| 92 | titles: docsIndexing.map((doc) => doc.title), |
| 93 | progressPercentage: mergeProgress(docsIndexing), |
| 94 | }); |
| 95 | } |
| 96 | return mergedStates; |
| 97 | }, [config, indexingStatuses]); |
| 98 | |
| 99 | if (!mergedIndexingStates.length) return null; |
| 100 | |
| 101 | return ( |
| 102 | <div className="flex flex-col gap-1"> |
| 103 | {mergedIndexingStates.map((state) => { |
| 104 | return <ChatIndexingPeek key={state.type} state={state} />; |
| 105 | })} |
| 106 | </div> |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | export default ChatIndexingPeeks; |