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

feat(core): add useCreateForm hook #8557

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

Conversation

pedrobonamin
Copy link
Contributor

@pedrobonamin pedrobonamin commented Feb 10, 2025

Description

This PR implements a new hook useCreateForm which handles the necessary logic to build a form and pass the values to the <FormProvider>

It also simplifies the logic inside the <DocumentPaneProvider> to make it easier to digest:

Further steps:

  • update sanity assist to use this and deprecate the forcedVersion in DocumentPaneProvider
  • refactor the inspect legacy dialog into the new inspectors logic suggestion

Why we need this?

Currently if you want to render a form using the patch mechanism, have presence, validation and compare values you would need to reuse the <DocumentPaneProvider> which bring with it some unnecessary things like history , actions, perspective etc, or you needed to write it in your own like in useTasksFormBuilder. This is not ideal and it takes a lot of effort from the developer.

By implementing this hook you can now do use this minimal implementation to get all the necessary form values and handlers. (see example in code)

const form = useCreateForm({documentId, documentType, initialValue})

This should also be useful for:

What to review

Please help me review in detail this PR as it touches a critical surface which handles a lot of logic, my recommendation is going commit per commit to see how the changes build on top of each other.

Testing

Manual testing has been done to the PR, focusing on history, versions and tasks which are not covered by e2e tests.
Existing tests should cover the rest of the functionality

Notes for release

adds useCreateForm hook

@pedrobonamin pedrobonamin requested review from a team as code owners February 10, 2025 10:31
@pedrobonamin pedrobonamin requested review from cngonzalez and removed request for a team February 10, 2025 10:31
Copy link

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

Copy link
Contributor

No changes to documentation

Copy link
Contributor

github-actions bot commented Feb 10, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 42.41% 53750 / 126732
🔵 Statements 42.41% 53750 / 126732
🔵 Functions 47.89% 2767 / 5777
🔵 Branches 78.77% 10282 / 13053
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/sanity/src/core/form/index.ts 100% 100% 100% 100%
packages/sanity/src/core/form/useCreateForm.ts 6.87% 100% 0% 6.87% 110-449, 452-469
packages/sanity/src/core/i18n/bundles/studio.ts 100% 100% 100% 100%
packages/sanity/src/core/tasks/components/form/tasksFormBuilder/TasksFormBuilder.tsx 20.56% 100% 0% 20.56% 27-35, 49-115, 123-173
packages/sanity/src/structure/i18n/resources.ts 100% 100% 100% 100%
packages/sanity/src/structure/panes/document/DocumentPaneContext.ts 0% 0% 0% 0%
packages/sanity/src/structure/panes/document/DocumentPaneProvider.tsx 3.81% 100% 100% 3.81% 51-507
packages/sanity/src/structure/panes/document/useDocumentPaneInitialValue.ts 25% 100% 0% 25% 19-44
packages/sanity/src/structure/panes/document/useDocumentPaneInspector.ts 4.69% 100% 0% 4.69% 11-192
packages/sanity/src/structure/panes/document/document-layout/DocumentLayout.tsx 13.48% 100% 0% 13.48% 67-294
packages/sanity/src/structure/panes/document/statusBar/DocumentStatusBarActions.tsx 12.72% 100% 0% 12.72% 30-93, 96-142, 145-175, 178-181
Generated in workflow #30090 for commit 5a4dff2 by the Vitest Coverage Report Action

Copy link
Contributor

github-actions bot commented Feb 10, 2025

Component Testing Report Updated Feb 10, 2025 5:18 PM (UTC)

❌ Failed Tests (4) -- expand for details
File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 1m 11s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 12s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ❌ Failed (Inspect) 2m 35s 3 0 3
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 54s 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 38s 21 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 2m 7s 21 0 0
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 13s 3 9 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 27s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ❌ Failed (Inspect) 2m 26s 20 0 1
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 Feb 10, 2025

⚡️ Editor Performance Report

Updated Mon, 10 Feb 2025 16:15:58 GMT

Benchmark reference
latency of sanity@latest
experiment
latency of this branch
Δ (%)
latency difference
article (title) 25.6 efps (39ms) 25.0 efps (40ms) +1ms (+2.6%)
article (body) 72.2 efps (14ms) 78.1 efps (13ms) -1ms (-/-%)
article (string inside object) 27.4 efps (37ms) 26.3 efps (38ms) +2ms (+4.1%)
article (string inside array) 23.8 efps (42ms) 23.3 efps (43ms) +1ms (+2.4%)
recipe (name) 45.5 efps (22ms) 54.1 efps (19ms) -4ms (-15.9%)
recipe (description) 50.0 efps (20ms) 58.8 efps (17ms) -3ms (-15.0%)
recipe (instructions) 99.9+ efps (5ms) 99.9+ efps (6ms) +1ms (-/-%)
synthetic (title) 18.2 efps (55ms) 20.0 efps (50ms) -5ms (-9.1%)
synthetic (string inside object) 19.6 efps (51ms) 19.4 efps (52ms) +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) 39ms 43ms 86ms 511ms 1086ms 11.3s
article (body) 14ms 15ms 21ms 146ms 189ms 5.0s
article (string inside object) 37ms 38ms 42ms 79ms 110ms 6.8s
article (string inside array) 42ms 44ms 48ms 258ms 382ms 7.4s
recipe (name) 22ms 24ms 25ms 43ms 0ms 7.6s
recipe (description) 20ms 21ms 24ms 37ms 0ms 4.9s
recipe (instructions) 5ms 6ms 7ms 13ms 0ms 3.0s
synthetic (title) 55ms 58ms 63ms 287ms 1134ms 26.5s
synthetic (string inside object) 51ms 55ms 82ms 351ms 1296ms 9.0s

🧪 Experiment result

The performance result of this branch

Benchmark latency p75 p90 p99 blocking time test duration
article (title) 40ms 45ms 57ms 195ms 211ms 10.5s
article (body) 13ms 14ms 18ms 80ms 64ms 4.6s
article (string inside object) 38ms 41ms 48ms 258ms 312ms 7.3s
article (string inside array) 43ms 46ms 51ms 101ms 318ms 7.5s
recipe (name) 19ms 20ms 24ms 38ms 0ms 7.3s
recipe (description) 17ms 17ms 18ms 30ms 0ms 4.5s
recipe (instructions) 6ms 8ms 8ms 10ms 0ms 3.3s
synthetic (title) 50ms 56ms 69ms 360ms 1221ms 13.0s
synthetic (string inside object) 52ms 55ms 67ms 323ms 692ms 8.0s

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

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.

1 participant