mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-06-26 18:26:38 +00:00
Optimistically remove deleted chats from local state and database queries (#104)
This commit is contained in:
parent
0e1363aadc
commit
8ea104c6cc
@ -62,6 +62,9 @@ export const Menu = () => {
|
||||
const deleteItem = useCallback((event: React.UIEvent, item: ChatContents) => {
|
||||
event.preventDefault();
|
||||
|
||||
// Optimistically remove the item from the list while we update the database.
|
||||
setList(list.filter((chat) => chat.id !== item.id));
|
||||
|
||||
database
|
||||
.deleteChat(item.id)
|
||||
.then(() => {
|
||||
@ -76,7 +79,7 @@ export const Menu = () => {
|
||||
toast.error('Failed to delete conversation');
|
||||
logger.error(error);
|
||||
});
|
||||
}, []);
|
||||
}, [list]);
|
||||
|
||||
const closeDialog = () => {
|
||||
setDialogContent(null);
|
||||
|
||||
@ -50,6 +50,11 @@ function setLocalChats(chats: ChatContents[] | undefined): void {
|
||||
}
|
||||
}
|
||||
|
||||
// Chats we've deleted locally. We never return these from the database afterwards
|
||||
// to present a coherent view of the chats in case the chats are queried before the
|
||||
// delete finishes.
|
||||
const deletedChats = new Set<string>();
|
||||
|
||||
async function getAllChats(): Promise<ChatContents[]> {
|
||||
const userId = await getCurrentUserId();
|
||||
|
||||
@ -63,7 +68,8 @@ async function getAllChats(): Promise<ChatContents[]> {
|
||||
throw error;
|
||||
}
|
||||
|
||||
return data.map(databaseRowToChatContents);
|
||||
const chats = data.map(databaseRowToChatContents);
|
||||
return chats.filter(chat => !deletedChats.has(chat.id));
|
||||
}
|
||||
|
||||
async function syncLocalChats(): Promise<void> {
|
||||
@ -152,6 +158,8 @@ async function getChatContents(id: string): Promise<ChatContents | undefined> {
|
||||
}
|
||||
|
||||
async function deleteChat(id: string): Promise<void> {
|
||||
deletedChats.add(id);
|
||||
|
||||
const userId = await getCurrentUserId();
|
||||
|
||||
if (!userId) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user