|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { useParams, useNavigate } from 'react-router-dom'; |
| 3 | +import Box from '@mui/joy/Box'; |
| 4 | +import CircularProgress from '@mui/joy/CircularProgress'; |
| 5 | +import Typography from '@mui/joy/Typography'; |
| 6 | +import Chip from '@mui/joy/Chip'; |
| 7 | +import IconButton from '@mui/joy/IconButton'; |
| 8 | +import Tooltip from '@mui/joy/Tooltip'; |
| 9 | +import List from '@mui/joy/List'; |
| 10 | +import ListItem from '@mui/joy/ListItem'; |
| 11 | +import ListItemButton from '@mui/joy/ListItemButton'; |
| 12 | +import { ArrowLeftIcon, LinkIcon } from 'lucide-react'; |
| 13 | +import { useExperimentInfo } from 'renderer/lib/ExperimentInfoContext'; |
| 14 | +import { useSWRWithAuth } from 'renderer/lib/authContext'; |
| 15 | +import * as chatAPI from 'renderer/lib/transformerlab-api-sdk'; |
| 16 | +import { jobChipColor } from 'renderer/lib/utils'; |
| 17 | +import { |
| 18 | + getDefaultSection, |
| 19 | + getVisibleSections, |
| 20 | + generateJobPermalink, |
| 21 | + type SectionKey, |
| 22 | + type JobRecord, |
| 23 | +} from './jobDetailUtils'; |
| 24 | +import OverviewSection from './OverviewSection'; |
| 25 | +import LogsSection from './LogsSection'; |
| 26 | +import CheckpointsSection from './CheckpointsSection'; |
| 27 | +import ArtifactsSection from './ArtifactsSection'; |
| 28 | +import EvalResultsSection from './EvalResultsSection'; |
| 29 | +import SweepResultsSection from './SweepResultsSection'; |
| 30 | + |
| 31 | +const SECTION_LABELS: Record<SectionKey, string> = { |
| 32 | + overview: 'Overview', |
| 33 | + logs: 'Logs', |
| 34 | + checkpoints: 'Checkpoints', |
| 35 | + artifacts: 'Artifacts', |
| 36 | + evalResults: 'Eval Results', |
| 37 | + sweepResults: 'Sweep Results', |
| 38 | +}; |
| 39 | + |
| 40 | +export default function JobDetailPage() { |
| 41 | + const { experimentName = '', jobId = '' } = useParams<{ |
| 42 | + experimentName: string; |
| 43 | + jobId: string; |
| 44 | + }>(); |
| 45 | + const navigate = useNavigate(); |
| 46 | + const { experimentInfo, setExperimentId } = useExperimentInfo(); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + if (experimentName) setExperimentId(experimentName); |
| 50 | + }, [experimentName, setExperimentId]); |
| 51 | + |
| 52 | + const { |
| 53 | + data: jobData, |
| 54 | + isError, |
| 55 | + isLoading: jobLoading, |
| 56 | + } = useSWRWithAuth( |
| 57 | + experimentInfo?.id && jobId |
| 58 | + ? chatAPI.Endpoints.Jobs.Get(experimentInfo.id, jobId) |
| 59 | + : null, |
| 60 | + ); |
| 61 | + |
| 62 | + const job: JobRecord | null = jobData ?? null; |
| 63 | + const visibleSections: SectionKey[] = job |
| 64 | + ? getVisibleSections(job) |
| 65 | + : ['overview', 'logs']; |
| 66 | + const [activeSection, setActiveSection] = useState<SectionKey | null>(null); |
| 67 | + |
| 68 | + const effectiveSection: SectionKey = |
| 69 | + activeSection ?? (job ? getDefaultSection(job.status ?? '') : 'overview'); |
| 70 | + |
| 71 | + if (!experimentInfo) { |
| 72 | + return ( |
| 73 | + <Box |
| 74 | + sx={{ |
| 75 | + display: 'flex', |
| 76 | + justifyContent: 'center', |
| 77 | + alignItems: 'center', |
| 78 | + height: '100%', |
| 79 | + }} |
| 80 | + > |
| 81 | + <CircularProgress /> |
| 82 | + </Box> |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + if (isError) { |
| 87 | + return ( |
| 88 | + <Box sx={{ p: 4 }}> |
| 89 | + <Typography level="h4" color="danger"> |
| 90 | + Job not found |
| 91 | + </Typography> |
| 92 | + <Typography |
| 93 | + sx={{ mt: 1, cursor: 'pointer', textDecoration: 'underline' }} |
| 94 | + onClick={() => navigate(`/experiment/${experimentName}/tasks`)} |
| 95 | + > |
| 96 | + Back to Tasks |
| 97 | + </Typography> |
| 98 | + </Box> |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + if (jobLoading) { |
| 103 | + return ( |
| 104 | + <Box |
| 105 | + sx={{ |
| 106 | + display: 'flex', |
| 107 | + justifyContent: 'center', |
| 108 | + alignItems: 'center', |
| 109 | + height: '100%', |
| 110 | + }} |
| 111 | + > |
| 112 | + <CircularProgress /> |
| 113 | + </Box> |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + return ( |
| 118 | + <Box sx={{ display: 'flex', flexDirection: 'column', height: '100%' }}> |
| 119 | + {/* Top bar */} |
| 120 | + <Box |
| 121 | + sx={{ |
| 122 | + display: 'flex', |
| 123 | + alignItems: 'center', |
| 124 | + justifyContent: 'space-between', |
| 125 | + px: 2, |
| 126 | + py: 1, |
| 127 | + borderBottom: '1px solid', |
| 128 | + borderColor: 'divider', |
| 129 | + flexShrink: 0, |
| 130 | + }} |
| 131 | + > |
| 132 | + <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}> |
| 133 | + <Tooltip title={`Back to ${experimentName} tasks`}> |
| 134 | + <IconButton |
| 135 | + size="sm" |
| 136 | + variant="plain" |
| 137 | + onClick={() => navigate(`/experiment/${experimentName}/tasks`)} |
| 138 | + > |
| 139 | + <ArrowLeftIcon size={16} /> |
| 140 | + </IconButton> |
| 141 | + </Tooltip> |
| 142 | + <Typography level="title-sm" sx={{ color: 'text.secondary' }}> |
| 143 | + {experimentName} |
| 144 | + </Typography> |
| 145 | + <Typography level="title-sm" sx={{ color: 'text.tertiary' }}> |
| 146 | + / |
| 147 | + </Typography> |
| 148 | + <Typography level="title-sm"> |
| 149 | + Job {job?.id ? job.id.slice(0, 8) : jobId.slice(0, 8)} |
| 150 | + </Typography> |
| 151 | + {job?.type && ( |
| 152 | + <Chip size="sm" color="primary" variant="soft"> |
| 153 | + {job.type} |
| 154 | + </Chip> |
| 155 | + )} |
| 156 | + {job?.status && ( |
| 157 | + <Chip |
| 158 | + size="sm" |
| 159 | + color={jobChipColor(job.status) as any} |
| 160 | + variant="soft" |
| 161 | + > |
| 162 | + {job.status} |
| 163 | + </Chip> |
| 164 | + )} |
| 165 | + </Box> |
| 166 | + <Tooltip title="Copy permalink"> |
| 167 | + <IconButton |
| 168 | + size="sm" |
| 169 | + variant="plain" |
| 170 | + color="neutral" |
| 171 | + onClick={() => { |
| 172 | + navigator.clipboard |
| 173 | + .writeText( |
| 174 | + window.location.href.split('#')[0] + |
| 175 | + generateJobPermalink(experimentName, jobId), |
| 176 | + ) |
| 177 | + .catch((err) => |
| 178 | + console.error('Failed to copy permalink:', err), |
| 179 | + ); |
| 180 | + }} |
| 181 | + > |
| 182 | + <LinkIcon size={16} /> |
| 183 | + </IconButton> |
| 184 | + </Tooltip> |
| 185 | + </Box> |
| 186 | + |
| 187 | + {/* Body: sidebar + content */} |
| 188 | + <Box sx={{ display: 'flex', flex: 1, overflow: 'hidden' }}> |
| 189 | + {/* Sidebar */} |
| 190 | + <Box |
| 191 | + sx={{ |
| 192 | + width: 160, |
| 193 | + flexShrink: 0, |
| 194 | + borderRight: '1px solid', |
| 195 | + borderColor: 'divider', |
| 196 | + overflowY: 'auto', |
| 197 | + }} |
| 198 | + > |
| 199 | + <List size="sm" sx={{ py: 1 }}> |
| 200 | + {visibleSections.map((key) => ( |
| 201 | + <ListItem key={key}> |
| 202 | + <ListItemButton |
| 203 | + selected={effectiveSection === key} |
| 204 | + onClick={() => setActiveSection(key)} |
| 205 | + > |
| 206 | + {SECTION_LABELS[key]} |
| 207 | + </ListItemButton> |
| 208 | + </ListItem> |
| 209 | + ))} |
| 210 | + </List> |
| 211 | + </Box> |
| 212 | + |
| 213 | + {/* Main content */} |
| 214 | + <Box sx={{ flex: 1, overflow: 'auto', p: 2 }}> |
| 215 | + {effectiveSection === 'overview' && <OverviewSection job={job} />} |
| 216 | + {effectiveSection === 'logs' && ( |
| 217 | + <LogsSection jobId={jobId} jobStatus={job?.status ?? ''} /> |
| 218 | + )} |
| 219 | + {effectiveSection === 'checkpoints' && ( |
| 220 | + <CheckpointsSection jobId={jobId} /> |
| 221 | + )} |
| 222 | + {effectiveSection === 'artifacts' && ( |
| 223 | + <ArtifactsSection jobId={jobId} /> |
| 224 | + )} |
| 225 | + {effectiveSection === 'evalResults' && ( |
| 226 | + <EvalResultsSection |
| 227 | + jobId={jobId} |
| 228 | + evalFiles={job?.job_data?.eval_results ?? []} |
| 229 | + /> |
| 230 | + )} |
| 231 | + {effectiveSection === 'sweepResults' && ( |
| 232 | + <SweepResultsSection jobId={jobId} /> |
| 233 | + )} |
| 234 | + </Box> |
| 235 | + </Box> |
| 236 | + </Box> |
| 237 | + ); |
| 238 | +} |
0 commit comments