fix: implement liquid glass theme, custom media deletion modal, and unload recording beacon cleanup
CD — Kai-Sa-Cam Deployment / deploy (push) Successful in 10s
CD — Kai-Sa-Cam Deployment / deploy (push) Successful in 10s
This commit is contained in:
@@ -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<MediaItem[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [exporting, setExporting] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [media, setMedia] = useState<MediaItem[]>([]);
|
||||
const [selectedItem, setSelectedItem] = useState<MediaItem | null>(null);
|
||||
const [itemToDelete, setItemToDelete] = useState<string | null>(null);
|
||||
const [error, setError] = useState<string | null>(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 (
|
||||
<div className="flex-1 flex items-center justify-center text-gray-400">
|
||||
Chargement des médias...
|
||||
</div>
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex-1 flex flex-col p-4 gap-4 overflow-auto">
|
||||
<div className="flex items-center justify-between">
|
||||
<h2 className="text-xl font-semibold">Médias ({media.length})</h2>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
onClick={handleExport}
|
||||
disabled={exporting || media.length === 0}
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50"
|
||||
<div className="flex-1 flex flex-col p-4 gap-4 overflow-y-auto">
|
||||
{error && <div className="text-red-400 text-sm text-center">{error}</div>}
|
||||
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
|
||||
{media.map((item) => (
|
||||
<div
|
||||
key={item.filename}
|
||||
className="glass-card rounded-xl overflow-hidden flex flex-col"
|
||||
>
|
||||
{exporting ? 'Export ZIP...' : 'Exporter ZIP (supprime après)'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="text-red-400 text-sm mb-4 p-2 bg-red-900/20 rounded">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{media.length === 0 ? (
|
||||
<div className="flex-1 flex items-center justify-center text-gray-500">
|
||||
Aucun média. Lancez un enregistrement ou une capture.
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
{media.map((item) => (
|
||||
<div key={item.filename} className="bg-gray-800 rounded-lg overflow-hidden border border-gray-700 flex flex-col">
|
||||
{item.type === 'video' ? (
|
||||
<video
|
||||
src={item.url}
|
||||
controls
|
||||
className="w-full aspect-video bg-black"
|
||||
preload="metadata"
|
||||
/>
|
||||
<div
|
||||
className="relative aspect-video bg-black/40 flex items-center justify-center cursor-pointer"
|
||||
onClick={() => setSelectedItem(item)}
|
||||
>
|
||||
{item.type === "video" ? (
|
||||
<video src={item.url} className="w-full h-full object-cover" />
|
||||
) : (
|
||||
<img
|
||||
src={item.url}
|
||||
alt={item.filename}
|
||||
className="w-full aspect-video object-cover bg-gray-900"
|
||||
className="w-full h-full object-cover"
|
||||
/>
|
||||
)}
|
||||
<div className="p-3 flex flex-col gap-2">
|
||||
<div className="flex items-center justify-between text-sm">
|
||||
<span className="truncate font-medium">{item.filename}</span>
|
||||
<span className="text-gray-400">{formatSize(item.size)}</span>
|
||||
</div>
|
||||
<div className="text-xs text-gray-500">{formatDate(item.mtime)}</div>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
onClick={() => handleDownload(item.filename)}
|
||||
className="flex-1 py-2 px-3 bg-blue-600 text-white rounded hover:bg-blue-700 text-sm"
|
||||
>
|
||||
Télécharger
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleDelete(item.filename)}
|
||||
className="flex-1 py-2 px-3 bg-red-600 text-white rounded hover:bg-red-700 text-sm"
|
||||
>
|
||||
Supprimer
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
<div className="p-3 flex items-center justify-between gap-2">
|
||||
<span className="text-xs truncate text-zinc-300">
|
||||
{item.filename}
|
||||
</span>
|
||||
<button
|
||||
onClick={() => setItemToDelete(item.filename)}
|
||||
className="px-2 py-1 text-xs bg-red-500/20 text-red-400 rounded-lg hover:bg-red-500/30 transition-colors"
|
||||
>
|
||||
Supprimer
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{itemToDelete && (
|
||||
<div className="fixed inset-0 bg-black/60 backdrop-blur-sm flex items-center justify-center z-50 p-4">
|
||||
<div className="glass-card p-6 rounded-2xl max-w-sm w-full flex flex-col gap-4 border border-white/10">
|
||||
<h3 className="text-lg font-semibold text-white">
|
||||
Confirmer la suppression
|
||||
</h3>
|
||||
<p className="text-sm text-zinc-400">
|
||||
Voulez-vous vraiment supprimer ce fichier ?
|
||||
</p>
|
||||
<div className="flex gap-2 justify-end">
|
||||
<button
|
||||
onClick={() => setItemToDelete(null)}
|
||||
className="px-4 py-2 text-sm glass-button rounded-xl text-zinc-300"
|
||||
>
|
||||
Annuler
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleDelete(itemToDelete)}
|
||||
className="px-4 py-2 text-sm bg-red-500 text-white rounded-xl hover:bg-red-600 transition-colors"
|
||||
>
|
||||
Confirmer
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{selectedItem && (
|
||||
<div
|
||||
className="fixed inset-0 bg-black/80 backdrop-blur-md flex items-center justify-center z-50 p-4"
|
||||
onClick={() => setSelectedItem(null)}
|
||||
>
|
||||
<div
|
||||
className="relative max-w-4xl w-full max-h-[90vh] flex items-center justify-center"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
{selectedItem.type === "video" ? (
|
||||
<video
|
||||
src={selectedItem.url}
|
||||
controls
|
||||
autoPlay
|
||||
className="max-w-full max-h-[85vh] rounded-2xl"
|
||||
/>
|
||||
) : (
|
||||
<img
|
||||
src={selectedItem.url}
|
||||
alt={selectedItem.filename}
|
||||
className="max-w-full max-h-[85vh] rounded-2xl object-contain"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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<string | null>(null)
|
||||
const intervalRef = useRef<number>()
|
||||
const startTimeRef = useRef<number>()
|
||||
|
||||
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<string | null>(null);
|
||||
const intervalRef = useRef<number>();
|
||||
const startTimeRef = useRef<number>();
|
||||
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 }
|
||||
}
|
||||
return { isRecording, error, startRecording, stopRecording };
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user