'use client'; import { useState } from 'react'; import { Button, CloseButton, Dialog, IconButton, Portal, Text, } from '@chakra-ui/react'; import { FiTrash2 } from 'react-icons/fi'; import { deleteBacktest } from '#actions/backtest'; import { useBacktestMutations } from '#store'; import { toaster } from '#ui'; import { useTestContext } from '../context'; interface TestCardDeleteDialogProps { open: boolean; onOpenChange: (open: boolean) => void; } export const TestCardDeleteDialog = ({ open, onOpenChange, }: TestCardDeleteDialogProps) => { const [isDeleting, setIsDeleting] = useState(false); const [error, setError] = useState(null); const { removeBacktestTest } = useBacktestMutations(); const { testResult } = useTestContext(); const handleDelete = async () => { if (isDeleting) { return; } setIsDeleting(true); setError(null); try { const isDeleted = await deleteBacktest( testResult.test.name, testResult.test.strategyName, ); if (!isDeleted) { setError('Failed to delete backtest.'); toaster.error({ title: 'Delete failed', description: 'Backtest was not deleted.', }); return; } await removeBacktestTest(testResult.test.name); onOpenChange(false); toaster.success({ title: 'Backtest deleted', description: testResult.test.name, }); } catch { setError('Failed to delete backtest.'); toaster.error({ title: 'Delete failed', description: 'Unexpected error while deleting backtest.', }); } finally { setIsDeleting(false); } }; return ( { onOpenChange(e.open); if (!e.open) { setError(null); } }} > Delete test Delete backtest {testResult.test.name}? This action cannot be undone. {error ? ( {error} ) : null} ); }; export const TestCardDeleteButton = () => { const [open, setOpen] = useState(false); return ( <> setOpen(true)} > ); };