feat: paginate chat history (#903)
This commit is contained in:
parent
235b0edb91
commit
a07a3aad5b
6 changed files with 346 additions and 212 deletions
|
|
@ -1,10 +1,14 @@
|
|||
'use client';
|
||||
|
||||
import { updateChatVisibility } from '@/app/(chat)/actions';
|
||||
import { VisibilityType } from '@/components/visibility-selector';
|
||||
import { Chat } from '@/lib/db/schema';
|
||||
import { useMemo } from 'react';
|
||||
import useSWR, { useSWRConfig } from 'swr';
|
||||
import { unstable_serialize } from 'swr/infinite';
|
||||
import { updateChatVisibility } from '@/app/(chat)/actions';
|
||||
import {
|
||||
getChatHistoryPaginationKey,
|
||||
type ChatHistory,
|
||||
} from '@/components/sidebar-history';
|
||||
import type { VisibilityType } from '@/components/visibility-selector';
|
||||
|
||||
export function useChatVisibility({
|
||||
chatId,
|
||||
|
|
@ -14,7 +18,7 @@ export function useChatVisibility({
|
|||
initialVisibility: VisibilityType;
|
||||
}) {
|
||||
const { mutate, cache } = useSWRConfig();
|
||||
const history: Array<Chat> = cache.get('/api/history')?.data;
|
||||
const history: ChatHistory = cache.get('/api/history')?.data;
|
||||
|
||||
const { data: localVisibility, mutate: setLocalVisibility } = useSWR(
|
||||
`${chatId}-visibility`,
|
||||
|
|
@ -26,31 +30,14 @@ export function useChatVisibility({
|
|||
|
||||
const visibilityType = useMemo(() => {
|
||||
if (!history) return localVisibility;
|
||||
const chat = history.find((chat) => chat.id === chatId);
|
||||
const chat = history.chats.find((chat) => chat.id === chatId);
|
||||
if (!chat) return 'private';
|
||||
return chat.visibility;
|
||||
}, [history, chatId, localVisibility]);
|
||||
|
||||
const setVisibilityType = (updatedVisibilityType: VisibilityType) => {
|
||||
setLocalVisibility(updatedVisibilityType);
|
||||
|
||||
mutate<Array<Chat>>(
|
||||
'/api/history',
|
||||
(history) => {
|
||||
return history
|
||||
? history.map((chat) => {
|
||||
if (chat.id === chatId) {
|
||||
return {
|
||||
...chat,
|
||||
visibility: updatedVisibilityType,
|
||||
};
|
||||
}
|
||||
return chat;
|
||||
})
|
||||
: [];
|
||||
},
|
||||
{ revalidate: false },
|
||||
);
|
||||
mutate(unstable_serialize(getChatHistoryPaginationKey));
|
||||
|
||||
updateChatVisibility({
|
||||
chatId: chatId,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue