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';
|
|
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
|
import { setConversation } from '~/store/convoSlice';
|
|
|
|
|
import { setMessages } from '~/store/messageSlice';
|
|
|
|
|
|
2023-02-11 10:22:15 -05:00
|
|
|
export default function DeleteButton({ conversationId, renaming, cancelHandler }) {
|
2023-02-07 16:22:35 -05:00
|
|
|
const dispatch = useDispatch();
|
2023-02-21 21:31:36 -05:00
|
|
|
const { trigger } = manualSWR(
|
2023-03-06 10:15:07 -05:00
|
|
|
`http://localhost:3080/convos/clear`,
|
2023-02-07 16:22:35 -05:00
|
|
|
'post',
|
|
|
|
|
() => {
|
|
|
|
|
dispatch(setMessages([]));
|
2023-02-11 12:49:07 -05:00
|
|
|
dispatch(setConversation({ title: 'New chat', conversationId: null, parentMessageId: null }));
|
2023-02-07 16:22:35 -05:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const clickHandler = () => trigger({ conversationId });
|
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-02-11 10:22:15 -05:00
|
|
|
{ renaming ? <CrossIcon/> : <TrashIcon />}
|
2023-02-06 21:17:46 -05:00
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|