Keep preservation choice safe when eligibility changes

This commit is contained in:
Jinjing
2026-11-08 15:01:38 -08:00
parent 415426c3ba
commit d04f0b2d0d
7 changed files with 151 additions and 69 deletions
@@ -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() {
<RemovalUnsavedFilesNotice targets={unsavedTargets} enabled={isOpen} />
{childWorkspaceCount > 0 && (
{(childWorkspaceCount > 0 || preserveChildren) && (
<DeleteWorktreeLineageNotice
descendants={lineageDelete.descendants}
parent={worktree}
@@ -375,6 +378,7 @@ const DeleteWorktreeDialog = React.memo(function DeleteWorktreeDialog() {
isMainWorktree={isMainWorktree}
isDeleting={isDeleting || isSavingForcePreference}
isDetachingChildren={isDetachingChildren}
deleteDisabled={preserveChildren && !canPreserveChildren}
canForceDelete={canForceDelete}
isBatchDelete={isBatchDelete}
worktreeCount={worktrees.length}
@@ -11,6 +11,7 @@ export function DeleteWorktreeDialogFooter({
isMainWorktree,
isDeleting,
isDetachingChildren = false,
deleteDisabled = false,
canForceDelete,
isBatchDelete,
worktreeCount,
@@ -26,6 +27,7 @@ export function DeleteWorktreeDialogFooter({
isMainWorktree: boolean
isDeleting: boolean
isDetachingChildren?: boolean
deleteDisabled?: boolean
canForceDelete: boolean
isBatchDelete: boolean
worktreeCount: number
@@ -68,7 +70,7 @@ export function DeleteWorktreeDialogFooter({
<WorktreeForceDeleteButton
buttonRef={confirmButtonRef}
size="default"
disabled={isDeleting}
disabled={isDeleting || deleteDisabled}
onForceDelete={onForceDelete}
onSavingChange={onSavingChange}
onAlwaysForceDelete={() =>
@@ -82,7 +84,7 @@ export function DeleteWorktreeDialogFooter({
<WorktreeForceDeleteButton
buttonRef={confirmButtonRef}
size="default"
disabled={isDeleting}
disabled={isDeleting || deleteDisabled}
onForceDelete={onDelete}
onSavingChange={onSavingChange}
onAlwaysForceDelete={onAlwaysDelete}
@@ -101,7 +103,7 @@ export function DeleteWorktreeDialogFooter({
ref={confirmButtonRef}
variant="destructive"
onClick={onDelete}
disabled={isDeleting}
disabled={isDeleting || deleteDisabled}
>
{isDeleting ? <LoaderCircle className="size-4 animate-spin" /> : <Trash2 />}
{label}
@@ -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(<DeleteWorktreeLineageNotice {...props} />)
const checkbox = screen.getByRole('checkbox')
expect(checkbox.hasAttribute('disabled')).toBe(false)
fireEvent.click(checkbox)
expect(onChange).toHaveBeenCalledWith(false)
rerender(<DeleteWorktreeLineageNotice {...props} descendants={[]} />)
fireEvent.click(screen.getByRole('checkbox'))
expect(onChange).toHaveBeenCalledTimes(2)
expect(onChange).toHaveBeenLastCalledWith(false)
})
@@ -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({
<Workflow className="mt-0.5 size-3.5 shrink-0 text-muted-foreground" />
<div className="min-w-0 flex-1">
<div className="font-medium text-foreground">
{preserveChildren
? t('worktree.delete.childrenPreservedTitle', 'Child workspaces will be kept')
: t(
'auto.components.sidebar.DeleteWorktreeLineageNotice.a940f3c96e',
'Child workspaces will be deleted'
)}
</div>
<div className="mt-1 text-muted-foreground">
{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'
)}
</div>
{(!preserveChildren || canPreserveChildren) && (
<div className="mt-1 text-muted-foreground">
{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}
</div>
)}
{/* Constrain grid min-content width so nowrap paths cannot widen the modal. */}
<div className="mt-2 min-w-0 max-w-full space-y-1 overflow-hidden rounded-sm border border-border/60 bg-background/60 px-2 py-1.5">
{descendants.slice(0, 4).map((child) => (
<div key={child.id} className="min-w-0 overflow-hidden">
<div className="truncate font-medium text-foreground">
{child.displayName}
{projectLabels.has(child.id) ? (
<span className="text-muted-foreground"> · {projectLabels.get(child.id)}</span>
) : null}
{childWorkspaceCount > 0 && (
<div className="mt-2 min-w-0 max-w-full space-y-1 overflow-hidden rounded-sm border border-border/60 bg-background/60 px-2 py-1.5">
{descendants.slice(0, 4).map((child) => (
<div key={child.id} className="min-w-0 overflow-hidden">
<div className="truncate font-medium text-foreground">
{child.displayName}
{projectLabels.has(child.id) ? (
<span className="text-muted-foreground">
{' '}
· {projectLabels.get(child.id)}
</span>
) : null}
</div>
<div className="truncate text-muted-foreground">{child.path}</div>
{!preserveChildren && (
<DeleteWorktreeDirtyChangeHint
checkState={changeCheckStatesByWorktreeId?.get(
child.hostId ? getWorktreeHostIdentity(child) : child.id
)}
changeCount={dirtyChangeCountsByWorktreeId.get(
child.hostId ? getWorktreeHostIdentity(child) : child.id
)}
preview={dirtyChangePreviewsByWorktreeId.get(
child.hostId ? getWorktreeHostIdentity(child) : child.id
)}
/>
)}
</div>
<div className="truncate text-muted-foreground">{child.path}</div>
{!preserveChildren && (
<DeleteWorktreeDirtyChangeHint
checkState={changeCheckStatesByWorktreeId?.get(
child.hostId ? getWorktreeHostIdentity(child) : child.id
)}
changeCount={dirtyChangeCountsByWorktreeId.get(
child.hostId ? getWorktreeHostIdentity(child) : child.id
)}
preview={dirtyChangePreviewsByWorktreeId.get(
child.hostId ? getWorktreeHostIdentity(child) : child.id
)}
/>
)}
</div>
))}
{descendants.length > 4 ? (
<div className="text-muted-foreground">
{t(
'auto.components.sidebar.DeleteWorktreeLineageNotice.moreChildren',
'+{{count}} more',
{ count: descendants.length - 4, defaultValue_one: '+{{count}} more' }
)}
</div>
) : null}
</div>
))}
{descendants.length > 4 ? (
<div className="text-muted-foreground">
{t(
'auto.components.sidebar.DeleteWorktreeLineageNotice.moreChildren',
'+{{count}} more',
{ count: descendants.length - 4, defaultValue_one: '+{{count}} more' }
)}
</div>
) : null}
</div>
)}
<label className="mt-3 flex items-center gap-2 text-foreground">
<Checkbox
checked={preserveChildren}
disabled={disabled || !canPreserveChildren}
disabled={disabled || (!canPreserveChildren && !preserveChildren)}
onCheckedChange={(checked) => onPreserveChildrenChange(checked === true)}
/>
{t('worktree.delete.detachChildren', 'Detach and keep child workspaces')}
</label>
{!canPreserveChildren && (
<p className="mt-1 text-muted-foreground">
{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.'
)}
</p>
)}
</div>
@@ -50,7 +50,9 @@ export function useDetachWorktreeDeleteChildren({
const [isDetachingChildren, setIsDetachingChildren] = useState(false)
const [detachError, setDetachError] = useState<string | null>(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)
+2
View File
@@ -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}}"
}
},
@@ -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",