160 lines
5.9 KiB
TypeScript
160 lines
5.9 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { Link, useSearchParams } from 'react-router-dom';
|
|
import type { ExperimentSummary } from '@stock-bot/stock-config/experiments';
|
|
import { Button } from '@/components/ui/Button';
|
|
import { ExperimentCounts } from './components/ExperimentCounts';
|
|
import { ExperimentEditor } from './components/ExperimentEditor';
|
|
import { SavedChronologicalValidations } from './components/SavedChronologicalValidations';
|
|
import { SavedWalkForwards } from './components/SavedWalkForwards';
|
|
import { experimentApi } from './services/experimentApi';
|
|
|
|
export function ExperimentListPage() {
|
|
const [params] = useSearchParams();
|
|
const backtestId = params.get('backtestId') || '';
|
|
return <ExperimentList key={backtestId} backtestId={backtestId} />;
|
|
}
|
|
function ExperimentList({ backtestId }: { backtestId: string }) {
|
|
const [rows, setRows] = useState<ExperimentSummary[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState('');
|
|
const [reload, setReload] = useState(0);
|
|
const [dirty, setDirty] = useState(false);
|
|
const [selected, setSelected] = useState<string[]>([]);
|
|
const [leftId, rightId] = selected;
|
|
const compareUrl =
|
|
leftId && rightId ? `/experiments/compare?${new URLSearchParams({ leftId, rightId })}` : null;
|
|
useEffect(() => {
|
|
let current = true;
|
|
const controller = new AbortController();
|
|
setLoading(true);
|
|
setError('');
|
|
setRows([]);
|
|
void experimentApi
|
|
.list(backtestId || undefined, controller.signal)
|
|
.then(value => {
|
|
if (current) {
|
|
setRows(value);
|
|
setSelected(previous => previous.filter(id => value.some(item => item.id === id)));
|
|
}
|
|
})
|
|
.catch(cause => {
|
|
if (current) {
|
|
setError(cause instanceof Error ? cause.message : 'Could not load experiments');
|
|
}
|
|
})
|
|
.finally(() => {
|
|
if (current) {
|
|
setLoading(false);
|
|
}
|
|
});
|
|
return () => {
|
|
current = false;
|
|
controller.abort();
|
|
};
|
|
}, [backtestId, reload]);
|
|
return (
|
|
<div
|
|
className="h-full overflow-auto space-y-6 p-1"
|
|
onClickCapture={event => {
|
|
if (
|
|
(event.target as HTMLElement).closest('a') &&
|
|
dirty &&
|
|
!event.ctrlKey &&
|
|
!event.metaKey &&
|
|
!event.shiftKey &&
|
|
!window.confirm(
|
|
'Leave this experiment draft or unconfirmed submission? Its retry inputs and key will be discarded. You can inspect saved experiments in another tab.'
|
|
)
|
|
) {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
}
|
|
}}
|
|
>
|
|
<div>
|
|
<h1 className="text-lg font-bold text-text-primary">Experiments</h1>
|
|
<p className="text-sm text-text-secondary">
|
|
Retained-input parameter sweeps and every trial outcome.
|
|
</p>
|
|
</div>
|
|
<ExperimentEditor initialBacktestId={backtestId} onDirty={setDirty} />
|
|
<SavedChronologicalValidations backtestId={backtestId} />
|
|
<SavedWalkForwards backtestId={backtestId} />
|
|
<section aria-label="Saved experiments" className="space-y-3">
|
|
<div className="flex items-center justify-between gap-3">
|
|
<h2 className="font-semibold">Saved experiments</h2>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
disabled={loading}
|
|
onClick={() => setReload(value => value + 1)}
|
|
>
|
|
Refresh experiments
|
|
</Button>
|
|
</div>
|
|
<div className="text-sm space-y-2">
|
|
<p>Select two experiments in order. The first supplies the reference trial.</p>
|
|
{compareUrl && !loading && !error ? (
|
|
<Link className="text-primary-500" to={compareUrl}>
|
|
Compare selected experiments
|
|
</Link>
|
|
) : (
|
|
<Button variant="outline" size="sm" disabled>
|
|
Compare selected experiments
|
|
</Button>
|
|
)}
|
|
</div>
|
|
{backtestId && (
|
|
<p className="text-sm">
|
|
Showing experiments for the selected backtest.{' '}
|
|
<Link className="text-primary-500" to="/experiments">
|
|
Show all experiments
|
|
</Link>
|
|
</p>
|
|
)}
|
|
{loading && <p role="status">Loading experiments...</p>}
|
|
{error && (
|
|
<p role="alert" className="text-error">
|
|
{error}
|
|
</p>
|
|
)}
|
|
{!loading && !error && rows.length === 0 && <p>No experiments yet.</p>}
|
|
{rows.map(item => (
|
|
<article key={item.id} className="rounded-lg border border-border p-3 space-y-2">
|
|
<label className="flex gap-2 items-center text-sm">
|
|
<input
|
|
type="checkbox"
|
|
aria-label={`Select ${item.name} for comparison`}
|
|
checked={selected.includes(item.id)}
|
|
disabled={loading || (!selected.includes(item.id) && selected.length === 2)}
|
|
onChange={() =>
|
|
setSelected(previous =>
|
|
previous.includes(item.id)
|
|
? previous.filter(id => id !== item.id)
|
|
: previous.length < 2
|
|
? [...previous, item.id]
|
|
: previous
|
|
)
|
|
}
|
|
/>
|
|
{selected.includes(item.id)
|
|
? `Selected ${selected.indexOf(item.id) + 1}`
|
|
: 'Select for comparison'}
|
|
</label>
|
|
<Link
|
|
className="font-medium text-primary-500"
|
|
to={`/experiments/${encodeURIComponent(item.id)}`}
|
|
>
|
|
{item.name}
|
|
</Link>
|
|
{item.kind === 'cost-sensitivity' && (
|
|
<p className="text-sm">Cost sensitivity · template diagnostics</p>
|
|
)}
|
|
<p className="text-xs text-text-secondary">Created {item.createdAt}</p>
|
|
<ExperimentCounts experiment={item} />
|
|
</article>
|
|
))}
|
|
</section>
|
|
</div>
|
|
);
|
|
}
|