branch backend + ajout du serveur backend, next, et sqlite
This commit is contained in:
98
backendia/dashboard/src/components/DatasetToolbar.tsx
Normal file
98
backendia/dashboard/src/components/DatasetToolbar.tsx
Normal file
@@ -0,0 +1,98 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Download, Archive, AlertTriangle, Check, RefreshCw } from "lucide-react";
|
||||
import { API_BASE_URL } from "@/lib/api";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
export default function DatasetToolbar() {
|
||||
const router = useRouter();
|
||||
const [isExporting, setIsExporting] = useState(false);
|
||||
const [isArchiving, setIsArchiving] = useState(false);
|
||||
const [showArchiveConfirm, setShowArchiveConfirm] = useState(false);
|
||||
|
||||
const handleExport = async () => {
|
||||
setIsExporting(true);
|
||||
try {
|
||||
// On ouvre simplement l'URL dans un nouvel onglet pour déclencher le téléchargement
|
||||
window.open(`${API_BASE_URL}/api/export/zip`, '_blank');
|
||||
} catch (error) {
|
||||
console.error("Export error:", error);
|
||||
} finally {
|
||||
setIsExporting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleArchive = async () => {
|
||||
setIsArchiving(true);
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/export/archive`, {
|
||||
method: "POST"
|
||||
});
|
||||
if (response.ok) {
|
||||
setShowArchiveConfirm(false);
|
||||
router.refresh(); // Rafraîchir la galerie
|
||||
} else {
|
||||
alert("Erreur lors de l'archivage");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Archive error:", error);
|
||||
alert("Erreur réseau");
|
||||
} finally {
|
||||
setIsArchiving(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="bg-slate-900 border border-slate-800 rounded-2xl p-6 flex flex-col md:flex-row items-center justify-between gap-6 shadow-xl shadow-indigo-500/5">
|
||||
<div className="space-y-1">
|
||||
<h3 className="font-bold text-lg flex items-center gap-2">
|
||||
<RefreshCw size={20} className="text-indigo-400" />
|
||||
Gestion du Dataset
|
||||
</h3>
|
||||
<p className="text-xs text-slate-500">Exportez vos sessions étiquetées en ZIP et archivez-les pour nettoyer la galerie.</p>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-3">
|
||||
<button
|
||||
onClick={handleExport}
|
||||
disabled={isExporting}
|
||||
className="flex items-center gap-2 bg-indigo-600 hover:bg-indigo-500 disabled:opacity-50 px-5 py-2.5 rounded-xl text-sm font-bold transition-all hover:scale-105 active:scale-95 shadow-lg shadow-indigo-600/20"
|
||||
>
|
||||
<Download size={18} />
|
||||
{isExporting ? "Préparation..." : "Télécharger ZIP"}
|
||||
</button>
|
||||
|
||||
{!showArchiveConfirm ? (
|
||||
<button
|
||||
onClick={() => setShowArchiveConfirm(true)}
|
||||
className="flex items-center gap-2 bg-slate-800 hover:bg-slate-700 px-5 py-2.5 rounded-xl text-sm font-bold transition-all border border-slate-700"
|
||||
>
|
||||
<Archive size={18} />
|
||||
Archiver les sessions
|
||||
</button>
|
||||
) : (
|
||||
<div className="flex items-center gap-2 animate-in fade-in slide-in-from-right-4 duration-300">
|
||||
<div className="flex items-center gap-2 px-4 py-2 bg-rose-500/10 border border-rose-500/20 rounded-xl text-rose-400 text-xs font-bold">
|
||||
<AlertTriangle size={14} />
|
||||
Vider la galerie ?
|
||||
</div>
|
||||
<button
|
||||
onClick={handleArchive}
|
||||
disabled={isArchiving}
|
||||
className="bg-rose-600 hover:bg-rose-500 p-2.5 rounded-xl transition-colors disabled:opacity-50"
|
||||
>
|
||||
<Check size={18} className="text-white" />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setShowArchiveConfirm(false)}
|
||||
className="bg-slate-800 hover:bg-slate-700 p-2.5 rounded-xl transition-colors"
|
||||
>
|
||||
<Archive size={18} className="text-slate-400" />
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
226
backendia/dashboard/src/components/PhotoEditor.tsx
Normal file
226
backendia/dashboard/src/components/PhotoEditor.tsx
Normal file
@@ -0,0 +1,226 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import PhotoOverlay from "./PhotoOverlay";
|
||||
import { Info, Calendar, User, Smartphone, Cpu, Save, Edit2, X, Trash2, Check, Tag } from "lucide-react";
|
||||
import { API_BASE_URL } from "@/lib/api";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
interface PhotoEditorProps {
|
||||
initialPhoto: any;
|
||||
}
|
||||
|
||||
const PREDEFINED_LABELS = ["bullet_hole", "tear", "miss", "rebound", "other"];
|
||||
|
||||
export default function PhotoEditor({ initialPhoto }: PhotoEditorProps) {
|
||||
const router = useRouter();
|
||||
const [isEditing, setIsEditing] = useState(false);
|
||||
const [photoData, setPhotoData] = useState(initialPhoto.data);
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
|
||||
const handleImpactsChange = (newImpacts: any[]) => {
|
||||
setPhotoData({
|
||||
...photoData,
|
||||
plotting: {
|
||||
...photoData.plotting,
|
||||
impacts: newImpacts
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const updateLabel = (index: number, label: string) => {
|
||||
const newImpacts = [...photoData.plotting.impacts];
|
||||
newImpacts[index] = { ...newImpacts[index], label };
|
||||
handleImpactsChange(newImpacts);
|
||||
};
|
||||
|
||||
const updateScore = (index: number, score: number) => {
|
||||
const newImpacts = [...photoData.plotting.impacts];
|
||||
newImpacts[index] = { ...newImpacts[index], score };
|
||||
handleImpactsChange(newImpacts);
|
||||
};
|
||||
|
||||
const handleSave = async () => {
|
||||
setIsSaving(true);
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/data/${initialPhoto.filename}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(photoData),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
setIsEditing(false);
|
||||
router.refresh();
|
||||
} else {
|
||||
alert("Erreur lors de la sauvegarde");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Save error:", error);
|
||||
alert("Erreur réseau");
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const impacts = photoData?.plotting?.impacts || [];
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-1 xl:grid-cols-3 gap-8">
|
||||
{/* Main View */}
|
||||
<div className="xl:col-span-2 space-y-4">
|
||||
<div className="flex items-center justify-between bg-slate-900/50 p-4 rounded-xl border border-slate-800">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className={`w-2 h-2 rounded-full ${isEditing ? 'bg-amber-500 animate-pulse' : 'bg-emerald-500'}`} />
|
||||
<span className="text-sm font-medium">
|
||||
{isEditing ? "Mode Édition Actif" : "Mode Visualisation"}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-2">
|
||||
{isEditing ? (
|
||||
<>
|
||||
<button
|
||||
onClick={() => {
|
||||
setPhotoData(initialPhoto.data);
|
||||
setIsEditing(false);
|
||||
}}
|
||||
className="flex items-center gap-2 bg-slate-800 hover:bg-slate-700 px-4 py-2 rounded-lg text-sm font-medium transition-colors"
|
||||
>
|
||||
<X size={18} />
|
||||
Annuler
|
||||
</button>
|
||||
<button
|
||||
onClick={handleSave}
|
||||
disabled={isSaving}
|
||||
className="flex items-center gap-2 bg-indigo-600 hover:bg-indigo-500 px-4 py-2 rounded-lg text-sm font-medium transition-colors disabled:opacity-50"
|
||||
>
|
||||
<Save size={18} />
|
||||
{isSaving ? "Enregistrement..." : "Enregistrer"}
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<button
|
||||
onClick={() => setIsEditing(true)}
|
||||
className="flex items-center gap-2 bg-indigo-600 hover:bg-indigo-500 px-4 py-2 rounded-lg text-sm font-medium transition-colors"
|
||||
>
|
||||
<Edit2 size={18} />
|
||||
Modifier les points
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<PhotoOverlay
|
||||
imageUrl={`${API_BASE_URL}${initialPhoto.imageUrl}`}
|
||||
impacts={impacts}
|
||||
targetCorners={photoData?.plotting?.target_corners || []}
|
||||
isEditing={isEditing}
|
||||
onImpactsChange={handleImpactsChange}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Sidebar Info & Labeling */}
|
||||
<div className="space-y-6">
|
||||
{/* Labeling Section (Only if editing or has impacts) */}
|
||||
<div className="bg-slate-900 border border-slate-800 rounded-2xl p-6 space-y-4">
|
||||
<div className="flex items-center justify-between pb-4 border-b border-slate-800">
|
||||
<div className="flex items-center gap-3">
|
||||
<Tag className="text-indigo-400" size={20} />
|
||||
<h3 className="font-bold text-lg">Étiquetage (Impacts)</h3>
|
||||
</div>
|
||||
<span className="bg-slate-800 text-slate-400 text-[10px] font-bold px-2 py-0.5 rounded">
|
||||
{impacts.length} TOTAL
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="space-y-3 max-h-[400px] overflow-auto pr-2 custom-scrollbar">
|
||||
{impacts.map((impact: any, index: number) => (
|
||||
<div key={impact.id} className="bg-slate-950 p-3 rounded-xl border border-slate-800 space-y-3 group">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-[10px] font-bold text-slate-500 uppercase">Impact #{impact.id}</span>
|
||||
{isEditing && (
|
||||
<button
|
||||
onClick={(e) => {
|
||||
const newImpacts = impacts.filter((_: any, i: number) => i !== index);
|
||||
handleImpactsChange(newImpacts);
|
||||
}}
|
||||
className="text-slate-600 hover:text-rose-500 transition-colors"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<div className="space-y-1">
|
||||
<label className="text-[10px] text-slate-500 font-bold uppercase">Label</label>
|
||||
{isEditing ? (
|
||||
<select
|
||||
value={impact.label}
|
||||
onChange={(e) => updateLabel(index, e.target.value)}
|
||||
className="w-full bg-slate-900 border border-slate-800 rounded px-2 py-1 text-xs outline-none focus:border-indigo-500"
|
||||
>
|
||||
{PREDEFINED_LABELS.map(l => <option key={l} value={l}>{l}</option>)}
|
||||
</select>
|
||||
) : (
|
||||
<div className="text-xs text-indigo-300 font-medium">{impact.label}</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<label className="text-[10px] text-slate-500 font-bold uppercase">Score</label>
|
||||
{isEditing ? (
|
||||
<input
|
||||
type="number"
|
||||
min="0"
|
||||
max="10"
|
||||
value={impact.score}
|
||||
onChange={(e) => updateScore(index, parseInt(e.target.value) || 0)}
|
||||
className="w-full bg-slate-900 border border-slate-800 rounded px-2 py-1 text-xs outline-none focus:border-indigo-500"
|
||||
/>
|
||||
) : (
|
||||
<div className="text-xs text-emerald-400 font-bold">{impact.score}</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
{impacts.length === 0 && (
|
||||
<p className="text-center py-8 text-xs text-slate-500 italic">
|
||||
Aucun impact détecté. {isEditing ? "Cliquez sur l'image pour en ajouter." : ""}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Metadata Card (Read Only) */}
|
||||
<div className="bg-slate-900/50 border border-slate-800 rounded-2xl p-6 space-y-4 opacity-80">
|
||||
<div className="flex items-center gap-3 pb-2 border-b border-slate-800">
|
||||
<Info className="text-slate-400" size={18} />
|
||||
<h3 className="font-bold text-sm text-slate-300">Métadonnées Session</h3>
|
||||
</div>
|
||||
<div className="space-y-3">
|
||||
<InfoItem icon={<Calendar size={14} />} label="Date" value={photoData ? new Date(photoData.timestamp).toLocaleString() : 'Inconnue'} />
|
||||
<InfoItem icon={<User size={14} />} label="Contributeur" value={photoData?.wallet_hash || 'Anonyme'} isMono />
|
||||
<InfoItem icon={<Smartphone size={14} />} label="Appareil" value={photoData?.device_info?.model || 'Inconnu'} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function InfoItem({ icon, label, value, isMono = false }: any) {
|
||||
return (
|
||||
<div className="flex flex-col gap-0.5">
|
||||
<div className="flex items-center gap-1.5 text-[10px] text-slate-500 font-bold uppercase">
|
||||
{icon}
|
||||
{label}
|
||||
</div>
|
||||
<p className={`text-xs text-slate-300 ${isMono ? 'font-mono truncate' : ''}`}>
|
||||
{value}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
183
backendia/dashboard/src/components/PhotoOverlay.tsx
Normal file
183
backendia/dashboard/src/components/PhotoOverlay.tsx
Normal file
@@ -0,0 +1,183 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
interface Point {
|
||||
norm_x: number;
|
||||
norm_y: number;
|
||||
}
|
||||
|
||||
interface Impact {
|
||||
id: number;
|
||||
label: string;
|
||||
score: number;
|
||||
coords: Point;
|
||||
}
|
||||
|
||||
interface PhotoOverlayProps {
|
||||
imageUrl: string;
|
||||
impacts: Impact[];
|
||||
targetCorners: Point[];
|
||||
isEditing?: boolean;
|
||||
onImpactsChange?: (impacts: Impact[]) => void;
|
||||
}
|
||||
|
||||
export default function PhotoOverlay({
|
||||
imageUrl,
|
||||
impacts,
|
||||
targetCorners,
|
||||
isEditing = false,
|
||||
onImpactsChange
|
||||
}: PhotoOverlayProps) {
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
|
||||
const [dragIndex, setDragIndex] = useState<number | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const updateDimensions = () => {
|
||||
if (containerRef.current) {
|
||||
setDimensions({
|
||||
width: containerRef.current.offsetWidth,
|
||||
height: containerRef.current.offsetHeight,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
updateDimensions();
|
||||
window.addEventListener("resize", updateDimensions);
|
||||
return () => window.removeEventListener("resize", updateDimensions);
|
||||
}, []);
|
||||
|
||||
const handleImageClick = (e: React.MouseEvent) => {
|
||||
if (!isEditing || !onImpactsChange || dragIndex !== null) return;
|
||||
|
||||
const rect = containerRef.current?.getBoundingClientRect();
|
||||
if (!rect) return;
|
||||
|
||||
// Calculer les coordonnées normalisées
|
||||
const x = (e.clientX - rect.left) / dimensions.width;
|
||||
const y = (e.clientY - rect.top) / dimensions.height;
|
||||
|
||||
// Créer un nouvel impact
|
||||
const newImpact: Impact = {
|
||||
id: impacts.length > 0 ? Math.max(...impacts.map(i => i.id)) + 1 : 1,
|
||||
label: "bullet_hole",
|
||||
score: 10,
|
||||
coords: { norm_x: x, norm_y: y }
|
||||
};
|
||||
|
||||
onImpactsChange([...impacts, newImpact]);
|
||||
};
|
||||
|
||||
const handleMouseMove = (e: React.MouseEvent) => {
|
||||
if (dragIndex === null || !onImpactsChange) return;
|
||||
|
||||
const rect = containerRef.current?.getBoundingClientRect();
|
||||
if (!rect) return;
|
||||
|
||||
const x = (e.clientX - rect.left) / dimensions.width;
|
||||
const y = (e.clientY - rect.top) / dimensions.height;
|
||||
|
||||
const newImpacts = [...impacts];
|
||||
newImpacts[dragIndex] = {
|
||||
...newImpacts[dragIndex],
|
||||
coords: { norm_x: x, norm_y: y }
|
||||
};
|
||||
|
||||
onImpactsChange(newImpacts);
|
||||
};
|
||||
|
||||
const deleteImpact = (e: React.MouseEvent, index: number) => {
|
||||
e.stopPropagation();
|
||||
if (!isEditing || !onImpactsChange) return;
|
||||
const newImpacts = impacts.filter((_, i) => i !== index);
|
||||
onImpactsChange(newImpacts);
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={containerRef}
|
||||
className={`relative w-full flex items-center justify-center bg-slate-900 rounded-2xl overflow-hidden border border-slate-800 shadow-2xl min-h-[400px] ${isEditing ? 'cursor-crosshair' : ''}`}
|
||||
onClick={handleImageClick}
|
||||
onMouseMove={handleMouseMove}
|
||||
onMouseUp={() => setDragIndex(null)}
|
||||
onMouseLeave={() => setDragIndex(null)}
|
||||
>
|
||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||
<img
|
||||
src={imageUrl}
|
||||
alt="Target Analysis"
|
||||
className="max-w-full max-h-[80vh] object-contain select-none"
|
||||
draggable={false}
|
||||
onLoad={(e) => {
|
||||
const img = e.currentTarget;
|
||||
setDimensions({ width: img.clientWidth, height: img.clientHeight });
|
||||
}}
|
||||
/>
|
||||
|
||||
{dimensions.width > 0 && (
|
||||
<svg
|
||||
className="absolute pointer-events-none"
|
||||
viewBox={`0 0 ${dimensions.width} ${dimensions.height}`}
|
||||
style={{ width: dimensions.width, height: dimensions.height }}
|
||||
>
|
||||
{/* Draw Target Box if corners exist */}
|
||||
{targetCorners && targetCorners.length === 4 && (
|
||||
<polygon
|
||||
points={targetCorners.map(c => `${c.norm_x * dimensions.width},${c.norm_y * dimensions.height}`).join(' ')}
|
||||
fill="rgba(79, 70, 229, 0.15)"
|
||||
stroke="#6366f1"
|
||||
strokeWidth="2"
|
||||
strokeDasharray="4 2"
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Draw Impacts */}
|
||||
{impacts.map((impact, index) => (
|
||||
<g key={impact.id}>
|
||||
{/* Glow effect */}
|
||||
<circle
|
||||
cx={impact.coords.norm_x * dimensions.width}
|
||||
cy={impact.coords.norm_y * dimensions.height}
|
||||
r={isEditing ? "12" : "8"}
|
||||
fill={isEditing ? "rgba(244, 63, 94, 0.2)" : "rgba(244, 63, 94, 0.3)"}
|
||||
className={isEditing ? 'pointer-events-auto cursor-move' : ''}
|
||||
onMouseDown={(e) => {
|
||||
if (!isEditing) return;
|
||||
e.stopPropagation();
|
||||
setDragIndex(index);
|
||||
}}
|
||||
onContextMenu={(e) => deleteImpact(e, index)}
|
||||
/>
|
||||
{/* Main circle */}
|
||||
<circle
|
||||
cx={impact.coords.norm_x * dimensions.width}
|
||||
cy={impact.coords.norm_y * dimensions.height}
|
||||
r="5"
|
||||
fill="#f43f5e"
|
||||
stroke="white"
|
||||
strokeWidth="1.5"
|
||||
/>
|
||||
{/* Score text */}
|
||||
<text
|
||||
x={impact.coords.norm_x * dimensions.width}
|
||||
y={impact.coords.norm_y * dimensions.height - 10}
|
||||
className="text-[12px] font-bold fill-white text-center select-none"
|
||||
textAnchor="middle"
|
||||
style={{ textShadow: '0 0 4px rgba(0,0,0,0.9), 1px 1px 0 rgba(0,0,0,0.9)' }}
|
||||
>
|
||||
{impact.score}
|
||||
</text>
|
||||
</g>
|
||||
))}
|
||||
</svg>
|
||||
)}
|
||||
|
||||
{isEditing && (
|
||||
<div className="absolute bottom-4 right-4 bg-slate-950/80 backdrop-blur-md px-3 py-1.5 rounded-full text-[10px] font-bold text-indigo-400 border border-indigo-500/30">
|
||||
MODE ÉDITION : CLIC POUR AJOUTER • DRAG POUR DÉPLACER • CLIC DROIT POUR SUPPRIMER
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user