import { useRef } from "react"; import { FileText, Upload, X } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useToast } from "@/hooks/use-toast"; import { formatSrtTime, parseSrt, srtEnd, type SrtCue } from "@shared/srt"; const MAX_SRT_BYTES = 512 * 1024; export interface SrtFile { name: string; cues: SrtCue[]; } interface SrtUploadProps { value: SrtFile | null; onChange: (value: SrtFile | null) => void; } /** * Import d'un fichier de sous-titres SRT. Tant qu'un fichier est chargé, il * remplace le texte libre : la voix lit chaque sous-titre à son instant. */ export function SrtUpload({ value, onChange }: SrtUploadProps) { const { toast } = useToast(); const inputRef = useRef(null); const handleFile = async (file: File | undefined) => { if (!file) return; try { if (file.size > MAX_SRT_BYTES) throw new Error("Fichier SRT trop volumineux (512 Ko au plus)"); const cues = parseSrt(await file.text()); onChange({ name: file.name, cues }); toast({ title: "Sous-titres importés", description: `${cues.length} sous-titre(s) · ${formatSrtTime(srtEnd(cues))}` }); } catch (error) { toast({ title: "Fichier SRT invalide", description: error instanceof Error ? error.message : "Lecture impossible", variant: "destructive", }); } finally { if (inputRef.current) inputRef.current.value = ""; } }; return (
handleFile(e.target.files?.[0])} /> {value ? (
{value.name}

{value.cues.length} sous-titre(s) · jusqu'à {formatSrtTime(srtEnd(value.cues))}. La voix lira chaque sous-titre à son instant et accélérera si besoin pour tenir dans sa durée.

    {value.cues.map((cue, i) => (
  • {formatSrtTime(cue.start)}–{formatSrtTime(cue.end)} {cue.text}
  • ))}
) : ( )}
); }