"use client" import { useCallback, useState } from "react" import { Loader2 } from "lucide-react" import { useTranslations } from "next-intl" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Switch } from "@/components/ui/switch" import { createChatChannel, saveChatChannelToken } from "@/lib/api" import type { ChannelType } from "@/lib/types" interface AddChatChannelDialogProps { open: boolean onOpenChange: (open: boolean) => void onChannelAdded: () => void } export function AddChatChannelDialog({ open, onOpenChange, onChannelAdded, }: AddChatChannelDialogProps) { const t = useTranslations("ChatChannelSettings") const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [name, setName] = useState("") const [channelType, setChannelType] = useState("telegram") const [token, setToken] = useState("") const [chatId, setChatId] = useState("") const [appId, setAppId] = useState("") const [dailyReportEnabled, setDailyReportEnabled] = useState(false) const [dailyReportTime, setDailyReportTime] = useState("18:00") const resetForm = useCallback(() => { setName("") setChannelType("telegram") setToken("") setChatId("") setAppId("") setDailyReportEnabled(false) setDailyReportTime("18:00") setError(null) }, []) const handleOpenChange = useCallback( (nextOpen: boolean) => { if (!nextOpen) resetForm() onOpenChange(nextOpen) }, [onOpenChange, resetForm] ) const handleSubmit = useCallback(async () => { if (!name.trim()) { setError(t("nameRequired")) return } if (!token.trim()) { setError(t("tokenRequired")) return } if (!chatId.trim()) { setError(t("chatIdRequired")) return } setLoading(true) setError(null) try { const configJson = channelType === "lark" ? JSON.stringify({ app_id: appId, chat_id: chatId }) : JSON.stringify({ chat_id: chatId }) const channel = await createChatChannel({ name: name.trim(), channelType, configJson, enabled: true, dailyReportEnabled, dailyReportTime: dailyReportEnabled ? dailyReportTime : null, }) await saveChatChannelToken(channel.id, token.trim()) handleOpenChange(false) onChannelAdded() } catch (err) { const msg = err instanceof Error ? err.message : String(err) setError(msg) } finally { setLoading(false) } }, [ name, token, chatId, channelType, appId, dailyReportEnabled, dailyReportTime, handleOpenChange, onChannelAdded, t, ]) return ( {t("addChannel")}
setName(e.target.value)} placeholder={t("channelNamePlaceholder")} />
{channelType === "lark" && (
setAppId(e.target.value)} placeholder="cli_xxxxx" />
)}
setToken(e.target.value)} placeholder={ channelType === "telegram" ? "123456:ABC-DEF..." : "xxxxx" } />
setChatId(e.target.value)} placeholder={ channelType === "telegram" ? "-100123456789" : "oc_xxxxx" } />
{dailyReportEnabled && (
setDailyReportTime(e.target.value)} />
)} {error && (
{error}
)}
) }