import React, { useEffect, useState } from 'react' import { listDirectives, DirectiveSummary } from '../../services/directiveApi' interface DirectiveFileTreeProps { selectedDirectiveId: string | null onSelectDirective: (id: string) => void onNewDirective: () => void } interface GroupState { [key: string]: boolean } const STATUS_GROUPS = [ { key: 'active', label: 'Active', defaultExpanded: true }, { key: 'idle', label: 'Idle', defaultExpanded: true }, { key: 'draft', label: 'Draft', defaultExpanded: false }, { key: 'archived', label: 'Archived', defaultExpanded: false }, ] as const function statusColor(status: string): string { switch (status.toLowerCase()) { case 'active': case 'running': return '#4caf50' case 'idle': case 'paused': return '#ffc107' case 'draft': case 'pending': return '#9e9e9e' case 'archived': case 'failed': return '#f44336' default: return '#9e9e9e' } } function groupDirectives(directives: DirectiveSummary[]): Record { const groups: Record = { active: [], idle: [], draft: [], archived: [], } for (const d of directives) { const s = d.status.toLowerCase() if (s === 'active' || s === 'running') { groups.active.push(d) } else if (s === 'idle' || s === 'paused') { groups.idle.push(d) } else if (s === 'draft' || s === 'pending') { groups.draft.push(d) } else { groups.archived.push(d) } } return groups } export function DirectiveFileTree({ selectedDirectiveId, onSelectDirective, onNewDirective }: DirectiveFileTreeProps) { const [directives, setDirectives] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [expanded, setExpanded] = useState(() => { const state: GroupState = {} for (const g of STATUS_GROUPS) { state[g.key] = g.defaultExpanded } return state }) useEffect(() => { async function load() { try { setLoading(true) const data = await listDirectives() setDirectives(data) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load directives') } finally { setLoading(false) } } load() }, []) const toggleGroup = (key: string) => { setExpanded(prev => ({ ...prev, [key]: !prev[key] })) } const grouped = groupDirectives(directives) return (
Directives
{loading &&
Loading...
} {error &&
{error}
} {!loading && !error && (
{STATUS_GROUPS.map(group => { const items = grouped[group.key] if (!items || items.length === 0) return null return (
{expanded[group.key] && (
{items.map(directive => ( ))}
)}
) })}
)}
) }