Skip to content

refactor: consistently apply current perspective for previews #8655

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

Merged
merged 10 commits into from
Feb 27, 2025
Merged

Conversation

bjoerge
Copy link
Member

@bjoerge bjoerge commented Feb 17, 2025

Description

This is a somewhat big refactor of how we resolve values for previews internally.

Previously, for each document the studio was previewing, we'd fetch a version of the preview for each known release.
This created some overhead (althought the requests are batched), but most notably it caused an issue with previews ending up showing referenced values from the published version.

This is now solved by always applying the current perspective stack when fetching preview values. The perspective stack is applied recursively, so if a document type has someReference.title in it's preview config, the title of the referenced document will also reflect the current perspective (i.e. what the title is in the selected release).

Making the perspective apply to previews enabled me to remove some layering logic, since this is now done in the query that fetches the previews.

Using the just the current perspective stack poses a challenge: Sometimes you want to preview something that does not exist in the current perspective. For example: you are in the drafts perspective, but you want to preview a document that only exists as a version (i.e. it has neither a draft nor a published variant). This happens e.g. when previewing document list items or search results since we these collections using perspective=raw because we want people to still find version documents that exists outside of the current perspective.

For these cases, we always have a version id, so in these cases we'll fetch:

  • A preview for the document with current perspective applied
  • A preview for the document with the version perspective applied.
    If the document doesn’t exist in the current perspective, it certainly exist in it’s own version perspective, so that means we’ll always have something to preview.

Things to note:

  • Since perspective applies to the query request, I had to change the preview observer batch fetcher memo to include the perspective stack as key.
  • The previous implementation fetched a preview version for each known release in order to answer "what other releases does this document exist in". This question can be answered more simply by fetching the _rev for each version document and check for existence (so no need to fetch the full preview). The information about what other version exists is now decoupled from previews entirely, and can be retrieved using the new useDocumentVersionInfo()-hook. We no longer have preview for the version documents because we didn't have any use for that at the moment. We can always come back to that decision in the future, but for now, they return a stub with _id, _createdAt, _updatedAt, _rev, etc.
  • We were currently supporting a perspective for search, which only seems to apply to the global search atm. I took a chance on removing that, because I don't think that's desired. As of this PR, global search will use perspective=raw.

Other changes

  • Added a useDocumentVersionInfo(<documentId>)-hook that will return the versions that exists for the given document id
  • Simplified the getPreviewStateObservable() so it no longer requires a fallback title – the fallback can trivially be applied in the consumer end, and it lead to a bunch of cases where we passed an empty string as the third argument.

What to review

This should be reviewed a bit carefully. Esp. in terms of API changes and naming (is it sensible and understandable?)

Testing

We have fairly good test coverage already, and e2e tests already made me aware of a few oversights. Would love some additional testing of everything related to previews, search and references e.g:

  • Document list previews
  • Global search
  • Adding documents to release
  • Searching document lists
  • Searching for references
  • Reference previews in Portable Text Editor, arrays and objects

Notes for release

  • Fixes a bug that caused previews to sometimes display values from published documents instead of drafts or versions.
  • Fixes a bug that caused documents outside of the current release to not appear in global search
  • Fixes a bug that would show "Missing document" for any document outside of the selected release without published or draft variants.

Copy link

vercel bot commented Feb 17, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
page-building-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 27, 2025 2:47pm
performance-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 27, 2025 2:47pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 27, 2025 2:47pm
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Feb 27, 2025 2:47pm
test-next-studio ⬜️ Ignored (Inspect) Feb 27, 2025 2:47pm

Copy link
Contributor

No changes to documentation

Copy link
Contributor

github-actions bot commented Feb 19, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 42.54% 54432 / 127950
🔵 Statements 42.54% 54432 / 127950
🔵 Functions 47.45% 2739 / 5772
🔵 Branches 79.48% 10286 / 12940
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/sanity/src/core/index.ts 100% 100% 100% 100%
packages/sanity/src/core/comments/context/comments/CommentsProvider.tsx 6.55% 100% 0% 6.55% 72-354
packages/sanity/src/core/comments/hooks/useNotificationTarget.ts 24.24% 100% 0% 24.24% 32-61
packages/sanity/src/core/comments/store/useCommentsStore.ts 18.18% 100% 0% 18.18% 64-200
packages/sanity/src/core/components/documentStatus/DocumentStatus.tsx 20.63% 100% 0% 20.63% 33-79, 101-117
packages/sanity/src/core/components/documentStatusIndicator/DocumentStatusIndicator.tsx 15.68% 100% 0% 15.68% 48-96
packages/sanity/src/core/form/inputs/ReferenceInput/OptionPreview.tsx 13.95% 100% 0% 13.95% 24-83
packages/sanity/src/core/form/inputs/ReferenceInput/PreviewReferenceValue.tsx 6.62% 100% 0% 6.62% 24-235
packages/sanity/src/core/form/inputs/ReferenceInput/ReferenceField.tsx 10.81% 100% 0% 10.81% 43-59, 63-310
packages/sanity/src/core/form/inputs/ReferenceInput/ReferenceInput.tsx 11.02% 100% 0% 11.02% 37-39, 46-373
packages/sanity/src/core/form/inputs/ReferenceInput/ReferenceItem.tsx 9.06% 100% 0% 9.06% 54-70, 74-406
packages/sanity/src/core/form/inputs/ReferenceInput/ReferencePreview.tsx 16.66% 100% 0% 16.66% 23-78
packages/sanity/src/core/form/inputs/ReferenceInput/types.ts 0% 0% 0% 0%
packages/sanity/src/core/form/inputs/ReferenceInput/useReferenceInput.tsx 12.5% 100% 0% 12.5% 23-28, 39-137
packages/sanity/src/core/form/studio/contexts/ReferenceInputOptions.tsx 28.57% 100% 0% 28.57% 63, 72-93
packages/sanity/src/core/form/studio/inputs/client-adapters/reference.ts 10.98% 100% 0% 10.98% 31-179, 182-234
packages/sanity/src/core/perspective/GlobalPerspectiveProvider.tsx 23.88% 100% 0% 23.88% 19-70, 82-100
packages/sanity/src/core/perspective/PerspectiveProvider.tsx 23.07% 100% 0% 23.07% 16-54
packages/sanity/src/core/perspective/types.ts 0% 0% 0% 0%
packages/sanity/src/core/perspective/useSetPerspective.tsx 66.66% 100% 50% 66.66% 13-16
packages/sanity/src/core/perspective/navbar/GlobalPerspectiveMenu.tsx 98% 92.3% 50% 98% 32
packages/sanity/src/core/perspective/navbar/ReleaseTypeMenuSection.tsx 100% 95.83% 100% 100%
packages/sanity/src/core/preview/createPathObserver.ts 49.5% 52.94% 83.33% 49.5% 11-16, 24-25, 45-46, 53-59, 70, 75-89, 95-115
packages/sanity/src/core/preview/createPreviewObserver.ts 40% 100% 75% 40% 28-30, 55-76, 80-101, 104-110
packages/sanity/src/core/preview/documentPreviewStore.ts 83.05% 100% 33.33% 83.05% 146-154, 179
packages/sanity/src/core/preview/observeFields.ts 80.58% 100% 66.66% 80.58% 145, 150, 159-162, 178-184, 197, 214, 248-255, 258-267
packages/sanity/src/core/preview/types.ts 0% 0% 0% 0%
packages/sanity/src/core/preview/useValuePreview.ts 93.93% 66.66% 66.66% 93.93% 56-57
packages/sanity/src/core/preview/utils/getPreviewStateObservable.ts 87.87% 33.33% 100% 87.87% 48, 52-54
packages/sanity/src/core/preview/utils/getPreviewValueWithFallback.tsx 16.66% 100% 0% 16.66% 6-15, 24-38
packages/sanity/src/core/releases/hooks/useVersionOperations.tsx 64% 100% 100% 64% 43-49, 56-62, 69-75
packages/sanity/src/core/releases/hooks/utils.ts 83.92% 97.05% 100% 83.92% 28-29, 32-33, 39-40, 49, 70-71
packages/sanity/src/core/releases/store/index.ts 100% 100% 100% 100%
packages/sanity/src/core/releases/store/types.ts 100% 100% 100% 100%
packages/sanity/src/core/releases/store/useDocumentVersionInfo.ts 81.63% 100% 50% 81.63% 13-15, 41-46
packages/sanity/src/core/releases/tool/components/ReleaseDocumentPreview.tsx 97.91% 78.57% 100% 97.91% 69
packages/sanity/src/core/releases/tool/detail/AddDocumentSearch.tsx 100% 100% 100% 100%
packages/sanity/src/core/releases/tool/detail/ReleaseReview.tsx 16.88% 100% 0% 16.88% 43-121
packages/sanity/src/core/releases/tool/detail/ReleaseSummary.tsx 67.08% 75% 66.66% 67.08% 92, 102-147, 156-166, 172-173
packages/sanity/src/core/releases/tool/detail/useBundleDocuments.ts 8.91% 100% 0% 8.91% 54-158, 161-219, 222-261, 267-285
packages/sanity/src/core/releases/tool/detail/documentTable/DocumentTableColumnDefs.tsx 85.29% 74.19% 100% 85.29% 72-77, 143, 201-221
packages/sanity/src/core/releases/tool/detail/review/DocumentReviewHeader.tsx 15.62% 100% 0% 15.62% 37-104
packages/sanity/src/core/releases/util/getReleaseIdFromReleaseDocumentId.ts 63.63% 50% 100% 63.63% 12-15
packages/sanity/src/core/scheduledPublishing/components/scheduleItem/PreviewWrapper.tsx 15.32% 100% 0% 15.32% 45-203, 209-211
packages/sanity/src/core/store/_legacy/document/getPairListener.ts 76.1% 96.66% 75% 76.1% 59-72, 86-87, 121, 136-137, 140-153, 176, 185, 192-202
packages/sanity/src/core/studio/components/navbar/search/components/SearchPopover.tsx 40% 100% 0% 40% 91-159
packages/sanity/src/core/studio/components/navbar/search/components/filters/common/ReferencePreviewTitle.tsx 35.71% 100% 0% 35.71% 16-32
packages/sanity/src/core/studio/components/navbar/search/components/searchResults/SearchResults.tsx 19.75% 100% 0% 19.75% 42-139
packages/sanity/src/core/studio/components/navbar/search/components/searchResults/item/SearchResultItem.tsx 15.85% 100% 0% 15.85% 42-127
packages/sanity/src/core/studio/components/navbar/search/components/searchResults/item/SearchResultItemPreview.tsx 22.44% 100% 0% 22.44% 49-107
packages/sanity/src/core/studio/components/navbar/search/contexts/search/SearchProvider.tsx 92.9% 50% 25% 92.9% 114-119, 148, 171-172, 200-201
packages/sanity/src/core/studio/components/navbar/search/contexts/search/reducer.ts 89.06% 70.73% 100% 89.06% 169-170, 180-183, 242-250, 252-259, 369-376, 401-407, 495-505, 578
packages/sanity/src/core/tasks/hooks/useDocumentPreviewValues.ts 22.58% 100% 0% 22.58% 22-50
packages/sanity/src/core/util/draftUtils.ts 81.18% 97.14% 73.33% 81.18% 54-55, 112-113, 148-152, 156-160, 164-168
packages/sanity/src/presentation/editor/ContentEditor.tsx 0% 0% 0% 0% 1-156
packages/sanity/src/presentation/editor/usePreviewState.ts 0% 0% 0% 0% 1-37
packages/sanity/src/structure/components/paneHeaderActions/PaneHeaderCreateButton.tsx 11.42% 100% 0% 11.42% 32-51, 58-208
packages/sanity/src/structure/components/paneItem/PaneItemPreview.tsx 10% 100% 0% 10% 38-98
packages/sanity/src/structure/components/paneRouter/types.ts 0% 0% 0% 0%
packages/sanity/src/structure/panes/types.ts 0% 0% 0% 0%
packages/sanity/src/structure/panes/document/DocumentEventsPane.tsx 9.23% 100% 0% 9.23% 27-112
packages/sanity/src/structure/panes/document/DocumentPaneContext.ts 0% 0% 0% 0%
packages/sanity/src/structure/panes/documentList/sheetList/useDocumentSheetColumns.tsx 97.77% 85.36% 100% 97.77% 103, 136-137
Generated in workflow #31238 for commit 8e52bf8 by the Vitest Coverage Report Action

Copy link
Contributor

github-actions bot commented Feb 19, 2025

⚡️ Editor Performance Report

Updated Thu, 27 Feb 2025 14:52:16 GMT

Benchmark reference
latency of sanity@latest
experiment
latency of this branch
Δ (%)
latency difference
article (title) 27.8 efps (36ms) 26.0 efps (39ms) +3ms (+6.9%)
article (body) 73.0 efps (14ms) 74.9 efps (13ms) -0ms (-/-%)
article (string inside object) 27.4 efps (37ms) 27.8 efps (36ms) -1ms (-1.4%)
article (string inside array) 24.7 efps (41ms) 25.0 efps (40ms) -1ms (-1.2%)
recipe (name) 52.6 efps (19ms) 52.6 efps (19ms) +0ms (-/-%)
recipe (description) 55.6 efps (18ms) 58.8 efps (17ms) -1ms (-5.6%)
recipe (instructions) 99.9+ efps (5ms) 99.9+ efps (5ms) +0ms (-/-%)
synthetic (title) 19.6 efps (51ms) 20.0 efps (50ms) -1ms (-2.0%)
synthetic (string inside object) 20.0 efps (50ms) 19.8 efps (51ms) +1ms (+1.0%)

efps — editor "frames per second". The number of updates assumed to be possible within a second.

Derived from input latency. efps = 1000 / input_latency

Detailed information

🏠 Reference result

The performance result of sanity@latest

Benchmark latency p75 p90 p99 blocking time test duration
article (title) 36ms 42ms 64ms 231ms 325ms 9.2s
article (body) 14ms 15ms 20ms 57ms 292ms 5.6s
article (string inside object) 37ms 41ms 52ms 158ms 233ms 7.0s
article (string inside array) 41ms 43ms 47ms 142ms 252ms 6.8s
recipe (name) 19ms 22ms 25ms 47ms 5ms 8.1s
recipe (description) 18ms 19ms 22ms 33ms 0ms 4.5s
recipe (instructions) 5ms 6ms 7ms 9ms 0ms 3.1s
synthetic (title) 51ms 54ms 65ms 283ms 1063ms 19.3s
synthetic (string inside object) 50ms 57ms 69ms 231ms 714ms 8.0s

🧪 Experiment result

The performance result of this branch

Benchmark latency p75 p90 p99 blocking time test duration
article (title) 39ms 67ms 81ms 536ms 967ms 10.7s
article (body) 13ms 15ms 18ms 157ms 251ms 5.0s
article (string inside object) 36ms 38ms 42ms 94ms 233ms 6.7s
article (string inside array) 40ms 41ms 45ms 229ms 223ms 7.2s
recipe (name) 19ms 21ms 24ms 64ms 0ms 8.2s
recipe (description) 17ms 18ms 20ms 55ms 0ms 4.5s
recipe (instructions) 5ms 7ms 8ms 33ms 0ms 3.1s
synthetic (title) 50ms 56ms 67ms 352ms 1141ms 12.7s
synthetic (string inside object) 51ms 57ms 60ms 497ms 1218ms 8.9s

📚 Glossary

column definitions

  • benchmark — the name of the test, e.g. "article", followed by the label of the field being measured, e.g. "(title)".
  • latency — the time between when a key was pressed and when it was rendered. derived from a set of samples. the median (p50) is shown to show the most common latency.
  • p75 — the 75th percentile of the input latency in the test run. 75% of the sampled inputs in this benchmark were processed faster than this value. this provides insight into the upper range of typical performance.
  • p90 — the 90th percentile of the input latency in the test run. 90% of the sampled inputs were faster than this. this metric helps identify slower interactions that occurred less frequently during the benchmark.
  • p99 — the 99th percentile of the input latency in the test run. only 1% of sampled inputs were slower than this. this represents the worst-case scenarios encountered during the benchmark, useful for identifying potential performance outliers.
  • blocking time — the total time during which the main thread was blocked, preventing user input and UI updates. this metric helps identify performance bottlenecks that may cause the interface to feel unresponsive.
  • test duration — how long the test run took to complete.

Copy link
Contributor

github-actions bot commented Feb 19, 2025

Component Testing Report Updated Feb 27, 2025 2:51 PM (UTC)

❌ Failed Tests (2) -- expand for details
File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 1m 4s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 12s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ❌ Failed (Inspect) 1m 54s 4 0 2
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 47s 11 7 0
formBuilder/inputs/PortableText/copyPaste/CopyPasteFields.spec.tsx ✅ Passed (Inspect) 0s 0 12 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 26s 6 0 0
formBuilder/inputs/PortableText/DisableFocusAndUnset.spec.tsx ✅ Passed (Inspect) 15s 3 0 0
formBuilder/inputs/PortableText/DragAndDrop.spec.tsx ✅ Passed (Inspect) 27s 6 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 1m 8s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 31s 21 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 1m 45s 21 0 0
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 13s 3 9 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 26s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 1m 40s 21 0 0
formBuilder/tree-editing/TreeEditing.spec.tsx ✅ Passed (Inspect) 0s 0 3 0
formBuilder/tree-editing/TreeEditingNestedObjects.spec.tsx ✅ Passed (Inspect) 0s 0 3 0

@bjoerge bjoerge force-pushed the sapp-2116 branch 2 times, most recently from 63ab336 to 0fbe326 Compare February 19, 2025 18:51
@@ -193,6 +193,7 @@ export function collate<T extends {_id: string; _type: string}>(documents: T[]):
res.set(publishedId, entry)
}

// note: this attaches versions to the `draft` property, which is likely ok for most practical purposes
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh! We are treading the same path 😅.

Copy link
Member Author

Choose a reason for hiding this comment

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

🤝

juice49
juice49 previously approved these changes Feb 26, 2025
Copy link
Contributor

@juice49 juice49 left a comment

Choose a reason for hiding this comment

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

This is great! Still works as expected in my testing. Thanks 🙌.

juice49
juice49 previously approved these changes Feb 27, 2025
@bjoerge bjoerge enabled auto-merge (squash) February 27, 2025 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document created in release shows up as /Missing Document/ in lists
3 participants