240 lines
7.7 KiB
TypeScript
240 lines
7.7 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
import {
|
|
createResearchArtifactSchema,
|
|
createResearchHypothesisSchema,
|
|
} from '@stock-bot/stock-config/research';
|
|
import type { ResearchArtifact, ResearchContentOrigin, ResearchHypothesis } 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';
|
|
|
|
interface Props {
|
|
projectId: string;
|
|
kind: 'hypothesis' | 'note';
|
|
writeBlocked: string;
|
|
onCreated: (id: string) => void;
|
|
onRefresh: () => void;
|
|
onClose: () => void;
|
|
onDirty: (dirty: boolean) => void;
|
|
}
|
|
export function CreateResearchEntry({
|
|
projectId,
|
|
kind,
|
|
writeBlocked,
|
|
onCreated,
|
|
onRefresh,
|
|
onClose,
|
|
onDirty,
|
|
}: Props) {
|
|
const mounted = useRef(false);
|
|
const pending = useRef(false);
|
|
const [title, setTitle] = useState('');
|
|
const [body, setBody] = useState('');
|
|
const [criteria, setCriteria] = useState('');
|
|
const [origin, setOrigin] = useState<ResearchContentOrigin>('human');
|
|
const [saving, setSaving] = useState(false);
|
|
const [uncertain, setUncertain] = useState(false);
|
|
const [error, setError] = useState('');
|
|
const dirty = Boolean(title || body || criteria || origin !== 'human');
|
|
useUnsavedWarning(dirty);
|
|
useEffect(() => {
|
|
onDirty(dirty);
|
|
}, [dirty, onDirty]);
|
|
useEffect(() => {
|
|
mounted.current = true;
|
|
return () => {
|
|
mounted.current = false;
|
|
onDirty(false);
|
|
};
|
|
}, [onDirty]);
|
|
const submit = async (event: React.FormEvent) => {
|
|
event.preventDefault();
|
|
if (
|
|
pending.current ||
|
|
writeBlocked ||
|
|
uncertain ||
|
|
!title.trim() ||
|
|
(kind === 'hypothesis' && !body.trim())
|
|
) {
|
|
return;
|
|
}
|
|
pending.current = true;
|
|
setSaving(true);
|
|
setError('');
|
|
try {
|
|
let saved: ResearchHypothesis | ResearchArtifact;
|
|
let matches: boolean;
|
|
if (kind === 'hypothesis') {
|
|
const input = createResearchHypothesisSchema.parse({
|
|
title,
|
|
proposition: body,
|
|
validationCriteria: criteria,
|
|
contentOrigin: origin,
|
|
});
|
|
({ hypothesis: saved } = await researchApi.createHypothesis(projectId, input));
|
|
matches =
|
|
saved.latestRevision.hypothesisId === saved.id &&
|
|
saved.latestRevision.title === input.title &&
|
|
saved.latestRevision.proposition === input.proposition &&
|
|
saved.latestRevision.validationCriteria === input.validationCriteria;
|
|
} else {
|
|
const input = createResearchArtifactSchema.parse({
|
|
kind: 'note',
|
|
title,
|
|
body,
|
|
contentOrigin: origin,
|
|
});
|
|
({ artifact: saved } = await researchApi.createArtifact(projectId, input));
|
|
matches =
|
|
saved.kind === 'note' &&
|
|
saved.hypothesisRevisionId === null &&
|
|
saved.latestRevision.artifactId === saved.id &&
|
|
saved.latestRevision.title === input.title &&
|
|
saved.latestRevision.body === input.body &&
|
|
(saved.latestRevision.source ?? null) === null &&
|
|
saved.latestRevision.evidenceState === 'none' &&
|
|
Array.isArray(saved.latestRevision.evidence) &&
|
|
saved.latestRevision.evidence.length === 0;
|
|
}
|
|
if (
|
|
!matches ||
|
|
saved.projectId !== projectId ||
|
|
saved.archived !== false ||
|
|
saved.latestRevision.ordinal !== 1 ||
|
|
saved.latestRevision.contentOrigin !== origin ||
|
|
saved.latestRevision.validationState !== 'unvalidated'
|
|
) {
|
|
throw new ResearchApiError(
|
|
'The saved research entry response could not be verified.',
|
|
null
|
|
);
|
|
}
|
|
if (mounted.current) {
|
|
onCreated(saved.id);
|
|
}
|
|
} catch (cause) {
|
|
if (mounted.current) {
|
|
setError(cause instanceof Error ? cause.message : 'Could not save research entry');
|
|
setUncertain(!(cause instanceof ResearchApiError) || cause.ambiguous);
|
|
}
|
|
} finally {
|
|
pending.current = false;
|
|
if (mounted.current) {
|
|
setSaving(false);
|
|
}
|
|
}
|
|
};
|
|
return (
|
|
<Card className="p-4">
|
|
<form
|
|
aria-label={`Create ${kind}`}
|
|
className="space-y-3"
|
|
onSubmit={event => void submit(event)}
|
|
>
|
|
<h2 className="font-semibold">New {kind}</h2>
|
|
<label className="block">
|
|
Title
|
|
<input
|
|
className={fieldClass}
|
|
aria-label={`${kind === 'hypothesis' ? 'Hypothesis' : 'Note'} title`}
|
|
value={title}
|
|
onChange={event => setTitle(event.target.value)}
|
|
required
|
|
disabled={saving || uncertain || Boolean(writeBlocked)}
|
|
/>
|
|
</label>
|
|
<label className="block">
|
|
{kind === 'hypothesis' ? 'Proposition' : 'Body'}
|
|
<textarea
|
|
className={fieldClass}
|
|
aria-label={kind === 'hypothesis' ? 'Proposition' : 'Note body'}
|
|
value={body}
|
|
onChange={event => setBody(event.target.value)}
|
|
required={kind === 'hypothesis'}
|
|
disabled={saving || uncertain || Boolean(writeBlocked)}
|
|
/>
|
|
</label>
|
|
{kind === 'hypothesis' && (
|
|
<label className="block">
|
|
Validation criteria
|
|
<textarea
|
|
className={fieldClass}
|
|
aria-label="Validation criteria"
|
|
value={criteria}
|
|
onChange={event => setCriteria(event.target.value)}
|
|
disabled={saving || uncertain || Boolean(writeBlocked)}
|
|
/>
|
|
</label>
|
|
)}
|
|
<label className="block">
|
|
Declared content origin
|
|
<select
|
|
className={fieldClass}
|
|
aria-label="Declared content origin"
|
|
value={origin}
|
|
onChange={event => setOrigin(event.target.value as ResearchContentOrigin)}
|
|
disabled={saving || uncertain || Boolean(writeBlocked)}
|
|
>
|
|
<option value="human">Human</option>
|
|
<option value="ai">AI</option>
|
|
<option value="unknown">Unknown</option>
|
|
</select>
|
|
</label>
|
|
<p className="text-sm text-text-secondary">
|
|
This is unvalidated narrative. A note or hypothesis does not establish performance
|
|
evidence.
|
|
</p>
|
|
{writeBlocked && <p role="status">{writeBlocked} Your draft is retained.</p>}
|
|
{error && (
|
|
<p role="alert" className="text-error">
|
|
{error}
|
|
</p>
|
|
)}
|
|
{uncertain && (
|
|
<div role="status">
|
|
<p>The request may have saved. Check saved records before creating another entry.</p>
|
|
<button type="button" className={actionClass} onClick={onRefresh}>
|
|
Check saved records
|
|
</button>{' '}
|
|
<button
|
|
type="button"
|
|
className={actionClass}
|
|
onClick={() => {
|
|
setUncertain(false);
|
|
setError('');
|
|
}}
|
|
>
|
|
Start new request
|
|
</button>
|
|
</div>
|
|
)}
|
|
<button
|
|
className={actionClass}
|
|
type="submit"
|
|
disabled={
|
|
saving ||
|
|
uncertain ||
|
|
Boolean(writeBlocked) ||
|
|
!title.trim() ||
|
|
(kind === 'hypothesis' && !body.trim())
|
|
}
|
|
>
|
|
{saving ? 'Saving...' : `Create ${kind}`}
|
|
</button>{' '}
|
|
<button
|
|
className={actionClass}
|
|
type="button"
|
|
disabled={saving}
|
|
onClick={() => {
|
|
if (confirmDiscard(dirty)) {
|
|
onClose();
|
|
}
|
|
}}
|
|
>
|
|
Discard entry draft
|
|
</button>
|
|
</form>
|
|
</Card>
|
|
);
|
|
}
|