From 92b69fcff28c5e04ebfc6d5dc13e4ee3e76cdd30 Mon Sep 17 00:00:00 2001 From: Binoy Patel Date: Mon, 11 Sep 2023 14:06:53 -0400 Subject: [PATCH 1/2] fix(core): fixes issue with resotring document in nested panes --- .../documentActions/HistoryRestoreAction.tsx | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/sanity/src/desk/documentActions/HistoryRestoreAction.tsx b/packages/sanity/src/desk/documentActions/HistoryRestoreAction.tsx index b8a61661c81..947ae37db3c 100644 --- a/packages/sanity/src/desk/documentActions/HistoryRestoreAction.tsx +++ b/packages/sanity/src/desk/documentActions/HistoryRestoreAction.tsx @@ -1,20 +1,36 @@ import {RestoreIcon} from '@sanity/icons' -import React, {useCallback, useMemo, useState} from 'react' -import {DocumentActionComponent, DocumentActionDialogProps, useDocumentOperation} from 'sanity' +import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react' +import { + DocumentActionComponent, + DocumentActionDialogProps, + useDocumentOperation, + useDocumentOperationEvent, +} from 'sanity' import {useRouter} from 'sanity/router' /** @internal */ export const HistoryRestoreAction: DocumentActionComponent = ({id, type, revision, onComplete}) => { const {restore} = useDocumentOperation(id, type) + const event = useDocumentOperationEvent(id, type) const {navigateIntent} = useRouter() + const prevEvent = useRef(event) const [isConfirmDialogOpen, setConfirmDialogOpen] = useState(false) const handleConfirm = useCallback(() => { restore.execute(revision!) onComplete() - // wrapping in setTimeout gives the onComplete time to finish before navigating - setTimeout(() => navigateIntent('edit', {id, type}), 0) - }, [restore, revision, navigateIntent, id, type, onComplete]) + }, [restore, revision, onComplete]) + + /** + * If the restore operation is successful, navigate to the document edit view + */ + useEffect(() => { + if (!event || event === prevEvent.current) return + + if (event.type === 'success' && event.op === 'restore') { + navigateIntent('edit', {id, type}) + } + }, [event, id, navigateIntent, type]) const handle = useCallback(() => { setConfirmDialogOpen(true) From 1c0c6ada2973d51a0392f8142fca1f92a03fdbdd Mon Sep 17 00:00:00 2001 From: Binoy Patel Date: Wed, 13 Sep 2023 11:03:59 -0400 Subject: [PATCH 2/2] fix(core): update the prevEvent with new event --- .../sanity/src/desk/documentActions/HistoryRestoreAction.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/sanity/src/desk/documentActions/HistoryRestoreAction.tsx b/packages/sanity/src/desk/documentActions/HistoryRestoreAction.tsx index 947ae37db3c..d68f1b501c4 100644 --- a/packages/sanity/src/desk/documentActions/HistoryRestoreAction.tsx +++ b/packages/sanity/src/desk/documentActions/HistoryRestoreAction.tsx @@ -30,6 +30,8 @@ export const HistoryRestoreAction: DocumentActionComponent = ({id, type, revisio if (event.type === 'success' && event.op === 'restore') { navigateIntent('edit', {id, type}) } + + prevEvent.current = event }, [event, id, navigateIntent, type]) const handle = useCallback(() => {