import { useEffect, useMemo, useRef, useState } from 'react'; import { Controller, useForm, useWatch, type FieldErrors } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { Send } from 'lucide-react'; import { __ } from '@wordpress/i18n'; import { Button } from '@/components/ui/button'; import { Checkbox } from '@/components/ui/checkbox'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Modal } from '@/components/ui/modal'; import Notice from '@/components/Notice'; import boot from '@/lib/boot'; import { ajax } from '@/lib/api'; const FORM_ID = 'wtofe-rf-form'; const MESSAGE_ID = 'wtofe-rf-msg'; const EMAIL_ID = 'wtofe-rf-email'; function makeSchema() { return z .object( { message: z.string().refine( ( value ) => '' !== value.trim(), { error: __( 'Please enter a message.', 'ai-content-generator-for-woocommerce' ) } ), contactOk: z.boolean(), email: z.string(), } ) .refine( ( values ) => ! values.contactOk || '' !== values.email.trim(), { error: __( 'We need your email address to contact you back.', 'ai-content-generator-for-woocommerce' ), path: [ 'email' ], } ); } type Values = z.infer>; const EMPTY: Values = { message: '', contactOk: false, email: '' }; function FieldError( { id, message }: { id: string; message?: string } ) { return message ? ( ) : null; } /** * "Missing a feature?" form, posted to AI_Content_Generator_Request_Feature::send_suggestion(). */ export default function RequestFeatureDialog( { open, onClose }: { open: boolean; onClose: () => void } ) { const config = boot.requestFeature; const schema = useMemo( makeSchema, [] ); // Invalid fields are focused by id (onInvalid below): WordPress 6.0's // TextareaControl and CheckboxControl don't pass refs through. const form = useForm( { resolver: zodResolver( schema ), defaultValues: EMPTY, shouldFocusError: false } ); const contactOk = useWatch( { control: form.control, name: 'contactOk' } ); const { errors, isSubmitting: sending } = form.formState; const [ notice, setNotice ] = useState<{ tone: 'success' | 'error'; text: string } | null>( null ); const closeTimer = useRef( undefined ); useEffect( () => { if ( open ) { form.reset( EMPTY ); setNotice( null ); } return () => window.clearTimeout( closeTimer.current ); }, [ open, form ] ); if ( ! config ) { return null; } const onInvalid = ( invalid: FieldErrors ) => { document.getElementById( invalid.message ? MESSAGE_ID : EMAIL_ID )?.focus(); }; const submit = form.handleSubmit( async ( values ) => { const failed = __( 'Unable to submit. Please try again later.', 'ai-content-generator-for-woocommerce' ); setNotice( null ); try { const response = await ajax( config.action, { wtofe_rf_nonce: config.nonce, wtofe_rf_msg: values.message, wtofe_rf_email: values.email, ...( values.contactOk ? { wtofe_rf_contact_ok: 'yes' } : {} ), } ); if ( response?.success ) { setNotice( { tone: 'success', text: __( 'Thank you for your suggestion!', 'ai-content-generator-for-woocommerce' ) } ); closeTimer.current = window.setTimeout( onClose, 2200 ); } else { setNotice( { tone: 'error', text: response?.msg || failed } ); } } catch { setNotice( { tone: 'error', text: failed } ); } }, onInvalid ); return ( } >

{ __( 'More the details you share, the better.', 'ai-content-generator-for-woocommerce' ) }

(