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

137 lines
4.3 KiB
React
Raw Normal View History

2023-03-05 14:41:50 -05:00
import React, { useState, useEffect, useRef } from 'react';
2023-02-06 13:27:28 -05:00
import NewChat from './NewChat';
import Spinner from '../svg/Spinner';
import Conversations from '../Conversations';
2023-02-06 13:27:28 -05:00
import NavLinks from './NavLinks';
import useDidMountEffect from '~/hooks/useDidMountEffect';
import { swr } from '~/utils/fetchers';
2023-03-05 14:41:50 -05:00
import { useDispatch, useSelector } from 'react-redux';
2023-03-06 08:58:52 -05:00
import { incrementPage, setConvos } from '~/store/convoSlice';
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);
const { conversationId, convos, pageNumber, refreshConvoHint } = useSelector((state) => state.convo);
2023-03-06 08:58:52 -05:00
const onSuccess = (data) => {
dispatch(setConvos(data));
};
2023-03-05 14:41:50 -05:00
const { data, isLoading, mutate } = swr(
`/api/convos?pageNumber=${pageNumber}`,
2023-03-07 13:53:23 -05:00
onSuccess
);
2023-03-05 14:41:50 -05:00
const containerRef = useRef(null);
const scrollPositionRef = useRef(null);
2023-03-07 13:53:23 -05:00
const showMore = async (increment = true) => {
const container = containerRef.current;
if (container) {
scrollPositionRef.current = container.scrollTop;
}
2023-03-07 13:53:23 -05:00
if (increment) {
dispatch(incrementPage());
await mutate();
2023-03-05 14:41:50 -05:00
}
};
useDidMountEffect(() => mutate(), [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={"dark nav 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}>
{isLoading && pageNumber === 1 ? (
<Spinner />
) : (
<Conversations
conversations={convos}
conversationId={conversationId}
showMore={showMore}
pageNumber={pageNumber}
/>
)}
</div>
</div>
<NavLinks />
</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 hover:text-gray-900 focus:outline-none focus:ring-white hover:text-white text-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}>
2023-02-06 13:27:28 -05:00
</div>
</>
2023-02-06 13:27:28 -05:00
);
}