diff --git a/src/client/TaskWindow.tsx b/src/client/TaskWindow.tsx index a0d6d1e0..3f983c0b 100644 --- a/src/client/TaskWindow.tsx +++ b/src/client/TaskWindow.tsx @@ -244,6 +244,11 @@ export function TaskWindow(props: TaskWindowProps): ReactNode { * new revisions while the window stays open, and saving with the live one * would silently overwrite a teammate's write. */ const editRevisionRef = useRef(task?.revision) + /** Render-time mirror of the projection's latest revision: `mutate`'s + * closure captures the `task` of the render that began the save, which can + * be stale by the time a conflict comes back. */ + const latestRevisionRef = useRef(task?.revision) + latestRevisionRef.current = task?.revision // Re-seed the draft when the caller swaps the subject task: the projection // hands us fresh revisions, and unsaved text must not leak across tasks. @@ -291,6 +296,15 @@ export function TaskWindow(props: TaskWindowProps): ReactNode { // projection, so a committed revision lands in this window's props // by itself. } else { + if (result.code === CONFLICT_CODE) { + // A stale-revision write is rejected WITHOUT advancing the revision, + // so the snapshot is still behind. Re-base it to the projection's + // latest revision: the conflict note stays up, the draft and editing + // stay put, and the reader's NEXT Save submits against the new base + // (an explicit "overwrite anyway"). Exiting here would lose the draft. + // Read the render-time mirror, not the `task` this closure captured. + editRevisionRef.current = latestRevisionRef.current + } setNote(result.code === CONFLICT_CODE ? t('teamTaskConflict') : t('teamTaskError', { message: result.message })) diff --git a/tests/task-window-edit-revision.spec.tsx b/tests/task-window-edit-revision.spec.tsx index 3df71b08..10bc9354 100644 --- a/tests/task-window-edit-revision.spec.tsx +++ b/tests/task-window-edit-revision.spec.tsx @@ -109,4 +109,56 @@ describe('TaskWindow edit revision snapshot', () => { expectedRevision: 3, })) }) + + it('re-bases after a conflict so the next Save commits, keeping the draft', async () => { + await render(taskView(1, 'First')) + act(() => { button('Edit').click() }) + + // The reader types a real draft into both fields. + await act(async () => { + const subject = document.querySelector('input[aria-label="Subject"]')! + const subjectSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set + subjectSetter?.call(subject, 'My draft') + subject.dispatchEvent(new Event('input', { bubbles: true })) + + const area = document.querySelector('textarea[aria-label="Description"]')! + const areaSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value')?.set + areaSetter?.call(area, 'draft body') + area.dispatchEvent(new Event('input', { bubbles: true })) + }) + + // A teammate's write lands while the draft is open: projection → rev2. + await render(taskView(2, 'Changed elsewhere')) + + // First Save carries the stale rev1 snapshot → rejected (409 team-conflict, + // which does NOT advance the revision). Second Save must use rev2 → ok. + teamsTaskUpdate + .mockImplementationOnce(async () => ({ ok: false, code: 'team-conflict', message: 'stale' })) + .mockImplementationOnce(async () => ({ ok: true })) + + await act(async () => { button('Save').click() }) + expect(teamsTaskUpdate).toHaveBeenNthCalledWith(1, 'root-1', expect.objectContaining({ + action: 'edit', + expectedRevision: 1, + subject: 'My draft', + description: 'draft body', + })) + // The conflict is reported, but the draft is NOT discarded and editing is + // NOT left (the only exit used to throw away unsaved text): the textarea + // is still here with the reader's text, not the task's current content. + expect(document.body.textContent).toContain('The task changed elsewhere; refreshed') + const kept = document.querySelector('textarea[aria-label="Description"]')! + expect(kept.value).toBe('draft body') + + // Second Save now submits against the re-based rev2 and succeeds (the + // window returns to the read-only view, i.e. the Edit button is back). + await act(async () => { button('Save').click() }) + expect(teamsTaskUpdate).toHaveBeenNthCalledWith(2, 'root-1', expect.objectContaining({ + action: 'edit', + expectedRevision: 2, + subject: 'My draft', + description: 'draft body', + })) + expect(button('Edit')).toBeDefined() + }) })