"use client" import { useEffect, useState } from "react" import { Users, Loader2, AlertCircle, CheckCircle2, Clock, XCircle, Building2, Mail, Package, Globe, X, Store } from "lucide-react" interface Connection { id: string status: "PENDING" | "CONNECTED" | "REJECTED" createdAt: string supplier: { id: string companyName: string description: string | null user: { name: string | null email: string | null } _count: { products: number } } store?: { id: string businessName: string isActive: boolean } } export default function MySuppliersPage() { const [connections, setConnections] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState("") const [success, setSuccess] = useState("") const [filter, setFilter] = useState<"ALL" | "PENDING" | "CONNECTED" | "REJECTED">("ALL") const [cancellingId, setCancellingId] = useState(null) useEffect(() => { fetchMySuppliers() }, []) // Poll for updates every 5 seconds if there are pending connections useEffect(() => { const hasPending = connections.some(c => c.status === "PENDING") if (!hasPending) return // Don't poll if no pending connections const interval = setInterval(() => { fetchMySuppliers() }, 5000) // Poll every 5 seconds return () => clearInterval(interval) }, [connections]) // Re-run when connections change useEffect(() => { if (success) { const timer = setTimeout(() => setSuccess(""), 5000) return () => clearTimeout(timer) } }, [success]) useEffect(() => { if (error) { const timer = setTimeout(() => setError(""), 5000) return () => clearTimeout(timer) } }, [error]) const fetchMySuppliers = async () => { try { setLoading(true) const res = await fetch("/api/vendors/suppliers/my") if (!res.ok) throw new Error("Failed to fetch suppliers") const data = await res.json() setConnections(data.connections || []) setError("") } catch { setError("Failed to load suppliers") } finally { setLoading(false) } } const handleCancelInvitation = async (supplierId: string) => { setCancellingId(supplierId) setError("") try { const res = await fetch(`/api/vendors/suppliers/${supplierId}/invite`, { method: "DELETE" }) const data = await res.json() if (!res.ok) throw new Error(data.message || "Failed to cancel invitation") setSuccess("Invitation cancelled successfully. You can now send a new invitation.") fetchMySuppliers() // Refresh the list } catch (err) { setError(err instanceof Error ? err.message : "Failed to cancel invitation") } finally { setCancellingId(null) } } const filteredConnections = connections.filter((conn) => filter === "ALL" ? true : conn.status === filter ) const getStatusBadge = (status: string) => { switch (status) { case "CONNECTED": return ( Connected ) case "PENDING": return ( Pending ) case "REJECTED": return ( Rejected ) default: return null } } return (

My Suppliers

Manage your existing supplier connections.

{success && (
{success}
)} {error && (
{error}
)} {/* Filter Tabs */}
{(["ALL", "PENDING", "CONNECTED", "REJECTED"] as const).map((status) => ( ))}
{loading ? (
) : filteredConnections.length === 0 ? (

No Connected Suppliers

{filter === "ALL" ? "You haven't connected with any suppliers yet. Browse the global marketplace to find suppliers." : `No ${filter.toLowerCase()} suppliers`}

) : (
{filteredConnections.map((connection) => (

{connection.supplier.companyName}

{connection.supplier.user.name && (

{connection.supplier.user.name}

)}
{getStatusBadge(connection.status)}
{connection.supplier.description && (

{connection.supplier.description}

)}
{connection.supplier._count.products} products
{connection.supplier.user.email && (
{connection.supplier.user.email}
)}
{connection.status === "PENDING" ? "Invited" : "Connected"}: {new Date(connection.createdAt).toLocaleDateString()}
{connection.status === "PENDING" && ( )}
{connection.store && (
{connection.store.businessName} {!connection.store.isActive && ( Inactive )}
)}
))}
)}
) }