101 lines
3 KiB
TypeScript
101 lines
3 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/Dialog';
|
|
|
|
export function ArchiveBacktestDialog({
|
|
name,
|
|
archived,
|
|
onConfirm,
|
|
onClose,
|
|
}: {
|
|
name: string;
|
|
archived: boolean;
|
|
onConfirm: () => Promise<void>;
|
|
onClose: () => void;
|
|
}) {
|
|
const [saving, setSaving] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const pending = useRef(false);
|
|
const active = useRef(true);
|
|
const title = archived ? 'Archive backtest' : 'Restore backtest';
|
|
useEffect(() => {
|
|
active.current = true;
|
|
return () => {
|
|
active.current = false;
|
|
};
|
|
}, []);
|
|
const confirm = async () => {
|
|
if (pending.current) {
|
|
return;
|
|
}
|
|
pending.current = true;
|
|
setSaving(true);
|
|
setError(null);
|
|
try {
|
|
await onConfirm();
|
|
if (active.current) {
|
|
onClose();
|
|
}
|
|
} catch (failure) {
|
|
if (active.current) {
|
|
setError(failure instanceof Error ? failure.message : 'Could not change archive status');
|
|
}
|
|
} finally {
|
|
pending.current = false;
|
|
if (active.current) {
|
|
setSaving(false);
|
|
}
|
|
}
|
|
};
|
|
return (
|
|
<Dialog
|
|
open
|
|
onOpenChange={open => {
|
|
if (!open && !pending.current) {
|
|
onClose();
|
|
}
|
|
}}
|
|
>
|
|
<DialogContent className="max-w-lg">
|
|
<div role="dialog" aria-modal="true" aria-label={title} className="space-y-4">
|
|
<DialogHeader>
|
|
<DialogTitle>{title}</DialogTitle>
|
|
</DialogHeader>
|
|
<p className="text-sm text-text-primary">{name}</p>
|
|
<p className="text-sm text-text-secondary">
|
|
{archived
|
|
? 'Saved runs, results and dataset links remain available. Accepted runs can finish, recover or be cancelled. Restore the backtest to edit it or start new runs.'
|
|
: 'Restore this backtest to the active list and allow editing and new runs.'}
|
|
</p>
|
|
{error && (
|
|
<p role="alert" className="text-sm text-error">
|
|
{error}
|
|
</p>
|
|
)}
|
|
{saving && (
|
|
<p role="status" className="text-sm text-text-secondary">
|
|
{archived ? 'Archiving...' : 'Restoring...'}
|
|
</p>
|
|
)}
|
|
<div className="flex justify-end gap-3">
|
|
<button
|
|
type="button"
|
|
disabled={saving}
|
|
onClick={onClose}
|
|
className="px-4 py-2 border border-border rounded-md text-sm text-text-primary disabled:opacity-50"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={saving}
|
|
onClick={() => void confirm()}
|
|
className="px-4 py-2 bg-primary-500 rounded-md text-sm text-white disabled:opacity-50"
|
|
>
|
|
{archived ? 'Confirm archive' : 'Confirm restore'}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|