import React, { useState, useEffect, useCallback } from 'react'; import { FiLink, FiUsers, FiClock, FiCheck, FiX, FiMessageCircle, FiRefreshCw, FiUser } from 'react-icons/fi'; import { Button } from '../components/ui/Button'; import { Badge } from '../components/ui/badge'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from '../components/ui/Card'; import { toast } from 'sonner'; import { Avatar, AvatarFallback, AvatarImage } from '../components/ui/avatar'; import { Skeleton } from '../components/ui/skeleton'; import { Tabs, TabsList, TabsTrigger, TabsContent } from '../components/ui/tabs'; import { ScrollArea } from '../components/ui/scroll-area'; import Typography from '../components/ui/Typography'; interface Connection { id: string; accountId: string; profile?: any; status: 'pending' | 'accepted' | 'rejected'; direction: 'incoming' | 'outgoing'; createdAt: Date; updatedAt?: Date; topicId?: string; } const ConnectionsPage: React.FC = () => { const [connections, setConnections] = useState([]); const [isLoading, setIsLoading] = useState(false); const [activeTab, setActiveTab] = useState<'all' | 'pending' | 'accepted'>('all'); /** * Loads connections from the backend */ const loadConnections = useCallback(async () => { try { setIsLoading(true); const result = await window.api.invoke('hcs10:get-connections'); if (result.success) { setConnections(result.data || []); } else { toast.error('Failed to Load Connections', { description: result.error || 'Could not retrieve connections', }); } } catch (error) { toast.error('Error', { description: 'Failed to connect to connection service', }); } finally { setIsLoading(false); } }, []); /** * Accepts a connection request */ const handleAccept = useCallback(async (connectionId: string) => { try { const result = await window.api.invoke('hcs10:accept-connection', { connectionId, }); if (result.success) { toast.success('Connection Accepted', { description: 'You are now connected with this agent', }); loadConnections(); } else { toast.error('Failed to Accept', { description: result.error || 'Could not accept connection', }); } } catch (error) { toast.error('Error', { description: 'Failed to accept connection', }); } }, [loadConnections]); /** * Rejects a connection request */ const handleReject = useCallback(async (connectionId: string) => { try { const result = await window.api.invoke('hcs10:reject-connection', { connectionId, }); if (result.success) { toast.success('Connection Rejected', { description: 'The connection request has been rejected', }); loadConnections(); } else { toast.error('Failed to Reject', { description: result.error || 'Could not reject connection', }); } } catch (error) { toast.error('Error', { description: 'Failed to reject connection', }); } }, [loadConnections]); /** * Starts a chat with a connected agent */ const handleStartChat = useCallback((accountId: string) => { window.location.href = `#/chat/${accountId}`; }, []); const handleDiscoverAgents = () => { window.location.href = '#/discover'; }; useEffect(() => { loadConnections(); }, [loadConnections]); /** * Filters connections based on active tab */ const filteredConnections = connections.filter((conn) => { if (activeTab === 'all') return true; if (activeTab === 'pending') return conn.status === 'pending'; if (activeTab === 'accepted') return conn.status === 'accepted'; return true; }); /** * Gets statistics for connections */ const stats = { total: connections.length, pending: connections.filter((c) => c.status === 'pending').length, accepted: connections.filter((c) => c.status === 'accepted').length, incoming: connections.filter((c) => c.direction === 'incoming' && c.status === 'pending').length, }; const ConnectionSkeleton: React.FC = () => (
); interface ConnectionCardProps { connection: Connection; onAccept: (connectionId: string) => void; onReject: (connectionId: string) => void; onStartChat: (accountId: string) => void; } const ConnectionCard: React.FC = ({ connection, onAccept, onReject, onStartChat }) => { const displayName = connection.profile?.display_name || `Account ${connection.accountId?.split('.').pop() || 'Unknown'}`; const bio = connection.profile?.bio; const handleAcceptClick = () => onAccept(connection.id); const handleRejectClick = () => onReject(connection.id); const handleStartChatClick = () => onStartChat(connection.accountId); return (
{displayName.charAt(0).toUpperCase()}
{displayName}

{connection.accountId}

{bio && (

{bio}

)}
{connection.direction === 'incoming' ? ( Incoming request ) : ( Outgoing request )}
{connection.status === 'pending' && connection.direction === 'incoming' && ( <> )} {connection.status === 'accepted' && ( )} {connection.status === 'pending' && connection.direction === 'outgoing' && ( Waiting for response )}
); }; return (
Connections

Manage your agent connections and requests

Total

{stats.total}

Connected

{stats.accepted}

Pending

{stats.pending}

New Requests

{stats.incoming}

setActiveTab(v as any)}> All ({stats.total}) Pending ({stats.pending}) {stats.incoming > 0 && ( {stats.incoming} )} Connected ({stats.accepted})
{isLoading ? (
{Array.from({ length: 3 }).map((_, i) => ( ))}
) : filteredConnections.length > 0 ? (
{filteredConnections.map((connection) => ( ))}
) : (

No connections yet

{activeTab === 'pending' ? 'No pending connection requests' : activeTab === 'accepted' ? 'No accepted connections yet' : 'Start discovering agents to make connections'}

)}
); }; export default ConnectionsPage;