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 (
{[...Array(3)].map((_, i) => (
))}
); } if (error) { return (

Error loading channels

There was an error loading the platform channels. Please try again.

); } return (

Platform Channels

Manage your publishing channels and their associated accounts.

{!channels || channels.length === 0 ? (

No channels

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.

) : (
{channels.map((channel) => (

{channel.display_name || channel.name}

{channel.display_name && channel.display_name !== channel.name && (

@{channel.name}

)}
Channel ID: {channel.channel_id}
{channel.description && (

{channel.description}

)}
{channel.platform_accounts?.length || 0} account{(channel.platform_accounts?.length || 0) !== 1 ? 's' : ''} linked
{channel.platform_accounts && channel.platform_accounts.length > 0 && (
{channel.platform_accounts.map((account) => (
@{account.username}
))}
)}
{channel.is_active ? 'Active' : 'Inactive'}
Created {new Date(channel.created_at).toLocaleDateString()}
))}
)} {/* Account Management Modal */} {showAccountModal && (
setShowAccountModal(null)}>

Manage Accounts for {showAccountModal.channelName}

Select a platform account to link to this channel:

{accounts && accounts.length > 0 ? (
{accounts .filter(account => !channels?.find(c => c.id === showAccountModal.channelId)?.platform_accounts?.some(pa => pa.id === account.id)) .map((account) => ( ))} {accounts.filter(account => !channels?.find(c => c.id === showAccountModal.channelId)?.platform_accounts?.some(pa => pa.id === account.id)).length === 0 && (

All available accounts are already linked to this channel.

)}
) : (

No platform accounts available. Create a platform account first.

)}
)}
); }; export default Channels;