"use client" import { useCallback, useEffect, useState } from "react" import { Loader2 } from "lucide-react" import { useTranslations } from "next-intl" import { toast } from "sonner" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Switch } from "@/components/ui/switch" import { updateChatChannel, saveChatChannelToken, getChatChannelHasToken, } from "@/lib/api" import type { ChatChannelInfo } from "@/lib/types" interface EditChatChannelDialogProps { open: boolean channel: ChatChannelInfo onOpenChange: (open: boolean) => void onChannelUpdated: () => void } export function EditChatChannelDialog({ open, channel, onOpenChange, onChannelUpdated, }: EditChatChannelDialogProps) { const t = useTranslations("ChatChannelSettings") const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const config = JSON.parse(channel.config_json || "{}") const [name, setName] = useState(channel.name) const [token, setToken] = useState("") const [chatId, setChatId] = useState(config.chat_id ?? "") const [appId, setAppId] = useState(config.app_id ?? "") const [baseUrl] = useState(config.base_url ?? "") const [dailyReportEnabled, setDailyReportEnabled] = useState( channel.daily_report_enabled ) const [dailyReportTime, setDailyReportTime] = useState( channel.daily_report_time || "18:00" ) const [hasToken, setHasToken] = useState(false) useEffect(() => { if (open) { getChatChannelHasToken(channel.id) .then(setHasToken) .catch(() => {}) } }, [open, channel.id]) const handleSubmit = useCallback(async () => { if (!name.trim()) { setError(t("nameRequired")) return } if (channel.channel_type !== "weixin" && !chatId.trim()) { setError(t("chatIdRequired")) return } setLoading(true) setError(null) try { const configJson = channel.channel_type === "weixin" ? JSON.stringify({ base_url: baseUrl }) : channel.channel_type === "lark" ? JSON.stringify({ app_id: appId, chat_id: chatId }) : JSON.stringify({ chat_id: chatId }) await updateChatChannel({ id: channel.id, name: name.trim(), configJson, dailyReportEnabled, dailyReportTime: dailyReportEnabled ? dailyReportTime : null, }) if (token.trim()) { await saveChatChannelToken(channel.id, token.trim()) } onOpenChange(false) onChannelUpdated() toast.success(t("editSuccess")) } catch (err: unknown) { const msg = err instanceof Error ? err.message : String(err) setError(msg) } finally { setLoading(false) } }, [ name, token, chatId, channel, appId, baseUrl, dailyReportEnabled, dailyReportTime, onOpenChange, onChannelUpdated, t, ]) return ( {t("editChannel")}
setName(e.target.value)} placeholder={t("channelNamePlaceholder")} />
{channel.channel_type === "lark" && (
setAppId(e.target.value)} placeholder="cli_xxxxx" />
)} {channel.channel_type !== "weixin" && (
setToken(e.target.value)} placeholder={ hasToken ? t("tokenPlaceholderKeep") : t("tokenRequired") } />
)} {channel.channel_type !== "weixin" && (
setChatId(e.target.value)} placeholder={ channel.channel_type === "telegram" ? "-100123456789" : "oc_xxxxx" } />
)} {channel.channel_type === "weixin" && baseUrl && (
)}
{dailyReportEnabled && (
setDailyReportTime(e.target.value)} />
)} {error && (
{error}
)}
) }