// Copyright 2026 Arctel.net // SPDX-License-Identifier: Apache-2.0 'use client'; import * as React from 'react'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { Bot, Loader2, Unlink } 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 { Label } from '@/components/ui/label'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { UserMessageGatewayService } from '@/lib/services/message-gateway'; export function BotBindingCard() { const t = useTranslations('settings.botBinding'); const queryClient = useQueryClient(); const [open, setOpen] = React.useState(false); const [channelId, setChannelId] = React.useState(''); const [code, setCode] = React.useState(''); const bindingsQuery = useQuery({ queryKey: ['message-gateway', 'bindings'], queryFn: () => UserMessageGatewayService.listBindings(), }); const channelsQuery = useQuery({ queryKey: ['message-gateway', 'channels'], queryFn: () => UserMessageGatewayService.listChannels(), enabled: open, }); const bindMutation = useMutation({ mutationFn: () => UserMessageGatewayService.bind({ channel_id: channelId, code }), onSuccess: () => { toast.success(t('bindSuccess')); queryClient.invalidateQueries({ queryKey: ['message-gateway', 'bindings'], }); setOpen(false); setChannelId(''); setCode(''); }, onError: (err: unknown) => { toast.error(t('bindFailed') + ': ' + (err as Error).message); }, }); const unbindMutation = useMutation({ mutationFn: (id: string) => UserMessageGatewayService.unbind(id), onSuccess: () => { toast.success(t('unbindSuccess')); queryClient.invalidateQueries({ queryKey: ['message-gateway', 'bindings'], }); }, onError: (err: unknown) => { toast.error(t('unbindFailed') + ': ' + (err as Error).message); }, }); const typeLabel = (typ: string) => typ === 'qq' ? t('typeQQ') : t('typeTelegram'); return (
{t('description')}
{t('loadFailed')}
) : (bindingsQuery.data ?? []).length > 0 ? (