/** * Chatbot Settings - module-level settings with TabPanel. */ import { useState, useEffect, useCallback } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { Button, TextControl, TextareaControl, SelectControl, ToggleControl, TabPanel, Spinner } from '@aime/wp-components'; import useApi from '../../../../hooks/useApi'; import Card from '../../../common/Card'; import Loader from '../../../common/Loader'; import Notice from '../../../common/Notice'; import { toast } from '../../../common/Toast'; const TABS = [ { name: 'general', title: __( 'General', 'ai-marketing-expert' ) }, { name: 'behavior', title: __( 'Behavior', 'ai-marketing-expert' ) }, { name: 'notifications', title: __( 'Notifications', 'ai-marketing-expert' ) }, { name: 'privacy', title: __( 'Privacy & GDPR', 'ai-marketing-expert' ) }, ]; const THEME_OPTIONS = [ { label: 'Default (Indigo)', value: 'default' }, { label: 'Modern (Dark)', value: 'modern' }, { label: 'Minimal (Green)', value: 'minimal' }, ]; const parseNumber = ( value, fallback, allowZero = false ) => { const parsed = parseInt( value, 10 ); if ( Number.isNaN( parsed ) ) return fallback; if ( allowZero ) return Math.max( 0, parsed ); return Math.max( 1, parsed ); }; const Settings = () => { const { get, post, loading, error, clearError } = useApi(); const [ settings, setSettings ] = useState( null ); const [ saving, setSaving ] = useState( false ); const [ success, setSuccess ] = useState( '' ); const fetchSettings = useCallback( async () => { try { const res = await get( '/chatbot/settings' ); setSettings( res ); } catch ( e ) { // silent } }, [ get ] ); useEffect( () => { fetchSettings(); }, [ fetchSettings ] ); const handleSave = async () => { setSaving( true ); setSuccess( '' ); try { const res = await post( '/chatbot/settings', settings ); if ( res?.settings ) { setSettings( res.settings ); } setSuccess( __( 'Settings saved.', 'ai-marketing-expert' ) ); } catch ( e ) { toast( e.message, 'error' ); } finally { setSaving( false ); } }; if ( loading && ! settings ) { return ; } if ( ! settings ) return null; const setField = ( key, value ) => { setSettings( ( prev ) => ( { ...prev, [ key ]: value } ) ); }; return (
{ error && } { success && setSuccess( '' ) } /> }

{ __( 'Chatbot Settings', 'ai-marketing-expert' ) }

{ loading ? : { ( tab ) => { /* General tab */ if ( tab.name === 'general' ) { return (
setField( 'global_system_prompt', v ) } rows={ 5 } /> setField( 'default_welcome_message', v ) } __nextHasNoMarginBottom /> setField( 'default_theme', v ) } __nextHasNoMarginBottom />
); } /* Behavior tab */ if ( tab.name === 'behavior' ) { return (
setField( 'auto_sync_knowledge', v ) } /> setField( 'enable_sound_notification', v ) } /> setField( 'enable_typing_indicator', v ) } /> setField( 'enable_buy_now_link', v ) } /> setField( 'enable_read_receipts', v ) } />
setField( 'poll_interval_seconds', parseNumber( v, 5 ) ) } __nextHasNoMarginBottom /> setField( 'max_message_length', parseNumber( v, 500 ) ) } __nextHasNoMarginBottom />
setField( 'rate_limit_per_minute', parseNumber( v, 10, true ) ) } __nextHasNoMarginBottom />
); } /* Notifications tab */ if ( tab.name === 'notifications' ) { return (
setField( 'notify_new_chat', v ) } /> { !! settings.notify_new_chat && ( setField( 'notify_email', v ) } __nextHasNoMarginBottom /> ) }
); } /* Privacy and GDPR tab */ return (
setField( 'gdpr_enabled', v ) } /> { settings.gdpr_enabled && ( setField( 'consent_message', v ) } rows={ 3 } /> ) } setField( 'data_retention_days', parseNumber( v, 90, true ) ) } __nextHasNoMarginBottom />
); } }
}
); }; export default Settings;