fix(research): verify project updates and archive outcomes (RP32)
This commit is contained in:
parent
233178d4ad
commit
f84fa82bba
5 changed files with 670 additions and 46 deletions
|
|
@ -134,6 +134,7 @@ export function ResearchEntry({
|
|||
const [saving, setSaving] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [blocked, setBlocked] = useState(false);
|
||||
const [archiveUncertain, setArchiveUncertain] = useState(false);
|
||||
const [downloadError, setDownloadError] = useState('');
|
||||
useUnsavedWarning(editing);
|
||||
useEffect(() => {
|
||||
|
|
@ -241,7 +242,7 @@ export function ResearchEntry({
|
|||
const readonly =
|
||||
archived || Boolean(writeBlocked) || entry?.entity.archived || Boolean(revisionId);
|
||||
const beginEdit = () => {
|
||||
if (!revision || readonly) {
|
||||
if (!revision || readonly || archiveUncertain) {
|
||||
return;
|
||||
}
|
||||
setTitle(revision.title);
|
||||
|
|
@ -260,6 +261,7 @@ export function ResearchEntry({
|
|||
pending.current ||
|
||||
readonly ||
|
||||
blocked ||
|
||||
archiveUncertain ||
|
||||
!title.trim() ||
|
||||
(kind === 'hypothesis' && !body.trim())
|
||||
) {
|
||||
|
|
@ -345,6 +347,7 @@ export function ResearchEntry({
|
|||
archived ||
|
||||
Boolean(writeBlocked) ||
|
||||
pending.current ||
|
||||
archiveUncertain ||
|
||||
!confirmDiscard(editing) ||
|
||||
!window.confirm(`${entry.entity.archived ? 'Restore' : 'Archive'} this research entry?`)
|
||||
) {
|
||||
|
|
@ -354,10 +357,29 @@ export function ResearchEntry({
|
|||
setSaving(true);
|
||||
setError('');
|
||||
try {
|
||||
if (kind === 'hypothesis') {
|
||||
await researchApi.archiveHypothesis(id, !entry.entity.archived);
|
||||
const targetArchived = !entry.entity.archived;
|
||||
let saved: ResearchHypothesis | ResearchArtifact;
|
||||
let matches: boolean;
|
||||
if (entry.kind === 'hypothesis') {
|
||||
({ hypothesis: saved } = await researchApi.archiveHypothesis(id, targetArchived));
|
||||
matches = saved.latestRevision.hypothesisId === id;
|
||||
} else {
|
||||
await researchApi.archiveArtifact(id, !entry.entity.archived);
|
||||
({ artifact: saved } = await researchApi.archiveArtifact(id, targetArchived));
|
||||
matches =
|
||||
saved.latestRevision.artifactId === id &&
|
||||
saved.kind === entry.entity.kind &&
|
||||
saved.hypothesisRevisionId === entry.entity.hypothesisRevisionId;
|
||||
}
|
||||
// Archive has no narrative revision precondition. A concurrent edit may be newer.
|
||||
if (
|
||||
!matches ||
|
||||
saved.id !== id ||
|
||||
saved.projectId !== projectId ||
|
||||
saved.ownerPrincipal !== entry.entity.ownerPrincipal ||
|
||||
saved.archived !== targetArchived ||
|
||||
saved.latestRevision.validationState !== 'unvalidated'
|
||||
) {
|
||||
throw new ResearchApiError('The archive response could not be verified.', null);
|
||||
}
|
||||
if (mounted.current) {
|
||||
setEditing(false);
|
||||
|
|
@ -367,6 +389,7 @@ export function ResearchEntry({
|
|||
} catch (cause) {
|
||||
if (mounted.current) {
|
||||
setError(cause instanceof Error ? cause.message : 'Could not update archive state');
|
||||
setArchiveUncertain(!(cause instanceof ResearchApiError) || cause.ambiguous);
|
||||
}
|
||||
} finally {
|
||||
pending.current = false;
|
||||
|
|
@ -449,12 +472,20 @@ export function ResearchEntry({
|
|||
)}
|
||||
<div className="flex gap-2 flex-wrap">
|
||||
{!readonly && !editing && (
|
||||
<button className={actionClass} onClick={beginEdit} disabled={saving}>
|
||||
<button
|
||||
className={actionClass}
|
||||
onClick={beginEdit}
|
||||
disabled={saving || archiveUncertain}
|
||||
>
|
||||
Edit latest revision
|
||||
</button>
|
||||
)}
|
||||
{!archived && !writeBlocked && (
|
||||
<button className={actionClass} onClick={() => void archive()} disabled={saving}>
|
||||
<button
|
||||
className={actionClass}
|
||||
onClick={() => void archive()}
|
||||
disabled={saving || archiveUncertain}
|
||||
>
|
||||
{entry.entity.archived ? 'Restore entry' : 'Archive entry'}
|
||||
</button>
|
||||
)}
|
||||
|
|
@ -464,7 +495,33 @@ export function ResearchEntry({
|
|||
{error}
|
||||
</p>
|
||||
)}
|
||||
{editing && !readonly && (
|
||||
{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={() => {
|
||||
if (confirmDiscard(editing)) {
|
||||
setLoading(true);
|
||||
setEditing(false);
|
||||
setArchiveUncertain(false);
|
||||
setBlocked(false);
|
||||
setError('');
|
||||
setReload(value => value + 1);
|
||||
onRefresh();
|
||||
}
|
||||
}}
|
||||
>
|
||||
Discard draft and reload archive state
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{editing && !archived && !entry.entity.archived && !revisionId && (
|
||||
<form
|
||||
aria-label="Revise research entry"
|
||||
className="space-y-3"
|
||||
|
|
@ -477,7 +534,7 @@ export function ResearchEntry({
|
|||
className={fieldClass}
|
||||
value={title}
|
||||
onChange={event => setTitle(event.target.value)}
|
||||
disabled={saving || blocked}
|
||||
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
|
|
@ -488,7 +545,7 @@ export function ResearchEntry({
|
|||
className={fieldClass}
|
||||
value={body}
|
||||
onChange={event => setBody(event.target.value)}
|
||||
disabled={saving || blocked}
|
||||
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
|
||||
required={kind === 'hypothesis'}
|
||||
/>
|
||||
</label>
|
||||
|
|
@ -500,7 +557,7 @@ export function ResearchEntry({
|
|||
className={fieldClass}
|
||||
value={criteria}
|
||||
onChange={event => setCriteria(event.target.value)}
|
||||
disabled={saving || blocked}
|
||||
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
|
||||
/>
|
||||
</label>
|
||||
)}
|
||||
|
|
@ -511,7 +568,7 @@ export function ResearchEntry({
|
|||
className={fieldClass}
|
||||
value={origin}
|
||||
onChange={event => setOrigin(event.target.value as ResearchContentOrigin)}
|
||||
disabled={saving || blocked}
|
||||
disabled={saving || blocked || archiveUncertain || Boolean(writeBlocked)}
|
||||
>
|
||||
<option value="human">Human</option>
|
||||
<option value="ai">AI</option>
|
||||
|
|
@ -545,7 +602,12 @@ export function ResearchEntry({
|
|||
className={actionClass}
|
||||
type="submit"
|
||||
disabled={
|
||||
saving || blocked || !title.trim() || (kind === 'hypothesis' && !body.trim())
|
||||
saving ||
|
||||
blocked ||
|
||||
archiveUncertain ||
|
||||
Boolean(writeBlocked) ||
|
||||
!title.trim() ||
|
||||
(kind === 'hypothesis' && !body.trim())
|
||||
}
|
||||
>
|
||||
{saving ? 'Saving revision...' : 'Save new revision'}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue