365 lines
12 KiB
TypeScript
365 lines
12 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { Link } from 'react-router-dom';
|
|
import type { ResearchContentOrigin, ResearchHypothesis } from '@stock-bot/types';
|
|
import { Card } from '@/components/ui/Card';
|
|
import { actionClass, fieldClass } from '@/components/ui/formStyles';
|
|
import { useResearchEntry, type ResearchEntryWorkflowOptions } from '../hooks/useResearchEntry';
|
|
import { researchApi } from '../services/researchApi';
|
|
import { FindingSource } from './FindingSource';
|
|
import { ResearchAuthorshipBadge, ResearchEvidenceList } from './ResearchEvidence';
|
|
|
|
interface Props extends ResearchEntryWorkflowOptions {
|
|
hypotheses: ResearchHypothesis[];
|
|
onSelectRevision: (id: string | null) => void;
|
|
}
|
|
function HypothesisReference({
|
|
projectId,
|
|
revisionId,
|
|
hypotheses,
|
|
}: {
|
|
projectId: string;
|
|
revisionId: string;
|
|
hypotheses: ResearchHypothesis[];
|
|
}) {
|
|
const [hypothesisId, setHypothesisId] = useState<string | null>(null);
|
|
const [error, setError] = useState('');
|
|
useEffect(() => {
|
|
let current = true;
|
|
const controller = new AbortController();
|
|
setHypothesisId(null);
|
|
setError('');
|
|
const candidates = hypotheses.filter(item => item.projectId === projectId);
|
|
const known = candidates.find(item => item.latestRevision.id === revisionId);
|
|
if (known) {
|
|
setHypothesisId(known.id);
|
|
} else {
|
|
void Promise.allSettled(
|
|
candidates.map(async item => {
|
|
const { revisions } = await researchApi.hypothesisRevisions(item.id, controller.signal);
|
|
return revisions.some(
|
|
revision => revision.id === revisionId && revision.hypothesisId === item.id
|
|
)
|
|
? item.id
|
|
: null;
|
|
})
|
|
).then(results => {
|
|
if (current) {
|
|
const ids = results.flatMap(result =>
|
|
result.status === 'fulfilled' ? [result.value] : []
|
|
);
|
|
const found = ids.find(Boolean) ?? null;
|
|
setHypothesisId(found);
|
|
if (!found) {
|
|
setError(
|
|
results.some(result => result.status === 'rejected')
|
|
? 'Could not resolve the linked hypothesis history. Refresh the project to try again.'
|
|
: 'The linked hypothesis revision is unavailable in the loaded project history.'
|
|
);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
return () => {
|
|
current = false;
|
|
controller.abort();
|
|
};
|
|
}, [hypotheses, projectId, revisionId]);
|
|
return (
|
|
<div className="text-sm">
|
|
<p>
|
|
Linked hypothesis revision: <code>{revisionId}</code>
|
|
</p>
|
|
{hypothesisId && (
|
|
<Link
|
|
className="text-primary-500"
|
|
to={`/research/${projectId}?hypothesis=${hypothesisId}&revision=${revisionId}`}
|
|
>
|
|
Open exact hypothesis revision
|
|
</Link>
|
|
)}
|
|
{error && <p role="alert">{error}</p>}
|
|
</div>
|
|
);
|
|
}
|
|
export function ResearchEntry({
|
|
projectId,
|
|
id,
|
|
kind,
|
|
revisionId,
|
|
archived,
|
|
writeBlocked,
|
|
hypotheses,
|
|
onSelectRevision,
|
|
onRefresh,
|
|
onDirty,
|
|
}: Props) {
|
|
const {
|
|
entry,
|
|
revision,
|
|
loading,
|
|
loadError,
|
|
downloadError,
|
|
download,
|
|
saving,
|
|
readonly,
|
|
archiveUncertain,
|
|
editing,
|
|
title,
|
|
setTitle,
|
|
body,
|
|
setBody,
|
|
criteria,
|
|
setCriteria,
|
|
origin,
|
|
setOrigin,
|
|
save,
|
|
error,
|
|
blocked,
|
|
beginEdit,
|
|
archive,
|
|
retry,
|
|
discardArchiveDraft,
|
|
discardRevisionAndReload,
|
|
discardRevisionDraft,
|
|
} = useResearchEntry({
|
|
projectId,
|
|
id,
|
|
kind,
|
|
revisionId,
|
|
archived,
|
|
writeBlocked,
|
|
onRefresh,
|
|
onDirty,
|
|
});
|
|
return (
|
|
<Card className="p-4 space-y-4">
|
|
{loading && <p role="status">Loading research entry and revision history...</p>}
|
|
{loadError && (
|
|
<div>
|
|
<p role="alert" className="text-error">
|
|
{loadError}
|
|
</p>
|
|
<button className={actionClass} onClick={retry}>
|
|
Retry research entry
|
|
</button>
|
|
</div>
|
|
)}
|
|
{!loading && entry && revision && (
|
|
<>
|
|
<div className="flex flex-wrap justify-between gap-2">
|
|
<h2 className="font-semibold">{revision.title}</h2>
|
|
<span>
|
|
{entry.kind === 'hypothesis' ? 'Hypothesis' : entry.entity.kind}{' '}
|
|
{entry.entity.archived ? '· Archived' : ''}
|
|
</span>
|
|
</div>
|
|
<ResearchAuthorshipBadge authorship={revision} />
|
|
<p className="text-xs text-text-secondary">
|
|
Revision {revision.ordinal} · {revision.createdAt} · {revision.id}
|
|
</p>
|
|
<div className="space-y-2">
|
|
<button type="button" className={actionClass} onClick={download} disabled={saving}>
|
|
{downloadError ? 'Retry saved revision download' : 'Download saved revision'}
|
|
</button>
|
|
{editing && (
|
|
<p className="text-sm text-text-secondary">
|
|
Downloads the saved revision displayed above. Unsaved edits are excluded.
|
|
</p>
|
|
)}
|
|
{downloadError && (
|
|
<p role="alert" className="text-error text-sm">
|
|
{downloadError}
|
|
</p>
|
|
)}
|
|
</div>
|
|
{revisionId && <p role="status">Immutable history: this revision is read-only.</p>}
|
|
{writeBlocked && (
|
|
<p role="status">
|
|
{writeBlocked} Research entries are read-only until the project can be verified.
|
|
</p>
|
|
)}
|
|
{archived && <p role="status">Archived project: research entries are read-only.</p>}
|
|
<p className="whitespace-pre-wrap">
|
|
{'proposition' in revision ? revision.proposition : revision.body}
|
|
</p>
|
|
{'validationCriteria' in revision && (
|
|
<div>
|
|
<h3 className="text-sm font-semibold">Validation criteria</h3>
|
|
<p className="whitespace-pre-wrap">
|
|
{revision.validationCriteria || 'No criteria recorded.'}
|
|
</p>
|
|
</div>
|
|
)}
|
|
{'evidence' in revision && (
|
|
<>
|
|
<FindingSource source={revision.source} />
|
|
<ResearchEvidenceList evidence={revision.evidence} />
|
|
</>
|
|
)}
|
|
{entry.kind === 'artifact' && entry.entity.hypothesisRevisionId && (
|
|
<HypothesisReference
|
|
projectId={projectId}
|
|
hypotheses={hypotheses}
|
|
revisionId={entry.entity.hypothesisRevisionId}
|
|
/>
|
|
)}
|
|
<div className="flex gap-2 flex-wrap">
|
|
{!readonly && !editing && (
|
|
<button
|
|
className={actionClass}
|
|
onClick={beginEdit}
|
|
disabled={saving || archiveUncertain}
|
|
>
|
|
Edit latest revision
|
|
</button>
|
|
)}
|
|
{!archived && !writeBlocked && (
|
|
<button
|
|
className={actionClass}
|
|
onClick={() => void archive()}
|
|
disabled={saving || archiveUncertain}
|
|
>
|
|
{entry.entity.archived ? 'Restore entry' : 'Archive entry'}
|
|
</button>
|
|
)}
|
|
</div>
|
|
{error && (
|
|
<p role="alert" className="text-error">
|
|
{error}
|
|
</p>
|
|
)}
|
|
{archiveUncertain && (
|
|
<div role="status">
|
|
<p>
|
|
The archive request may have saved. Your draft is preserved. Reload saved history
|
|
before another write; this request will not be retried.
|
|
</p>
|
|
<button
|
|
className={actionClass}
|
|
type="button"
|
|
disabled={saving}
|
|
onClick={discardArchiveDraft}
|
|
>
|
|
Discard draft and reload archive state
|
|
</button>
|
|
</div>
|
|
)}
|
|
{editing && !archived && !entry.entity.archived && !revisionId && (
|
|
<form
|
|
aria-label="Revise research entry"
|
|
className="space-y-3"
|
|
onSubmit={event => void save(event)}
|
|
>
|
|
<label className="block">
|
|
Revision title
|
|
<input
|
|
aria-label="Revision title"
|
|
className={fieldClass}
|
|
value={title}
|
|
onChange={event => setTitle(event.target.value)}
|
|
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
|
|
required
|
|
/>
|
|
</label>
|
|
<label className="block">
|
|
{kind === 'hypothesis' ? 'Proposition' : 'Body'}
|
|
<textarea
|
|
aria-label="Revision body"
|
|
className={fieldClass}
|
|
value={body}
|
|
onChange={event => setBody(event.target.value)}
|
|
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
|
|
required={kind === 'hypothesis'}
|
|
/>
|
|
</label>
|
|
{kind === 'hypothesis' && (
|
|
<label className="block">
|
|
Validation criteria
|
|
<textarea
|
|
aria-label="Revision validation criteria"
|
|
className={fieldClass}
|
|
value={criteria}
|
|
onChange={event => setCriteria(event.target.value)}
|
|
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
|
|
/>
|
|
</label>
|
|
)}
|
|
<label className="block">
|
|
Declared content origin
|
|
<select
|
|
aria-label="Revision content origin"
|
|
className={fieldClass}
|
|
value={origin}
|
|
onChange={event => setOrigin(event.target.value as ResearchContentOrigin)}
|
|
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
|
|
>
|
|
<option value="human">Human</option>
|
|
<option value="ai">AI</option>
|
|
<option value="unknown">Unknown</option>
|
|
</select>
|
|
</label>
|
|
{blocked && (
|
|
<div role="status">
|
|
<p>
|
|
The draft is preserved. Reload and review saved history before starting another
|
|
revision; this request will not be retried.
|
|
</p>
|
|
<button className={actionClass} type="button" onClick={discardRevisionAndReload}>
|
|
Discard draft and reload history
|
|
</button>
|
|
</div>
|
|
)}
|
|
<button
|
|
className={actionClass}
|
|
type="submit"
|
|
disabled={
|
|
saving ||
|
|
blocked ||
|
|
archiveUncertain ||
|
|
Boolean(writeBlocked) ||
|
|
!title.trim() ||
|
|
(kind === 'hypothesis' && !body.trim())
|
|
}
|
|
>
|
|
{saving ? 'Saving revision...' : 'Save new revision'}
|
|
</button>{' '}
|
|
<button
|
|
className={actionClass}
|
|
type="button"
|
|
disabled={saving}
|
|
onClick={discardRevisionDraft}
|
|
>
|
|
Discard revision draft
|
|
</button>
|
|
</form>
|
|
)}
|
|
<div>
|
|
<h3 className="font-semibold text-sm">Revision history</h3>
|
|
<button
|
|
className={actionClass}
|
|
disabled={saving}
|
|
onClick={() => onSelectRevision(null)}
|
|
>
|
|
View latest revision
|
|
</button>
|
|
<ul className="space-y-2 mt-2">
|
|
{entry.revisions.map(item => (
|
|
<li key={item.id} className="text-sm">
|
|
<button
|
|
className="text-primary-500"
|
|
disabled={saving}
|
|
onClick={() => onSelectRevision(item.id)}
|
|
>
|
|
View revision {item.ordinal}
|
|
</button>{' '}
|
|
· {item.createdAt} · Submitted by {item.submittedBy} · Declared{' '}
|
|
{item.contentOrigin}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
</>
|
|
)}
|
|
</Card>
|
|
);
|
|
}
|