Skip to content

Self-improving AI Employees for everyone

feat(ui): Phase 5 (finish) - ProviderDefaults + RateLimit on RHF+zod

trohitgbbc056f1a2fb
diff --git a/client/src/components/credentials/panels/EmailPanel.tsx b/client/src/components/credentials/panels/EmailPanel.tsx
index 7857ee9d..e81015f0 100644
--- a/client/src/components/credentials/panels/EmailPanel.tsx
+++ b/client/src/components/credentials/panels/EmailPanel.tsx
@@ -66,7 +66,7 @@ const EmailPanel: React.FC<{ config: ProviderConfig; visible: boolean }> = ({ co
// is on file). Recreate the resolver when that flips.
const schema = useMemo(() => createEmailFormSchema(!stored), [stored]);
- const form = useForm<EmailFormValues>({
+ const form = useForm({
resolver: zodResolver(schema),
defaultValues: DEFAULT_VALUES,
mode: 'onSubmit',
diff --git a/client/src/components/credentials/sections/ProviderDefaultsSection.tsx b/client/src/components/credentials/sections/ProviderDefaultsSection.tsx
index 4458dfff..3542c741 100644
--- a/client/src/components/credentials/sections/ProviderDefaultsSection.tsx
+++ b/client/src/components/credentials/sections/ProviderDefaultsSection.tsx
@@ -1,29 +1,58 @@
/**
- * ProviderDefaultsSection — antd Form bound to a remote ProviderDefaults object.
+ * ProviderDefaultsSection — provider default LLM parameters.
*
- * Uses:
- * - Form.useForm() + setFieldsValue() — replaces manual defaults state
- * - Form.Item with label+extra — replaces custom label/desc styling
- * - Form.Item shouldUpdate — reactive conditional fields from Form state
- * - Form.useWatch — react to default_model changes to refetch constraints
- * - Collapse native header — no custom rendering
+ * shadcn Form composition (react-hook-form + zod). Watches `default_model`
+ * to refetch model constraints, and toggles conditional thinking / reasoning
+ * fields based on `thinking_enabled` + the constraint's thinking_type.
*/
-import React, { useState, useEffect, useCallback } from 'react';
-import { Form, Select, InputNumber, Switch } from 'antd';
-import { Settings, Loader2 } from 'lucide-react';
+import React, { useCallback, useEffect, useMemo, useState } from 'react';
+import { useForm } from 'react-hook-form';
+import { zodResolver } from '@hookform/resolvers/zod';
+import { z } from 'zod';
+import { Loader2, Settings } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
+import { Input } from '@/components/ui/input';
+import { Switch } from '@/components/ui/switch';
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from '@/components/ui/select';
+import {
+ Form,
+ FormControl,
+ FormDescription,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from '@/components/ui/form';
import {
Accordion,
+ AccordionContent,
AccordionItem,
AccordionTrigger,
- AccordionContent,
} from '@/components/ui/accordion';
import { useAppTheme } from '../../../hooks/useAppTheme';
import { useApiKeys, type ProviderDefaults, type ModelConstraints } from '../../../hooks/useApiKeys';
+const formSchema = z.object({
+ default_model: z.string().optional().default(''),
+ temperature: z.number().optional(),
+ max_tokens: z.number().int().min(1).optional(),
+ thinking_enabled: z.boolean().optional().default(false),
+ thinking_budget: z.number().int().min(1024).max(16000).optional(),
+ reasoning_effort: z.enum(['low', 'medium', 'high']).optional(),
+ reasoning_format: z.enum(['parsed', 'hidden']).optional(),
+});
+
+type FormValues = z.infer<typeof formSchema>;
+
interface Props {
providerId: string;
}
@@ -32,28 +61,32 @@ const ProviderDefaultsSection: React.FC<Props> = ({ providerId }) => {
const theme = useAppTheme();
const { getProviderDefaults, saveProviderDefaults, getStoredModels, getModelConstraints, isConnected } = useApiKeys();
- const [form] = Form.useForm<ProviderDefaults>();
const [models, setModels] = useState<string[]>([]);
const [constraints, setConstraints] = useState<ModelConstraints | null>(null);
const [loading, setLoading] = useState(false);
- const [dirty, setDirty] = useState(false);
- // Watch default_model from the Form — refetch constraints when it changes
- const selectedModel = Form.useWatch('default_model', form);
- const thinkingEnabled = Form.useWatch('thinking_enabled', form);
+ const form = useForm({
+ resolver: zodResolver(formSchema),
+ defaultValues: {},
+ });
+ const selectedModel = form.watch('default_model');
+ const thinkingEnabled = form.watch('thinking_enabled');
+ const { isDirty } = form.formState;
- // Load defaults + models on mount
+ // Load defaults + models on mount.
useEffect(() => {
if (!isConnected) return;
let cancelled = false;
(async () => {
setLoading(true);
try {
- const [d, m] = await Promise.all([getProviderDefaults(providerId), getStoredModels(providerId)]);
+ const [d, m] = await Promise.all([
+ getProviderDefaults(providerId),
+ getStoredModels(providerId),
+ ]);
if (!cancelled) {
- form.setFieldsValue(d);
+ form.reset((d as Partial<ProviderDefaults>) ?? {});
if (m?.length) setModels(m);
- setDirty(false);
}
} finally {
if (!cancelled) setLoading(false);
@@ -62,41 +95,55 @@ const ProviderDefaultsSection: React.FC<Props> = ({ providerId }) => {
return () => { cancelled = true; };
}, [providerId, isConnected]);
- // Refetch constraints when selected model changes
+ // Refetch constraints when selected model changes.
useEffect(() => {
if (!isConnected || !selectedModel) return;
let cancelled = false;
- getModelConstraints(selectedModel, providerId).then(c => {
+ getModelConstraints(selectedModel, providerId).then((c) => {
if (cancelled) return;
setConstraints(c);
- if (c?.max_output_tokens && form.getFieldValue('max_tokens') !== c.max_output_tokens) {
- form.setFieldValue('max_tokens', c.max_output_tokens);
+ if (c?.max_output_tokens && form.getValues('max_tokens') !== c.max_output_tokens) {
+ form.setValue('max_tokens', c.max_output_tokens, { shouldDirty: false });
}
});
return () => { cancelled = true; };
}, [selectedModel, providerId, isConnected]);
- const handleSave = useCallback(async () => {
- setLoading(true);
- try {
- const values = form.getFieldsValue();
- const ok = await saveProviderDefaults(providerId, values);
- if (ok) setDirty(false);
- } finally {
- setLoading(false);
- }
- }, [form, providerId, saveProviderDefaults]);
-
- // Derived from constraints
+ const onSubmit = useCallback(
+ async (values: FormValues) => {
+ setLoading(true);
+ try {
+ const ok = await saveProviderDefaults(providerId, values as ProviderDefaults);
+ if (ok) form.reset(values);
+ } finally {
+ setLoading(false);
+ }
+ },
+ [providerId, saveProviderDefaults, form],
+ );
+
const [tempMin, tempMax] = constraints?.temperature_range ?? [0, 2];
const maxOut = constraints?.max_output_tokens;
const thinkType = constraints?.thinking_type;
const canThink = constraints?.supports_thinking;
const fixedTemp = constraints?.is_reasoning_model && tempMin === tempMax;
- const chipStyle = (bg: string): React.CSSProperties => ({
- backgroundColor: `${bg}20`, borderColor: `${bg}50`, color: bg,
- });
+ const chipStyle = useMemo(
+ () =>
+ (bg: string): React.CSSProperties => ({
+ backgroundColor: `${bg}20`,
+ borderColor: `${bg}50`,
+ color: bg,
+ }),
+ [],
+ );
+
+ const numberOnChange =
+ (field: { onChange: (v: number | undefined) => void }) =>
+ (e: React.ChangeEvent<HTMLInputElement>) => {
+ const raw = e.target.value;
+ field.onChange(raw === '' ? undefined : Number(raw));
+ };
return (
<Accordion type="single" collapsible defaultValue="defaults">
@@ -108,91 +155,228 @@ const ProviderDefaultsSection: React.FC<Props> = ({ providerId }) => {
</AccordionTrigger>
<AccordionContent>
<div className={loading ? 'pointer-events-none opacity-60' : ''}>
- <Form form={form} layout="vertical" size="small" onValuesChange={() => setDirty(true)} preserve>
- <Form.Item
- label="Default Model"
- name="default_model"
- extra="Model used when none specified"
- >
- <Select showSearch placeholder="Select model"
- options={models.map(m => ({ label: m, value: m }))}
- filterOption={(input, option) => (option?.label ?? '').toLowerCase().includes(input.toLowerCase())}
- notFoundContent={models.length ? 'No models found' : 'Validate API key first'}
- popupMatchSelectWidth={false}
- getPopupContainer={trigger => trigger.parentElement || document.body}
- listHeight={300}
- />
- </Form.Item>
-
- {constraints && (
- <div className="mb-3 flex flex-wrap items-center gap-1.5">
- {maxOut != null && <Badge style={chipStyle(theme.dracula.green)}>Max Output: {maxOut.toLocaleString()}</Badge>}
- {constraints.context_length != null && <Badge style={chipStyle(theme.dracula.cyan)}>Context: {constraints.context_length.toLocaleString()}</Badge>}
- <Badge style={chipStyle(theme.dracula.purple)}>Temp: {tempMin}-{tempMax}</Badge>
- {canThink && <Badge style={chipStyle(theme.dracula.orange)}>Thinking: {thinkType}</Badge>}
- {constraints.is_reasoning_model && <Badge style={chipStyle(theme.dracula.pink)}>Reasoning</Badge>}
- </div>
- )}
-
- {!fixedTemp && (
- <Form.Item label="Temperature" name="temperature" extra={`Controls randomness (${tempMin}-${tempMax})`}>
- <InputNumber min={tempMin} max={tempMax} step={0.1} style={{ width: 100 }} />
- </Form.Item>
- )}
-
- <Form.Item label="Max Tokens" name="max_tokens"
- extra={maxOut != null ? `Up to ${maxOut.toLocaleString()}` : 'Maximum response length'}>
- <InputNumber min={1} max={maxOut || undefined} style={{ width: 140 }} />
- </Form.Item>
-
- {canThink && (
- <Form.Item label="Thinking / Reasoning" name="thinking_enabled" valuePropName="checked"
- extra={`Extended thinking (${thinkType})`}>
- <Switch />
- </Form.Item>
- )}
-
- {canThink && thinkType === 'budget' && thinkingEnabled && (
- <Form.Item label="Thinking Budget" name="thinking_budget" extra="Token budget (1024-16000)">
- <InputNumber min={1024} max={16000} style={{ width: 120 }} />
- </Form.Item>
- )}
-
- {canThink && thinkType === 'effort' && thinkingEnabled && (
- <Form.Item label="Reasoning Effort" name="reasoning_effort" extra="Low, medium, or high">
- <Select style={{ width: 130 }}
- options={[{ label: 'Low', value: 'low' }, { label: 'Medium', value: 'medium' }, { label: 'High', value: 'high' }]}
- getPopupContainer={trigger => trigger.parentElement || document.body} />
- </Form.Item>
- )}
-
- {canThink && thinkType === 'format' && thinkingEnabled && (
- <Form.Item label="Reasoning Format" name="reasoning_format" extra="Parsed or hidden">
- <Select style={{ width: 130 }}
- options={[{ label: 'Parsed', value: 'parsed' }, { label: 'Hidden', value: 'hidden' }]}
- getPopupContainer={trigger => trigger.parentElement || document.body} />
- </Form.Item>
- )}
-
- <Button
- onClick={handleSave}
- disabled={!dirty || loading}
- variant="outline"
- className="w-full"
- style={
- dirty
- ? {
- backgroundColor: `${theme.dracula.green}25`,
- borderColor: `${theme.dracula.green}60`,
- color: theme.dracula.green,
- }
- : undefined
- }
- >
- {loading && <Loader2 className="h-4 w-4 animate-spin" />}
- Save Defaults
- </Button>
- </Form>
+ <Form {...form}>
+ <form
+ id={`provider-defaults-form-${providerId}`}
+ onSubmit={form.handleSubmit(onSubmit)}
+ className="flex flex-col gap-3"
+ >
+ <FormField
+ control={form.control}
+ name="default_model"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Default Model</FormLabel>
+ <Select value={field.value ?? ''} onValueChange={field.onChange}>
+ <FormControl>
+ <SelectTrigger>
+ <SelectValue
+ placeholder={models.length ? 'Select model' : 'Validate API key first'}
+ />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ {models.map((m) => (
+ <SelectItem key={m} value={m}>
+ {m}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <FormDescription>Model used when none specified</FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ {constraints && (
+ <div className="flex flex-wrap items-center gap-1.5">
+ {maxOut != null && (
+ <Badge style={chipStyle(theme.dracula.green)}>
+ Max Output: {maxOut.toLocaleString()}
+ </Badge>
+ )}
+ {constraints.context_length != null && (
+ <Badge style={chipStyle(theme.dracula.cyan)}>
+ Context: {constraints.context_length.toLocaleString()}
+ </Badge>
+ )}
+ <Badge style={chipStyle(theme.dracula.purple)}>
+ Temp: {tempMin}-{tempMax}
+ </Badge>
+ {canThink && (
+ <Badge style={chipStyle(theme.dracula.orange)}>Thinking: {thinkType}</Badge>
+ )}
+ {constraints.is_reasoning_model && (
+ <Badge style={chipStyle(theme.dracula.pink)}>Reasoning</Badge>
+ )}
+ </div>
+ )}
+
+ {!fixedTemp && (
+ <FormField
+ control={form.control}
+ name="temperature"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Temperature</FormLabel>
+ <FormControl>
+ <Input
+ type="number"
+ min={tempMin}
+ max={tempMax}
+ step={0.1}
+ className="w-24"
+ value={field.value ?? ''}
+ onChange={numberOnChange(field)}
+ />
+ </FormControl>
+ <FormDescription>
+ Controls randomness ({tempMin}-{tempMax})
+ </FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ )}
+
+ <FormField
+ control={form.control}
+ name="max_tokens"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Max Tokens</FormLabel>
+ <FormControl>
+ <Input
+ type="number"
+ min={1}
+ max={maxOut || undefined}
+ className="w-32"
+ value={field.value ?? ''}
+ onChange={numberOnChange(field)}
+ />
+ </FormControl>
+ <FormDescription>
+ {maxOut != null ? `Up to ${maxOut.toLocaleString()}` : 'Maximum response length'}
+ </FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ {canThink && (
+ <FormField
+ control={form.control}
+ name="thinking_enabled"
+ render={({ field }) => (
+ <FormItem className="flex flex-row items-center justify-between rounded-md border border-border p-3">
+ <div className="space-y-0.5">
+ <FormLabel>Thinking / Reasoning</FormLabel>
+ <FormDescription>Extended thinking ({thinkType})</FormDescription>
+ </div>
+ <FormControl>
+ <Switch checked={!!field.value} onCheckedChange={field.onChange} />
+ </FormControl>
+ </FormItem>
+ )}
+ />
+ )}
+
+ {canThink && thinkType === 'budget' && thinkingEnabled && (
+ <FormField
+ control={form.control}
+ name="thinking_budget"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Thinking Budget</FormLabel>
+ <FormControl>
+ <Input
+ type="number"
+ min={1024}
+ max={16000}
+ className="w-28"
+ value={field.value ?? ''}
+ onChange={numberOnChange(field)}
+ />
+ </FormControl>
+ <FormDescription>Token budget (1024-16000)</FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ )}
+
+ {canThink && thinkType === 'effort' && thinkingEnabled && (
+ <FormField
+ control={form.control}
+ name="reasoning_effort"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Reasoning Effort</FormLabel>
+ <Select value={field.value ?? ''} onValueChange={field.onChange}>
+ <FormControl>
+ <SelectTrigger className="w-32">
+ <SelectValue placeholder="Select" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ <SelectItem value="low">Low</SelectItem>
+ <SelectItem value="medium">Medium</SelectItem>
+ <SelectItem value="high">High</SelectItem>
+ </SelectContent>
+ </Select>
+ <FormDescription>Low, medium, or high</FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ )}
+
+ {canThink && thinkType === 'format' && thinkingEnabled && (
+ <FormField
+ control={form.control}
+ name="reasoning_format"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Reasoning Format</FormLabel>
+ <Select value={field.value ?? ''} onValueChange={field.onChange}>
+ <FormControl>
+ <SelectTrigger className="w-32">
+ <SelectValue placeholder="Select" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ <SelectItem value="parsed">Parsed</SelectItem>
+ <SelectItem value="hidden">Hidden</SelectItem>
+ </SelectContent>
+ </Select>
+ <FormDescription>Parsed or hidden</FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ )}
+
+ <Button
+ type="submit"
+ disabled={!isDirty || loading}
+ variant="outline"
+ className="w-full"
+ style={
+ isDirty
+ ? {
+ backgroundColor: `${theme.dracula.green}25`,
+ borderColor: `${theme.dracula.green}60`,
+ color: theme.dracula.green,
+ }
+ : undefined
+ }
+ >
+ {loading && <Loader2 className="h-4 w-4 animate-spin" />}
+ Save Defaults
+ </Button>
+ </form>
+ </Form>
</div>
</AccordionContent>
</AccordionItem>
diff --git a/client/src/components/credentials/sections/RateLimitSection.tsx b/client/src/components/credentials/sections/RateLimitSection.tsx
index bf88516a..6b853697 100644
--- a/client/src/components/credentials/sections/RateLimitSection.tsx
+++ b/client/src/components/credentials/sections/RateLimitSection.tsx
@@ -1,23 +1,54 @@
/**
* RateLimitSection — WhatsApp rate limit configuration.
- * antd Form bound to RateLimitConfig from WebSocket.
+ *
+ * shadcn Form composition (react-hook-form + zod). The header Switch
+ * toggles rate-limiting entirely; interior fields govern delays, limits
+ * and behavior. `response_rate_threshold` is stored as a fraction but
+ * displayed as a percentage — transformed in the Input onChange/value.
*/
-import React, { useState, useEffect, useCallback } from 'react';
-import { Form, InputNumber, Switch } from 'antd';
+import React, { useCallback, useEffect, useState } from 'react';
+import { useForm } from 'react-hook-form';
+import { zodResolver } from '@hookform/resolvers/zod';
+import { z } from 'zod';
import { Loader2 } from 'lucide-react';
import { Button } from '@/components/ui/button';
+import { Input } from '@/components/ui/input';
+import { Switch } from '@/components/ui/switch';
import { Alert, AlertDescription } from '@/components/ui/alert';
+import {
+ Form,
+ FormControl,
+ FormDescription,
+ FormField,
+ FormItem,
+ FormLabel,
+} from '@/components/ui/form';
import {
Accordion,
+ AccordionContent,
AccordionItem,
AccordionTrigger,
- AccordionContent,
} from '@/components/ui/accordion';
import { useAppTheme } from '../../../hooks/useAppTheme';
import { useWebSocket, type RateLimitConfig, type RateLimitStats } from '../../../contexts/WebSocketContext';
+const formSchema = z.object({
+ enabled: z.boolean().default(false),
+ min_delay_ms: z.number().int().min(0).optional(),
+ max_delay_ms: z.number().int().min(0).optional(),
+ typing_delay_ms: z.number().int().min(0).optional(),
+ link_extra_delay_ms: z.number().int().min(0).optional(),
+ max_messages_per_minute: z.number().int().min(0).optional(),
+ max_messages_per_hour: z.number().int().min(0).optional(),
+ max_new_contacts_per_day: z.number().int().min(0).optional(),
+ simulate_typing: z.boolean().optional(),
+ randomize_delays: z.boolean().optional(),
+ pause_on_low_response: z.boolean().optional(),
+ response_rate_threshold: z.number().min(0).max(1).optional(),
+});
+
const Stat: React.FC<{ label: string; value: React.ReactNode }> = ({ label, value }) => (
<div className="flex flex-col">
<span className="text-xs text-muted-foreground">{label}</span>
@@ -25,23 +56,34 @@ const Stat: React.FC<{ label: string; value: React.ReactNode }> = ({ label, valu
</div>
);
+const numberOnChange =
+ (field: { onChange: (v: number | undefined) => void }) =>
+ (e: React.ChangeEvent<HTMLInputElement>) => {
+ const raw = e.target.value;
+ field.onChange(raw === '' ? undefined : Number(raw));
+ };
+
const RateLimitSection: React.FC = () => {
const theme = useAppTheme();
const { getWhatsAppRateLimitConfig, setWhatsAppRateLimitConfig, unpauseWhatsAppRateLimit } = useWebSocket();
- const [form] = Form.useForm<RateLimitConfig>();
const [stats, setStats] = useState<RateLimitStats | null>(null);
const [loading, setLoading] = useState(false);
- const [dirty, setDirty] = useState(false);
const [loaded, setLoaded] = useState(false);
+ const form = useForm({
+ resolver: zodResolver(formSchema),
+ defaultValues: { enabled: false },
+ });
+ const pauseOnLowResponse = form.watch('pause_on_low_response');
+ const { isDirty } = form.formState;
+
const load = useCallback(async () => {
setLoading(true);
try {
const result = await getWhatsAppRateLimitConfig();
if (result.success && result.config) {
- form.setFieldsValue(result.config);
+ form.reset(result.config as Partial<RateLimitConfig>);
setStats(result.stats ?? null);
- setDirty(false);
setLoaded(true);
}
} finally {
@@ -51,18 +93,20 @@ const RateLimitSection: React.FC = () => {
useEffect(() => { load(); }, [load]);
- const handleSave = useCallback(async () => {
- setLoading(true);
- try {
- const result = await setWhatsAppRateLimitConfig(form.getFieldsValue());
- if (result.success && result.config) {
- form.setFieldsValue(result.config);
- setDirty(false);
+ const onSubmit = useCallback(
+ async (values: z.infer<typeof formSchema>) => {
+ setLoading(true);
+ try {
+ const result = await setWhatsAppRateLimitConfig(values as RateLimitConfig);
+ if (result.success && result.config) {
+ form.reset(result.config as Partial<RateLimitConfig>);
+ }
+ } finally {
+ setLoading(false);
}
- } finally {
- setLoading(false);
- }
- }, [form, setWhatsAppRateLimitConfig]);
+ },
+ [form, setWhatsAppRateLimitConfig],
+ );
const handleUnpause = useCallback(async () => {
setLoading(true);
@@ -80,103 +124,189 @@ const RateLimitSection: React.FC = () => {
<AccordionTrigger>
<div className="flex w-full items-center justify-between gap-2">
<span>Rate Limits</span>
- <span onClick={(e) => e.stopPropagation()}>
- <Form form={form} component={false}>
- <Form.Item name="enabled" valuePropName="checked" noStyle>
- <Switch size="small" onChange={() => setDirty(true)} />
- </Form.Item>
- </Form>
- </span>
+ <FormField
+ control={form.control}
+ name="enabled"
+ render={({ field }) => (
+ <span onClick={(e) => e.stopPropagation()}>
+ <Switch checked={!!field.value} onCheckedChange={field.onChange} />
+ </span>
+ )}
+ />
</div>
</AccordionTrigger>
<AccordionContent>
{!loaded ? (
- <div className="flex justify-center p-4 text-sm text-muted-foreground">Loading...</div>
- ) : (
- <Form form={form} layout="vertical" size="small" onValuesChange={() => setDirty(true)}>
- {stats && (
- <div className="mb-3 flex flex-wrap gap-4">
- <Stat label="Last Minute" value={stats.messages_sent_last_minute} />
- <Stat label="Last Hour" value={stats.messages_sent_last_hour} />
- <Stat label="Today" value={stats.messages_sent_today} />
- <Stat label="New Contacts" value={stats.new_contacts_today} />
- <Stat label="Responses" value={stats.responses_received} />
- <Stat
- label="Response Rate"
- value={`${Math.round((stats.response_rate || 0) * 100)}%`}
- />
- </div>
- )}
- {stats?.is_paused && (
- <Alert variant="warning" className="mb-3">
- <AlertDescription className="flex items-center justify-between gap-3">
- <span>{stats.pause_reason || 'Paused'}</span>
- <Button size="sm" variant="outline" onClick={handleUnpause}>Unpause</Button>
- </AlertDescription>
- </Alert>
- )}
+ <div className="flex justify-center p-4 text-sm text-muted-foreground">Loading...</div>
+ ) : (
+ <Form {...form}>
+ <form onSubmit={form.handleSubmit(onSubmit)} className="flex flex-col gap-3">
+ {stats && (
+ <div className="mb-3 flex flex-wrap gap-4">
+ <Stat label="Last Minute" value={stats.messages_sent_last_minute} />
+ <Stat label="Last Hour" value={stats.messages_sent_last_hour} />
+ <Stat label="Today" value={stats.messages_sent_today} />
+ <Stat label="New Contacts" value={stats.new_contacts_today} />
+ <Stat label="Responses" value={stats.responses_received} />
+ <Stat
+ label="Response Rate"
+ value={`${Math.round((stats.response_rate || 0) * 100)}%`}
+ />
+ </div>
+ )}
- <div className="mb-1 text-xs font-medium text-muted-foreground">
- Message Delays (milliseconds)
- </div>
- <div className="flex flex-wrap gap-2">
- <Form.Item label="Min Delay" name="min_delay_ms"><InputNumber style={{ width: 120 }} /></Form.Item>
- <Form.Item label="Max Delay" name="max_delay_ms"><InputNumber style={{ width: 120 }} /></Form.Item>
- <Form.Item label="Typing Duration" name="typing_delay_ms"><InputNumber style={{ width: 120 }} /></Form.Item>
- <Form.Item label="Link Extra Delay" name="link_extra_delay_ms"><InputNumber style={{ width: 120 }} /></Form.Item>
- </div>
+ {stats?.is_paused && (
+ <Alert variant="warning">
+ <AlertDescription className="flex items-center justify-between gap-3">
+ <span>{stats.pause_reason || 'Paused'}</span>
+ <Button size="sm" variant="outline" type="button" onClick={handleUnpause}>
+ Unpause
+ </Button>
+ </AlertDescription>
+ </Alert>
+ )}
- <div className="mb-1 text-xs font-medium text-muted-foreground">Message Limits</div>
- <div className="flex flex-wrap gap-2">
- <Form.Item label="Per Minute" name="max_messages_per_minute"><InputNumber style={{ width: 120 }} /></Form.Item>
- <Form.Item label="Per Hour" name="max_messages_per_hour"><InputNumber style={{ width: 120 }} /></Form.Item>
- <Form.Item label="New Contacts/Day" name="max_new_contacts_per_day"><InputNumber style={{ width: 140 }} /></Form.Item>
- </div>
+ <div className="text-xs font-medium text-muted-foreground">
+ Message Delays (milliseconds)
+ </div>
+ <div className="flex flex-wrap gap-3">
+ {(
+ [
+ ['min_delay_ms', 'Min Delay'],
+ ['max_delay_ms', 'Max Delay'],
+ ['typing_delay_ms', 'Typing Duration'],
+ ['link_extra_delay_ms', 'Link Extra Delay'],
+ ] as const
+ ).map(([name, label]) => (
+ <FormField
+ key={name}
+ control={form.control}
+ name={name}
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel className="text-xs">{label}</FormLabel>
+ <FormControl>
+ <Input
+ type="number"
+ min={0}
+ className="w-28"
+ value={field.value ?? ''}
+ onChange={numberOnChange(field)}
+ />
+ </FormControl>
+ </FormItem>
+ )}
+ />
+ ))}
+ </div>
- <div className="mb-1 text-xs font-medium text-muted-foreground">Behavior</div>
- <div className="mb-3 flex w-full flex-col gap-2">
- <Form.Item label="Simulate Typing" name="simulate_typing" valuePropName="checked"
- extra="Show typing indicator before sending" style={{ marginBottom: 0 }}>
- <Switch />
- </Form.Item>
- <Form.Item label="Randomize Delays" name="randomize_delays" valuePropName="checked"
- extra="Add variance between min/max delay" style={{ marginBottom: 0 }}>
- <Switch />
- </Form.Item>
- <Form.Item label="Pause on Low Response" name="pause_on_low_response" valuePropName="checked"
- extra="Auto-pause if response rate drops below threshold" style={{ marginBottom: 0 }}>
- <Switch />
- </Form.Item>
- <Form.Item shouldUpdate={(p, c) => p.pause_on_low_response !== c.pause_on_low_response} noStyle>
- {({ getFieldValue }) => getFieldValue('pause_on_low_response') && (
- <Form.Item label="Response Rate Threshold (%)" name="response_rate_threshold"
- normalize={(v: number) => v / 100}
- getValueProps={(v: number) => ({ value: Math.round((v ?? 0.3) * 100) })}>
- <InputNumber min={0} max={100} style={{ width: '100%' }} />
- </Form.Item>
- )}
- </Form.Item>
- </div>
+ <div className="text-xs font-medium text-muted-foreground">Message Limits</div>
+ <div className="flex flex-wrap gap-3">
+ {(
+ [
+ ['max_messages_per_minute', 'Per Minute'],
+ ['max_messages_per_hour', 'Per Hour'],
+ ['max_new_contacts_per_day', 'New Contacts/Day'],
+ ] as const
+ ).map(([name, label]) => (
+ <FormField
+ key={name}
+ control={form.control}
+ name={name}
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel className="text-xs">{label}</FormLabel>
+ <FormControl>
+ <Input
+ type="number"
+ min={0}
+ className="w-32"
+ value={field.value ?? ''}
+ onChange={numberOnChange(field)}
+ />
+ </FormControl>
+ </FormItem>
+ )}
+ />
+ ))}
+ </div>
+
+ <div className="text-xs font-medium text-muted-foreground">Behavior</div>
+ <div className="flex w-full flex-col gap-2">
+ {(
+ [
+ ['simulate_typing', 'Simulate Typing', 'Show typing indicator before sending'],
+ ['randomize_delays', 'Randomize Delays', 'Add variance between min/max delay'],
+ [
+ 'pause_on_low_response',
+ 'Pause on Low Response',
+ 'Auto-pause if response rate drops below threshold',
+ ],
+ ] as const
+ ).map(([name, label, description]) => (
+ <FormField
+ key={name}
+ control={form.control}
+ name={name}
+ render={({ field }) => (
+ <FormItem className="flex flex-row items-center justify-between rounded-md border border-border p-3">
+ <div className="space-y-0.5">
+ <FormLabel>{label}</FormLabel>
+ <FormDescription>{description}</FormDescription>
+ </div>
+ <FormControl>
+ <Switch checked={!!field.value} onCheckedChange={field.onChange} />
+ </FormControl>
+ </FormItem>
+ )}
+ />
+ ))}
+
+ {pauseOnLowResponse && (
+ <FormField
+ control={form.control}
+ name="response_rate_threshold"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Response Rate Threshold (%)</FormLabel>
+ <FormControl>
+ <Input
+ type="number"
+ min={0}
+ max={100}
+ value={Math.round((field.value ?? 0.3) * 100)}
+ onChange={(e) => {
+ const raw = e.target.value;
+ field.onChange(raw === '' ? undefined : Number(raw) / 100);
+ }}
+ />
+ </FormControl>
+ </FormItem>
+ )}
+ />
+ )}
+ </div>
- <Button
- onClick={handleSave}
- disabled={!dirty || loading}
- variant="outline"
- className="w-full"
- style={
- dirty
- ? {
- backgroundColor: `${theme.dracula.green}25`,
- borderColor: `${theme.dracula.green}60`,
- color: theme.dracula.green,
+ <Button
+ type="submit"
+ disabled={!isDirty || loading}
+ variant="outline"
+ className="w-full"
+ style={
+ isDirty
+ ? {
+ backgroundColor: `${theme.dracula.green}25`,
+ borderColor: `${theme.dracula.green}60`,
+ color: theme.dracula.green,
+ }
+ : undefined
}
- : undefined
- }
- >
- {loading && <Loader2 className="h-4 w-4 animate-spin" />}
- Save Changes
- </Button>
- </Form>
+ >
+ {loading && <Loader2 className="h-4 w-4 animate-spin" />}
+ Save Changes
+ </Button>
+ </form>
+ </Form>
)}
</AccordionContent>
</AccordionItem>