"use client"; import { useState } from "react"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { Loader2, MessageCircle, Save, Send } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; import { PasswordInput } from "@/components/ui/password-input"; import { SettingsCardHeader } from "@/components/settings/_card-header"; import { useTranslations } from "@/lib/i18n/context"; import { queryKeys } from "@/lib/query-keys"; interface TelegramSettings { enabled: boolean; hasBotToken: boolean; chatId: string | null; } export function TelegramCard({ isAuthenticated, }: { isAuthenticated: boolean; }) { const { t } = useTranslations(); const queryClient = useQueryClient(); const [botToken, setBotToken] = useState(""); const [chatId, setChatId] = useState(""); const [enabled, setEnabled] = useState(false); const [saving, setSaving] = useState(false); const [testing, setTesting] = useState(false); const [msg, setMsg] = useState(null); const [msgType, setMsgType] = useState<"success" | "error" | null>(null); const { data: settings } = useQuery({ queryKey: queryKeys.telegramSettings(), queryFn: async () => { const res = await fetch("/api/settings/telegram"); if (!res.ok) throw new Error("Failed"); const json = await res.json(); return json.data as TelegramSettings; }, enabled: isAuthenticated, }); // Sync from server using the React-recommended "previous-payload-id" // pattern instead of setState-in-effect. This avoids the strict // `react-hooks/set-state-in-effect` lint failure that the legacy file // disabled at module scope. const settingsKey = settings ? `${settings.enabled}|${settings.chatId ?? ""}` : null; const [seededKey, setSeededKey] = useState(null); if (settingsKey && settingsKey !== seededKey) { setSeededKey(settingsKey); setEnabled(settings!.enabled); if (settings!.chatId) setChatId(settings!.chatId); } async function handleSave(e: React.FormEvent) { e.preventDefault(); setSaving(true); setMsg(null); setMsgType(null); const body: Record = { enabled }; if (botToken.trim()) body.botToken = botToken.trim(); if (chatId !== (settings?.chatId ?? "")) body.chatId = chatId; const res = await fetch("/api/settings/telegram", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); if (res.ok) { setMsg(t("settings.telegramSaved")); setMsgType("success"); setBotToken(""); queryClient.invalidateQueries({ queryKey: queryKeys.telegram() }); } else { const json = await res.json(); setMsg(json.error || t("settings.savingError")); setMsgType("error"); } setSaving(false); } async function handleTest() { setTesting(true); setMsg(null); setMsgType(null); const res = await fetch("/api/settings/telegram/test", { method: "POST" }); if (res.ok) { setMsg(t("settings.testSent")); setMsgType("success"); } else { const json = await res.json(); setMsg(json.error || t("common.error")); setMsgType("error"); } setTesting(false); } return (
setBotToken(e.target.value)} placeholder={ settings?.hasBotToken ? t("settings.withingsCredentialsSavedPlaceholder") : "123456:ABC-DEF..." } maxLength={100} autoComplete="off" inputMode="text" spellCheck={false} autoCapitalize="none" enterKeyHint="next" />
setChatId(e.target.value)} placeholder="123456789" maxLength={50} autoComplete="off" inputMode="text" spellCheck={false} autoCapitalize="none" enterKeyHint="done" />

{t("settings.telegramStep1")}

{t("settings.telegramStep2")}

{t("settings.telegramStep3")}

{msg && (

{msg}

)}
); }