From 9a402d2ac1bdbf259af443ade06247d9221f2478 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 1 Aug 2026 22:15:19 +0000 Subject: [PATCH] fix(OH6): bootstrap outbox on mount and tighten CAS/subscribe Restore IndexedDB pending rows on hybrid form mount, drop synced stubs so RSC props win, gate the post-lock re-read to CAS only, and notify per-pledge subscribers instead of every estimate island. Co-authored-by: Francisco Solla --- .../(campaign)/campanha/actions/votePledge.ts | 23 ++++---- .../campaign/opsSync/opsEstimateOutbox.ts | 14 +++-- .../votePledge/PledgeEstimateForm.tsx | 59 ++++++++++++------- 3 files changed, 58 insertions(+), 38 deletions(-) diff --git a/src/app/(campaign)/campanha/actions/votePledge.ts b/src/app/(campaign)/campanha/actions/votePledge.ts index 559fa08a..58650714 100644 --- a/src/app/(campaign)/campanha/actions/votePledge.ts +++ b/src/app/(campaign)/campanha/actions/votePledge.ts @@ -160,16 +160,19 @@ export const estimateVotesRecord = async ( `vote-pledge:${requireRelationshipId(pledge.leadership)}:${requireRelationshipId(pledge.municipality)}`, ]) - // Re-read under the lock so CAS sees the committed `estimatedAt`, not a - // pre-lock snapshot that another writer may have already bumped. - const lockedPledge = await payload.findByID({ - collection: 'votePledge', - id: pledge.id, - depth: 0, - user: currentActor, - overrideAccess: false, - req, - }) + // Re-read under the lock only when CAS is armed — LWW callers keep the + // pre-lock row (same access check + advisory lock as before OH6). + const lockedPledge = + enforceCas && data.baseEstimatedAt !== undefined + ? await payload.findByID({ + collection: 'votePledge', + id: pledge.id, + depth: 0, + user: currentActor, + overrideAccess: false, + req, + }) + : pledge if (enforceCas && data.baseEstimatedAt !== undefined) { const currentEstimatedAt = lockedPledge.estimatedAt ?? null diff --git a/src/components/campaign/opsSync/opsEstimateOutbox.ts b/src/components/campaign/opsSync/opsEstimateOutbox.ts index c9e2ea32..78114eeb 100644 --- a/src/components/campaign/opsSync/opsEstimateOutbox.ts +++ b/src/components/campaign/opsSync/opsEstimateOutbox.ts @@ -55,11 +55,9 @@ const createEstimateOfflineExecutor = (): OfflineExecutor => }) opsEstimateOutboxCollection.utils.acceptMutations(transaction) if (opsEstimateOutboxCollection.has(row.pledgeId)) { - opsEstimateOutboxCollection.update(row.pledgeId, (draft) => { - draft.status = 'synced' - draft.serverEstimatedAt = undefined - draft.errorMessage = undefined - }) + // Drop the local row so RSC props win after refresh (OH7 will + // patch the mirror instead of keeping a synced stub). + opsEstimateOutboxCollection.delete(row.pledgeId) } } catch (error) { const message = error instanceof Error ? error.message : String(error) @@ -135,10 +133,14 @@ export const enqueueEstimateVotes = async ( } export const subscribeOpsEstimateOutboxRow = ( - _pledgeId: number, + pledgeId: number, onStoreChange: () => void, ): (() => void) => { + let previous = opsEstimateOutboxCollection.get(pledgeId) const subscription = opsEstimateOutboxCollection.subscribeChanges(() => { + const next = opsEstimateOutboxCollection.get(pledgeId) + if (next === previous) return + previous = next onStoreChange() }) return () => { diff --git a/src/components/campaign/votePledge/PledgeEstimateForm.tsx b/src/components/campaign/votePledge/PledgeEstimateForm.tsx index f431742c..4a08c0bb 100644 --- a/src/components/campaign/votePledge/PledgeEstimateForm.tsx +++ b/src/components/campaign/votePledge/PledgeEstimateForm.tsx @@ -15,6 +15,7 @@ import { toast } from 'sonner' import { discardOpsEstimateOutboxRow, enqueueEstimateVotes, + getOpsEstimateOfflineExecutor, readOpsEstimateOutboxRow, subscribeOpsEstimateOutboxRow, } from '@/components/campaign/opsSync/opsEstimateOutbox' @@ -180,9 +181,12 @@ const HybridPledgeEstimateForm = ({ const router = useRouter() const liveRegionId = useId() const formRef = useRef(null) + const previousStatusRef = useRef(undefined) const [isSubmitting, setIsSubmitting] = useState(false) const [formError, setFormError] = useState(null) - const [baseEstimatedAt, setBaseEstimatedAt] = useState(currentEstimatedAt) + // Override only until RSC props catch up after "Manter o meu". + const [baseOverride, setBaseOverride] = useState(undefined) + const baseEstimatedAt = baseOverride !== undefined ? baseOverride : currentEstimatedAt const outboxRow = useSyncExternalStore( (onStoreChange) => subscribeOpsEstimateOutboxRow(pledgeID, onStoreChange), @@ -191,13 +195,21 @@ const HybridPledgeEstimateForm = ({ ) useEffect(() => { - setBaseEstimatedAt(currentEstimatedAt) + // Restore IndexedDB outbox + optimistic rows across reload. + void getOpsEstimateOfflineExecutor() + }, []) + + useEffect(() => { + setBaseOverride(undefined) }, [currentEstimatedAt]) useEffect(() => { - if (outboxRow?.status !== 'synced') return - router.refresh() - }, [outboxRow?.status, router]) + const previous = previousStatusRef.current + previousStatusRef.current = outboxRow?.status + if (previous === 'pending' && outboxRow === undefined) { + router.refresh() + } + }, [outboxRow, router]) useEffect(() => { if (outboxRow?.status !== 'conflict') return @@ -224,7 +236,7 @@ const HybridPledgeEstimateForm = ({ baseEstimatedAt: outboxRow.serverEstimatedAt ?? null, }).then( () => { - setBaseEstimatedAt(outboxRow.serverEstimatedAt ?? null) + setBaseOverride(outboxRow.serverEstimatedAt ?? null) setFormError(null) }, (error: unknown) => { @@ -254,6 +266,7 @@ const HybridPledgeEstimateForm = ({ const status = outboxRow?.status const pending = isSubmitting || status === 'pending' + const fieldsKey = `pledge-estimate-fields-${pledgeID}-${outboxRow?.status ?? 'rsc'}-${currentEstimatedAt ?? 'none'}` const onSubmit = (event: FormEvent) => { event.preventDefault() @@ -300,23 +313,25 @@ const HybridPledgeEstimateForm = ({ {pending ? 'Salvando estimativa…' : null} - - - - Justificativa - - + - + + + Justificativa + + + +