Skip to content

πŸ™Œ OpenHands: AI-Driven Development

feat: implement optimistic updates for conversation deletion (#6745)

tofarr265e8ae5f4ed
diff --git a/frontend/src/components/features/conversation-panel/confirm-delete-modal.tsx b/frontend/src/components/features/conversation-panel/confirm-delete-modal.tsx
index 4dd7c183be..a649dc4ef8 100644
--- a/frontend/src/components/features/conversation-panel/confirm-delete-modal.tsx
+++ b/frontend/src/components/features/conversation-panel/confirm-delete-modal.tsx
@@ -22,7 +22,10 @@ export function ConfirmDeleteModal({
<BaseModalTitle title="Are you sure you want to delete this project?" />
<BaseModalDescription description="All data associated with this project will be lost." />
</div>
- <div className="flex flex-col gap-2 w-full">
+ <div
+ className="flex flex-col gap-2 w-full"
+ onClick={(event) => event.stopPropagation()}
+ >
<ModalButton
onClick={onConfirm}
className="bg-danger font-bold"
diff --git a/frontend/src/components/features/conversation-panel/conversation-card.tsx b/frontend/src/components/features/conversation-panel/conversation-card.tsx
index e7fb8119b4..f9b52f3857 100644
--- a/frontend/src/components/features/conversation-panel/conversation-card.tsx
+++ b/frontend/src/components/features/conversation-panel/conversation-card.tsx
@@ -68,6 +68,7 @@ export function ConversationCard({
event.preventDefault();
event.stopPropagation();
onDelete?.();
+ setContextMenuVisible(false);
};
const handleEdit = (event: React.MouseEvent<HTMLButtonElement>) => {
diff --git a/frontend/src/components/features/conversation-panel/conversation-panel.tsx b/frontend/src/components/features/conversation-panel/conversation-panel.tsx
index a96c649a54..d91a70755b 100644
--- a/frontend/src/components/features/conversation-panel/conversation-panel.tsx
+++ b/frontend/src/components/features/conversation-panel/conversation-panel.tsx
@@ -44,12 +44,16 @@ export function ConversationPanel({ onClose }: ConversationPanelProps) {
const handleConfirmDelete = () => {
if (selectedConversationId) {
- deleteConversation({ conversationId: selectedConversationId });
- setConfirmDeleteModalVisible(false);
-
- if (cid === selectedConversationId) {
- endSession();
- }
+ deleteConversation(
+ { conversationId: selectedConversationId },
+ {
+ onSuccess: () => {
+ if (cid === selectedConversationId) {
+ endSession();
+ }
+ },
+ },
+ );
}
};
@@ -110,7 +114,10 @@ export function ConversationPanel({ onClose }: ConversationPanelProps) {
{confirmDeleteModalVisible && (
<ConfirmDeleteModal
- onConfirm={handleConfirmDelete}
+ onConfirm={() => {
+ handleConfirmDelete();
+ setConfirmDeleteModalVisible(false);
+ }}
onCancel={() => setConfirmDeleteModalVisible(false)}
/>
)}
diff --git a/frontend/src/hooks/mutation/use-delete-conversation.ts b/frontend/src/hooks/mutation/use-delete-conversation.ts
index b0e3d6c90e..cedc5475ca 100644
--- a/frontend/src/hooks/mutation/use-delete-conversation.ts
+++ b/frontend/src/hooks/mutation/use-delete-conversation.ts
@@ -7,7 +7,32 @@ export const useDeleteConversation = () => {
return useMutation({
mutationFn: (variables: { conversationId: string }) =>
OpenHands.deleteUserConversation(variables.conversationId),
- onSuccess: () => {
+ onMutate: async (variables) => {
+ await queryClient.cancelQueries({ queryKey: ["user", "conversations"] });
+ const previousConversations = queryClient.getQueryData([
+ "user",
+ "conversations",
+ ]);
+
+ queryClient.setQueryData(
+ ["user", "conversations"],
+ (old: { conversation_id: string }[] | undefined) =>
+ old?.filter(
+ (conv) => conv.conversation_id !== variables.conversationId,
+ ),
+ );
+
+ return { previousConversations };
+ },
+ onError: (err, variables, context) => {
+ if (context?.previousConversations) {
+ queryClient.setQueryData(
+ ["user", "conversations"],
+ context.previousConversations,
+ );
+ }
+ },
+ onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["user", "conversations"] });
},
});