LibreChat/client/src/components/Nav/index.jsx

229 lines
6.8 KiB
React
Raw Normal View History

2023-03-18 15:59:59 -04:00
import React, { useState, useEffect, useRef, useCallback } from 'react';
import _ from 'lodash';
2023-02-06 13:27:28 -05:00
import NewChat from './NewChat';
import Spinner from '../svg/Spinner';
import Pages from '../Conversations/Pages';
import Conversations from '../Conversations';
2023-02-06 13:27:28 -05:00
import NavLinks from './NavLinks';
2023-04-02 12:49:12 -07:00
import { searchFetcher } from '~/utils/fetchers';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { useGetConversationsQuery } from '~/data-provider';
import store from '~/store';
export default function Nav({ navVisible, setNavVisible }) {
const [isHovering, setIsHovering] = useState(false);
const containerRef = useRef(null);
const scrollPositionRef = useRef(null);
// const dispatch = useDispatch();
const [conversations, setConversations] = useState([]);
// current page
const [pageNumber, setPageNumber] = useState(1);
// total pages
const [pages, setPages] = useState(1);
2023-04-02 12:49:12 -07:00
// data provider
const getConversationsQuery = useGetConversationsQuery(pageNumber);
// search
const searchQuery = useRecoilValue(store.searchQuery);
const isSearchEnabled = useRecoilValue(store.isSearchEnabled);
const isSearching = useRecoilValue(store.isSearching);
const { newConversation, searchPlaceholderConversation } = store.useConversation();
// current conversation
const conversation = useRecoilValue(store.conversation);
const { conversationId } = conversation || {};
const setSearchResultMessages = useSetRecoilState(store.searchResultMessages);
const refreshConversationsHint = useRecoilValue(store.refreshConversationsHint);
const { refreshConversations } = store.useConversations();
const [isFetching, setIsFetching] = useState(false);
const onSearchSuccess = (data, expectedPage) => {
const res = data;
setConversations(res.conversations);
if (expectedPage) {
setPageNumber(expectedPage);
}
setPageNumber(res.pageNumber);
setPages(res.pages);
2023-03-18 15:59:59 -04:00
setIsFetching(false);
searchPlaceholderConversation();
setSearchResultMessages(res.messages);
2023-03-06 08:58:52 -05:00
};
// TODO: dont need this
const fetch = useCallback(
_.partialRight(
searchFetcher.bind(null, () => setIsFetching(true)),
onSearchSuccess
),
[setIsFetching]
);
2023-03-18 15:59:59 -04:00
const clearSearch = () => {
setPageNumber(1);
refreshConversations();
if (conversationId == 'search') {
newConversation();
2023-03-22 16:06:11 -04:00
}
};
2023-03-15 04:05:14 +08:00
const nextPage = async () => {
moveToTop();
2023-03-15 04:05:14 +08:00
if (!isSearching) {
setPageNumber(prev => prev + 1);
2023-04-02 12:49:12 -07:00
await getConversationsQuery.refetch()
} else {
await fetch(searchQuery, +pageNumber + 1);
}
2023-03-15 04:05:14 +08:00
};
const previousPage = async () => {
moveToTop();
if (!isSearching) {
setPageNumber(prev => prev - 1);
2023-04-02 12:49:12 -07:00
await getConversationsQuery.refetch()
} else {
await fetch(searchQuery, +pageNumber - 1);
}
2023-03-05 14:41:50 -05:00
};
useEffect(() => {
2023-04-02 12:49:12 -07:00
if (getConversationsQuery.data) {
if (isSearching) {
return;
}
let { conversations, pages } = getConversationsQuery.data;
if (pageNumber > pages) {
setPageNumber(pages);
} else {
if (!isSearching) {
conversations = conversations.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
}
setConversations(conversations);
setPages(pages);
}
}
2023-04-02 12:49:12 -07:00
}, [getConversationsQuery.isSuccess, getConversationsQuery.data, isSearching, pageNumber]);
useEffect(() => {
if (!isSearching) {
getConversationsQuery.refetch();
}
}, [pageNumber, conversationId, refreshConversationsHint]);
const moveToTop = () => {
const container = containerRef.current;
if (container) {
scrollPositionRef.current = container.scrollTop;
}
};
const toggleNavVisible = () => {
setNavVisible(prev => !prev);
};
useEffect(() => {
setNavVisible(false);
2023-04-02 12:49:12 -07:00
}, [conversationId, setNavVisible]);
2023-03-07 13:53:23 -05:00
const containerClasses =
2023-04-02 12:49:12 -07:00
getConversationsQuery.isLoading && pageNumber === 1
2023-03-07 13:53:23 -05:00
? 'flex flex-col gap-2 text-gray-100 text-sm h-full justify-center items-center'
: 'flex flex-col gap-2 text-gray-100 text-sm';
2023-02-06 13:27:28 -05:00
return (
<>
<div
className={
'nav dark bg-gray-900 md:fixed md:inset-y-0 md:flex md:w-[260px] md:flex-col' +
(navVisible ? ' active' : '')
}
>
<div className="flex h-full min-h-0 flex-col ">
<div className="scrollbar-trigger flex h-full w-full flex-1 items-start border-white/20">
<nav className="flex h-full flex-1 flex-col space-y-1 p-2">
<NewChat />
<div
className={`flex-1 flex-col overflow-y-auto ${
isHovering ? '' : 'scrollbar-transparent'
} border-b border-white/20`}
onMouseEnter={() => setIsHovering(true)}
onMouseLeave={() => setIsHovering(false)}
ref={containerRef}
>
<div className={containerClasses}>
2023-04-02 12:49:12 -07:00
{(getConversationsQuery.isLoading && pageNumber === 1) || isFetching ? (
<Spinner />
) : (
<Conversations
conversations={conversations}
conversationId={conversationId}
2023-03-15 04:05:14 +08:00
moveToTop={moveToTop}
/>
)}
<Pages
pageNumber={pageNumber}
pages={pages}
nextPage={nextPage}
previousPage={previousPage}
/>
</div>
</div>
<NavLinks
2023-03-18 15:59:59 -04:00
fetch={fetch}
onSearchSuccess={onSearchSuccess}
clearSearch={clearSearch}
isSearchEnabled={isSearchEnabled}
/>
</nav>
</div>
2023-02-06 13:27:28 -05:00
</div>
<button
type="button"
className="nav-close-button -ml-0.5 -mt-0.5 inline-flex h-10 w-10 items-center justify-center rounded-md text-white hover:text-gray-900 hover:text-white focus:outline-none focus:ring-white"
onClick={toggleNavVisible}
>
<span className="sr-only">Open sidebar</span>
<svg
stroke="currentColor"
fill="none"
strokeWidth="1.5"
viewBox="0 0 24 24"
strokeLinecap="round"
strokeLinejoin="round"
className="h-6 w-6"
height="1em"
width="1em"
xmlns="http://www.w3.org/2000/svg"
>
<line
x1="3"
y1="6"
x2="15"
y2="18"
/>
<line
x1="3"
y1="18"
x2="15"
y2="6"
/>
</svg>
</button>
</div>
<div
className={'nav-mask' + (navVisible ? ' active' : '')}
onClick={toggleNavVisible}
></div>
</>
2023-02-06 13:27:28 -05:00
);
}