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

fix(structure): make sync document toast dismisable #7209

Merged
merged 4 commits into from
Aug 22, 2024

Conversation

pedrobonamin
Copy link
Contributor

@pedrobonamin pedrobonamin commented Jul 19, 2024

Description

An issue is present in structure in which sync toasts are forever present.
For what was discussed with the internal team, is that the toast is persisted even though the condition for it is gone or and even if the document is not in view anymore.

This seems to be caused because when the FormView component is unmounted, the toast doesn't trigger a close action, to solve this a cleanup function was added to the useEffect to close the toast if it is still shown.

This seems to be happening even if the FormView is not visually unmounted (document is still open), this seems to be caused because the FormView component takes a key key={documentId + (ready ? '_ready' : '_pending')}, so when the key changes, the form is unmounted and mounted again, but given there are no clean up functions in the conditional toast hook, the toast will be kept forever.

In this video two buttons were added to force the states that show or hide the toast.

Screen.Recording.2024-07-20.at.10.58.38.mov

It also adds support to close the syncing document toast.

Screenshot 2024-07-19 at 16 06 47

What to review

Testing

Notes for release

@pedrobonamin pedrobonamin requested a review from a team as a code owner July 19, 2024 14:04
@pedrobonamin pedrobonamin requested review from rexxars and removed request for a team July 19, 2024 14:04
Copy link

vercel bot commented Jul 19, 2024

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 Aug 21, 2024 7:15am
performance-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 21, 2024 7:15am
test-compiled-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 21, 2024 7:15am
test-next-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 21, 2024 7:15am
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 21, 2024 7:15am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Aug 21, 2024 7:15am

Copy link
Contributor

No changes to documentation

Copy link
Contributor

github-actions bot commented Jul 19, 2024

Component Testing Report Updated Aug 21, 2024 7:20 AM (UTC)

✅ All Tests Passed -- expand for details
File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 43s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 8s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 30s 6 0 0
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 37s 11 7 0
formBuilder/inputs/PortableText/copyPaste/CopyPasteFields.spec.tsx ✅ Passed (Inspect) 0s 0 12 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 17s 6 0 0
formBuilder/inputs/PortableText/DisableFocusAndUnset.spec.tsx ✅ Passed (Inspect) 10s 3 0 0
formBuilder/inputs/PortableText/DragAndDrop.spec.tsx ✅ Passed (Inspect) 3m 0s 0 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 43s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 47s 21 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 1m 15s 18 0 0
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 9s 3 9 0
formBuilder/inputs/PortableText/RangeDecoration.spec.tsx ✅ Passed (Inspect) 25s 9 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 18s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 1m 13s 21 0 0
formBuilder/tree-editing/TreeEditing.spec.tsx ✅ Passed (Inspect) 1m 46s 30 0 0
formBuilder/tree-editing/TreeEditingNestedObjects.spec.tsx ✅ Passed (Inspect) 20s 3 0 0

Comment on lines 70 to 81
const conditionalToastParams = useMemo(
() => ({
id: `sync-lock-${documentId}`,
status: 'warning' as const,
enabled: isLocked,
title: t('document-view.form-view.sync-lock-toast.title'),
description: t('document-view.form-view.sync-lock-toast.description'),
closable: true,
}),
[documentId, isLocked, t],
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Memoizing this params to avoid unnecessary calls to the useEffect in the useConditionalToast hook, which uses the params as a dependency.

Comment on lines 35 to 27
return () => {
// Close the toast when the component unmounts, even if the condition is still true
if (wasEnabled && params.enabled) {
toast.push({
...params,
// Note: @sanity/ui fallbacks to the default duration of 4s in case of falsey values
duration: 0.01,
})
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will close the toast when the component unmounts, preventing it to be forever present

@@ -18,19 +10,20 @@ const LONG_ENOUGH_BUT_NOT_TOO_LONG = 1000 * 60 * 60 * 24 * 24
export function useConditionalToast(params: ToastParams & {id: string; enabled?: boolean}) {
const toast = useToast()

const wasEnabled = usePrevious(params.enabled)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not necessary now that the cleanup function was added to the useEffect.
If the params.enabled condition was true, then the cleanup will take care of closing the toast when params.enabled changes or when the component is unmounted

@ricokahler ricokahler added this pull request to the merge queue Aug 22, 2024
Merged via the queue into next with commit dc40f6e Aug 22, 2024
42 checks passed
@ricokahler ricokahler deleted the dismissable-document-sync-toast branch August 22, 2024 04:38
jordanl17 pushed a commit that referenced this pull request Aug 29, 2024
* fix(structure): make sync document toast dismissable

* fix(structure): close conditional toast when component unmounts

* chore(structure): remove the usePrevious hook in useConditionalToast

* fix(structure): update sync lock toast id to make it unique across documents
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