mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-26 21:53:06 +00:00
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 <fsolla@users.noreply.github.com>
This commit is contained in:
parent
f7e3591ea9
commit
9a402d2ac1
3 changed files with 58 additions and 38 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 () => {
|
||||
|
|
|
|||
|
|
@ -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<HTMLFormElement>(null)
|
||||
const previousStatusRef = useRef<OpsEstimateSyncStatus | undefined>(undefined)
|
||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||
const [formError, setFormError] = useState<string | null>(null)
|
||||
const [baseEstimatedAt, setBaseEstimatedAt] = useState<string | null>(currentEstimatedAt)
|
||||
// Override only until RSC props catch up after "Manter o meu".
|
||||
const [baseOverride, setBaseOverride] = useState<string | null | undefined>(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<HTMLFormElement>) => {
|
||||
event.preventDefault()
|
||||
|
|
@ -300,23 +313,25 @@ const HybridPledgeEstimateForm = ({
|
|||
{pending ? 'Salvando estimativa…' : null}
|
||||
</span>
|
||||
</div>
|
||||
<VoteEstimateScenarioInputs
|
||||
fieldPrefix="estimatedVotes"
|
||||
values={outboxRow?.estimatedVotes ?? currentEstimatedVotes}
|
||||
idPrefix={`pledge-estimate-${pledgeID}`}
|
||||
/>
|
||||
<Field>
|
||||
<FieldLabel htmlFor={`pledge-note-${pledgeID}`} className="text-xs">
|
||||
Justificativa
|
||||
</FieldLabel>
|
||||
<Input
|
||||
id={`pledge-note-${pledgeID}`}
|
||||
name="estimateNote"
|
||||
maxLength={1000}
|
||||
defaultValue={outboxRow?.estimateNote ?? currentEstimateNote ?? undefined}
|
||||
className="min-h-11"
|
||||
<div key={fieldsKey} className="flex flex-col gap-3">
|
||||
<VoteEstimateScenarioInputs
|
||||
fieldPrefix="estimatedVotes"
|
||||
values={outboxRow?.estimatedVotes ?? currentEstimatedVotes}
|
||||
idPrefix={`pledge-estimate-${pledgeID}`}
|
||||
/>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor={`pledge-note-${pledgeID}`} className="text-xs">
|
||||
Justificativa
|
||||
</FieldLabel>
|
||||
<Input
|
||||
id={`pledge-note-${pledgeID}`}
|
||||
name="estimateNote"
|
||||
maxLength={1000}
|
||||
defaultValue={outboxRow?.estimateNote ?? currentEstimateNote ?? undefined}
|
||||
className="min-h-11"
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
<Button type="submit" variant="secondary" disabled={pending} className="min-h-11 self-start">
|
||||
{pending ? <Spinner data-icon="inline-start" aria-hidden="true" /> : null}
|
||||
Salvar estimativa
|
||||
|
|
|
|||
Loading…
Reference in a new issue