import { useEffect, useState } from 'react' import { useParams, useNavigate } from 'react-router-dom' import { Wrench, Calendar, Play, Settings, Clock, CheckCircle, AlertCircle } from 'lucide-react' import { treesApi } from '@/api/trees' import { sessionsApi } from '@/api/sessions' import { maintenanceSchedulesApi } from '@/api/maintenanceSchedules' import { BatchLaunchModal } from '@/components/maintenance/BatchLaunchModal' import { toast } from '@/lib/toast' import { cn } from '@/lib/utils' import type { Tree, MaintenanceSchedule, Session } from '@/types' export default function MaintenanceFlowDetailPage() { const { id } = useParams<{ id: string }>() const navigate = useNavigate() const [tree, setTree] = useState(null) const [schedule, setSchedule] = useState(null) const [recentSessions, setRecentSessions] = useState([]) const [showBatchModal, setShowBatchModal] = useState(false) const [isLoading, setIsLoading] = useState(true) useEffect(() => { if (!id) return const load = async () => { try { const treeData = await treesApi.get(id) setTree(treeData) // Load recent sessions for this tree try { const sessionData = await sessionsApi.list({ tree_id: id, size: 30 }) setRecentSessions(Array.isArray(sessionData) ? sessionData : []) } catch { // Sessions load is optional } // Try to load schedule (404 is fine) try { const sched = await maintenanceSchedulesApi.getForTree(id) setSchedule(sched) } catch { // No schedule yet is fine } } catch { toast.error('Failed to load maintenance flow') navigate('/trees?type=maintenance') } finally { setIsLoading(false) } } load() }, [id, navigate]) const handleLaunched = (_batchId: string, count: number) => { setShowBatchModal(false) toast.success(`${count} sessions created — view them in Sessions`) navigate('/sessions') } if (isLoading) { return (
) } if (!tree) return null // Group sessions by batch_id for run history const batchMap = new Map() for (const s of recentSessions) { const key = s.batch_id ?? s.id const existing = batchMap.get(key) ?? [] batchMap.set(key, [...existing, s]) } const batches = Array.from(batchMap.entries()).slice(0, 10) return (
{/* Header */}

{tree.name}

{tree.description && (

{tree.description}

)}
{/* Schedule Panel */}

Schedule

{schedule ? (
{schedule.is_active ? : } {schedule.is_active ? 'Active' : 'Paused'} {schedule.cron_expression} ({schedule.timezone})
{schedule.next_run_at && (

Next run: {new Date(schedule.next_run_at).toLocaleString()}

)}
) : (

No schedule configured. Sessions can still be launched manually via Batch Launch.

)}
{/* Run History */}

Run History

{batches.length === 0 ? (

No runs yet. Launch a batch to get started.

) : (
{batches.map(([batchKey, sessions]) => { const completed = sessions.filter(s => s.completed_at).length const total = sessions.length const date = sessions[0]?.started_at return (

{total} target{total !== 1 ? 's' : ''}

{date && (

{new Date(date).toLocaleDateString()}

)}
{completed}/{total} complete
) })}
)}
{showBatchModal && ( setShowBatchModal(false)} onLaunched={handleLaunched} /> )}
) }