diff --git a/packages/sanity/src/structure/panes/document/documentPanel/documentViews/useConditionalToast.ts b/packages/sanity/src/structure/panes/document/documentPanel/documentViews/useConditionalToast.ts index fa0f2c190ffc..cae97e64cd6e 100644 --- a/packages/sanity/src/structure/panes/document/documentPanel/documentViews/useConditionalToast.ts +++ b/packages/sanity/src/structure/panes/document/documentPanel/documentViews/useConditionalToast.ts @@ -1,13 +1,5 @@ import {type ToastParams, useToast} from '@sanity/ui' -import {useEffect, useRef} from 'react' - -function usePrevious(value: T) { - const ref = useRef() - useEffect(() => { - ref.current = value - }, [value]) - return ref.current -} +import {useEffect} from 'react' // https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value const LONG_ENOUGH_BUT_NOT_TOO_LONG = 1000 * 60 * 60 * 24 * 24 @@ -18,20 +10,12 @@ 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) // note1: there's a `duration || 0` in Sanity UI's pushToast(), so make it non-falsey // note2: cannot use `Infinity` as duration, since it exceeds setTimeout's maximum delay value useEffect(() => { - if (!wasEnabled && params.enabled) { + if (params.enabled) { toast.push({...params, duration: LONG_ENOUGH_BUT_NOT_TOO_LONG}) } - 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, - }) - } return () => { if (params.enabled) { toast.push({ @@ -41,5 +25,5 @@ export function useConditionalToast(params: ToastParams & {id: string; enabled?: }) } } - }, [params, toast, wasEnabled]) + }, [params, toast]) }