Files
supabase/apps/studio/components/ui-patterns/Dialogs/DiscardChangesConfirmationDialog.tsx
Drew McLellan 6957daa3b7 feat(studio): add setting to skip the SQL editor unsaved-changes prompt (#52029)
## What

Adds an opt-out for the "Unsaved changes" dialog shown when closing SQL
editor tabs in manual-save mode. Resolves FE-4170.

- New **Confirm on discard** toggle in the SQL editor's More actions
menu (both toolbar layouts).
- New **Don't ask again** checkbox in the dialog itself. It turns the
same setting off when the user confirms the discard.
- `DiscardChangesConfirmationDialog` gains a `children` slot. The
description now renders as a `div` so block-level children are valid
HTML.
- `TabCloseConfirmation` gains an optional `optOut`, so any tab type can
offer a "don't ask again" checkbox. Only SQL tabs use it. The Explorer
tabs are unchanged.
- The Intellisense and Confirm on discard items now use
`DropdownMenuCheckboxItem`, so both settings announce their checked
state to screen readers. The check indicator is still shown on the right
via a local class override.

<img width="353" height="229" alt="Screenshot 2026-10-20 at 16 10 14"
src="https://github.com/user-attachments/assets/c6f67934-51ba-48dd-8aee-f279ae7eb486"
/>

<img width="432" height="207" alt="Screenshot 2026-10-20 at 16 10 02"
src="https://github.com/user-attachments/assets/d064dcb1-1097-4ac3-bbf0-6a47506820c1"
/>


## Behavior notes

- The setting is stored in `localStorage`
(`sql-editor-confirm-unsaved-changes`). It is device-local and global,
not per project. It defaults to on.
- With the prompt off, closing a tab still **discards** its unsaved
edits. It only skips the confirmation.
- It only applies in manual-save mode. In auto-save mode there is
nothing to discard, so the toggle is hidden.
- The browser's native `beforeunload` prompt, shown when closing or
reloading the browser tab with unsaved work, is unaffected. Browsers
don't allow customizing it.

## Testing

- New unit tests for the SQL tab close handler: it confirms by default,
skips the dialog when the setting is off, and the opt-out writes the
setting.
- `lint:ratchet` and `knip` pass.
- Manually checked the menu at both widths and the dialog.
2026-10-21 11:20:55 +01:00

89 lines
2.3 KiB
TypeScript

'use client'
import { useCallback, useEffect, useRef, type ReactNode } from 'react'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from 'ui'
import { type ConfirmOnCloseModalProps } from '@/hooks/ui/useConfirmOnClose'
export interface DiscardChangesConfirmationDialogProps extends ConfirmOnCloseModalProps {
title?: ReactNode
description?: ReactNode
confirmLabel?: ReactNode
cancelLabel?: ReactNode
size?: React.ComponentProps<typeof AlertDialogContent>['size']
/** Extra content rendered between the description and the footer, e.g. an opt-out checkbox. */
children?: ReactNode
}
export const DiscardChangesConfirmationDialog = ({
visible,
onClose,
onCancel,
title = 'Unsaved changes',
description = 'You have unsaved changes. Are you sure you want to discard them?',
confirmLabel = 'Discard changes',
cancelLabel = 'Keep editing',
size = 'tiny',
children,
}: DiscardChangesConfirmationDialogProps) => {
const isConfirmingRef = useRef(false)
useEffect(() => {
if (visible) {
isConfirmingRef.current = false
}
}, [visible])
const handleConfirm = useCallback(() => {
isConfirmingRef.current = true
onClose()
}, [onClose])
const handleOpenChange = useCallback(
(open: boolean) => {
if (open) return
if (isConfirmingRef.current) {
isConfirmingRef.current = false
return
}
onCancel()
},
[onCancel]
)
return (
<AlertDialog open={visible} onOpenChange={handleOpenChange}>
<AlertDialogContent size={size}>
<AlertDialogHeader>
<AlertDialogTitle>{title}</AlertDialogTitle>
{description !== undefined && description !== null && (
<AlertDialogDescription asChild>
<div>
{description}
{children && <div className="mt-4">{children}</div>}
</div>
</AlertDialogDescription>
)}
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{cancelLabel}</AlertDialogCancel>
<AlertDialogAction variant="danger" onClick={handleConfirm}>
{confirmLabel}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}