feat: enhance chat query handling and sidebar updates

- Updated chat query to include ordering by creation date for better organization.
- Improved optimistic updates in chat deletion and creation mutations to ensure UI consistency.
- Invalidate chat queries after mutations to refresh sidebar and components effectively.
- Adjusted cache key structure for chat queries to include additional parameters for better cache management.
This commit is contained in:
Anish Sarkar 2025-12-19 22:37:04 +05:30
parent 5fcaadf60b
commit ee46a43afc
4 changed files with 40 additions and 13 deletions

View file

@ -27,12 +27,21 @@ export const deleteChatMutationAtom = atomWithMutation((get) => {
onSuccess: (_, request: DeleteChatRequest) => {
toast.success("Chat deleted successfully");
// Optimistically update the current query
queryClient.setQueryData(
cacheKeys.chats.globalQueryParams(chatsQueryParams),
(oldData: ChatSummary[]) => {
return oldData.filter((chat) => chat.id !== request.id);
return oldData?.filter((chat) => chat.id !== request.id) ?? [];
}
);
// Invalidate all chat queries to ensure consistency across components
queryClient.invalidateQueries({
queryKey: ["chats"],
});
// Also invalidate the "all-chats" query used by AllChatsSidebar
queryClient.invalidateQueries({
queryKey: ["all-chats"],
});
},
};
});
@ -50,8 +59,14 @@ export const createChatMutationAtom = atomWithMutation((get) => {
},
onSuccess: () => {
// Invalidate ALL chat queries to ensure sidebar and other components refresh
// Using a partial key match to avoid stale closure issues with specific query params
queryClient.invalidateQueries({
queryKey: cacheKeys.chats.globalQueryParams(chatsQueryParams),
queryKey: ["chats"],
});
// Also invalidate the "all-chats" query used by AllChatsSidebar
queryClient.invalidateQueries({
queryKey: ["all-chats"],
});
},
};