'use client'; import { useState } from 'react'; import type { AssistRules } from '@/lib/types/config'; import { updateAssistRulesAction } from '@/lib/actions/configuration'; interface AssistRulesEditorProps { serverId: string; config: AssistRules; } export function AssistRulesEditor({ serverId, config }: AssistRulesEditorProps) { const [formData, setFormData] = useState(config); const [restart, setRestart] = useState(true); const [isSubmitting, setIsSubmitting] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); const formDataObj = new FormData(); Object.entries(formData).forEach(([key, value]) => { formDataObj.append(key, value.toString()); }); if (restart) { formDataObj.append('restart', 'on'); } try { const result = await updateAssistRulesAction(serverId, formDataObj); if (!result.success) { console.error('Failed to update assist rules:', result.message); } } finally { setIsSubmitting(false); } }; const handleInputChange = (key: keyof AssistRules, value: string | number) => { setFormData((prev) => ({ ...prev, [key]: typeof value === 'string' ? parseInt(value) : value })); }; const assistFields = [ { key: 'stabilityControlLevelMax' as keyof AssistRules, label: 'Stability Control Level Max', type: 'number' }, { key: 'disableAutosteer' as keyof AssistRules, label: 'Disable Autosteer', type: 'select' }, { key: 'disableAutoLights' as keyof AssistRules, label: 'Disable Auto Lights', type: 'select' }, { key: 'disableAutoWiper' as keyof AssistRules, label: 'Disable Auto Wiper', type: 'select' }, { key: 'disableAutoEngineStart' as keyof AssistRules, label: 'Disable Auto Engine Start', type: 'select' }, { key: 'disableAutoPitLimiter' as keyof AssistRules, label: 'Disable Auto Pit Limiter', type: 'select' }, { key: 'disableAutoGear' as keyof AssistRules, label: 'Disable Auto Gear', type: 'select' }, { key: 'disableAutoClutch' as keyof AssistRules, label: 'Disable Auto Clutch', type: 'select' }, { key: 'disableIdealLine' as keyof AssistRules, label: 'Disable Ideal Line', type: 'select' } ]; return (
{assistFields.map(({ key, label, type }) => (
{type === 'number' ? ( handleInputChange(key, e.target.value)} className="form-input w-full" min="0" max="5" /> ) : ( )}
))}
); }