Skip to content

Commit

Permalink
fix(core): fixes issue with restoring document in nested panes (#4915)
Browse files Browse the repository at this point in the history
* fix(core): fixes issue with resotring document in nested panes

* fix(core): update the prevEvent with new event
  • Loading branch information
binoy14 committed Sep 13, 2023
1 parent e7a8e32 commit 47f3be9
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions packages/sanity/src/desk/documentActions/HistoryRestoreAction.tsx
@@ -1,20 +1,38 @@
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})
}

prevEvent.current = event
}, [event, id, navigateIntent, type])

const handle = useCallback(() => {
setConfirmDialogOpen(true)
Expand Down

2 comments on commit 47f3be9

@vercel
Copy link

@vercel vercel bot commented on 47f3be9 Sep 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

performance-studio – ./

performance-studio.sanity.build
performance-studio-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 47f3be9 Sep 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

Please sign in to comment.