Просмотр исходного кода

Skip opening root document if delete originated from a file-restore (#27992)

* Skip opening root document if delete originated from a file-restore

* handle project-restore origin

* Refactor isFileRestore logic

GitOrigin-RevId: f2a34189140deb4e614d93e8d197b8a6a90c8f65
Domagoj Kriskovic 11 месяцев назад
Родитель
Сommit
dff92f1e6b

+ 6 - 1
services/web/frontend/js/features/file-tree/components/file-tree-root.tsx

@@ -20,6 +20,7 @@ import { useDragLayer } from 'react-dnd'
 import classnames from 'classnames'
 import { pathInFolder } from '@/features/file-tree/util/path'
 import { useIsNewEditorEnabled } from '@/features/ide-redesign/utils/new-editor-utils'
+import { FileTreeFindResult } from '@/features/ide-react/types/file-tree'
 
 const FileTreeRoot = React.memo<{
   onSelect: () => void
@@ -115,7 +116,11 @@ const FileTreeRoot = React.memo<{
   )
 })
 
-function FileTreeRootFolder({ onDelete }: { onDelete: () => void }) {
+function FileTreeRootFolder({
+  onDelete,
+}: {
+  onDelete: (entity: FileTreeFindResult, isFileRestore?: boolean) => void
+}) {
   useFileTreeSocketListener(onDelete)
   const { fileTreeData } = useFileTreeData()
 

+ 14 - 4
services/web/frontend/js/features/file-tree/hooks/file-tree-socket-listener.ts

@@ -6,8 +6,11 @@ import { useFileTreeSelectable } from '../contexts/file-tree-selectable'
 import { findInTree, findInTreeOrThrow } from '../util/find-in-tree'
 import { useIdeContext } from '@/shared/context/ide-context'
 import { useSnapshotContext } from '@/features/ide-react/context/snapshot-context'
+import { FileTreeFindResult } from '@/features/ide-react/types/file-tree'
 
-export function useFileTreeSocketListener(onDelete: (entity: any) => void) {
+export function useFileTreeSocketListener(
+  onDelete: (entity: FileTreeFindResult, isFileRestore?: boolean) => void
+) {
   const user = useUserContext()
   const {
     dispatchRename,
@@ -53,7 +56,10 @@ export function useFileTreeSocketListener(onDelete: (entity: any) => void) {
 
   useEffect(() => {
     if (fileTreeFromHistory) return
-    function handleDispatchDelete(entityId: string) {
+    function handleDispatchDelete(
+      entityId: string,
+      origin?: { kind: string } | string
+    ) {
       const entity = findInTree(fileTreeData, entityId)
       unselect(entityId)
       if (selectedEntityParentIds.has(entityId)) {
@@ -70,8 +76,12 @@ export function useFileTreeSocketListener(onDelete: (entity: any) => void) {
         }
       }
       dispatchDelete(entityId)
-      if (onDelete) {
-        onDelete(entity)
+      if (onDelete && entity) {
+        const isFileRestore =
+          typeof origin === 'object' &&
+          (origin.kind === 'file-restore' || origin.kind === 'project-restore')
+
+        onDelete(entity, isFileRestore)
       }
     }
     if (socket) socket.on('removeEntity', handleDispatchDelete)

+ 3 - 3
services/web/frontend/js/features/ide-react/context/file-tree-open-context.tsx

@@ -121,10 +121,10 @@ export const FileTreeOpenProvider: FC<React.PropsWithChildren> = ({
   )
 
   const handleFileTreeDelete = useCallback(
-    (entity: FileTreeFindResult) => {
+    (entity: FileTreeFindResult, isFileRestore?: boolean) => {
       eventEmitter.emit('entity:deleted', entity)
-      // Select the root document if the current document was deleted
-      if (entity.entity._id === currentDocumentId) {
+      // Select the root document if the current document was deleted and delete is not part of a file restore
+      if (!isFileRestore && entity.entity._id === currentDocumentId) {
         openDocWithId(rootDocId!)
       }
     },