Optimistically remove deleted chats from local state and database queries (#104)

This commit is contained in:
Brian Hackett 2025-04-15 08:38:47 -07:00 committed by GitHub
parent 0e1363aadc
commit 8ea104c6cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View File

@ -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);

View File

@ -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) {