Skip to content

Commit dc40f6e

Browse files
authored
fix(structure): make sync document toast dismisable (#7209)
* 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
1 parent 445384d commit dc40f6e

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

packages/sanity/src/structure/panes/document/documentPanel/documentViews/FormView.tsx

+13-7
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,19 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
6767
const isLocked = editState?.transactionSyncLock?.enabled
6868
const {t} = useTranslation(structureLocaleNamespace)
6969

70-
useConditionalToast({
71-
id: `sync-lock-${documentId}`,
72-
status: 'warning',
73-
enabled: isLocked,
74-
title: t('document-view.form-view.sync-lock-toast.title'),
75-
description: t('document-view.form-view.sync-lock-toast.description'),
76-
})
70+
const conditionalToastParams = useMemo(
71+
() => ({
72+
id: `sync-lock`,
73+
status: 'warning' as const,
74+
enabled: isLocked,
75+
title: t('document-view.form-view.sync-lock-toast.title'),
76+
description: t('document-view.form-view.sync-lock-toast.description'),
77+
closable: true,
78+
}),
79+
[isLocked, t],
80+
)
81+
82+
useConditionalToast(conditionalToastParams)
7783

7884
useEffect(() => {
7985
const sub = documentStore.pair
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import {type ToastParams, useToast} from '@sanity/ui'
2-
import {useEffect, useRef} from 'react'
3-
4-
function usePrevious<T>(value: T) {
5-
const ref = useRef<T>()
6-
useEffect(() => {
7-
ref.current = value
8-
}, [value])
9-
return ref.current
10-
}
2+
import {useEffect} from 'react'
113

124
// https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value
135
const LONG_ENOUGH_BUT_NOT_TOO_LONG = 1000 * 60 * 60 * 24 * 24
@@ -18,19 +10,20 @@ const LONG_ENOUGH_BUT_NOT_TOO_LONG = 1000 * 60 * 60 * 24 * 24
1810
export function useConditionalToast(params: ToastParams & {id: string; enabled?: boolean}) {
1911
const toast = useToast()
2012

21-
const wasEnabled = usePrevious(params.enabled)
2213
// note1: there's a `duration || 0` in Sanity UI's pushToast(), so make it non-falsey
2314
// note2: cannot use `Infinity` as duration, since it exceeds setTimeout's maximum delay value
2415
useEffect(() => {
25-
if (!wasEnabled && params.enabled) {
16+
if (params.enabled) {
2617
toast.push({...params, duration: LONG_ENOUGH_BUT_NOT_TOO_LONG})
2718
}
28-
if (wasEnabled && !params.enabled) {
29-
toast.push({
30-
...params,
31-
// Note: @sanity/ui fallbacks to the default duration of 4s in case of falsey values
32-
duration: 0.01,
33-
})
19+
return () => {
20+
if (params.enabled) {
21+
toast.push({
22+
...params,
23+
// Note: @sanity/ui fallbacks to the default duration of 4s in case of falsey values
24+
duration: 0.01,
25+
})
26+
}
3427
}
35-
}, [params, toast, wasEnabled])
28+
}, [params, toast])
3629
}

0 commit comments

Comments
 (0)