"use client"; import { useEffect, useState } from 'react'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Button } from '@/components/ui/button'; import { Skeleton } from '@/components/ui/skeleton'; import { Input } from '@/components/ui/input'; import { useToast } from '@/hooks/use-toast'; import { Globe, Plus, Settings, Search, X } from 'lucide-react'; import { AddDomainDialog } from './add-domain-dialog'; import { DomainSettingsDialog } from './domain-settings-dialog'; interface Domain { domain: string; date_created: string; dnssec?: "enabled" | "disabled"; } interface DomainsListProps { apiKey: string; onSelectDomain: (domain: string) => void; selectedDomain: string | null; } export function DomainsList({ apiKey, onSelectDomain, selectedDomain }: DomainsListProps) { const [domains, setDomains] = useState([]); const [filteredDomains, setFilteredDomains] = useState([]); const [searchQuery, setSearchQuery] = useState(''); const [loading, setLoading] = useState(true); const [isAddDomainOpen, setIsAddDomainOpen] = useState(false); const [isSettingsOpen, setIsSettingsOpen] = useState(false); const { toast } = useToast(); const fetchDomains = async () => { try { setLoading(true); const response = await fetch('/api/vultr/domains', { headers: { 'X-API-Key': apiKey, }, }); if (!response.ok) { throw new Error('Failed to fetch domains'); } const data = await response.json(); setDomains(data.domains || []); setFilteredDomains(data.domains || []); } catch (error) { console.error('Error fetching domains:', error); toast({ title: 'Error', description: 'Failed to fetch domains. Please check your API key.', variant: 'destructive', }); } finally { setLoading(false); } }; useEffect(() => { fetchDomains(); }, [apiKey, toast]); // Filter domains when search query changes useEffect(() => { if (searchQuery.trim() === '') { setFilteredDomains(domains); } else { const query = searchQuery.toLowerCase(); const filtered = domains.filter(domain => domain.domain.toLowerCase().includes(query) ); setFilteredDomains(filtered); } }, [searchQuery, domains]); const handleDomainAdded = (newDomain: Domain) => { const updatedDomains = [newDomain, ...domains]; setDomains(updatedDomains); setFilteredDomains(updatedDomains); toast({ title: 'Domain Added', description: `${newDomain.domain} has been added successfully.`, }); setIsAddDomainOpen(false); }; const handleDomainUpdated = (updatedDomain: Domain) => { const updatedDomains = domains.map(domain => domain.domain === updatedDomain.domain ? updatedDomain : domain ); setDomains(updatedDomains); setFilteredDomains(updatedDomains); toast({ title: 'Domain Updated', description: `${updatedDomain.domain} settings have been updated successfully.`, }); setIsSettingsOpen(false); }; const clearSearch = () => { setSearchQuery(''); }; if (loading) { return (
{Array.from({ length: 5 }).map((_, i) => ( ))}
); } const selectedDomainData = selectedDomain ? domains.find(d => d.domain === selectedDomain) : null; return ( <>
setSearchQuery(e.target.value)} className="pl-9 pr-9" /> {searchQuery && ( )}
{selectedDomain && ( )}
{domains.length === 0 ? (

No domains found

You don't have any domains configured in your Vultr account.

) : filteredDomains.length === 0 ? (

No matching domains

No domains match your search query "{searchQuery}"

) : (
{filteredDomains.map((domain) => ( ))}
)} {selectedDomainData && ( )} ); }