2023-03-18 15:59:59 -04:00
|
|
|
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
2023-03-18 14:28:10 -04:00
|
|
|
import _ from 'lodash';
|
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-03-18 14:28:10 -04:00
|
|
|
import Pages from '../Conversations/Pages';
|
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-03-18 17:49:24 -04:00
|
|
|
import { searchFetcher, swr } from '~/utils/fetchers';
|
2023-03-28 20:36:21 +08:00
|
|
|
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
|
|
|
|
|
|
|
|
|
|
import store from '~/store';
|
2023-03-18 14:28:10 -04:00
|
|
|
|
2023-03-12 00:32:03 +08:00
|
|
|
export default function Nav({ navVisible, setNavVisible }) {
|
2023-02-14 16:15:45 -05:00
|
|
|
const [isHovering, setIsHovering] = useState(false);
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
const containerRef = useRef(null);
|
|
|
|
|
const scrollPositionRef = useRef(null);
|
|
|
|
|
|
|
|
|
|
// const dispatch = useDispatch();
|
|
|
|
|
const [conversations, setConversations] = useState([]);
|
|
|
|
|
// current page
|
|
|
|
|
const [pageNumber, setPageNumber] = useState(1);
|
|
|
|
|
// total pages
|
2023-03-18 14:28:10 -04:00
|
|
|
const [pages, setPages] = useState(1);
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
// search
|
|
|
|
|
const searchQuery = useRecoilValue(store.searchQuery);
|
|
|
|
|
const isSearchEnabled = useRecoilValue(store.isSearchEnabled);
|
|
|
|
|
const isSearching = useRecoilValue(store.isSearching);
|
|
|
|
|
const { newConversation } = store.useConversation();
|
|
|
|
|
|
|
|
|
|
// current conversation
|
|
|
|
|
const conversation = useRecoilValue(store.conversation);
|
|
|
|
|
const { conversationId } = conversation || {};
|
|
|
|
|
const setMessages = useSetRecoilState(store.messages);
|
|
|
|
|
|
|
|
|
|
// refreshConversationsHint is used for other components to ask refresh of Nav
|
|
|
|
|
const refreshConversationsHint = useRecoilValue(store.refreshConversationsHint);
|
|
|
|
|
|
|
|
|
|
const { refreshConversations } = store.useConversations();
|
|
|
|
|
|
|
|
|
|
const [isFetching, setIsFetching] = useState(false);
|
|
|
|
|
|
2023-03-18 14:28:10 -04:00
|
|
|
const onSuccess = (data, searchFetch = false) => {
|
2023-03-28 20:36:21 +08:00
|
|
|
if (isSearching) {
|
2023-03-18 14:28:10 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2023-03-15 04:05:14 +08:00
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
let { conversations, pages } = data;
|
2023-03-14 20:21:41 -04:00
|
|
|
if (pageNumber > pages) {
|
2023-03-28 20:36:21 +08:00
|
|
|
setPageNumber(pages);
|
2023-03-14 20:21:41 -04:00
|
|
|
} else {
|
2023-03-28 20:36:21 +08:00
|
|
|
if (!searchFetch)
|
|
|
|
|
conversations = conversations.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
|
|
|
|
|
setConversations(conversations);
|
2023-03-18 14:28:10 -04:00
|
|
|
setPages(pages);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSearchSuccess = (data, expectedPage) => {
|
|
|
|
|
const res = data;
|
2023-03-28 20:36:21 +08:00
|
|
|
setConversations(res.conversations);
|
2023-03-18 14:28:10 -04:00
|
|
|
if (expectedPage) {
|
2023-03-28 20:36:21 +08:00
|
|
|
setPageNumber(expectedPage);
|
2023-03-14 20:21:41 -04:00
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
setPageNumber(res.pageNumber);
|
2023-03-18 14:28:10 -04:00
|
|
|
setPages(res.pages);
|
2023-03-18 15:59:59 -04:00
|
|
|
setIsFetching(false);
|
2023-03-22 18:26:29 -04:00
|
|
|
if (res.messages?.length > 0) {
|
2023-03-28 20:36:21 +08:00
|
|
|
setMessages(res.messages);
|
|
|
|
|
// dispatch(setDisabled(true));
|
2023-03-18 23:18:36 -04:00
|
|
|
}
|
2023-03-06 08:58:52 -05:00
|
|
|
};
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
// TODO: dont need this
|
|
|
|
|
const fetch = useCallback(
|
|
|
|
|
_.partialRight(
|
|
|
|
|
searchFetcher.bind(null, () => setIsFetching(true)),
|
|
|
|
|
onSearchSuccess
|
|
|
|
|
),
|
|
|
|
|
[setIsFetching]
|
|
|
|
|
);
|
2023-03-18 15:59:59 -04:00
|
|
|
|
2023-03-18 14:28:10 -04:00
|
|
|
const clearSearch = () => {
|
2023-03-28 20:36:21 +08:00
|
|
|
setPageNumber(1);
|
|
|
|
|
refreshConversations();
|
|
|
|
|
if (conversationId == 'search') {
|
|
|
|
|
newConversation();
|
2023-03-22 16:06:11 -04:00
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
// dispatch(setDisabled(false));
|
2023-03-18 14:28:10 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { data, isLoading, mutate } = swr(`/api/convos?pageNumber=${pageNumber}`, onSuccess, {
|
2023-03-28 20:36:21 +08:00
|
|
|
revalidateOnMount: false
|
2023-03-14 20:21:41 -04:00
|
|
|
});
|
2023-03-13 14:04:47 +08:00
|
|
|
|
2023-03-15 04:05:14 +08:00
|
|
|
const nextPage = async () => {
|
2023-03-14 20:21:41 -04:00
|
|
|
moveToTop();
|
2023-03-15 04:05:14 +08:00
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
if (!isSearching) {
|
|
|
|
|
setPageNumber(prev => prev + 1);
|
2023-03-18 14:28:10 -04:00
|
|
|
await mutate();
|
|
|
|
|
} else {
|
2023-03-28 20:36:21 +08:00
|
|
|
await fetch(searchQuery, +pageNumber + 1);
|
2023-03-18 14:28:10 -04:00
|
|
|
}
|
2023-03-15 04:05:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const previousPage = async () => {
|
2023-03-14 20:21:41 -04:00
|
|
|
moveToTop();
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
if (!isSearching) {
|
|
|
|
|
setPageNumber(prev => prev - 1);
|
2023-03-18 14:28:10 -04:00
|
|
|
await mutate();
|
|
|
|
|
} else {
|
2023-03-28 20:36:21 +08:00
|
|
|
await fetch(searchQuery, +pageNumber - 1);
|
2023-03-18 14:28:10 -04:00
|
|
|
}
|
2023-03-05 14:41:50 -05:00
|
|
|
};
|
2023-02-14 16:15:45 -05:00
|
|
|
|
2023-03-14 20:21:41 -04:00
|
|
|
useEffect(() => {
|
2023-03-28 20:36:21 +08:00
|
|
|
if (!isSearching) {
|
2023-03-18 14:28:10 -04:00
|
|
|
mutate();
|
|
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
}, [pageNumber, conversationId, refreshConversationsHint]);
|
2023-02-13 23:14:35 -05:00
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
const moveToTop = () => {
|
|
|
|
|
const container = containerRef.current;
|
|
|
|
|
if (container) {
|
|
|
|
|
scrollPositionRef.current = container.scrollTop;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const moveTo = () => {
|
2023-03-05 14:41:50 -05:00
|
|
|
const container = containerRef.current;
|
|
|
|
|
|
|
|
|
|
if (container && scrollPositionRef.current !== null) {
|
|
|
|
|
const { scrollHeight, clientHeight } = container;
|
|
|
|
|
const maxScrollTop = scrollHeight - clientHeight;
|
|
|
|
|
|
|
|
|
|
container.scrollTop = Math.min(maxScrollTop, scrollPositionRef.current);
|
|
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const toggleNavVisible = () => {
|
|
|
|
|
setNavVisible(prev => !prev);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
moveTo();
|
2023-03-18 23:18:36 -04:00
|
|
|
}, [data]);
|
2023-03-05 14:41:50 -05:00
|
|
|
|
2023-03-12 00:32:03 +08:00
|
|
|
useEffect(() => {
|
2023-03-14 20:21:41 -04:00
|
|
|
setNavVisible(false);
|
|
|
|
|
}, [conversationId]);
|
2023-03-12 00:32:03 +08:00
|
|
|
|
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 (
|
2023-03-12 00:32:03 +08:00
|
|
|
<>
|
2023-03-14 20:21:41 -04:00
|
|
|
<div
|
|
|
|
|
className={
|
|
|
|
|
'nav dark bg-gray-900 md:fixed md:inset-y-0 md:flex md:w-[260px] md:flex-col' +
|
|
|
|
|
(navVisible ? ' active' : '')
|
|
|
|
|
}
|
|
|
|
|
>
|
2023-03-12 00:32:03 +08:00
|
|
|
<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) ? ( */}
|
2023-03-28 20:36:21 +08:00
|
|
|
{(isLoading && pageNumber === 1) || isFetching ? (
|
2023-03-12 00:32:03 +08:00
|
|
|
<Spinner />
|
|
|
|
|
) : (
|
|
|
|
|
<Conversations
|
2023-03-28 20:36:21 +08:00
|
|
|
conversations={conversations}
|
2023-03-12 00:32:03 +08:00
|
|
|
conversationId={conversationId}
|
2023-03-15 04:05:14 +08:00
|
|
|
moveToTop={moveToTop}
|
2023-03-12 00:32:03 +08:00
|
|
|
/>
|
|
|
|
|
)}
|
2023-03-18 14:28:10 -04:00
|
|
|
<Pages
|
|
|
|
|
pageNumber={pageNumber}
|
|
|
|
|
pages={pages}
|
|
|
|
|
nextPage={nextPage}
|
|
|
|
|
previousPage={previousPage}
|
|
|
|
|
/>
|
2023-03-12 00:32:03 +08:00
|
|
|
</div>
|
2023-02-14 16:15:45 -05:00
|
|
|
</div>
|
2023-03-18 14:28:10 -04:00
|
|
|
<NavLinks
|
2023-03-18 15:59:59 -04:00
|
|
|
fetch={fetch}
|
2023-03-18 14:28:10 -04:00
|
|
|
onSearchSuccess={onSearchSuccess}
|
|
|
|
|
clearSearch={clearSearch}
|
2023-03-28 20:36:21 +08:00
|
|
|
isSearchEnabled={isSearchEnabled}
|
2023-03-18 14:28:10 -04:00
|
|
|
/>
|
2023-03-12 00:32:03 +08:00
|
|
|
</nav>
|
|
|
|
|
</div>
|
2023-02-06 13:27:28 -05:00
|
|
|
</div>
|
2023-03-12 00:32:03 +08:00
|
|
|
<button
|
|
|
|
|
type="button"
|
2023-03-14 20:21:41 -04:00
|
|
|
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"
|
2023-03-12 00:32:03 +08:00
|
|
|
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>
|
2023-03-14 20:21:41 -04:00
|
|
|
<div
|
|
|
|
|
className={'nav-mask' + (navVisible ? ' active' : '')}
|
|
|
|
|
onClick={toggleNavVisible}
|
|
|
|
|
></div>
|
2023-03-12 00:32:03 +08:00
|
|
|
</>
|
2023-02-06 13:27:28 -05:00
|
|
|
);
|
|
|
|
|
}
|