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';
|
2023-02-14 16:15:45 -05:00
|
|
|
import Spinner from '../svg/Spinner';
|
2023-02-07 10:26:19 -05:00
|
|
|
import Conversations from '../Conversations';
|
2023-02-06 13:27:28 -05:00
|
|
|
import NavLinks from './NavLinks';
|
2023-02-14 16:15:45 -05:00
|
|
|
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
|
|
|
|
2023-02-13 10:20:21 -05:00
|
|
|
export default function Nav() {
|
2023-03-05 14:41:50 -05:00
|
|
|
const dispatch = useDispatch();
|
2023-02-14 16:15:45 -05:00
|
|
|
const [isHovering, setIsHovering] = useState(false);
|
2023-03-06 08:58:52 -05:00
|
|
|
const { conversationId, convos, pageNumber } = useSelector((state) => state.convo);
|
|
|
|
|
const onSuccess = (data) => {
|
|
|
|
|
dispatch(setConvos(data));
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-05 14:41:50 -05:00
|
|
|
const { data, isLoading, mutate } = swr(
|
2023-03-10 21:06:13 +08:00
|
|
|
`/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) => {
|
|
|
|
|
if (increment) {
|
|
|
|
|
const container = containerRef.current;
|
|
|
|
|
if (container) {
|
|
|
|
|
scrollPositionRef.current = container.scrollTop;
|
|
|
|
|
}
|
|
|
|
|
dispatch(incrementPage());
|
2023-03-05 14:41:50 -05:00
|
|
|
}
|
|
|
|
|
await mutate();
|
|
|
|
|
};
|
2023-02-14 16:15:45 -05:00
|
|
|
|
|
|
|
|
useDidMountEffect(() => mutate(), [conversationId]);
|
2023-02-13 23:14:35 -05:00
|
|
|
|
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]);
|
|
|
|
|
|
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-25 10:16:21 -05:00
|
|
|
|
2023-02-06 13:27:28 -05:00
|
|
|
return (
|
|
|
|
|
<div className="dark hidden bg-gray-900 md:fixed md:inset-y-0 md:flex md:w-[260px] md:flex-col">
|
|
|
|
|
<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 />
|
2023-02-14 16:15:45 -05:00
|
|
|
<div
|
|
|
|
|
className={`-mr-2 flex-1 flex-col overflow-y-auto ${
|
|
|
|
|
isHovering ? '' : 'scrollbar-transparent'
|
|
|
|
|
} border-b border-white/20`}
|
|
|
|
|
onMouseEnter={() => setIsHovering(true)}
|
|
|
|
|
onMouseLeave={() => setIsHovering(false)}
|
2023-03-05 14:41:50 -05:00
|
|
|
ref={containerRef}
|
2023-02-14 16:15:45 -05:00
|
|
|
>
|
2023-02-25 10:16:21 -05:00
|
|
|
<div className={containerClasses}>
|
2023-03-05 14:41:50 -05:00
|
|
|
{isLoading && pageNumber === 1 ? (
|
2023-02-25 10:16:21 -05:00
|
|
|
<Spinner />
|
|
|
|
|
) : (
|
|
|
|
|
<Conversations
|
2023-03-06 08:58:52 -05:00
|
|
|
conversations={convos}
|
2023-02-25 10:16:21 -05:00
|
|
|
conversationId={conversationId}
|
2023-03-05 14:41:50 -05:00
|
|
|
showMore={showMore}
|
|
|
|
|
pageNumber={pageNumber}
|
2023-02-25 10:16:21 -05:00
|
|
|
/>
|
|
|
|
|
)}
|
2023-02-14 16:15:45 -05:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2023-02-06 13:27:28 -05:00
|
|
|
<NavLinks />
|
|
|
|
|
</nav>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|