mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 17:30:16 +01:00
feat: add conversation query to nav
This commit is contained in:
parent
2048e34311
commit
ccc2f392e2
1 changed files with 26 additions and 49 deletions
|
|
@ -5,8 +5,9 @@ import Spinner from '../svg/Spinner';
|
||||||
import Pages from '../Conversations/Pages';
|
import Pages from '../Conversations/Pages';
|
||||||
import Conversations from '../Conversations';
|
import Conversations from '../Conversations';
|
||||||
import NavLinks from './NavLinks';
|
import NavLinks from './NavLinks';
|
||||||
import { searchFetcher, swr } from '~/utils/fetchers';
|
import { searchFetcher } from '~/utils/fetchers';
|
||||||
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
|
import { useRecoilValue, useSetRecoilState } from 'recoil';
|
||||||
|
import { useGetConversationsQuery } from '~/data-provider';
|
||||||
|
|
||||||
import store from '~/store';
|
import store from '~/store';
|
||||||
|
|
||||||
|
|
@ -23,6 +24,9 @@ export default function Nav({ navVisible, setNavVisible }) {
|
||||||
// total pages
|
// total pages
|
||||||
const [pages, setPages] = useState(1);
|
const [pages, setPages] = useState(1);
|
||||||
|
|
||||||
|
// data provider
|
||||||
|
const getConversationsQuery = useGetConversationsQuery(pageNumber);
|
||||||
|
|
||||||
// search
|
// search
|
||||||
const searchQuery = useRecoilValue(store.searchQuery);
|
const searchQuery = useRecoilValue(store.searchQuery);
|
||||||
const isSearchEnabled = useRecoilValue(store.isSearchEnabled);
|
const isSearchEnabled = useRecoilValue(store.isSearchEnabled);
|
||||||
|
|
@ -34,29 +38,10 @@ export default function Nav({ navVisible, setNavVisible }) {
|
||||||
const { conversationId } = conversation || {};
|
const { conversationId } = conversation || {};
|
||||||
const setSearchResultMessages = useSetRecoilState(store.searchResultMessages);
|
const setSearchResultMessages = useSetRecoilState(store.searchResultMessages);
|
||||||
|
|
||||||
// refreshConversationsHint is used for other components to ask refresh of Nav
|
|
||||||
const refreshConversationsHint = useRecoilValue(store.refreshConversationsHint);
|
|
||||||
|
|
||||||
const { refreshConversations } = store.useConversations();
|
const { refreshConversations } = store.useConversations();
|
||||||
|
|
||||||
const [isFetching, setIsFetching] = useState(false);
|
const [isFetching, setIsFetching] = useState(false);
|
||||||
|
|
||||||
const onSuccess = (data, searchFetch = false) => {
|
|
||||||
if (isSearching) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let { conversations, pages } = data;
|
|
||||||
if (pageNumber > pages) {
|
|
||||||
setPageNumber(pages);
|
|
||||||
} else {
|
|
||||||
if (!searchFetch)
|
|
||||||
conversations = conversations.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
|
|
||||||
setConversations(conversations);
|
|
||||||
setPages(pages);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const onSearchSuccess = (data, expectedPage) => {
|
const onSearchSuccess = (data, expectedPage) => {
|
||||||
const res = data;
|
const res = data;
|
||||||
setConversations(res.conversations);
|
setConversations(res.conversations);
|
||||||
|
|
@ -85,19 +70,14 @@ export default function Nav({ navVisible, setNavVisible }) {
|
||||||
if (conversationId == 'search') {
|
if (conversationId == 'search') {
|
||||||
newConversation();
|
newConversation();
|
||||||
}
|
}
|
||||||
// dispatch(setDisabled(false));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const { data, isLoading, mutate } = swr(`/api/convos?pageNumber=${pageNumber}`, onSuccess, {
|
|
||||||
revalidateOnMount: false
|
|
||||||
});
|
|
||||||
|
|
||||||
const nextPage = async () => {
|
const nextPage = async () => {
|
||||||
moveToTop();
|
moveToTop();
|
||||||
|
|
||||||
if (!isSearching) {
|
if (!isSearching) {
|
||||||
setPageNumber(prev => prev + 1);
|
setPageNumber(prev => prev + 1);
|
||||||
await mutate();
|
await getConversationsQuery.refetch()
|
||||||
} else {
|
} else {
|
||||||
await fetch(searchQuery, +pageNumber + 1);
|
await fetch(searchQuery, +pageNumber + 1);
|
||||||
}
|
}
|
||||||
|
|
@ -108,17 +88,29 @@ export default function Nav({ navVisible, setNavVisible }) {
|
||||||
|
|
||||||
if (!isSearching) {
|
if (!isSearching) {
|
||||||
setPageNumber(prev => prev - 1);
|
setPageNumber(prev => prev - 1);
|
||||||
await mutate();
|
await getConversationsQuery.refetch()
|
||||||
} else {
|
} else {
|
||||||
await fetch(searchQuery, +pageNumber - 1);
|
await fetch(searchQuery, +pageNumber - 1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isSearching) {
|
if (getConversationsQuery.data) {
|
||||||
mutate();
|
if (isSearching) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}, [pageNumber, conversationId, refreshConversationsHint]);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [getConversationsQuery.isSuccess, getConversationsQuery.data, isSearching, pageNumber]);
|
||||||
|
|
||||||
const moveToTop = () => {
|
const moveToTop = () => {
|
||||||
const container = containerRef.current;
|
const container = containerRef.current;
|
||||||
|
|
@ -127,31 +119,17 @@ export default function Nav({ navVisible, setNavVisible }) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const moveTo = () => {
|
|
||||||
const container = containerRef.current;
|
|
||||||
|
|
||||||
if (container && scrollPositionRef.current !== null) {
|
|
||||||
const { scrollHeight, clientHeight } = container;
|
|
||||||
const maxScrollTop = scrollHeight - clientHeight;
|
|
||||||
|
|
||||||
container.scrollTop = Math.min(maxScrollTop, scrollPositionRef.current);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const toggleNavVisible = () => {
|
const toggleNavVisible = () => {
|
||||||
setNavVisible(prev => !prev);
|
setNavVisible(prev => !prev);
|
||||||
};
|
};
|
||||||
|
|
||||||
// useEffect(() => {
|
|
||||||
// moveTo();
|
|
||||||
// }, [data]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setNavVisible(false);
|
setNavVisible(false);
|
||||||
}, [conversationId]);
|
}, [conversationId, setNavVisible]);
|
||||||
|
|
||||||
const containerClasses =
|
const containerClasses =
|
||||||
isLoading && pageNumber === 1
|
getConversationsQuery.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 h-full justify-center items-center'
|
||||||
: 'flex flex-col gap-2 text-gray-100 text-sm';
|
: 'flex flex-col gap-2 text-gray-100 text-sm';
|
||||||
|
|
||||||
|
|
@ -176,8 +154,7 @@ export default function Nav({ navVisible, setNavVisible }) {
|
||||||
ref={containerRef}
|
ref={containerRef}
|
||||||
>
|
>
|
||||||
<div className={containerClasses}>
|
<div className={containerClasses}>
|
||||||
{/* {(isLoading && pageNumber === 1) ? ( */}
|
{(getConversationsQuery.isLoading && pageNumber === 1) || isFetching ? (
|
||||||
{(isLoading && pageNumber === 1) || isFetching ? (
|
|
||||||
<Spinner />
|
<Spinner />
|
||||||
) : (
|
) : (
|
||||||
<Conversations
|
<Conversations
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue