import { useEffect, useRef, useState } from 'react'; import { NavLink } from 'react-router-dom'; import Arrow1 from './assets/arrow.svg'; import Arrow2 from './assets/dropdown-arrow.svg'; import Exit from './assets/exit.svg'; import Message from './assets/message.svg'; import Hamburger from './assets/hamburger.svg'; import Key from './assets/key.svg'; import Info from './assets/info.svg'; import Link from './assets/link.svg'; import UploadIcon from './assets/upload.svg'; import { ActiveState } from './models/misc'; import APIKeyModal from './preferences/APIKeyModal'; import { useDispatch, useSelector } from 'react-redux'; import { selectApiKeyStatus, selectSelectedDocs, selectSelectedDocsStatus, selectSourceDocs, setSelectedDocs, selectConversations, setConversations, selectConversationId, } from './preferences/preferenceSlice'; import { setConversation, updateConversationId, } from './conversation/conversationSlice'; import { useOutsideAlerter } from './hooks'; import Upload from './upload/Upload'; import { Doc, getConversations } from './preferences/preferenceApi'; import SelectDocsModal from './preferences/SelectDocsModal'; export default function Navigation({ navState, setNavState, }: { navState: ActiveState; setNavState: React.Dispatch>; }) { const dispatch = useDispatch(); const docs = useSelector(selectSourceDocs); const selectedDocs = useSelector(selectSelectedDocs); const conversations = useSelector(selectConversations); const conversationId = useSelector(selectConversationId); const [isDocsListOpen, setIsDocsListOpen] = useState(false); const isApiKeySet = useSelector(selectApiKeyStatus); const [apiKeyModalState, setApiKeyModalState] = useState('INACTIVE'); const isSelectedDocsSet = useSelector(selectSelectedDocsStatus); const [selectedDocsModalState, setSelectedDocsModalState] = useState(isSelectedDocsSet ? 'INACTIVE' : 'ACTIVE'); const [uploadModalState, setUploadModalState] = useState('INACTIVE'); const navRef = useRef(null); const apiHost = import.meta.env.VITE_API_HOST || 'https://docsapi.arc53.com'; const embeddingsName = import.meta.env.VITE_EMBEDDINGS_NAME || 'openai_text-embedding-ada-002'; useEffect(() => { if (!conversations) { getConversations() .then((fetchedConversations) => { dispatch(setConversations(fetchedConversations)); }) .catch((error) => { console.error('Failed to fetch conversations: ', error); }); } }, [conversations, dispatch]); const handleDeleteConversation = (id: string) => { fetch(`${apiHost}/api/delete_conversation?id=${id}`, { method: 'POST', }) .then(() => { // remove the image element from the DOM const imageElement = document.querySelector( `#img-${id}`, ) as HTMLElement; const parentElement = imageElement.parentNode as HTMLElement; parentElement.parentNode?.removeChild(parentElement); }) .catch((error) => console.error(error)); }; const handleDeleteClick = (index: number, doc: Doc) => { const docPath = 'indexes/' + 'local' + '/' + doc.name; fetch(`${apiHost}/api/delete_old?path=${docPath}`, { method: 'GET', }) .then(() => { // remove the image element from the DOM const imageElement = document.querySelector( `#img-${index}`, ) as HTMLElement; const parentElement = imageElement.parentNode as HTMLElement; parentElement.parentNode?.removeChild(parentElement); }) .catch((error) => console.error(error)); }; const handleConversationClick = (index: string) => { // fetch the conversation from the server and setConversation in the store fetch(`${apiHost}/api/get_single_conversation?id=${index}`, { method: 'GET', }) .then((response) => response.json()) .then((data) => { dispatch(setConversation(data)); dispatch( updateConversationId({ query: { conversationId: index }, }), ); }); }; useOutsideAlerter( navRef, () => { if ( window.matchMedia('(max-width: 768px)').matches && navState === 'ACTIVE' && apiKeyModalState === 'INACTIVE' ) { setNavState('INACTIVE'); setIsDocsListOpen(false); } }, [navState, isDocsListOpen, apiKeyModalState], ); /* Needed to fix bug where if mobile nav was closed and then window was resized to desktop, nav would still be closed but the button to open would be gone, as per #1 on issue #146 */ useEffect(() => { window.addEventListener('resize', () => { if (window.matchMedia('(min-width: 768px)').matches) { setNavState('ACTIVE'); } else { setNavState('INACTIVE'); } }); }, []); return ( <>
{ dispatch(setConversation([])); dispatch(updateConversationId({ query: { conversationId: null } })); }} className={({ isActive }) => `${ isActive && conversationId === null ? 'bg-gray-3000' : '' } my-auto mx-4 mt-4 flex h-12 cursor-pointer gap-4 rounded-3xl hover:bg-gray-100` } >

New Chat

{conversations ? conversations.map((conversation) => { return (
{ handleConversationClick(conversation.id); }} className={`my-auto mx-4 mt-4 flex h-12 cursor-pointer items-center justify-between gap-4 rounded-3xl hover:bg-gray-100 ${ conversationId === conversation.id ? 'bg-gray-100' : '' }`} >

{conversation.name}

{conversationId === conversation.id ? ( Exit { event.stopPropagation(); handleDeleteConversation(conversation.id); }} /> ) : null}
); }) : null}
setIsDocsListOpen(!isDocsListOpen)} > {selectedDocs && (

{selectedDocs.name} {selectedDocs.version}

)} arrow
setUploadModalState('ACTIVE')} > {isDocsListOpen && (
{docs ? ( docs.map((doc, index) => { if (doc.model === embeddingsName) { return (
{ dispatch(setSelectedDocs(doc)); setIsDocsListOpen(false); }} className="flex h-10 w-full cursor-pointer items-center justify-between border-x-2 border-b-2 hover:bg-gray-100" >

{doc.name} {doc.version}

{doc.location === 'local' ? ( Exit { event.stopPropagation(); handleDeleteClick(index, doc); }} /> ) : null}
); } }) ) : (

No default documentation.

)}
)}

Source Docs

{ setApiKeyModalState('ACTIVE'); }} > key

Reset Key

`my-auto mx-4 flex h-12 cursor-pointer gap-4 rounded-3xl hover:bg-gray-100 ${ isActive ? 'bg-gray-3000' : '' }` } > info

About

link

Discord

link

Github

); }