diff --git a/apps/kai-sa-cam/src/components/MediaGallery.tsx b/apps/kai-sa-cam/src/components/MediaGallery.tsx index 6f8366e..4c77c70 100644 --- a/apps/kai-sa-cam/src/components/MediaGallery.tsx +++ b/apps/kai-sa-cam/src/components/MediaGallery.tsx @@ -1,180 +1,136 @@ -import { useState, useEffect } from 'react' +import { useState, useEffect } from "react"; interface MediaItem { - filename: string - type: 'video' | 'image' - size: number - mtime: string - url: string + filename: string; + type: "video" | "image"; + size: number; + mtime: string; + url: string; } export function MediaGallery() { - const [media, setMedia] = useState([]) - const [loading, setLoading] = useState(true) - const [exporting, setExporting] = useState(false) - const [error, setError] = useState(null) + const [media, setMedia] = useState([]); + const [selectedItem, setSelectedItem] = useState(null); + const [itemToDelete, setItemToDelete] = useState(null); + const [error, setError] = useState(null); const fetchMedia = async () => { try { - const res = await fetch('/api/media') - if (!res.ok) throw new Error('Failed to fetch media') - const data = await res.json() - setMedia(data) + const res = await fetch("/api/media"); + if (!res.ok) throw new Error("Erreur chargement médias"); + const data = await res.json(); + setMedia(data); } catch (e) { - console.error('Media fetch error:', e) - } finally { - setLoading(false) + setError("Impossible de charger les médias"); } - } + }; useEffect(() => { - fetchMedia() - }, []) - - const handleDownload = async (filename: string) => { - try { - const res = await fetch(`/api/media/${filename}?download=1`) - if (!res.ok) throw new Error('Download failed') - const blob = await res.blob() - const url = window.URL.createObjectURL(blob) - const a = document.createElement('a') - a.href = url - a.download = filename - a.click() - window.URL.revokeObjectURL(url) - } catch (e) { - console.error('Download error:', e) - setError('Téléchargement échoué') - } - } + fetchMedia(); + }, []); const handleDelete = async (filename: string) => { - if (!confirm(`Supprimer ${filename} ?`)) return try { - const res = await fetch(`/api/media/${filename}`, { method: 'DELETE' }) - if (!res.ok) throw new Error('Delete failed') - setMedia(prev => prev.filter(m => m.filename !== filename)) + const res = await fetch(`/api/media/${filename}`, { method: "DELETE" }); + if (!res.ok) throw new Error("Suppression échouée"); + setMedia((prev) => prev.filter((m) => m.filename !== filename)); + setItemToDelete(null); } catch (e) { - console.error('Delete error:', e) - setError('Suppression échouée') + setError("Erreur lors de la suppression"); } - } - - const handleExport = async () => { - setExporting(true) - setError(null) - try { - const res = await fetch('/api/media/export', { method: 'POST' }) - if (!res.ok) throw new Error('Export failed') - const blob = await res.blob() - const url = window.URL.createObjectURL(blob) - const a = document.createElement('a') - a.href = url - a.download = `kai-sa-cam-media-${new Date().toISOString().split('T')[0]}.zip` - a.click() - window.URL.revokeObjectURL(url) - // After successful export, the server deletes the files, so refresh - fetchMedia() - } catch (e) { - console.error('Export error:', e) - setError('Export ZIP échoué') - } finally { - setExporting(false) - } - } - - const formatSize = (bytes: number) => { - if (bytes < 1024) return `${bytes} B` - if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB` - return `${(bytes / 1024 / 1024).toFixed(1)} MB` - } - - const formatDate = (iso: string) => { - return new Date(iso).toLocaleString('fr-FR', { - day: '2-digit', - month: '2-digit', - year: 'numeric', - hour: '2-digit', - minute: '2-digit', - }) - } - - if (loading) { - return ( -
- Chargement des médias... -
- ) - } + }; return ( -
-
-

Médias ({media.length})

-
- -
-
- - {error && ( -
- {error} -
- )} - - {media.length === 0 ? ( -
- Aucun média. Lancez un enregistrement ou une capture. -
- ) : ( -
- {media.map((item) => ( -
- {item.type === 'video' ? ( -
+ ))} +
+ + {itemToDelete && ( +
+
+

+ Confirmer la suppression +

+

+ Voulez-vous vraiment supprimer ce fichier ? +

+
+ + +
+
+
+ )} + + {selectedItem && ( +
setSelectedItem(null)} + > +
e.stopPropagation()} + > + {selectedItem.type === "video" ? ( +
)}
- ) -} \ No newline at end of file + ); +} diff --git a/apps/kai-sa-cam/src/hooks/useRecorder.ts b/apps/kai-sa-cam/src/hooks/useRecorder.ts index ae182af..9553189 100644 --- a/apps/kai-sa-cam/src/hooks/useRecorder.ts +++ b/apps/kai-sa-cam/src/hooks/useRecorder.ts @@ -1,61 +1,80 @@ -import { useState, useCallback, useRef, useEffect } from 'react' +import { useState, useCallback, useRef, useEffect } from "react"; interface UseRecorderOptions { - onStart?: () => void - onStop?: () => void - onTimeUpdate?: (seconds: number) => void + onStart?: () => void; + onStop?: () => void; + onTimeUpdate?: (seconds: number) => void; } -export function useRecorder({ onStart, onStop, onTimeUpdate }: UseRecorderOptions) { - const [isRecording, setIsRecording] = useState(false) - const [error, setError] = useState(null) - const intervalRef = useRef() - const startTimeRef = useRef() - - const startRecording = useCallback(async () => { - setError(null) - try { - const res = await fetch('/api/recordings/start', { method: 'POST' }) - if (!res.ok) { - const data = await res.json() - throw new Error(data.error || 'Failed to start recording') - } - setIsRecording(true) - startTimeRef.current = Date.now() - onStart?.() - intervalRef.current = window.setInterval(() => { - onTimeUpdate?.(Math.floor((Date.now() - (startTimeRef.current || Date.now())) / 1000)) - }, 1000) - } catch (e) { - setError(e instanceof Error ? e.message : 'Erreur démarrage enregistrement') - } - }, [onStart, onTimeUpdate]) +export function useRecorder({ + onStart, + onStop, + onTimeUpdate, +}: UseRecorderOptions) { + const [isRecording, setIsRecording] = useState(false); + const [error, setError] = useState(null); + const intervalRef = useRef(); + const startTimeRef = useRef(); + const isRecordingRef = useRef(isRecording); + isRecordingRef.current = isRecording; const stopRecording = useCallback(async () => { - setError(null) + setError(null); try { - const res = await fetch('/api/recordings/stop', { method: 'POST' }) + const res = await fetch("/api/recordings/stop", { method: "POST" }); if (!res.ok) { - const data = await res.json() - throw new Error(data.error || 'Failed to stop recording') + const data = await res.json(); + throw new Error(data.error || "Failed to stop recording"); } - setIsRecording(false) + setIsRecording(false); if (intervalRef.current) { - clearInterval(intervalRef.current) + clearInterval(intervalRef.current); } - onStop?.() + onStop?.(); } catch (e) { - setError(e instanceof Error ? e.message : 'Erreur arrêt enregistrement') + setError(e instanceof Error ? e.message : "Erreur arrêt enregistrement"); } - }, [onStop]) + }, [onStop]); + + const startRecording = useCallback(async () => { + setError(null); + try { + const res = await fetch("/api/recordings/start", { method: "POST" }); + if (!res.ok) { + const data = await res.json(); + throw new Error(data.error || "Failed to start recording"); + } + setIsRecording(true); + startTimeRef.current = Date.now(); + onStart?.(); + intervalRef.current = window.setInterval(() => { + onTimeUpdate?.( + Math.floor( + (Date.now() - (startTimeRef.current || Date.now())) / 1000, + ), + ); + }, 1000); + } catch (e) { + setError( + e instanceof Error ? e.message : "Erreur démarrage enregistrement", + ); + } + }, [onStart, onTimeUpdate]); useEffect(() => { - return () => { - if (intervalRef.current) { - clearInterval(intervalRef.current) + const handleBeforeUnload = () => { + if (isRecordingRef.current) { + navigator.sendBeacon("/api/recordings/stop"); } - } - }, []) + }; + window.addEventListener("beforeunload", handleBeforeUnload); + return () => { + window.removeEventListener("beforeunload", handleBeforeUnload); + if (intervalRef.current) { + clearInterval(intervalRef.current); + } + }; + }, []); - return { isRecording, error, startRecording, stopRecording } -} \ No newline at end of file + return { isRecording, error, startRecording, stopRecording }; +} diff --git a/apps/kai-sa-cam/src/index.css b/apps/kai-sa-cam/src/index.css index f72818b..99ef0c8 100644 --- a/apps/kai-sa-cam/src/index.css +++ b/apps/kai-sa-cam/src/index.css @@ -8,12 +8,36 @@ body { margin: 0; - font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; - background: #111; - color: #eee; + font-family: + -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", + Arial, sans-serif; + background: #09090b; + color: #f4f4f5; } @layer utilities { + .glass { + background: rgba(255, 255, 255, 0.03); + backdrop-filter: blur(16px); + -webkit-backdrop-filter: blur(16px); + border: 1px solid rgba(255, 255, 255, 0.08); + } + .glass-card { + background: rgba(24, 24, 27, 0.6); + backdrop-filter: blur(20px); + -webkit-backdrop-filter: blur(20px); + border: 1px solid rgba(255, 255, 255, 0.1); + } + .glass-button { + background: rgba(255, 255, 255, 0.05); + backdrop-filter: blur(10px); + border: 1px solid rgba(255, 255, 255, 0.1); + transition: all 0.2s ease; + } + .glass-button:hover { + background: rgba(255, 255, 255, 0.1); + border-color: rgba(255, 255, 255, 0.2); + } .no-scrollbar::-webkit-scrollbar { display: none; } @@ -21,4 +45,4 @@ body { -ms-overflow-style: none; scrollbar-width: none; } -} \ No newline at end of file +}