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

refactor: consistently apply current perspective for previews #8655

Open
wants to merge 8 commits into
base: next
Choose a base branch
from

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 21, 2025 5:38pm
performance-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 21, 2025 5:38pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 21, 2025 5:38pm
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Feb 21, 2025 5:38pm
test-next-studio ⬜️ Ignored (Inspect) Feb 21, 2025 5:38pm

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 43.12% 55080 / 127733
🔵 Statements 43.12% 55080 / 127733
🔵 Functions 48.28% 2807 / 5813
🔵 Branches 79.58% 10692 / 13434
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 68.65% 76.92% 100% 68.65% 31-60
packages/sanity/src/core/perspective/PerspectiveProvider.tsx 80.76% 56.25% 50% 80.76% 26-30
packages/sanity/src/core/perspective/types.ts 0% 0% 0% 0%
packages/sanity/src/core/perspective/useSetPerspective.tsx 69.23% 100% 50% 69.23% 13-16
packages/sanity/src/core/perspective/navbar/GlobalPerspectiveMenu.tsx 98% 92.85% 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 94.11% 66.66% 66.66% 94.11% 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.14% 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% 85% 100% 97.91% 69
packages/sanity/src/core/releases/tool/detail/AddDocumentSearch.tsx 69.38% 100% 50% 69.38% 34-54, 61
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 98.66% 90.9% 50% 98.66% 76
packages/sanity/src/core/releases/tool/detail/useBundleDocuments.ts 8.91% 100% 0% 8.91% 53-157, 160-218, 221-260, 266-284
packages/sanity/src/core/releases/tool/detail/documentTable/DocumentTableColumnDefs.tsx 84.9% 82.75% 100% 84.9% 69-74, 185-205
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-106
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% 100% 97.77% 103, 136-137
Generated in workflow #30829 for commit b61034a by the Vitest Coverage Report Action

Copy link
Contributor

github-actions bot commented Feb 19, 2025

⚡️ Editor Performance Report

Updated Fri, 21 Feb 2025 17:44:40 GMT

Benchmark reference
latency of sanity@latest
experiment
latency of this branch
Δ (%)
latency difference
article (title) 25.0 efps (40ms) 23.8 efps (42ms) +2ms (+5.0%)
article (body) 53.8 efps (19ms) 59.5 efps (17ms) -2ms (-9.7%)
article (string inside object) 27.0 efps (37ms) 25.0 efps (40ms) +3ms (+8.1%)
article (string inside array) 23.3 efps (43ms) 21.3 efps (47ms) +4ms (+9.3%)
recipe (name) 52.6 efps (19ms) 50.0 efps (20ms) +1ms (+5.3%)
recipe (description) 58.8 efps (17ms) 62.5 efps (16ms) -1ms (-5.9%)
recipe (instructions) 99.9+ efps (6ms) 99.9+ efps (5ms) -1ms (-/-%)
synthetic (title) 20.0 efps (50ms) 20.6 efps (49ms) -2ms (-3.0%)
synthetic (string inside object) 19.2 efps (52ms) 20.2 efps (50ms) -3ms (-4.8%)

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) 40ms 47ms 71ms 492ms 712ms 10.5s
article (body) 19ms 28ms 82ms 299ms 453ms 6.8s
article (string inside object) 37ms 39ms 42ms 66ms 246ms 6.9s
article (string inside array) 43ms 46ms 50ms 80ms 308ms 7.4s
recipe (name) 19ms 22ms 24ms 37ms 0ms 6.9s
recipe (description) 17ms 19ms 23ms 34ms 0ms 4.5s
recipe (instructions) 6ms 7ms 8ms 23ms 0ms 3.2s
synthetic (title) 50ms 55ms 66ms 351ms 1213ms 12.8s
synthetic (string inside object) 52ms 55ms 87ms 431ms 1190ms 9.1s

🧪 Experiment result

The performance result of this branch

Benchmark latency p75 p90 p99 blocking time test duration
article (title) 42ms 50ms 94ms 455ms 1034ms 11.4s
article (body) 17ms 21ms 34ms 121ms 441ms 6.3s
article (string inside object) 40ms 44ms 49ms 91ms 275ms 7.3s
article (string inside array) 47ms 48ms 56ms 161ms 452ms 7.8s
recipe (name) 20ms 22ms 24ms 44ms 0ms 6.7s
recipe (description) 16ms 17ms 18ms 45ms 0ms 4.4s
recipe (instructions) 5ms 7ms 8ms 10ms 0ms 3.1s
synthetic (title) 49ms 52ms 67ms 266ms 872ms 12.2s
synthetic (string inside object) 50ms 52ms 63ms 420ms 925ms 8.3s

📚 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 21, 2025 5:47 PM (UTC)

❌ Failed Tests (5) -- expand for details
File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 1m 24s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 14s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ❌ Failed (Inspect) 2m 41s 3 0 3
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 1m 1s 11 7 0
formBuilder/inputs/PortableText/copyPaste/CopyPasteFields.spec.tsx ✅ Passed (Inspect) 0s 0 12 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 30s 6 0 0
formBuilder/inputs/PortableText/DisableFocusAndUnset.spec.tsx ✅ Passed (Inspect) 17s 3 0 0
formBuilder/inputs/PortableText/DragAndDrop.spec.tsx ✅ Passed (Inspect) 31s 6 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ❌ Failed (Inspect) 1m 58s 14 0 1
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 53s 21 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ❌ Failed (Inspect) 2m 58s 20 0 1
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 15s 3 9 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 30s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 2m 0s 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
const observable = useMemo<Observable<State>>(() => {
// this will render previews as "loaded" (i.e. not in loading state) – typically with "Untitled" text
if (!enabled || !previewValue || !schemaType) return of(IDLE_STATE)

return observeForPreview(previewValue as Previewable, schemaType, {
perspective:
// eslint-disable-next-line no-nested-ternary
selectedPerspective === 'drafts'
Copy link
Member

Choose a reason for hiding this comment

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

Q: Had we not mentioned about making this the default logic for perspectiveStack? Was there a reason not to do that in the end, or just to keep this PR from scope-creep?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, wanted to avoid scope creep and figured it's better to do it in a separate pass anyway to minimize blast radius :)

juice49
juice49 previously approved these changes Feb 21, 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.

Nice! Thanks so much for implementing this. The changes look sensible to me, and Studio is working nicely in my testing.

@juice49 juice49 self-requested a review February 21, 2025 15:49
@juice49 juice49 dismissed their stale review February 21, 2025 15:50

Investigating bug.

juice49
juice49 previously approved these changes Feb 21, 2025
ReleaseId from `@sanity/client` is deprecated as there is no guarantees of release ids starting with `r`
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.

3 participants