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

218 lines
6.4 KiB
React
Raw Normal View History

2023-03-18 15:59:59 -04:00
import React, { useState, useEffect, useRef, useCallback } from 'react';
import axios from 'axios';
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';
import { swr } from '~/utils/fetchers';
2023-03-05 14:41:50 -05:00
import { useDispatch, useSelector } from 'react-redux';
import { setConvos, refreshConversation } from '~/store/convoSlice';
2023-03-18 15:59:59 -04:00
const fetcher = async (pre, q, pageNumber, callback) => {
pre();
const { data } = await axios.get(`/api/search?q=${q}&pageNumber=${pageNumber}`);
console.log(data);
callback(data);
};
2023-02-06 13:27:28 -05:00
export default function Nav({ navVisible, setNavVisible }) {
2023-03-05 14:41:50 -05:00
const dispatch = useDispatch();
const [isHovering, setIsHovering] = useState(false);
2023-03-18 15:59:59 -04:00
const [isFetching, setIsFetching] = useState(false);
const [pages, setPages] = useState(1);
const [pageNumber, setPage] = useState(1);
const { search, query } = useSelector((state) => state.search);
const { conversationId, convos, refreshConvoHint } = useSelector((state) => state.convo);
2023-03-18 15:59:59 -04:00
const onSuccess = (data, searchFetch = false) => {
if (search) {
return;
}
2023-03-15 04:05:14 +08:00
const { conversations, pages } = data;
if (pageNumber > pages) {
setPage(pages);
} else {
dispatch(setConvos({ convos: conversations, searchFetch }));
setPages(pages);
}
};
const onSearchSuccess = (data, expectedPage) => {
const res = data;
dispatch(setConvos({ convos: res.conversations, searchFetch: true }));
if (expectedPage) {
setPage(expectedPage);
}
setPage(res.pageNumber);
setPages(res.pages);
2023-03-18 15:59:59 -04:00
setIsFetching(false);
2023-03-06 08:58:52 -05:00
};
2023-03-18 15:59:59 -04:00
const fetch = useCallback(_.partialRight(fetcher.bind(null, () => setIsFetching(true)), onSearchSuccess), [dispatch]);
const clearSearch = () => {
setPage(1);
dispatch(refreshConversation());
};
const { data, isLoading, mutate } = swr(`/api/convos?pageNumber=${pageNumber}`, onSuccess, {
revalidateOnMount: false,
});
2023-03-05 14:41:50 -05:00
const containerRef = useRef(null);
const scrollPositionRef = useRef(null);
2023-03-15 04:05:14 +08:00
const moveToTop = () => {
const container = containerRef.current;
if (container) {
scrollPositionRef.current = container.scrollTop;
}
};
2023-03-15 04:05:14 +08:00
const nextPage = async () => {
moveToTop();
2023-03-15 04:05:14 +08:00
if (!search) {
setPage((prev) => prev + 1);
await mutate();
} else {
2023-03-18 15:59:59 -04:00
// await fetch(query, +pageNumber + 1, onSearchSuccess);
await fetch(query, +pageNumber + 1);
}
2023-03-15 04:05:14 +08:00
};
const previousPage = async () => {
moveToTop();
if (!search) {
setPage((prev) => prev - 1);
await mutate();
} else {
2023-03-18 15:59:59 -04:00
// await fetch(query, +pageNumber - 1, onSearchSuccess);
await fetch(query, +pageNumber - 1);
}
2023-03-05 14:41:50 -05:00
};
useEffect(() => {
if (!search) {
mutate();
}
}, [pageNumber, conversationId, refreshConvoHint]);
2023-03-05 14:41:50 -05:00
useEffect(() => {
const container = containerRef.current;
if (container && scrollPositionRef.current !== null) {
const { scrollHeight, clientHeight } = container;
const maxScrollTop = scrollHeight - clientHeight;
container.scrollTop = Math.min(maxScrollTop, scrollPositionRef.current);
}
}, [data]);
useEffect(() => {
setNavVisible(false);
}, [conversationId]);
const toggleNavVisible = () => {
setNavVisible((prev) => {
return !prev;
});
};
2023-03-07 13:53:23 -05:00
const containerClasses =
isLoading && pageNumber === 1
? '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-03-18 15:59:59 -04:00
{/* {(isLoading && pageNumber === 1) ? ( */}
{(isLoading && pageNumber === 1) || (isFetching) ? (
<Spinner />
) : (
<Conversations
conversations={convos}
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}
/>
</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
);
}