2023-04-03 12:39:00 -07:00
|
|
|
import { useEffect } from 'react';
|
2023-02-07 10:26:19 -05:00
|
|
|
import TrashIcon from '../svg/TrashIcon';
|
2023-02-11 10:22:15 -05:00
|
|
|
import CrossIcon from '../svg/CrossIcon';
|
2023-03-28 20:36:21 +08:00
|
|
|
import { useRecoilValue } from 'recoil';
|
2023-04-03 12:39:00 -07:00
|
|
|
import { useDeleteConversationMutation } from '~/data-provider';
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
import store from '~/store';
|
2023-02-07 16:22:35 -05:00
|
|
|
|
2023-03-11 22:59:32 -05:00
|
|
|
export default function DeleteButton({ conversationId, renaming, cancelHandler, retainView }) {
|
2023-03-28 20:36:21 +08:00
|
|
|
const currentConversation = useRecoilValue(store.conversation) || {};
|
|
|
|
|
const { newConversation } = store.useConversation();
|
|
|
|
|
const { refreshConversations } = store.useConversations();
|
2023-02-07 16:22:35 -05:00
|
|
|
|
2023-04-03 12:39:00 -07:00
|
|
|
const deleteConvoMutation = useDeleteConversationMutation(conversationId);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-05-18 11:09:31 -07:00
|
|
|
if (deleteConvoMutation.isSuccess) {
|
|
|
|
|
if (currentConversation?.conversationId == conversationId) newConversation();
|
|
|
|
|
|
2023-04-03 12:39:00 -07:00
|
|
|
refreshConversations();
|
|
|
|
|
retainView();
|
|
|
|
|
}
|
|
|
|
|
}, [deleteConvoMutation.isSuccess]);
|
|
|
|
|
|
|
|
|
|
const clickHandler = () => {
|
2023-05-18 11:09:31 -07:00
|
|
|
deleteConvoMutation.mutate({ conversationId, source: 'button' });
|
2023-04-03 12:39:00 -07:00
|
|
|
};
|
|
|
|
|
|
2023-02-11 10:22:15 -05:00
|
|
|
const handler = renaming ? cancelHandler : clickHandler;
|
2023-02-06 21:17:46 -05:00
|
|
|
|
|
|
|
|
return (
|
2023-05-18 11:09:31 -07:00
|
|
|
<button className="p-1 hover:text-white" onClick={handler}>
|
2023-03-28 20:36:21 +08:00
|
|
|
{renaming ? <CrossIcon /> : <TrashIcon />}
|
2023-02-06 21:17:46 -05:00
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|