import { useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useDispatch, useSelector } from 'react-redux'; import { NavLink, useNavigate } from 'react-router-dom'; import conversationService from './api/services/conversationService'; import userService from './api/services/userService'; import Add from './assets/add.svg'; import openNewChat from './assets/openNewChat.svg'; import Hamburger from './assets/hamburger.svg'; import DocsGPT3 from './assets/cute_docsgpt3.svg'; import Discord from './assets/discord.svg'; import Expand from './assets/expand.svg'; import Github from './assets/github.svg'; import SettingGear from './assets/settingGear.svg'; import Twitter from './assets/TwitterX.svg'; import UploadIcon from './assets/upload.svg'; import SourceDropdown from './components/SourceDropdown'; import { setConversation, updateConversationId, handleAbort, } from './conversation/conversationSlice'; import ConversationTile from './conversation/ConversationTile'; import { useDarkTheme, useMediaQuery, useOutsideAlerter } from './hooks'; import useDefaultDocument from './hooks/useDefaultDocument'; import DeleteConvModal from './modals/DeleteConvModal'; import { ActiveState, Doc } from './models/misc'; import APIKeyModal from './preferences/APIKeyModal'; import { getConversations, getDocs } from './preferences/preferenceApi'; import { selectApiKeyStatus, selectConversationId, selectConversations, selectModalStateDeleteConv, selectSelectedDocs, selectSourceDocs, selectPaginatedDocuments, setConversations, setModalStateDeleteConv, setSelectedDocs, setSourceDocs, setPaginatedDocuments, } from './preferences/preferenceSlice'; import Spinner from './assets/spinner.svg'; import SpinnerDark from './assets/spinner-dark.svg'; import { selectQueries } from './conversation/conversationSlice'; import Upload from './upload/Upload'; import Help from './components/Help'; interface NavigationProps { navOpen: boolean; setNavOpen: React.Dispatch>; } /* const NavImage: React.FC<{ Light: string | undefined; Dark: string | undefined; }> = ({ Light, Dark }) => { return ( <> icon icon ); }; NavImage.propTypes = { Light: PropTypes.string, Dark: PropTypes.string, }; */ export default function Navigation({ navOpen, setNavOpen }: NavigationProps) { const dispatch = useDispatch(); const queries = useSelector(selectQueries); const docs = useSelector(selectSourceDocs); const selectedDocs = useSelector(selectSelectedDocs); const conversations = useSelector(selectConversations); const modalStateDeleteConv = useSelector(selectModalStateDeleteConv); const conversationId = useSelector(selectConversationId); const paginatedDocuments = useSelector(selectPaginatedDocuments); const [isDeletingConversation, setIsDeletingConversation] = useState(false); const { isMobile } = useMediaQuery(); const [isDarkTheme] = useDarkTheme(); const [isDocsListOpen, setIsDocsListOpen] = useState(false); const { t } = useTranslation(); const isApiKeySet = useSelector(selectApiKeyStatus); const [apiKeyModalState, setApiKeyModalState] = useState('INACTIVE'); const [uploadModalState, setUploadModalState] = useState('INACTIVE'); const navRef = useRef(null); const navigate = useNavigate(); useEffect(() => { if (!conversations?.data) { fetchConversations(); } if (queries.length === 0) { resetConversation(); } }, [conversations?.data, dispatch]); async function fetchConversations() { dispatch(setConversations({ ...conversations, loading: true })); return await getConversations() .then((fetchedConversations) => { dispatch(setConversations(fetchedConversations)); }) .catch((error) => { console.error('Failed to fetch conversations: ', error); dispatch(setConversations({ data: null, loading: false })); }); } const handleDeleteAllConversations = () => { setIsDeletingConversation(true); conversationService .deleteAll() .then(() => { fetchConversations(); }) .catch((error) => console.error(error)); }; const handleDeleteConversation = (id: string) => { setIsDeletingConversation(true); conversationService .delete(id, {}) .then(() => { fetchConversations(); resetConversation(); }) .catch((error) => console.error(error)); }; const handleDeleteClick = (doc: Doc) => { userService .deletePath(doc.id ?? '') .then(() => { return getDocs(); }) .then((updatedDocs) => { dispatch(setSourceDocs(updatedDocs)); const updatedPaginatedDocs = paginatedDocuments?.filter( (document) => document.id !== doc.id, ); dispatch( setPaginatedDocuments(updatedPaginatedDocs || paginatedDocuments), ); dispatch( setSelectedDocs( Array.isArray(updatedDocs) && updatedDocs?.find( (doc: Doc) => doc.name.toLowerCase() === 'default', ), ), ); }) .catch((error) => console.error(error)); }; const handleConversationClick = (index: string) => { conversationService .getConversation(index) .then((response) => response.json()) .then((data) => { navigate('/'); dispatch(setConversation(data)); dispatch( updateConversationId({ query: { conversationId: index }, }), ); }); }; const resetConversation = () => { handleAbort(); dispatch(setConversation([])); dispatch( updateConversationId({ query: { conversationId: null }, }), ); }; const newChat = () => { if (queries && queries?.length > 0) { resetConversation(); } }; async function updateConversationName(updatedConversation: { name: string; id: string; }) { await conversationService .update(updatedConversation) .then((response) => response.json()) .then((data) => { if (data) { navigate('/'); fetchConversations(); } }) .catch((err) => { console.error(err); }); } useOutsideAlerter(navRef, () => { if (isMobile && navOpen && apiKeyModalState === 'INACTIVE') { setNavOpen(false); setIsDocsListOpen(false); } }, [navOpen, 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(() => { setNavOpen(!isMobile); }, [isMobile]); useDefaultDocument(); return ( <> {!navOpen && (
{queries?.length > 0 && ( )}
DocsGPT
)}
{ if (isMobile) { setNavOpen(!navOpen); } }} >

DocsGPT

{ if (isMobile) { setNavOpen(!navOpen); } resetConversation(); }} className={({ isActive }) => `${ isActive ? 'bg-gray-3000 dark:bg-transparent' : '' } group sticky mx-4 mt-4 flex cursor-pointer gap-2.5 rounded-3xl border border-silver p-3 hover:border-rainy-gray hover:bg-gray-3000 dark:border-purple-taupe dark:text-white dark:hover:bg-transparent` } > new

{t('newChat')}

{conversations?.loading && !isDeletingConversation && (
Loading...
)} {conversations?.data && conversations.data.length > 0 ? (

{t('chats')}

{conversations.data?.map((conversation) => ( handleConversationClick(id)} onCoversationClick={() => { if (isMobile) { setNavOpen(false); } }} onDeleteConversation={(id) => handleDeleteConversation(id)} onSave={(conversation) => updateConversationName(conversation) } /> ))}
) : ( <> )}
{ if (isMobile) { setNavOpen(!navOpen); } }} /> { setUploadModalState('ACTIVE'); if (isMobile) { setNavOpen(!navOpen); } }} >

{t('sourceDocs')}

{ if (isMobile) { setNavOpen(!navOpen); } resetConversation(); }} to="/settings" className={({ isActive }) => `my-auto mx-4 flex h-9 cursor-pointer gap-4 rounded-3xl hover:bg-gray-100 dark:hover:bg-[#28292E] ${ isActive ? 'bg-gray-3000 dark:bg-transparent' : '' }` } > icon

{t('settings.label')}

discord x github
DocsGPT
{uploadModalState === 'ACTIVE' && ( setUploadModalState('INACTIVE')} > )} ); }