'use client'; import {useState} from 'react'; import {useMutation} from '@tanstack/react-query'; import {MoreHorizontal, Pencil, Plus, Trash2} from 'lucide-react'; import {toast} from 'sonner'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'; import {Button} from '@/components/ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import {EmptyStateWithBorder} from '@/components/layout/empty'; import {Table, TableBody, TableCell, TableHead, TableHeader, TableRow} from '@/components/ui/table'; import {ZoneDomainService, type TlsCertificateItem, type ZoneDomainItem} from '@/lib/services/openflare'; import {ZoneDomainDialog} from './zone-domain-dialog'; export function ZoneDomainsTable({ zoneId, domains, certificates, onChanged, }: { zoneId: number; domains: ZoneDomainItem[]; certificates: TlsCertificateItem[]; onChanged(): Promise | void; }) { const [editing, setEditing] = useState(undefined); const [deleting, setDeleting] = useState(null); const remove = useMutation({ mutationFn: (id: number) => ZoneDomainService.deleteById(zoneId, id), onSuccess: async () => { toast.success('域名已删除'); setDeleting(null); await onChanged(); }, onError: (error) => toast.error(error instanceof Error ? error.message : '删除失败'), }); const certificateMap = new Map(certificates.map((certificate) => [certificate.id, certificate])); return ( <>
{domains.length === 0 ? ( ) : (
FQDN 证书 关联路由 备注 {domains.map((domain) => ( {domain.domain} {domain.cert_id ? (certificateMap.get(domain.cert_id)?.name ?? `证书 #${domain.cert_id}`) : '未绑定'} {domain.proxy_route_id ? `#${domain.proxy_route_id}` : '未关联'} {domain.remark || '—'} setEditing(domain)}> 编辑 setDeleting(domain)} > 删除 ))}
)} { if (!open) { setEditing(undefined); } }} zoneId={zoneId} domain={editing ?? null} onSaved={onChanged} /> { if (!open) { setDeleting(null); } }} > 确认删除域名 确认删除 {deleting?.domain} 吗?此操作不可撤销。 取消 { event.preventDefault(); if (deleting) { remove.mutate(deleting.id); } }} > {remove.isPending ? '删除中…' : '确认删除'} ); }