import { useCallback, useEffect, useState } from 'react'; import { Plus } from 'lucide-react'; import { Modal, ModalHeader, ModalBody } from '@/components/ui/modal'; import { Button } from '@/components/ui/button'; import { toast } from '@/components/ui/toast-store'; import { type BlueprintListItem, type CreateBlueprintInput, type UpdateBlueprintInput, listBlueprints, createBlueprint, listDistinctLabels, } from '@/lib/blueprintsApi'; import { BlueprintCatalog } from './BlueprintCatalog'; import { BlueprintEmptyState } from './BlueprintEmptyState'; import { FleetTabHeading, FleetEmptyState } from '../fleet/FleetEmptyState'; import { BlueprintDetail } from './BlueprintDetail'; import { BlueprintEditor } from './BlueprintEditor'; import { useAuth } from '@/context/AuthContext'; export function DeploymentsTab() { const { isAdmin } = useAuth(); const canEdit = isAdmin; const [blueprints, setBlueprints] = useState([]); const [distinctLabels, setDistinctLabels] = useState([]); const [loading, setLoading] = useState(true); const [loadError, setLoadError] = useState(null); const [selectedId, setSelectedId] = useState(null); const [createOpen, setCreateOpen] = useState(false); const [submitting, setSubmitting] = useState(false); const refresh = useCallback(async () => { setLoading(true); setLoadError(null); try { const [list, labels] = await Promise.all([ listBlueprints(), listDistinctLabels().catch(() => [] as string[]), ]); setBlueprints(list); setDistinctLabels(labels); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to load blueprints'; setLoadError(message); toast.error(message); } finally { setLoading(false); } }, []); useEffect(() => { void refresh(); }, [refresh]); async function handleCreate(input: CreateBlueprintInput | UpdateBlueprintInput) { setSubmitting(true); try { const created = await createBlueprint(input as CreateBlueprintInput); toast.success('Blueprint created'); setCreateOpen(false); await refresh(); setSelectedId(created.id); } catch (err) { toast.error(err instanceof Error ? err.message : 'Failed to create blueprint'); } finally { setSubmitting(false); } } if (loading) { return (
Loading blueprints…
); } if (loadError) { return (
Could not load blueprints

{loadError}

); } return (
{blueprints.length === 0 ? ( <> setCreateOpen(true)}> New Blueprint ) : undefined} /> setCreateOpen(true)} canCreate={canEdit} /> ) : ( setCreateOpen(true)} canCreate={canEdit} /> )} {selectedId !== null && ( { if (!o) setSelectedId(null); }} onChanged={refresh} canEdit={canEdit} distinctLabels={distinctLabels} /> )} setCreateOpen(false)} onSubmit={handleCreate} submitting={submitting} />
); }