2023-02-06 21:17:46 -05:00
|
|
|
import React 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-02-07 16:22:35 -05:00
|
|
|
import manualSWR from '~/utils/fetchers';
|
2023-03-28 20:36:21 +08:00
|
|
|
import { useRecoilValue } from 'recoil';
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
const { trigger } = manualSWR(`/api/convos/clear`, 'post', () => {
|
|
|
|
|
if (currentConversation?.conversationId == conversationId) newConversation();
|
|
|
|
|
refreshConversations();
|
|
|
|
|
retainView();
|
|
|
|
|
});
|
2023-02-07 16:22:35 -05:00
|
|
|
|
2023-03-28 11:42:36 -04:00
|
|
|
const clickHandler = () => trigger({ conversationId, source: 'button' });
|
2023-02-11 10:22:15 -05:00
|
|
|
const handler = renaming ? cancelHandler : clickHandler;
|
2023-02-06 21:17:46 -05:00
|
|
|
|
|
|
|
|
return (
|
2023-02-07 16:22:35 -05:00
|
|
|
<button
|
|
|
|
|
className="p-1 hover:text-white"
|
2023-02-11 10:22:15 -05:00
|
|
|
onClick={handler}
|
2023-02-07 16:22:35 -05:00
|
|
|
>
|
2023-03-28 20:36:21 +08:00
|
|
|
{renaming ? <CrossIcon /> : <TrashIcon />}
|
2023-02-06 21:17:46 -05:00
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|