sanity-io / visual-editing

https://visual-editing-studio.sanity.build
MIT License
37 stars 20 forks source link

feat(visual-editing): add async getSnapshot to optimistic documents #2180

Closed rdunk closed 4 days ago

rdunk commented 5 days ago

This is kind of a fix dressed up as a feat.

Accessing optimistic document snapshots is currently error prone as it is assumed the snapshot will be available, in actual fact it may not be as snapshots have to be fetched from Content Lake via Presentation tool. In most situations this is fine as user interaction is required to trigger some document snapshot getter and so will likely not occur immediately

doc.get() returns a snapshot and throws if the snapshot is unavailable. This is now deprecated.

doc.getSnapshot() replaces it. It returns a promise which is resolved when the snapshot is actually returned.

As a result the Context Menu now has a loading state (video below demonstrates this with an artificial 5s delay added to snapshot fetching), and interactions like drag and drop will await the snapshot resolution before patches are applied, rather than just throwing.

Similarly doc.patch((context) => {...} will currently throw if used before snapshots have been fetched. In this PR snapshot is deprecated but still available on this context object, however the property has been replaced with a getter so that it will not throw unless explicitly accessed. snapshot is replaced with getSnapshot which similarly returns a promise.

https://github.com/user-attachments/assets/38ad7c1b-68b8-4d17-95f9-da2ee5523ff3

vercel[bot] commented 5 days ago

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
live-visual-editing-next βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Nov 18, 2024 5:28pm
visual-editing-astro βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Nov 18, 2024 5:28pm
visual-editing-next βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Nov 18, 2024 5:28pm
visual-editing-next-with-i18n βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Nov 18, 2024 5:28pm
visual-editing-nuxt βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Nov 18, 2024 5:28pm
visual-editing-page-builder-demo βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Nov 18, 2024 5:28pm
visual-editing-remix βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Nov 18, 2024 5:28pm
visual-editing-storybook βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Nov 18, 2024 5:28pm
visual-editing-svelte βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Nov 18, 2024 5:28pm
1 Skipped Deployment | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **visual-editing-studio** | ⬜️ Skipped ([Inspect](https://vercel.com/sanity-sandbox/visual-editing-studio/2Knn2fftkizVfReSZhNE4Q6JSEPf)) | | | Nov 18, 2024 5:28pm |