/** * CertificatesPage -- Main certificate management page. * * Two sections: * 1. CA Status Card -- shows CA state or initialization prompt * 2. Device Certificates Table -- per-device cert status with actions */ import { useQuery } from '@tanstack/react-query' import { useUIStore } from '@/lib/store' import { Shield, Building2 } from 'lucide-react' import { certificatesApi } from '@/lib/certificatesApi' import { useAuth, isSuperAdmin } from '@/lib/auth' import { canWrite } from '@/lib/auth' import { CAStatusCard } from './CAStatusCard' import { DeviceCertTable } from './DeviceCertTable' import { EmptyState } from '@/components/ui/empty-state' import { TableSkeleton } from '@/components/ui/page-skeleton' export function CertificatesPage() { const { user } = useAuth() const writable = canWrite(user) const { selectedTenantId } = useUIStore() const tenantId = isSuperAdmin(user) ? (selectedTenantId ?? '') : (user?.tenant_id ?? '') // ── Queries ── const { data: ca, isLoading: caLoading, } = useQuery({ queryKey: ['ca', tenantId], queryFn: () => certificatesApi.getCA(tenantId), enabled: !!tenantId, }) const { data: deviceCerts = [], isLoading: certsLoading, } = useQuery({ queryKey: ['deviceCerts', tenantId], queryFn: () => certificatesApi.getDeviceCerts(undefined, tenantId), enabled: !!tenantId && ca !== undefined, }) // Super admin needs to select a tenant from the header if (isSuperAdmin(user) && !tenantId) { return (