From d04f0b2d0dd5b7382e89062158da9f199d2d5eec Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:58:04 -0800 Subject: [PATCH] Keep preservation choice safe when eligibility changes --- .../deletion/DeleteWorktreeDialog.tsx | 8 +- .../deletion/DeleteWorktreeDialogFooter.tsx | 8 +- .../DeleteWorktreeLineageNotice.test.tsx | 56 +++++++ .../deletion/DeleteWorktreeLineageNotice.tsx | 140 ++++++++++-------- .../use-detach-worktree-delete-children.ts | 4 +- src/renderer/src/i18n/locales/en.json | 2 + .../languages/protected-copy-keys.json | 2 + 7 files changed, 151 insertions(+), 69 deletions(-) create mode 100644 src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeLineageNotice.test.tsx diff --git a/src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeDialog.tsx b/src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeDialog.tsx index 30f7c37720d..d8aeabc49eb 100644 --- a/src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeDialog.tsx +++ b/src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeDialog.tsx @@ -160,7 +160,10 @@ const DeleteWorktreeDialog = React.memo(function DeleteWorktreeDialog() { t ) const allowSkipConfirm = - !isBatchDelete && modalData.allowSkipConfirm !== false && childWorkspaceCount === 0 + !isBatchDelete && + modalData.allowSkipConfirm !== false && + childWorkspaceCount === 0 && + !preserveChildren const [isSavingForcePreference, setIsSavingForcePreference] = useState(false) const deleteTargets = useMemo( () => (deleteAllLineage ? lineageDelete.deleteAllTargets : worktrees), @@ -348,7 +351,7 @@ const DeleteWorktreeDialog = React.memo(function DeleteWorktreeDialog() { - {childWorkspaceCount > 0 && ( + {(childWorkspaceCount > 0 || preserveChildren) && ( @@ -82,7 +84,7 @@ export function DeleteWorktreeDialogFooter({ {isDeleting ? : } {label} diff --git a/src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeLineageNotice.test.tsx b/src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeLineageNotice.test.tsx new file mode 100644 index 00000000000..a22b8af0a9b --- /dev/null +++ b/src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeLineageNotice.test.tsx @@ -0,0 +1,56 @@ +// @vitest-environment happy-dom + +import { cleanup, fireEvent, render, screen } from '@testing-library/react' +import { afterEach, expect, it, vi } from 'vitest' +import type { Worktree } from '../../../../../../shared/worktree/types' +import { DeleteWorktreeLineageNotice } from './DeleteWorktreeLineageNotice' + +vi.mock('@/store', () => ({ + useAppStore: (selector: (state: { projects: never[]; projectHostSetups: never[] }) => unknown) => + selector({ projects: [], projectHostSetups: [] }) +})) + +afterEach(cleanup) + +it('lets a selected preserve option be cleared when it becomes unavailable, even with no children', () => { + const onChange = vi.fn() + const child: Worktree = { + id: 'child', + instanceId: 'child-instance', + repoId: 'repo', + path: '/parent/child', + displayName: 'child', + head: 'abc123', + branch: 'child', + isBare: false, + isMainWorktree: false, + comment: '', + linkedIssue: null, + linkedPR: null, + linkedLinearIssue: null, + isArchived: false, + isUnread: false, + isPinned: false, + sortOrder: 0, + lastActivityAt: 0 + } + const props = { + descendants: [child], + dirtyChangeCountsByWorktreeId: new Map(), + dirtyChangePreviewsByWorktreeId: new Map(), + preserveChildren: true, + canPreserveChildren: false, + disabled: false, + onPreserveChildrenChange: onChange + } + const { rerender } = render() + const checkbox = screen.getByRole('checkbox') + expect(checkbox.hasAttribute('disabled')).toBe(false) + fireEvent.click(checkbox) + expect(onChange).toHaveBeenCalledWith(false) + + rerender() + fireEvent.click(screen.getByRole('checkbox')) + expect(onChange).toHaveBeenCalledTimes(2) + expect(onChange).toHaveBeenLastCalledWith(false) +}) diff --git a/src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeLineageNotice.tsx b/src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeLineageNotice.tsx index 58fe2994b89..eaae20644d4 100644 --- a/src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeLineageNotice.tsx +++ b/src/renderer/src/components/sidebar/workspace-actions/deletion/DeleteWorktreeLineageNotice.tsx @@ -64,7 +64,7 @@ export function DeleteWorktreeLineageNotice({ } const foreignProjects = [...new Set(projectLabels.values())] const childWorkspaceCount = descendants.length - if (childWorkspaceCount === 0) { + if (childWorkspaceCount === 0 && !preserveChildren) { return null } @@ -74,80 +74,94 @@ export function DeleteWorktreeLineageNotice({
- {preserveChildren - ? t('worktree.delete.childrenPreservedTitle', 'Child workspaces will be kept') - : t( - 'auto.components.sidebar.DeleteWorktreeLineageNotice.a940f3c96e', - 'Child workspaces will be deleted' - )} -
-
- {preserveChildren - ? t( - 'worktree.delete.childrenPreservedDescription', - 'Direct children will become top-level workspaces. Their files, terminals, and nested children will be kept.' - ) - : t( - 'auto.components.sidebar.DeleteWorktreeLineageNotice.alsoDeletesChildren', - 'Deleting this workspace also deletes {{count}} child workspaces.', - { - count: childWorkspaceCount, - defaultValue_one: - 'Deleting this workspace also deletes {{count}} child workspace.' - } - )} - {foreignProjects.length > 0 ? ` (${foreignProjects.join(', ')})` : null} + {preserveChildren && !canPreserveChildren + ? t('worktree.delete.childrenCannotBeKeptTitle', 'Child workspaces cannot be kept') + : preserveChildren + ? t('worktree.delete.childrenPreservedTitle', 'Child workspaces will be kept') + : t( + 'auto.components.sidebar.DeleteWorktreeLineageNotice.a940f3c96e', + 'Child workspaces will be deleted' + )}
+ {(!preserveChildren || canPreserveChildren) && ( +
+ {preserveChildren + ? t( + 'worktree.delete.childrenPreservedDescription', + 'Direct children will become top-level workspaces. Their files, terminals, and nested children will be kept.' + ) + : t( + 'auto.components.sidebar.DeleteWorktreeLineageNotice.alsoDeletesChildren', + 'Deleting this workspace also deletes {{count}} child workspaces.', + { + count: childWorkspaceCount, + defaultValue_one: + 'Deleting this workspace also deletes {{count}} child workspace.' + } + )} + {foreignProjects.length > 0 ? ` (${foreignProjects.join(', ')})` : null} +
+ )} {/* Constrain grid min-content width so nowrap paths cannot widen the modal. */} -
- {descendants.slice(0, 4).map((child) => ( -
-
- {child.displayName} - {projectLabels.has(child.id) ? ( - · {projectLabels.get(child.id)} - ) : null} + {childWorkspaceCount > 0 && ( +
+ {descendants.slice(0, 4).map((child) => ( +
+
+ {child.displayName} + {projectLabels.has(child.id) ? ( + + {' '} + · {projectLabels.get(child.id)} + + ) : null} +
+
{child.path}
+ {!preserveChildren && ( + + )}
-
{child.path}
- {!preserveChildren && ( - - )} -
- ))} - {descendants.length > 4 ? ( -
- {t( - 'auto.components.sidebar.DeleteWorktreeLineageNotice.moreChildren', - '+{{count}} more', - { count: descendants.length - 4, defaultValue_one: '+{{count}} more' } - )} -
- ) : null} -
+ ))} + {descendants.length > 4 ? ( +
+ {t( + 'auto.components.sidebar.DeleteWorktreeLineageNotice.moreChildren', + '+{{count}} more', + { count: descendants.length - 4, defaultValue_one: '+{{count}} more' } + )} +
+ ) : null} +
+ )} {!canPreserveChildren && (

- {t( - 'worktree.delete.nestedChildrenCannotBeKept', - 'Child workspace folders inside this workspace must be moved outside it before they can be kept.' - )} + {childWorkspaceCount === 0 || !parent + ? t( + 'worktree.delete.childrenChangedClearPreserve', + 'Child workspaces changed. Clear this option to review deletion.' + ) + : t( + 'worktree.delete.nestedChildrenCannotBeKept', + 'Child workspace folders inside this workspace must be moved outside it before they can be kept.' + )}

)}
diff --git a/src/renderer/src/components/sidebar/workspace-actions/deletion/use-detach-worktree-delete-children.ts b/src/renderer/src/components/sidebar/workspace-actions/deletion/use-detach-worktree-delete-children.ts index 82ed169f6fe..ff455f7449f 100644 --- a/src/renderer/src/components/sidebar/workspace-actions/deletion/use-detach-worktree-delete-children.ts +++ b/src/renderer/src/components/sidebar/workspace-actions/deletion/use-detach-worktree-delete-children.ts @@ -50,7 +50,9 @@ export function useDetachWorktreeDeleteChildren({ const [isDetachingChildren, setIsDetachingChildren] = useState(false) const [detachError, setDetachError] = useState(null) const canPreserveChildren = - !!parent && canPreserveWorkspaceDeleteChildren(parent, lineage.descendants, isFolderWorkspace) + !!parent && + lineage.descendants.length > 0 && + canPreserveWorkspaceDeleteChildren(parent, lineage.descendants, isFolderWorkspace) if (!isOpen && (preserveChildren || detachError)) { setPreserve(false) diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index a7b7ea88434..4c47d6cb183 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -22633,10 +22633,12 @@ "delete": { "detachingChildren": "Detaching children...", "childrenPreservedTitle": "Child workspaces will be kept", + "childrenCannotBeKeptTitle": "Child workspaces cannot be kept", "childrenPreservedDescription": "Direct children will become top-level workspaces. Their files, terminals, and nested children will be kept.", "detachChildren": "Detach and keep child workspaces", "nestedChildrenCannotBeKept": "Child workspace folders inside this workspace must be moved outside it before they can be kept.", "childrenChanged": "Child workspaces changed. Review the workspace list and try again.", + "childrenChangedClearPreserve": "Child workspaces changed. Clear this option to review deletion.", "detachFailed": "Could not detach all child workspaces. The parent was not deleted. {{error}}" } }, diff --git a/src/shared/plugins/languages/protected-copy-keys.json b/src/shared/plugins/languages/protected-copy-keys.json index a638ccb9b06..4312ab3e4c9 100644 --- a/src/shared/plugins/languages/protected-copy-keys.json +++ b/src/shared/plugins/languages/protected-copy-keys.json @@ -1019,7 +1019,9 @@ "workspaceDeletion.moreActions", "workspaceDeletion.moreDeleteOptions", "workspaceDeletion.preferenceSaveFailed", + "worktree.delete.childrenCannotBeKeptTitle", "worktree.delete.childrenChanged", + "worktree.delete.childrenChangedClearPreserve", "worktree.delete.childrenPreservedDescription", "worktree.delete.childrenPreservedTitle", "worktree.delete.detachChildren",