From cb3ea04e221204a0aaa640ab5351ea832b5e1f1c Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 1 Jun 2026 18:54:44 -0700 Subject: [PATCH] Make Enter confirm source-control discard dialogs (#4410) --- .../right-sidebar/SourceControl.tsx | 66 ++-------- .../source-control-discard-dialog.test.tsx | 119 ++++++++++++++++++ .../source-control-discard-dialog.tsx | 107 ++++++++++++++++ 3 files changed, 234 insertions(+), 58 deletions(-) create mode 100644 src/renderer/src/components/right-sidebar/source-control-discard-dialog.test.tsx create mode 100644 src/renderer/src/components/right-sidebar/source-control-discard-dialog.tsx diff --git a/src/renderer/src/components/right-sidebar/SourceControl.tsx b/src/renderer/src/components/right-sidebar/SourceControl.tsx index 5e5451cdd..774945091 100644 --- a/src/renderer/src/components/right-sidebar/SourceControl.tsx +++ b/src/renderer/src/components/right-sidebar/SourceControl.tsx @@ -81,10 +81,9 @@ import { type SourceControlTreeNode } from './source-control-tree' import { - getDiscardAreaConfirmationCopy, - getDiscardEntryConfirmationCopy, - type DiscardConfirmationCopy -} from './source-control-discard-confirmation' + SourceControlDiscardDialog, + type PendingDiscardConfirmation +} from './source-control-discard-dialog' import { refreshGitStatusForWorktree } from './git-status-refresh' import { toast } from 'sonner' import { @@ -423,10 +422,6 @@ export function requestSourceControlViewModePreferenceWrite({ return next } -type PendingDiscardConfirmation = - | { kind: 'entry'; entry: GitStatusEntry } - | { kind: 'area'; area: DiscardAllArea; paths: readonly string[] } - type GitStatusSourceControlTreeNode = SourceControlTreeNode< GitStatusEntry, (typeof SECTION_ORDER)[number] @@ -1776,16 +1771,6 @@ function SourceControlInner(): React.JSX.Element { }, [collapsedSections, flatEntries, sourceControlViewMode, visibleTreeRowsByArea]) const [isExecutingBulk, setIsExecutingBulk] = useState(false) - const pendingDiscardCopy = useMemo(() => { - if (!pendingDiscard) { - return null - } - if (pendingDiscard.kind === 'entry') { - return getDiscardEntryConfirmationCopy(pendingDiscard.entry) - } - return getDiscardAreaConfirmationCopy(pendingDiscard.area, pendingDiscard.paths.length) - }, [pendingDiscard]) - const unresolvedConflicts = useMemo( () => entries.filter((entry) => entry.conflictStatus === 'unresolved' && entry.conflictKind), [entries] @@ -4153,7 +4138,6 @@ function SourceControlInner(): React.JSX.Element { const showGenericEmptyState = !hasUncommittedEntries && branchSummary?.status === 'ready' && branchEntries.length === 0 const currentWorktreeId = activeWorktree.id - const PendingDiscardIcon = pendingDiscardCopy?.confirmLabel.startsWith('Delete') ? Trash : Undo2 return ( <> @@ -4854,45 +4838,11 @@ function SourceControlInner(): React.JSX.Element { - { - if (!open) { - setPendingDiscard(null) - } - }} - > - - - - {pendingDiscardCopy?.title ?? 'Discard changes?'} - - - {pendingDiscardCopy?.description ?? 'This cannot be undone.'} - - - {pendingDiscard?.kind === 'area' ? ( -
- {pendingDiscard.paths.length} {pendingDiscard.paths.length === 1 ? 'file' : 'files'} -
- ) : pendingDiscard?.kind === 'entry' ? ( -
-
- {pendingDiscard.entry.path} -
-
- ) : null} - - - - -
-
+ setPendingDiscard(null)} + onConfirm={confirmPendingDiscard} + /> diff --git a/src/renderer/src/components/right-sidebar/source-control-discard-dialog.test.tsx b/src/renderer/src/components/right-sidebar/source-control-discard-dialog.test.tsx new file mode 100644 index 000000000..3dd4246a7 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/source-control-discard-dialog.test.tsx @@ -0,0 +1,119 @@ +import { renderToStaticMarkup } from 'react-dom/server' +import type { ButtonHTMLAttributes, ReactNode } from 'react' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { GitStatusEntry } from '../../../../shared/types' + +type CapturedButtonProps = ButtonHTMLAttributes & { + children?: ReactNode + variant?: string +} + +const mocks = vi.hoisted(() => ({ + buttons: [] as CapturedButtonProps[], + dialogContentProps: [] as Record[] +})) + +vi.mock('@/components/ui/dialog', () => ({ + Dialog: ({ open, children }: { open: boolean; children: ReactNode }) => + open ?
{children}
: null, + DialogContent: ({ + children, + ...props + }: { + children: ReactNode + onOpenAutoFocus?: (event: Event) => void + }) => { + mocks.dialogContentProps.push(props) + return
{children}
+ }, + DialogDescription: ({ children }: { children: ReactNode }) =>

{children}

, + DialogFooter: ({ children }: { children: ReactNode }) =>
{children}
, + DialogHeader: ({ children }: { children: ReactNode }) =>
{children}
, + DialogTitle: ({ children }: { children: ReactNode }) =>

{children}

+})) + +vi.mock('@/components/ui/button', async () => { + const ReactModule = await import('react') + return { + Button: ReactModule.forwardRef(function Button( + { children, variant: _variant, ...props }, + ref + ) { + mocks.buttons.push({ ...props, variant: _variant, children }) + return ( + + ) + }) + } +}) + +function entry(partial: Partial & { path: string }): GitStatusEntry { + return { + area: 'unstaged', + status: 'modified', + ...partial + } +} + +function textContent(node: ReactNode): string { + if (node == null || typeof node === 'boolean') { + return '' + } + if (typeof node === 'string' || typeof node === 'number') { + return String(node) + } + if (Array.isArray(node)) { + return node.map(textContent).join('') + } + if (typeof node === 'object' && 'props' in node) { + return textContent((node as { props?: { children?: ReactNode } }).props?.children) + } + return '' +} + +describe('SourceControlDiscardDialog', () => { + beforeEach(() => { + mocks.buttons = [] + mocks.dialogContentProps = [] + }) + + it('makes the discard button the dialog default action', async () => { + const { SourceControlDiscardDialog } = await import('./source-control-discard-dialog') + + renderToStaticMarkup( + + ) + + const cancelButton = mocks.buttons.find((button) => textContent(button.children) === 'Cancel') + const discardButton = mocks.buttons.find((button) => + textContent(button.children).includes('Discard') + ) + + expect(cancelButton?.autoFocus).not.toBe(true) + expect(discardButton?.variant).toBe('destructive') + expect(discardButton?.autoFocus).toBe(true) + expect(mocks.dialogContentProps[0]?.onOpenAutoFocus).toEqual(expect.any(Function)) + }) +}) + +describe('focusDiscardDialogConfirmButton', () => { + it('prevents Radix from focusing the first tabbable button', async () => { + const { focusDiscardDialogConfirmButton } = await import('./source-control-discard-dialog') + const event = { preventDefault: vi.fn() } as unknown as Event + const confirmButton = { focus: vi.fn() } as unknown as HTMLButtonElement + + focusDiscardDialogConfirmButton(event, confirmButton) + + expect(event.preventDefault).toHaveBeenCalledTimes(1) + expect(confirmButton.focus).toHaveBeenCalledTimes(1) + }) +}) diff --git a/src/renderer/src/components/right-sidebar/source-control-discard-dialog.tsx b/src/renderer/src/components/right-sidebar/source-control-discard-dialog.tsx new file mode 100644 index 000000000..0c645d003 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/source-control-discard-dialog.tsx @@ -0,0 +1,107 @@ +import { useMemo, useRef } from 'react' +import { Trash, Undo2 } from 'lucide-react' + +import { Button } from '@/components/ui/button' +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle +} from '@/components/ui/dialog' +import type { GitStatusEntry } from '../../../../shared/types' +import type { DiscardAllArea } from './discard-all-sequence' +import { + getDiscardAreaConfirmationCopy, + getDiscardEntryConfirmationCopy +} from './source-control-discard-confirmation' + +export type PendingDiscardConfirmation = + | { kind: 'entry'; entry: GitStatusEntry } + | { kind: 'area'; area: DiscardAllArea; paths: readonly string[] } + +export function focusDiscardDialogConfirmButton( + event: Event, + confirmButton: HTMLButtonElement | null +): void { + if (!confirmButton) { + return + } + // Why: Radix otherwise focuses Cancel first, making Enter dismiss this destructive confirm. + event.preventDefault() + confirmButton.focus() +} + +export function SourceControlDiscardDialog({ + pendingDiscard, + onCancel, + onConfirm +}: { + pendingDiscard: PendingDiscardConfirmation | null + onCancel: () => void + onConfirm: () => void +}): React.JSX.Element { + const confirmButtonRef = useRef(null) + const pendingDiscardCopy = useMemo(() => { + if (!pendingDiscard) { + return null + } + if (pendingDiscard.kind === 'entry') { + return getDiscardEntryConfirmationCopy(pendingDiscard.entry) + } + return getDiscardAreaConfirmationCopy(pendingDiscard.area, pendingDiscard.paths.length) + }, [pendingDiscard]) + const PendingDiscardIcon = pendingDiscardCopy?.confirmLabel.startsWith('Delete') ? Trash : Undo2 + + return ( + { + if (!open) { + onCancel() + } + }} + > + + focusDiscardDialogConfirmButton(event, confirmButtonRef.current) + } + > + + + {pendingDiscardCopy?.title ?? 'Discard changes?'} + + + {pendingDiscardCopy?.description ?? 'This cannot be undone.'} + + + {pendingDiscard?.kind === 'area' ? ( +
+ {pendingDiscard.paths.length} {pendingDiscard.paths.length === 1 ? 'file' : 'files'} +
+ ) : pendingDiscard?.kind === 'entry' ? ( +
+
{pendingDiscard.entry.path}
+
+ ) : null} + + + + +
+
+ ) +}