import { useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { Domain, Provider } from "@shared/schema"; import { Link } from "wouter"; import { useOrganization } from "@/context/organization-context"; import { Pagination } from "@/components/shared/pagination"; import { Button } from "@/components/ui/button"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { formatDistanceToNow } from "date-fns"; import { Cloud, Home, MoreVertical, Loader2, CircleHelp } from "lucide-react"; interface DomainTableProps { onManageDomain?: (domain: Domain) => void; onDeleteDomain?: (domain: Domain) => void; onAddDomain?: () => void; } export function DomainTable({ onManageDomain, onDeleteDomain, onAddDomain }: DomainTableProps) { const { currentOrganization } = useOrganization(); const [currentPage, setCurrentPage] = useState(1); const pageSize = 5; const { data: domains = [], isLoading } = useQuery({ queryKey: ["/api/domains", currentOrganization?.id], enabled: !!currentOrganization, }); // Get providers to display provider names const { data: providers = [] } = useQuery({ queryKey: ["/api/providers"], }); const getProviderName = (providerId: string | null | undefined) => { // Add enhanced debugging to help track down the issue console.log("Provider lookup - ID provided:", providerId, typeof providerId); console.log("Available providers:", providers.map(p => ({ id: p.id, name: p.name }))); // Handle completely empty providerId cases if (providerId === null || providerId === undefined || providerId === "") { console.log("Provider is empty/null"); return "None"; } // Check if the provider exists in our providers list // The providerId from the database will be a string UUID const provider = providers.find(p => p.id === providerId); if (provider) { console.log("Found provider:", provider.name); return provider.name; } else { console.log("Provider not found for ID:", providerId); // If we have a providerId but can't find the provider (might be deleted) return "Unknown Provider"; } }; const getProviderIcon = (providerId: string | null | undefined) => { // Handle completely empty providerId cases if (!providerId || providerId === "") { return ; } // The providerId from the database will be a string UUID const provider = providers.find(p => p.id === providerId); if (!provider) { return ; } switch (provider.type) { case "cloudflare": return ; case "route53": return ; default: return ; } }; const getStatusBadge = (domain: Domain) => { if (!domain.isActive) { return ( Inactive ); } // If updated within the last hour, show "Active" if (domain.lastUpdated && new Date(domain.lastUpdated).getTime() > Date.now() - 3600000) { return ( Active ); } // If updated more than 24 hours ago, show "Update Pending" if (domain.lastUpdated && new Date(domain.lastUpdated).getTime() < Date.now() - 86400000) { return ( Update Pending ); } return ( Active ); }; // Calculate pagination const startIndex = (currentPage - 1) * pageSize; const endIndex = startIndex + pageSize; const paginatedDomains = domains.slice(startIndex, endIndex); const totalPages = Math.ceil(domains.length / pageSize); if (isLoading) { return (
); } if (domains.length === 0) { return (

No domains found

Get started by adding your first domain.

); } return (

Managed Domains

Domain Provider Records Last Update Status Actions {paginatedDomains.map(domain => ( {domain.name}
{getProviderIcon(domain.providerId)} {getProviderName(domain.providerId)}
View Records
{domain.lastUpdated ? formatDistanceToNow(new Date(domain.lastUpdated), { addSuffix: true }) : "Never"} {getStatusBadge(domain)} onManageDomain && onManageDomain(domain)} > Manage onDeleteDomain && onDeleteDomain(domain)} className="text-destructive focus:text-destructive" > Delete
))}
); }