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: migrate @sanity/presentation codebase #8312

Merged
merged 1 commit into from
Jan 17, 2025
Merged

Conversation

stipsan
Copy link
Member

@stipsan stipsan commented Jan 17, 2025

Description

This PR copies over the codebase for @sanity/presentation on its main branch. I've then refactored the code to pass our linter checks for things like:

  • React.createContext only being allowed in sanity/_singletons.
  • Components in ui-components like <Button />, <Tooltip> and such should be used instead of importing directly from @sanity/ui
  • Slight differences in TypeScript and ESLint

The codebase is otherwise the same. It's tempting to refactor and start cleaning things up, but it's important to note that after this PR is merged, I'll have to:

  • pull the changes on this PR on top of the corel branch
  • manually apply the changes from the other corel branch, on top of Presentation.
  • once both are merged I'll delete packages/presentation from the visual-editing monorepo, and start adding over tests for more advanced presentation features that aren't enabled in the current test studio.

It's more important that we reach the step where we can delete the code from visual-editing ASAP, so we avoid new work being done there, for example by external contributors.

What to review

Is the new code following conventions ok?
It does pull in a bunch of new dependencies, and I want to work through them and get rid of unnecessary ones (we only use suspend-react in one instance, and it doesn't really need to). But they are lazy loaded, and isn't much different from how we re-export @sanity/presentation as-is today.

Testing

Tests are lacking, once this is merged in we can start working on playwright component tests for Presentation, as well as better vitest coverage for utils and parsers.
For now the PR build seems to work like it did before the migration: https://test-studio-git-migrate-presentation.sanity.dev/presentation/presentation/simpleBlock/716e2c8a-a168-4852-87e2-0c2b4d42c94b/body%5B_key==%22161e19ce1c6b%22%5D.children%5B_key==%22af5299cd594b%22%5D.text

Notes for release

The @sanity/presentation codebase has been migrated into the sanity codebase. We've always recommended using Presentation Tool by importing it from sanity/presentation like so:

// sanity.config.ts
import {defineConfig} from 'sanity'
import {presentationTool} from 'sanity/presentation'
import {structureTool} from 'sanity/structure'

export default defineConfig({
  plugins: [presentationTool(), structureTool()]
})

If you've been following this pattern then this change won't impact you in any way.

If you've been installing @sanity/presentation and are using it directly (we've sometimes done this to let folks try out bugfixes before they ship in the next release of sanity):

// sanity.config.ts
import {defineConfig} from 'sanity'
import {presentationTool} from '@sanity/presentation'
import {structureTool} from 'sanity/structure'

export default defineConfig({
  plugins: [presentationTool(), structureTool()]
})

Then you need to search/replace all from '@sanity/presentation' statements in your codebase with from 'sanity/presentation', and uninstall @sanity/presentation as it'll no longer receive updates.

Copy link

vercel bot commented Jan 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 Jan 17, 2025 6:22pm
performance-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 17, 2025 6:22pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 17, 2025 6:22pm
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Jan 17, 2025 6:22pm
test-next-studio ⬜️ Ignored (Inspect) Jan 17, 2025 6:22pm

Copy link

socket-security bot commented Jan 17, 2025

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@sanity/[email protected] None +1 81.3 kB sanity-io

🚮 Removed packages: npm/@sanity/[email protected]

View full report↗︎

Copy link
Contributor

No changes to documentation

Copy link
Contributor

github-actions bot commented Jan 17, 2025

Component Testing Report Updated Jan 17, 2025 6:29 PM (UTC)

❌ Failed Tests (1) -- expand for details
File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 1m 2s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 12s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ❌ Failed (Inspect) 1m 20s 5 0 1
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 50s 11 7 0
formBuilder/inputs/PortableText/copyPaste/CopyPasteFields.spec.tsx ✅ Passed (Inspect) 0s 0 12 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 25s 6 0 0
formBuilder/inputs/PortableText/DisableFocusAndUnset.spec.tsx ✅ Passed (Inspect) 14s 3 0 0
formBuilder/inputs/PortableText/DragAndDrop.spec.tsx ✅ Passed (Inspect) 26s 6 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 1m 6s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 31s 21 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 2m 2s 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 43s 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

Copy link
Contributor

github-actions bot commented Jan 17, 2025

⚡️ Editor Performance Report

Updated Fri, 17 Jan 2025 18:31:28 GMT

Benchmark reference
latency of sanity@latest
experiment
latency of this branch
Δ (%)
latency difference
article (title) 24.4 efps (41ms) 23.8 efps (42ms) +1ms (+2.4%)
article (body) 63.7 efps (16ms) 77.5 efps (13ms) -3ms (-/-%)
article (string inside object) 24.7 efps (41ms) 25.0 efps (40ms) -1ms (-1.2%)
article (string inside array) 22.7 efps (44ms) 22.2 efps (45ms) +1ms (+2.3%)
recipe (name) 51.3 efps (20ms) 52.6 efps (19ms) -1ms (-2.6%)
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) 19.6 efps (51ms) +0ms (-/-%)
synthetic (string inside object) 19.6 efps (51ms) 20.0 efps (50ms) -1ms (-2.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) 41ms 46ms 66ms 336ms 828ms 10.6s
article (body) 16ms 18ms 28ms 192ms 270ms 5.8s
article (string inside object) 41ms 43ms 46ms 160ms 156ms 6.8s
article (string inside array) 44ms 46ms 51ms 177ms 261ms 7.1s
recipe (name) 20ms 22ms 25ms 52ms 0ms 7.5s
recipe (description) 18ms 20ms 21ms 31ms 0ms 4.7s
recipe (instructions) 5ms 7ms 7ms 15ms 0ms 3.1s
synthetic (title) 51ms 55ms 62ms 172ms 364ms 12.0s
synthetic (string inside object) 51ms 54ms 62ms 464ms 959ms 8.2s

🧪 Experiment result

The performance result of this branch

Benchmark latency p75 p90 p99 blocking time test duration
article (title) 42ms 53ms 72ms 496ms 827ms 11.6s
article (body) 13ms 16ms 19ms 149ms 191ms 5.0s
article (string inside object) 40ms 44ms 48ms 116ms 51ms 6.8s
article (string inside array) 45ms 47ms 51ms 201ms 366ms 7.4s
recipe (name) 19ms 21ms 24ms 37ms 0ms 7.1s
recipe (description) 17ms 20ms 23ms 37ms 0ms 4.6s
recipe (instructions) 5ms 7ms 8ms 19ms 0ms 3.1s
synthetic (title) 51ms 53ms 55ms 105ms 305ms 12.1s
synthetic (string inside object) 50ms 53ms 56ms 438ms 1046ms 8.2s

📚 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.

}
`

export const Resizer: FunctionComponent<{
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT, We have a Resizer that we could replace this one with as a next step. packages/sanity/src/core/components/resizer/Resizer.tsx

Copy link
Member Author

Choose a reason for hiding this comment

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

Could we do that in a follow up PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, sure! Added some small suggestions for follow up, nothing is blocking.

white-space: pre-wrap;
`

export function DocumentListPane(props: {
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT - follow up, could be good to rename this file to something else to avoid confusions with the structure/documentListPane

white-space: pre-wrap;
`

export function DocumentPane(props: {
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT - follow up, could be good to rename this file to something else to avoid confusions with the structure/DocumentPane

@@ -0,0 +1,12 @@
export function debounce<F extends (...args: Parameters<F>) => ReturnType<F>>(
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT, follow up, could be replaced for debounce from lodash

const value = props.value as SanityDocument
const documentId = value?._id ? getPublishedId(value?._id) : undefined

if (isDocumentSchemaType(props.schemaType)) {
Copy link
Contributor

@pedrobonamin pedrobonamin Jan 17, 2025

Choose a reason for hiding this comment

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

NIT , follow up this seems to be added only once for the document.
I think you could benefit from using unstable_layout instead of checking in each input if it's a document schema type.

import {API_VERSION} from './constants'
import {type PreviewUrlOption} from './types'

export function usePreviewUrl(
Copy link
Contributor

@pedrobonamin pedrobonamin Jan 17, 2025

Choose a reason for hiding this comment

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

Follow up. - we have a usePreviewURL function in structure would be nice to see if we can join the two https://github.com/sanity-io/sanity/blob/corel-references/packages/sanity/src/structure/panes/document/usePreviewUrl.ts#L11-L12

pedrobonamin
pedrobonamin previously approved these changes Jan 17, 2025
Copy link
Contributor

@pedrobonamin pedrobonamin left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks for moving this @stipsan !
Added some small suggestions but nothing is blocking, more of some future iterations we can do.

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.

2 participants