Files
supabase/apps/studio/hooks/ui/useConfirmOnClose.tsx
Gildas Garcia c6fc456910 chore: cleanup duplicate exports studio (#47387)
## Problem

Knip reports many duplicate exports (both named and default). Besides,
we're moving away from default exports and even have an eslint rule to
enforce it on new code.

## Solution

- Cleanup those exports
- Update imports when necessary

No functional changes. If it builds, it's fine
2026-06-29 15:46:16 +02:00

68 lines
1.4 KiB
TypeScript

import { useCallback, useMemo, useState } from 'react'
import { useLatest } from '../misc/useLatest'
export interface ConfirmOnCloseModalProps {
visible: boolean
onClose: () => void
onCancel: () => void
}
interface UseConfirmOnCloseProps {
checkIsDirty: () => boolean
onClose: () => void
}
export const useConfirmOnClose = ({ checkIsDirty, onClose }: UseConfirmOnCloseProps) => {
const [visible, setVisible] = useState(false)
const checkIsDirtyRef = useLatest(checkIsDirty)
const onCloseRef = useLatest(onClose)
const confirmOnClose = useCallback(() => {
if (checkIsDirtyRef.current()) {
setVisible(true)
} else {
onCloseRef.current()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const handleOpenChange = useCallback(
(open: boolean) => {
if (!open) {
confirmOnClose()
}
},
[confirmOnClose]
)
const onConfirm = useCallback(() => {
setVisible(false)
onCloseRef.current()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const onCancel = useCallback(() => {
setVisible(false)
}, [])
const modalProps: ConfirmOnCloseModalProps = useMemo(
() => ({
visible,
onClose: onConfirm,
onCancel,
}),
[visible, onConfirm, onCancel]
)
return useMemo(
() => ({
confirmOnClose,
handleOpenChange,
modalProps,
}),
[confirmOnClose, handleOpenChange, modalProps]
)
}