stock-bot/apps/stock/web-app/src/features/strategies/StrategyListPage.tsx

201 lines
6.8 KiB
TypeScript

import { useEffect, useRef, useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import type { StrategyRecord } from '@stock-bot/types';
import { actionClass, fieldClass } from '@/components/ui/formStyles';
import { confirmDiscard, useUnsavedWarning } from '@/hooks/useUnsavedWarning';
import { strategyApi } from './services/strategyApi';
export function StrategyListPage() {
const navigate = useNavigate();
const mounted = useRef(true);
const pendingCreate = useRef(false);
useEffect(() => {
mounted.current = true;
return () => {
mounted.current = false;
};
}, []);
const [strategies, setStrategies] = useState<StrategyRecord[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [retry, setRetry] = useState(0);
const [creating, setCreating] = useState(false);
const [showCreate, setShowCreate] = useState(false);
const [name, setName] = useState('');
const [description, setDescription] = useState('');
const [createError, setCreateError] = useState<string | null>(null);
const [showArchived, setShowArchived] = useState(false);
const dirty = showCreate && Boolean(name || description);
useUnsavedWarning(dirty);
useEffect(() => {
const controller = new AbortController();
setLoading(true);
setError(null);
void strategyApi
.list(controller.signal)
.then(result => {
if (!controller.signal.aborted) {
setStrategies(result.strategies);
}
})
.catch(cause => {
if (!controller.signal.aborted) {
setError(cause.message);
}
})
.finally(() => {
if (!controller.signal.aborted) {
setLoading(false);
}
});
return () => controller.abort();
}, [retry]);
const create = async (event: React.FormEvent) => {
event.preventDefault();
if (pendingCreate.current || !name.trim()) {
return;
}
pendingCreate.current = true;
setCreating(true);
setCreateError(null);
try {
const result = await strategyApi.create({ name, description });
if (mounted.current) {
navigate(`/strategies/${result.strategy.id}`);
}
} catch (cause) {
if (mounted.current) {
setCreateError(
`${cause instanceof Error ? cause.message : 'Could not create strategy'}. Refresh the catalog before retrying if the response was lost; the strategy may already have been created.`
);
}
} finally {
pendingCreate.current = false;
if (mounted.current) {
setCreating(false);
}
}
};
const visible = strategies.filter(item => showArchived || !item.archived);
return (
<div className="space-y-5 text-text-primary">
<div className="flex justify-between items-center">
<h1 className="text-lg font-bold">Strategies</h1>
<button className={actionClass} onClick={() => setShowCreate(true)}>
New strategy
</button>
</div>
<p className="text-sm text-text-secondary">
Organize immutable strategy packages and prepare backtests. Canonical execution requires a
reviewed built-in version, supported dataset and explicit simulation policy.
</p>
{showCreate && (
<form
className="space-y-3 border border-border rounded-md p-4"
onSubmit={event => void create(event)}
>
<label className="block text-sm">
Strategy name
<input
className={fieldClass}
aria-label="Strategy name"
value={name}
onChange={event => setName(event.target.value)}
required
disabled={creating}
/>
</label>
<label className="block text-sm">
Strategy description
<textarea
className={fieldClass}
aria-label="Strategy description"
value={description}
onChange={event => setDescription(event.target.value)}
disabled={creating}
/>
</label>
{dirty && (
<p role="status" className="text-sm text-text-secondary">
Unsaved strategy metadata. Save or discard before leaving.
</p>
)}
{createError && (
<p role="alert" className="text-sm text-error">
{createError}
</p>
)}
<button className={actionClass} disabled={creating || !name.trim()} type="submit">
{creating ? 'Creating strategy...' : 'Create strategy'}
</button>{' '}
<button
className={actionClass}
disabled={creating}
type="button"
onClick={() => {
if (confirmDiscard(dirty)) {
setShowCreate(false);
setName('');
setDescription('');
setCreateError(null);
}
}}
>
Discard
</button>
</form>
)}
<label className="flex gap-2 text-sm">
<input
type="checkbox"
checked={showArchived}
onChange={event => setShowArchived(event.target.checked)}
/>
Show archived strategies
</label>
{loading && <p role="status">Loading strategies...</p>}
{error && (
<div>
<p role="alert" className="text-error">
{error}
</p>
<button className={actionClass} onClick={() => setRetry(value => value + 1)}>
Retry strategies
</button>
</div>
)}
{!loading && !error && !visible.length && (
<p role="status">
No strategies found. Create a strategy family, then publish an immutable package through
the existing catalog API.
</p>
)}
{!loading && !error && (
<ul className="space-y-3">
{visible.map(strategy => (
<li className="border border-border rounded-md p-4" key={strategy.id}>
<Link
className="text-primary-500 font-medium"
to={`/strategies/${strategy.id}`}
onClick={event => {
if (!confirmDiscard(dirty)) {
event.preventDefault();
}
}}
>
{strategy.name}
</Link>
{strategy.archived && <span className="ml-2 text-xs">Archived</span>}
<p className="text-sm text-text-secondary whitespace-pre-wrap">
{strategy.description || 'No description yet.'}
</p>
<p className="text-xs text-text-muted">
Owner: {strategy.ownerPrincipal} · {strategy.id}
</p>
</li>
))}
</ul>
)}
</div>
);
}