From d82dcc382a08968e3534b7eee35345a7c7321fc7 Mon Sep 17 00:00:00 2001 From: Domagoj Kriskovic Date: Tue, 26 Aug 2025 13:01:14 +0200 Subject: [PATCH] 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 --- .../file-tree/components/file-tree-root.tsx | 7 ++++++- .../hooks/file-tree-socket-listener.ts | 18 ++++++++++++++---- .../context/file-tree-open-context.tsx | 6 +++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/services/web/frontend/js/features/file-tree/components/file-tree-root.tsx b/services/web/frontend/js/features/file-tree/components/file-tree-root.tsx index 17ca5bf504..cbc3b1f689 100644 --- a/services/web/frontend/js/features/file-tree/components/file-tree-root.tsx +++ b/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() diff --git a/services/web/frontend/js/features/file-tree/hooks/file-tree-socket-listener.ts b/services/web/frontend/js/features/file-tree/hooks/file-tree-socket-listener.ts index a9f15721fc..80c1e0078d 100644 --- a/services/web/frontend/js/features/file-tree/hooks/file-tree-socket-listener.ts +++ b/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) diff --git a/services/web/frontend/js/features/ide-react/context/file-tree-open-context.tsx b/services/web/frontend/js/features/ide-react/context/file-tree-open-context.tsx index 196c36f330..0b0003654a 100644 --- a/services/web/frontend/js/features/ide-react/context/file-tree-open-context.tsx +++ b/services/web/frontend/js/features/ide-react/context/file-tree-open-context.tsx @@ -121,10 +121,10 @@ export const FileTreeOpenProvider: FC = ({ ) 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!) } },