// Copyright 2026 Arctel.net // SPDX-License-Identifier: Apache-2.0 'use client'; import * as React from 'react'; import { motion } from 'motion/react'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { MessagesSquare, Plus } from 'lucide-react'; import { useTranslations } from 'next-intl'; import { toast } from 'sonner'; import { Button } from '@/components/ui/button'; import { ErrorInline } from '@/components/layout/error'; import { LoadingStateWithBorder } from '@/components/layout/loading'; import { AdminMessageGatewayService } from '@/lib/services/message-gateway'; import type { CreateMessageChannelRequest } from '@/lib/services/message-gateway'; import { AddChannelDialog } from './components/add-channel-dialog'; import { ChannelCard } from './components/channel-card'; export default function MessageGatewayAdminPage() { const t = useTranslations('admin.messageGateway'); const queryClient = useQueryClient(); const [addOpen, setAddOpen] = React.useState(false); const channelsQuery = useQuery({ queryKey: ['admin', 'message-gateway-channels'], queryFn: () => AdminMessageGatewayService.list(), }); const createMutation = useMutation({ mutationFn: (data: CreateMessageChannelRequest) => AdminMessageGatewayService.create(data), onSuccess: () => { toast.success(t('createSuccess')); queryClient.invalidateQueries({ queryKey: ['admin', 'message-gateway-channels'], }); setAddOpen(false); }, onError: (err: unknown) => { toast.error(t('createFailed') + ': ' + (err as Error).message); }, }); const toggleMutation = useMutation({ mutationFn: ({ id, enabled }: { id: string; enabled: boolean }) => AdminMessageGatewayService.update(id, { enabled }), onSuccess: () => { toast.success(t('updateSuccess')); queryClient.invalidateQueries({ queryKey: ['admin', 'message-gateway-channels'], }); }, onError: (err: unknown) => { toast.error(t('updateFailed') + ': ' + (err as Error).message); }, }); const deleteMutation = useMutation({ mutationFn: (id: string) => AdminMessageGatewayService.remove(id), onSuccess: () => { toast.success(t('deleteSuccess')); queryClient.invalidateQueries({ queryKey: ['admin', 'message-gateway-channels'], }); }, onError: (err: unknown) => { toast.error(t('deleteFailed') + ': ' + (err as Error).message); }, }); const channels = channelsQuery.data ?? []; return (

{t('pageTitle')}

{t('pageDescription')}

{channelsQuery.isLoading ? ( ) : channelsQuery.isError ? ( ) : channels.length === 0 ? (

{t('emptyTitle')}

{t('emptyDescription')}

) : (
{channels.map((ch) => ( toggleMutation.mutate({ id: ch.id, enabled }) } onDelete={() => deleteMutation.mutate(ch.id)} /> ))}
)} createMutation.mutate(data)} />
); }