247 lines
8.1 KiB
TypeScript
247 lines
8.1 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
import { Link, useNavigate } from 'react-router-dom';
|
|
import { createResearchProjectSchema } from '@stock-bot/stock-config/research';
|
|
import type { ResearchProject } from '@stock-bot/types';
|
|
import { Card } from '@/components/ui/Card';
|
|
import { actionClass, fieldClass } from '@/components/ui/formStyles';
|
|
import { confirmDiscard, useUnsavedWarning } from '@/hooks/useUnsavedWarning';
|
|
import { researchApi, ResearchApiError } from './services/researchApi';
|
|
|
|
export function ResearchListPage() {
|
|
const navigate = useNavigate();
|
|
const mounted = useRef(false);
|
|
const pending = useRef(false);
|
|
const [projects, setProjects] = useState<ResearchProject[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState('');
|
|
const [refresh, setRefresh] = useState(0);
|
|
const [archived, setArchived] = useState(false);
|
|
const [creating, setCreating] = useState(false);
|
|
const [open, setOpen] = useState(false);
|
|
const [title, setTitle] = useState('');
|
|
const [description, setDescription] = useState('');
|
|
const [saveError, setSaveError] = useState('');
|
|
const [uncertain, setUncertain] = useState(false);
|
|
const dirty = open && Boolean(title || description);
|
|
useUnsavedWarning(dirty);
|
|
useEffect(() => {
|
|
mounted.current = true;
|
|
return () => {
|
|
mounted.current = false;
|
|
};
|
|
}, []);
|
|
useEffect(() => {
|
|
let current = true;
|
|
const controller = new AbortController();
|
|
setLoading(true);
|
|
setError('');
|
|
void researchApi
|
|
.listProjects(archived, controller.signal)
|
|
.then(result => {
|
|
if (current) {
|
|
setProjects(result.projects);
|
|
}
|
|
})
|
|
.catch(cause => {
|
|
if (current) {
|
|
setError(cause instanceof Error ? cause.message : 'Could not load projects');
|
|
}
|
|
})
|
|
.finally(() => {
|
|
if (current) {
|
|
setLoading(false);
|
|
}
|
|
});
|
|
return () => {
|
|
current = false;
|
|
controller.abort();
|
|
};
|
|
}, [archived, refresh]);
|
|
const create = async (event: React.FormEvent) => {
|
|
event.preventDefault();
|
|
if (pending.current || uncertain || !title.trim()) {
|
|
return;
|
|
}
|
|
pending.current = true;
|
|
setCreating(true);
|
|
setSaveError('');
|
|
try {
|
|
const input = createResearchProjectSchema.parse({ title, description });
|
|
const { project } = await researchApi.createProject(input);
|
|
if (
|
|
project.title !== input.title ||
|
|
project.description !== input.description ||
|
|
project.revision !== 1 ||
|
|
project.archived !== false
|
|
) {
|
|
throw new ResearchApiError('The saved project response could not be verified.', null);
|
|
}
|
|
if (mounted.current) {
|
|
navigate(`/research/${project.id}`);
|
|
}
|
|
} catch (cause) {
|
|
if (mounted.current) {
|
|
setSaveError(cause instanceof Error ? cause.message : 'Could not create project');
|
|
setUncertain(!(cause instanceof ResearchApiError) || cause.ambiguous);
|
|
}
|
|
} finally {
|
|
pending.current = false;
|
|
if (mounted.current) {
|
|
setCreating(false);
|
|
}
|
|
}
|
|
};
|
|
return (
|
|
<div className="space-y-5 text-text-primary">
|
|
<Link to="/research/agents" className="text-primary-500">
|
|
Research agent access
|
|
</Link>
|
|
<div className="flex items-center justify-between">
|
|
<h1 className="text-lg font-bold">Research</h1>
|
|
<button className={actionClass} onClick={() => setOpen(true)}>
|
|
New project
|
|
</button>
|
|
</div>
|
|
<p className="text-sm text-text-secondary">
|
|
Organize hypotheses, strategies and findings with links to the exact backtests supporting
|
|
them.
|
|
</p>
|
|
{open && (
|
|
<Card className="p-4">
|
|
<form
|
|
className="space-y-3"
|
|
aria-label="Create research project"
|
|
onSubmit={event => void create(event)}
|
|
>
|
|
<label className="block">
|
|
Project title
|
|
<input
|
|
aria-label="Project title"
|
|
className={fieldClass}
|
|
value={title}
|
|
onChange={event => setTitle(event.target.value)}
|
|
disabled={creating || uncertain}
|
|
required
|
|
/>
|
|
</label>
|
|
<label className="block">
|
|
Project description
|
|
<textarea
|
|
aria-label="Project description"
|
|
className={fieldClass}
|
|
value={description}
|
|
onChange={event => setDescription(event.target.value)}
|
|
disabled={creating || uncertain}
|
|
/>
|
|
</label>
|
|
{saveError && (
|
|
<p role="alert" className="text-error">
|
|
{saveError}
|
|
</p>
|
|
)}
|
|
{uncertain && (
|
|
<div role="status">
|
|
<p>
|
|
The request may have saved. Check saved records before creating another project.
|
|
</p>
|
|
<button
|
|
type="button"
|
|
className={actionClass}
|
|
onClick={() => {
|
|
setArchived(true);
|
|
setRefresh(value => value + 1);
|
|
}}
|
|
>
|
|
Check saved projects
|
|
</button>{' '}
|
|
<button
|
|
type="button"
|
|
className={actionClass}
|
|
onClick={() => {
|
|
setUncertain(false);
|
|
setSaveError('');
|
|
}}
|
|
>
|
|
Start new request
|
|
</button>
|
|
</div>
|
|
)}
|
|
<button
|
|
type="submit"
|
|
className={actionClass}
|
|
disabled={creating || uncertain || !title.trim()}
|
|
>
|
|
{creating ? 'Creating project...' : 'Create project'}
|
|
</button>{' '}
|
|
<button
|
|
type="button"
|
|
className={actionClass}
|
|
disabled={creating}
|
|
onClick={() => {
|
|
if (confirmDiscard(dirty)) {
|
|
setOpen(false);
|
|
setTitle('');
|
|
setDescription('');
|
|
setSaveError('');
|
|
setUncertain(false);
|
|
}
|
|
}}
|
|
>
|
|
Discard project draft
|
|
</button>
|
|
</form>
|
|
</Card>
|
|
)}
|
|
<label className="flex gap-2">
|
|
<input
|
|
type="checkbox"
|
|
checked={archived}
|
|
onChange={event => setArchived(event.target.checked)}
|
|
/>
|
|
Show archived projects
|
|
</label>
|
|
{loading && <p role="status">Loading research projects...</p>}
|
|
{error && (
|
|
<div>
|
|
<p role="alert" className="text-error">
|
|
{error}
|
|
</p>
|
|
<button className={actionClass} onClick={() => setRefresh(value => value + 1)}>
|
|
Retry projects
|
|
</button>
|
|
</div>
|
|
)}
|
|
{!loading && !error && !projects.length && (
|
|
<p role="status">No research projects yet. Create a project to begin.</p>
|
|
)}
|
|
{!loading && !error && (
|
|
<ul className="space-y-3">
|
|
{projects.map(project => (
|
|
<li key={project.id}>
|
|
<Card className="p-4">
|
|
<Link
|
|
className="text-primary-500 font-medium"
|
|
to={`/research/${project.id}`}
|
|
onClick={event => {
|
|
if (!confirmDiscard(dirty)) {
|
|
event.preventDefault();
|
|
}
|
|
}}
|
|
>
|
|
{project.title}
|
|
</Link>
|
|
{project.archived && <span className="ml-2 text-xs">Archived</span>}
|
|
<p className="whitespace-pre-wrap text-sm text-text-secondary">
|
|
{project.description || 'No description yet.'}
|
|
</p>
|
|
<p className="text-xs text-text-muted">
|
|
Owner: {project.ownerPrincipal} · Updated {project.updatedAt}
|
|
</p>
|
|
</Card>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|