Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(visual-editing): prevent snapshot overfetching #2615

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions packages/visual-editing/src/ui/Overlays.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,35 @@
const DocumentReporter: FunctionComponent<{
documentIds: string[]
perspective: ClientPerspective
}> = (props) => {

Check failure on line 79 in packages/visual-editing/src/ui/Overlays.tsx

View workflow job for this annotation

GitHub Actions / build

[ReactCompilerBailout] [InferReferenceEffects] Unexpected LoadLocal with context kind (@:101:8)
const {documentIds} = props
const actor = useOptimisticActor()
const [uniqueIds, setUniqueIds] = useState<string[]>([])

useEffect((): (() => void) => {
return observeDocumentIds(actor, documentIds)
}, [actor, documentIds])
useEffect(() => {
setUniqueIds((prev) => {
const next = Array.from(new Set(documentIds))
return prev.length === next.length &&
prev.reduce((acc, prevId) => acc.filter((id) => id !== prevId), next)?.length === 0
? prev
: next
})
}, [documentIds])
const actor = useOptimisticActor()

return null
}
function observeDocumentIds(actor: ReturnType<typeof useOptimisticActor>, documentIds: string[]) {
const uniqueIds = Array.from(new Set(documentIds))
for (const id of uniqueIds) {
actor.send({type: 'observe', documentId: getDraftId(id)})
actor.send({type: 'observe', documentId: getPublishedId(id)})
}
return () => {
useEffect(() => {
for (const id of uniqueIds) {
actor.send({type: 'unobserve', documentId: getDraftId(id)})
actor.send({type: 'unobserve', documentId: getPublishedId(id)})
actor.send({type: 'observe', documentId: getDraftId(id)})
actor.send({type: 'observe', documentId: getPublishedId(id)})
}
}
return () => {
for (const id of uniqueIds) {
actor.send({type: 'unobserve', documentId: getDraftId(id)})
actor.send({type: 'unobserve', documentId: getPublishedId(id)})
}
}
}, [actor, uniqueIds])

return null
}

const OverlaysController: FunctionComponent<{
Expand Down Expand Up @@ -323,6 +330,7 @@
}, [overlayEnabled])

const documentIds = useMemo(() => {
console.log('recalc documentIds')

Check failure on line 333 in packages/visual-editing/src/ui/Overlays.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log('recalc documentIds')

return elements.flatMap((element) => ('id' in element.sanity ? [element.sanity.id] : []))
}, [elements])

Expand Down
Loading