import React, { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { Hash, Globe, ToggleLeft, ToggleRight, Users, Settings, ExternalLink, Link2, X } from 'lucide-react'; import { apiClient } from '../lib/api'; const Channels: React.FC = () => { const queryClient = useQueryClient(); const [showAccountModal, setShowAccountModal] = useState<{ channelId: number; channelName: string } | null>(null); const { data: channels, isLoading, error } = useQuery({ queryKey: ['platformChannels'], queryFn: () => apiClient.getPlatformChannels(), }); const { data: accounts } = useQuery({ queryKey: ['platformAccounts'], queryFn: () => apiClient.getPlatformAccounts(), enabled: !!showAccountModal, }); const toggleMutation = useMutation({ mutationFn: (channelId: number) => apiClient.togglePlatformChannel(channelId), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['platformChannels'] }); }, }); const attachAccountMutation = useMutation({ mutationFn: ({ channelId, accountId }: { channelId: number; accountId: number }) => apiClient.attachAccountToChannel(channelId, { platform_account_id: accountId }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['platformChannels'] }); setShowAccountModal(null); }, }); const detachAccountMutation = useMutation({ mutationFn: ({ channelId, accountId }: { channelId: number; accountId: number }) => apiClient.detachAccountFromChannel(channelId, accountId), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['platformChannels'] }); }, }); const handleToggle = (channelId: number) => { toggleMutation.mutate(channelId); }; const handleAttachAccount = (channelId: number, accountId: number) => { attachAccountMutation.mutate({ channelId, accountId }); }; const handleDetachAccount = (channelId: number, accountId: number) => { detachAccountMutation.mutate({ channelId, accountId }); }; if (isLoading) { return (
There was an error loading the platform channels. Please try again.
Manage your publishing channels and their associated accounts.
Get started by creating a new platform channel.
Channels are created during onboarding. If you need to create more channels, please go through the onboarding process again.
@{channel.name}
)}{channel.description}
)}Select a platform account to link to this channel:
{accounts && accounts.length > 0 ? (All available accounts are already linked to this channel.
)}No platform accounts available. Create a platform account first.
)}