Spaces:
Build error
Build error
File size: 14,965 Bytes
f842a06 4002561 f842a06 17c4592 f842a06 b3e5e1c f842a06 b3e5e1c 4002561 f842a06 1d5cfba f842a06 1d5cfba f842a06 17c4592 568e457 ed62931 17c4592 ed62931 f842a06 b3e5e1c f842a06 b3e5e1c f842a06 b3e5e1c 4002561 b3e5e1c f842a06 6736e17 f842a06 b3e5e1c 4002561 568e457 b3e5e1c f842a06 b3e5e1c 021b245 f842a06 b3e5e1c f842a06 b3e5e1c 6736e17 b3e5e1c 568e457 b3e5e1c f842a06 b3e5e1c 568e457 b3e5e1c f842a06 b3e5e1c f842a06 b3e5e1c f842a06 b3e5e1c 568e457 f842a06 b3e5e1c f842a06 b3e5e1c f842a06 b3e5e1c f842a06 b3e5e1c f842a06 b3e5e1c f842a06 b3e5e1c 568e457 b3e5e1c 568e457 b3e5e1c f842a06 b3e5e1c f842a06 b3e5e1c f842a06 b3e5e1c f842a06 b3e5e1c f842a06 b3e5e1c f842a06 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | import { useCallback, useEffect, useRef, useState } from 'react'
import { useNavigate, useParams } from 'react-router-dom'
import {
applyCorrections,
getHistory,
fetchMasterJson,
ApiError,
type PageMaster,
type VersionInfo,
} from '../lib/api.ts'
import Viewer from '../components/Viewer.tsx'
import {
RetroMenuBar,
RetroWindow,
RetroButton,
RetroTextarea,
RetroSelect,
RetroBadge,
} from '../components/retro'
type Panel = 'transcription' | 'commentary' | 'regions' | 'history'
const PANEL_LABELS: Record<Panel, string> = {
transcription: 'Transcription',
commentary: 'Commentaire',
regions: 'Regions',
history: 'Historique',
}
export default function Editor() {
const { pageId = '' } = useParams()
const navigate = useNavigate()
const [master, setMaster] = useState<PageMaster | null>(null)
const [history, setHistory] = useState<VersionInfo[]>([])
const [activePanel, setActivePanel] = useState<Panel>('transcription')
const [loading, setLoading] = useState(true)
const [saving, setSaving] = useState(false)
const [error, setError] = useState<string | null>(null)
const [saveError, setSaveError] = useState<string | null>(null)
const [saveSuccess, setSaveSuccess] = useState(false)
const [ocrText, setOcrText] = useState('')
const [commentaryPublic, setCommentaryPublic] = useState('')
const [commentaryScholarly, setCommentaryScholarly] = useState('')
const [editorialStatus, setEditorialStatus] = useState('')
const [regionValidations, setRegionValidations] = useState<Record<string, string>>({})
const successTimeout = useRef<ReturnType<typeof setTimeout> | null>(null)
// Nettoyage du timeout de succès lors du démontage du composant
useEffect(() => {
return () => {
if (successTimeout.current) clearTimeout(successTimeout.current)
}
}, [])
const loadData = useCallback(async () => {
setLoading(true)
setError(null)
try {
const [m, h] = await Promise.all([fetchMasterJson(pageId), getHistory(pageId)])
setMaster(m)
setHistory(h)
setOcrText(m.ocr?.diplomatic_text ?? '')
setCommentaryPublic(m.commentary?.public ?? '')
setCommentaryScholarly(m.commentary?.scholarly ?? '')
setEditorialStatus(m.editorial.status)
const ext = m.extensions as { region_validations?: Record<string, string> } | undefined
setRegionValidations(ext?.region_validations ?? {})
} catch (e: unknown) {
if (e instanceof ApiError && e.status === 404) {
setError('Cette page n\'a pas encore Γ©tΓ© analysΓ©e par l\'IA. Lancez le pipeline depuis Administration.')
} else {
const msg = e instanceof Error ? e.message : ''
setError(msg || 'Erreur de chargement')
}
} finally {
setLoading(false)
}
}, [pageId])
useEffect(() => {
void loadData()
}, [loadData])
const handleSave = async () => {
setSaving(true)
setSaveError(null)
setSaveSuccess(false)
try {
const updated = await applyCorrections(pageId, {
ocr_diplomatic_text: ocrText !== (master?.ocr?.diplomatic_text ?? '') ? ocrText : undefined,
editorial_status: editorialStatus !== master?.editorial.status ? editorialStatus : undefined,
commentary_public: commentaryPublic !== (master?.commentary?.public ?? '') ? commentaryPublic : undefined,
commentary_scholarly: commentaryScholarly !== (master?.commentary?.scholarly ?? '') ? commentaryScholarly : undefined,
region_validations: Object.keys(regionValidations).length > 0 ? regionValidations : undefined,
})
setMaster(updated)
const h = await getHistory(pageId)
setHistory(h)
setSaveSuccess(true)
if (successTimeout.current) clearTimeout(successTimeout.current)
successTimeout.current = setTimeout(() => setSaveSuccess(false), 3000)
} catch (e: unknown) {
setSaveError((e as Error).message)
} finally {
setSaving(false)
}
}
const handleRestore = async (version: number) => {
setSaving(true)
setSaveError(null)
try {
const updated = await applyCorrections(pageId, { restore_to_version: version })
setMaster(updated)
setOcrText(updated.ocr?.diplomatic_text ?? '')
setCommentaryPublic(updated.commentary?.public ?? '')
setCommentaryScholarly(updated.commentary?.scholarly ?? '')
setEditorialStatus(updated.editorial.status)
const h = await getHistory(pageId)
setHistory(h)
} catch (e: unknown) {
setSaveError((e as Error).message)
} finally {
setSaving(false)
}
}
const setRegionValidation = (regionId: string, val: string) => {
setRegionValidations((prev) => ({ ...prev, [regionId]: val }))
}
// ββ Loading / Error βββββββββββββββββββββββββββββββββββββββββββββββββ
if (loading) {
return (
<div className="min-h-screen bg-retro-dither flex items-center justify-center">
<RetroWindow title="Chargement" className="w-64">
<div className="p-4 text-retro-sm text-center">Chargement...</div>
</RetroWindow>
</div>
)
}
if (error) {
return (
<div className="min-h-screen bg-retro-dither flex items-center justify-center">
<RetroWindow title="Erreur" className="w-80">
<div className="p-4 text-retro-sm">
{error}
<div className="mt-2"><RetroButton onClick={() => navigate(-1)}>Retour</RetroButton></div>
</div>
</RetroWindow>
</div>
)
}
const iiifServiceUrl = master?.image?.iiif_service_url ?? null
const fallbackImageUrl = master?.image?.derivative_web ?? master?.image?.master ?? ''
const regions = master?.layout?.regions ?? []
return (
<div className="flex flex-col h-screen bg-retro-dither">
{/* ββ Menu bar βββββββββββββββββββββββββββββββββββββββββββββββββ */}
<RetroMenuBar
items={[
{ label: 'IIIF Studio', onClick: () => navigate('/') },
{ label: `Γditeur β ${master?.folio_label ?? pageId}` },
]}
right={
<div className="flex items-center gap-1">
{master && (
<span className="text-retro-xs px-2">
v{master.editorial.version} β {master.editorial.status}
</span>
)}
{saveSuccess && <RetroBadge variant="success">OK</RetroBadge>}
{saveError && <RetroBadge variant="error">Err</RetroBadge>}
<RetroButton
size="sm"
onClick={() => void handleSave()}
disabled={saving}
>
{saving ? 'Saving...' : 'Sauvegarder'}
</RetroButton>
</div>
}
/>
{/* ββ Main layout 50/50 ββββββββββββββββββββββββββββββββββββββββ */}
<div className="flex flex-1 min-h-0 overflow-hidden p-1 gap-1">
{/* ββ Viewer window (left) βββββββββββββββββββββββββββββββββ */}
<RetroWindow
title={`Image β ${master?.folio_label ?? pageId}`}
className="flex-1 min-w-0"
>
<div className="relative w-full h-full">
<Viewer iiifServiceUrl={iiifServiceUrl} fallbackImageUrl={fallbackImageUrl} onViewerReady={() => {}} />
{!iiifServiceUrl && !fallbackImageUrl && (
<div className="absolute inset-0 flex items-center justify-center bg-retro-gray text-retro-darkgray text-retro-sm">
AperΓ§u non disponible
</div>
)}
</div>
</RetroWindow>
{/* ββ Editor window (right) ββββββββββββββββββββββββββββββββ */}
<RetroWindow
title="Γditeur"
className="flex-1 min-w-0"
scrollable
>
<div className="flex flex-col">
{/* ββ Tab bar ββββββββββββββββββββββββββββββββββββββββ */}
<div className="flex shrink-0 border-b border-retro-black bg-retro-gray">
{(['transcription', 'commentary', 'regions', 'history'] as Panel[]).map((p) => (
<RetroButton
key={p}
size="sm"
pressed={activePanel === p}
onClick={() => setActivePanel(p)}
className="flex-1 border-0 border-r border-retro-darkgray last:border-r-0"
>
{PANEL_LABELS[p]}
</RetroButton>
))}
</div>
{/* ββ Panel content βββββββββββββββββββββββββββββββββββ */}
<div className="p-2">
{/* Transcription */}
{activePanel === 'transcription' && (
<div className="flex flex-col gap-2">
<RetroTextarea
label="Texte diplomatique (OCR)"
value={ocrText}
onChange={(e) => setOcrText(e.target.value)}
rows={12}
/>
<RetroSelect
label="Statut Γ©ditorial"
value={editorialStatus}
onChange={(e) => setEditorialStatus(e.target.value)}
options={[
{ value: 'machine_draft', label: 'machine_draft' },
{ value: 'needs_review', label: 'needs_review' },
{ value: 'reviewed', label: 'reviewed' },
{ value: 'validated', label: 'validated' },
{ value: 'published', label: 'published' },
]}
/>
{master?.ocr && (
<div className="text-retro-xs text-retro-darkgray">
Langue: {master.ocr.language} β Confiance: {(master.ocr.confidence * 100).toFixed(0)}%
</div>
)}
</div>
)}
{/* Commentary */}
{activePanel === 'commentary' && (
<div className="flex flex-col gap-2">
<RetroTextarea
label="Commentaire public"
value={commentaryPublic}
onChange={(e) => setCommentaryPublic(e.target.value)}
rows={6}
/>
<RetroTextarea
label="Commentaire savant"
value={commentaryScholarly}
onChange={(e) => setCommentaryScholarly(e.target.value)}
rows={8}
/>
</div>
)}
{/* Regions */}
{activePanel === 'regions' && (
<div className="flex flex-col gap-[2px]">
{regions.length === 0 ? (
<p className="text-retro-sm text-retro-darkgray p-2">Aucune rΓ©gion dΓ©tectΓ©e.</p>
) : (
regions.map((region) => {
const validation = regionValidations[region.id]
return (
<div
key={region.id}
className="
flex items-center justify-between
border border-retro-black p-2
bg-retro-white
"
>
<div>
<span className="text-retro-sm font-bold capitalize">
{region.type.replace(/_/g, ' ')}
</span>
<span className="ml-2 text-retro-xs text-retro-darkgray">
{region.id}
</span>
<div className="text-retro-xs text-retro-darkgray">
confiance: {(region.confidence * 100).toFixed(0)}%
</div>
</div>
<div className="flex gap-[2px] ml-2 shrink-0">
<RetroButton
size="sm"
pressed={validation === 'validated'}
onClick={() => setRegionValidation(region.id, 'validated')}
>
OK
</RetroButton>
<RetroButton
size="sm"
pressed={validation === 'rejected'}
onClick={() => setRegionValidation(region.id, 'rejected')}
>
X
</RetroButton>
</div>
</div>
)
})
)}
</div>
)}
{/* History */}
{activePanel === 'history' && (
<div className="flex flex-col gap-[2px]">
{history.length === 0 ? (
<p className="text-retro-sm text-retro-darkgray p-2">Aucune version archivΓ©e.</p>
) : (
history.map((v) => (
<div
key={v.version}
className="
flex items-center justify-between
border border-retro-black p-2
bg-retro-white
"
>
<div>
<span className="text-retro-sm font-bold">v{v.version}</span>
<RetroBadge className="ml-2">{v.status}</RetroBadge>
<div className="text-retro-xs text-retro-darkgray mt-[2px]">
{new Date(v.saved_at).toLocaleString('fr-FR')}
</div>
</div>
<RetroButton
size="sm"
onClick={() => void handleRestore(v.version)}
disabled={saving}
>
Restaurer
</RetroButton>
</div>
))
)}
</div>
)}
</div>
</div>
</RetroWindow>
</div>
</div>
)
}
|