import type { FormEvent } from 'react'; import { useState, useEffect } from 'react'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import { toast } from 'sonner'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { useClient } from 'hooks/useClient'; import { useTranslation } from '@/lib/i18n'; type SttConfig = { url: string; }; export const STTSection = () => { const { t } = useTranslation(); const client = useClient(); const queryClient = useQueryClient(); const { data: config, isLoading } = useQuery({ queryKey: ['STT_CONFIG'], queryFn: () => client.get('/server-settings/stt'), }); const [url, setUrl] = useState('http://localhost:64201'); const [isSaving, setIsSaving] = useState(false); const [isTesting, setIsTesting] = useState(false); useEffect(() => { if (!config) return; setUrl(config.url); }, [config]); const handleSave = async (ev?: FormEvent) => { ev?.preventDefault(); if (isSaving) return; setIsSaving(true); try { await client.put('/server-settings/stt', { url }); queryClient.invalidateQueries({ queryKey: ['STT_CONFIG'] }); toast.success(t('settings.server.stt.saved')); } catch { toast.error(t('settings.server.stt.saveFailed')); } finally { setIsSaving(false); } }; const handleTest = async () => { if (isTesting) return; setIsTesting(true); try { const res = await client.post<{ success?: boolean; error?: string }>('/server-settings/stt/test', { url }); if (res.error) { toast.error(res.error); } else { toast.success(t('settings.server.stt.reachable')); } } catch (err: unknown) { const raw = (err as { message?: string })?.message; let msg = 'Connection failed'; try { if (raw) msg = JSON.parse(raw).error ?? msg; } catch { /* ignore */ } toast.error(msg); } finally { setIsTesting(false); } }; if (isLoading) return

{t('settings.server.loading')}

; return (
); };