/** * Promise-based confirmation, the themed stand-in for window.confirm(): * * const confirm = useConfirm(); * if ( await confirm( { message, confirmLabel } ) ) { … } * * is mounted once in the app layout. Built on the app's * Modal (WordPress's Modal) rather than WordPress's ConfirmDialog, which in * WordPress 6.0 can't show a title or a destructive confirm button. */ import { useCallback } from 'react'; import { create } from 'zustand'; import { __ } from '@wordpress/i18n'; import { Button } from '@/components/ui/button'; import { Modal } from '@/components/ui/modal'; interface ConfirmOptions { message: string; confirmLabel?: string; /** Destructive actions get the red confirm button (the default). */ destructive?: boolean; } interface ConfirmRequest extends ConfirmOptions { resolve: ( answer: boolean ) => void; } const useConfirmStore = create<{ request: ConfirmRequest | null }>()( () => ( { request: null } ) ); /** Answers a pending confirmation once; later calls are ignored. */ function answer( value: boolean ) { const { request } = useConfirmStore.getState(); if ( request ) { request.resolve( value ); useConfirmStore.setState( { request: null } ); } } export function useConfirm() { return useCallback( ( options: ConfirmOptions ) => new Promise( ( resolve ) => useConfirmStore.setState( { request: { destructive: true, ...options, resolve } } ) ), [] ); } export function ConfirmHost() { const request = useConfirmStore( ( state ) => state.request ); return ( answer( false ) } role="alertdialog" title={ __( 'Are you sure?', 'ai-content-generator-for-woocommerce' ) } footer={ <> } >

{ request?.message }

); }