Skip to content

Commit

Permalink
fix(structure): close conditional toast when component unmounts
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Jul 20, 2024
1 parent 3867548 commit cd83cf1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,19 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
const isLocked = editState?.transactionSyncLock?.enabled
const {t} = useTranslation(structureLocaleNamespace)

useConditionalToast({
id: `sync-lock-${documentId}`,
status: 'warning',
enabled: isLocked,
title: t('document-view.form-view.sync-lock-toast.title'),
description: t('document-view.form-view.sync-lock-toast.description'),
closable: true,
})
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],
)

useConditionalToast(conditionalToastParams)

useEffect(() => {
const sub = documentStore.pair
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,15 @@ export function useConditionalToast(params: ToastParams & {id: string; enabled?:
duration: 0.01,
})
}
return () => {
// Close the toast when the component unmounts
if (params.enabled) {
toast.push({
...params,
// Note: @sanity/ui fallbacks to the default duration of 4s in case of falsey values
duration: 0.01,
})
}
}
}, [params, toast, wasEnabled])
}

0 comments on commit cd83cf1

Please sign in to comment.