2023-02-07 19:07:48 -05:00
|
|
|
import React from 'react';
|
|
|
|
|
import TrashIcon from '../svg/TrashIcon';
|
2023-03-07 14:43:55 -05:00
|
|
|
import { useSWRConfig } from 'swr';
|
2023-02-07 19:07:48 -05:00
|
|
|
import manualSWR from '~/utils/fetchers';
|
|
|
|
|
import { useDispatch } from 'react-redux';
|
2023-03-09 18:57:37 -05:00
|
|
|
import { setNewConvo, removeAll } from '~/store/convoSlice';
|
2023-02-07 19:07:48 -05:00
|
|
|
import { setMessages } from '~/store/messageSlice';
|
2023-03-13 05:21:30 +08:00
|
|
|
import { setSubmission } from '~/store/submitSlice';
|
2023-03-22 17:51:51 -04:00
|
|
|
import { Dialog, DialogTrigger } from '../ui/Dialog.tsx';
|
|
|
|
|
import DialogTemplate from '../ui/DialogTemplate';
|
2023-02-07 19:07:48 -05:00
|
|
|
|
|
|
|
|
export default function ClearConvos() {
|
|
|
|
|
const dispatch = useDispatch();
|
2023-03-07 14:43:55 -05:00
|
|
|
const { mutate } = useSWRConfig();
|
2023-02-07 19:07:48 -05:00
|
|
|
|
2023-03-10 21:06:13 +08:00
|
|
|
const { trigger } = manualSWR(`/api/convos/clear`, 'post', () => {
|
2023-03-07 14:43:55 -05:00
|
|
|
dispatch(setMessages([]));
|
2023-03-09 18:57:37 -05:00
|
|
|
dispatch(setNewConvo());
|
2023-03-11 22:35:39 -05:00
|
|
|
dispatch(setSubmission({}));
|
2023-03-10 21:06:13 +08:00
|
|
|
mutate(`/api/convos`);
|
2023-03-07 14:43:55 -05:00
|
|
|
});
|
2023-02-07 19:07:48 -05:00
|
|
|
|
2023-02-08 00:02:29 -05:00
|
|
|
const clickHandler = () => {
|
|
|
|
|
console.log('Clearing conversations...');
|
2023-03-07 14:43:55 -05:00
|
|
|
dispatch(removeAll());
|
2023-02-08 00:02:29 -05:00
|
|
|
trigger({});
|
|
|
|
|
};
|
2023-02-07 19:07:48 -05:00
|
|
|
|
|
|
|
|
return (
|
2023-03-22 17:51:51 -04:00
|
|
|
<Dialog>
|
|
|
|
|
<DialogTrigger asChild>
|
2023-02-08 08:27:23 -05:00
|
|
|
<a
|
|
|
|
|
className="flex cursor-pointer items-center gap-3 rounded-md py-3 px-3 text-sm text-white transition-colors duration-200 hover:bg-gray-500/10"
|
2023-03-22 17:51:51 -04:00
|
|
|
// onClick={clickHandler}
|
2023-02-08 08:27:23 -05:00
|
|
|
>
|
|
|
|
|
<TrashIcon />
|
|
|
|
|
Clear conversations
|
|
|
|
|
</a>
|
2023-03-22 17:51:51 -04:00
|
|
|
</DialogTrigger>
|
|
|
|
|
<DialogTemplate
|
|
|
|
|
title="Clear conversations"
|
|
|
|
|
description="Are you sure you want to clear all conversations? This is irreversible."
|
|
|
|
|
selection={{
|
|
|
|
|
selectHandler: clickHandler,
|
|
|
|
|
selectClasses: 'bg-red-600 hover:bg-red-700 dark:hover:bg-red-800 text-white',
|
|
|
|
|
selectText: 'Clear',
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Dialog>
|
2023-02-07 19:07:48 -05:00
|
|
|
);
|
|
|
|
|
}
|