import { useState } from '@wordpress/element'; import { Button, Card, CardBody, CardHeader, TextControl, ToggleControl, } from '@wordpress/components'; import apiFetch from '@wordpress/api-fetch'; export default function Settings( { navigate } ) { const initial = window.wlconduitData?.settings || {}; const [ settings, setSettings ] = useState( { cache_expiry_terms: initial.cache_expiry_terms || 30, cache_expiry_meta_keys: initial.cache_expiry_meta_keys || 30, log_retention_days: initial.log_retention_days || 30, logging_enabled: initial.logging_enabled !== false, rate_limiting_enabled: initial.rate_limiting_enabled !== false, rate_limit: initial.rate_limit || 20, rate_block_duration: initial.rate_block_duration || 60, } ); const [ saving, setSaving ] = useState( false ); const [ saved, setSaved ] = useState( false ); function setNumeric( key, val ) { setSettings( { ...settings, [ key ]: val } ); } async function handleSave() { setSaving( true ); setSaved( false ); try { // Parse numeric fields before sending — allows empty string while typing. const toInt = ( val, fallback ) => { const n = parseInt( val, 10 ); return isNaN( n ) ? fallback : n; }; const payload = { ...settings, cache_expiry_terms: toInt( settings.cache_expiry_terms, 30 ), cache_expiry_meta_keys: toInt( settings.cache_expiry_meta_keys, 30 ), log_retention_days: toInt( settings.log_retention_days, 30 ), rate_limit: toInt( settings.rate_limit, 20 ), rate_block_duration: toInt( settings.rate_block_duration, 60 ), }; const result = await apiFetch( { path: 'wlconduit/v1/settings', method: 'POST', data: payload, } ); setSettings( result ); setSaved( true ); setTimeout( () => setSaved( false ), 3000 ); } catch ( err ) { window.alert( 'Failed to save settings.' ); } setSaving( false ); } return (