use-delete.ts1.1 KBView on GitHub import { useThreadsOperations } from '@/modules/threads/threadList/hooks/use-threads-operations';
import { useTRPC } from '@/providers/query-provider';
import { useMutation } from '@tanstack/react-query';
import { useCedarStore } from '@/modules/store';
import { useState } from 'react';
import { toast } from 'sonner';
const useDelete = () => {
const [isLoading, setIsLoading] = useState(false);
const clearBulkSelection = useCedarStore((state) => state.clearBulkSelection);
const { refetch: refetchThreads } = useThreadsOperations();
const trpc = useTRPC();
const { mutateAsync: bulkDelete } = useMutation(trpc.mail.bulkDelete.mutationOptions());
return {
mutate: async (id: string, type: 'thread' | 'email' = 'thread') => {
setIsLoading(true);
try {
await bulkDelete({ ids: [id] });
} catch (error) {
console.error(`Error deleting ${type}:`, error);
toast.error('Failed to delete');
} finally {
clearBulkSelection();
setIsLoading(false);
await refetchThreads();
}
},
isLoading,
};
};
export default useDelete;