Redesign
This commit is contained in:
1 parent
4beb27e618
commit
76de333507
30 files changed
+5047
-2113
No files matched your search
+113
-98
@@ -1,27 +1,35 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useNavigate, useParams, useLocation } from 'react-router-dom';
|
||||
import { ArrowLeft, Server, HardDrive, Trash2, Save, Check, Pencil } from 'lucide-react';
|
||||
import { useNavigate, useParams, useLocation, Link } from 'react-router-dom';
|
||||
import { ArrowLeft, Server, HardDrive, Trash2, Save, Check, Pencil, X } from 'lucide-react';
|
||||
import type { LogService } from '../types';
|
||||
import LogTerminal from '../components/LogTerminal';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
import { DockerIcon } from '../components/icons/DockerIcon';
|
||||
|
||||
function DeleteWithConfirmation({ onDelete, serviceName }: { onDelete: () => void, serviceName: string }) {
|
||||
const [showConfirm, setShowConfirm] = useState(false);
|
||||
|
||||
if (showConfirm) {
|
||||
return (
|
||||
<div className="delete-confirm-box">
|
||||
<span>Supprimer {serviceName} ?</span>
|
||||
<button className="btn btn-danger btn-sm" onClick={onDelete}>Oui</button>
|
||||
<button className="btn btn-sm" onClick={() => setShowConfirm(false)}>Non</button>
|
||||
<div className="flex items-center gap-3 bg-destructive/10 p-1.5 px-3 rounded-xl border border-destructive/30 animate-in zoom-in-95 duration-200">
|
||||
<span className="text-sm font-semibold text-destructive">Supprimer {serviceName} ?</span>
|
||||
<div className="flex gap-1">
|
||||
<Button variant="destructive" size="sm" className="h-8 px-3 text-xs font-bold rounded-lg" onClick={onDelete}>Confirmer</Button>
|
||||
<Button variant="ghost" size="sm" className="h-8 px-3 text-xs rounded-lg" onClick={() => setShowConfirm(false)}>Annuler</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<button className="btn btn-danger-outline btn-sm" onClick={() => setShowConfirm(true)}>
|
||||
<Trash2 size={16} />
|
||||
<span>Supprimer</span>
|
||||
</button>
|
||||
<Button variant="outline" size="sm" onClick={() => setShowConfirm(true)} className="gap-2 border-destructive/30 text-destructive hover:bg-destructive/10 hover:border-destructive/40 rounded-xl">
|
||||
<Trash2 size={15} />
|
||||
<span className="hidden sm:inline">Supprimer</span>
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -36,11 +44,10 @@ export default function ServiceView() {
|
||||
const [isSaved, setIsSaved] = useState(false);
|
||||
const [isEditingName, setIsEditingName] = useState(false);
|
||||
const [tempName, setTempName] = useState('');
|
||||
const isTransitioningRef = useState(false); // Pour bloquer les redirects pendant la sauvegarde
|
||||
const [isTransitioning, setIsTransitioning] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchServiceAndLogs = async () => {
|
||||
// Si on a déjà les données via la navigation (state), on économise un fetch et on évite les redirects
|
||||
if (service && (service.id === Number(id) || (id === 'ephemeral' && typeof service.id === 'string'))) {
|
||||
setLoading(false);
|
||||
return;
|
||||
@@ -49,7 +56,6 @@ export default function ServiceView() {
|
||||
try {
|
||||
const apiUrl = import.meta.env.VITE_API_URL || 'http://localhost:3001';
|
||||
|
||||
// Mode Éphémère
|
||||
if (id === 'ephemeral') {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const type = params.get('type') || 'docker';
|
||||
@@ -61,7 +67,6 @@ export default function ServiceView() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch des logs via le nouvel endpoint éphémère
|
||||
const logsRes = await fetch(`${apiUrl}/api/logs/ephemeral?type=${type}&target=${target}&name=${encodeURIComponent(name)}`, {
|
||||
credentials: 'include'
|
||||
});
|
||||
@@ -80,14 +85,9 @@ export default function ServiceView() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Mode Classique (Base de données)
|
||||
const [svcRes, logsRes] = await Promise.all([
|
||||
fetch(`${apiUrl}/api/services`, {
|
||||
credentials: 'include'
|
||||
}),
|
||||
fetch(`${apiUrl}/api/services/${id}/logs`, {
|
||||
credentials: 'include'
|
||||
})
|
||||
fetch(`${apiUrl}/api/services`, { credentials: 'include' }),
|
||||
fetch(`${apiUrl}/api/services/${id}/logs`, { credentials: 'include' })
|
||||
]);
|
||||
|
||||
const allServices = await svcRes.json();
|
||||
@@ -98,8 +98,11 @@ export default function ServiceView() {
|
||||
if (found) {
|
||||
setService(found);
|
||||
setInitialLogs(logs);
|
||||
updateRecentHistory(Number(id));
|
||||
} else if (!isTransitioningRef[0]) { // Ne pas rediriger si on est en train de sauvegarder
|
||||
const recents: number[] = JSON.parse(localStorage.getItem('recentServiceIds') || '[]');
|
||||
const filtered = recents.filter(rid => rid !== Number(id));
|
||||
const updated = [Number(id), ...filtered].slice(0, 5);
|
||||
localStorage.setItem('recentServiceIds', JSON.stringify(updated));
|
||||
} else if (!isTransitioning) {
|
||||
navigate('/');
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -112,7 +115,6 @@ export default function ServiceView() {
|
||||
fetchServiceAndLogs();
|
||||
|
||||
return () => {
|
||||
// Cleanup de l'éphémère si nécessaire
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const target = params.get('target');
|
||||
if (id === 'ephemeral' && target) {
|
||||
@@ -125,14 +127,6 @@ export default function ServiceView() {
|
||||
};
|
||||
}, [id, window.location.search]);
|
||||
|
||||
const updateRecentHistory = (id: number) => {
|
||||
const recents: number[] = JSON.parse(localStorage.getItem('recentServiceIds') || '[]');
|
||||
// Filtrer si déjà présent et mettre au début
|
||||
const filtered = recents.filter(rid => rid !== id);
|
||||
const updated = [id, ...filtered].slice(0, 5); // Garder les 5 derniers
|
||||
localStorage.setItem('recentServiceIds', JSON.stringify(updated));
|
||||
};
|
||||
|
||||
const handleSaveName = async () => {
|
||||
if (!service || !tempName.trim() || tempName === service.name) {
|
||||
setIsEditingName(false);
|
||||
@@ -141,8 +135,6 @@ export default function ServiceView() {
|
||||
|
||||
try {
|
||||
const apiUrl = import.meta.env.VITE_API_URL || 'http://localhost:3001';
|
||||
|
||||
// Si on est en mode persistant, on sauve en base
|
||||
if (typeof service.id === 'number') {
|
||||
const res = await fetch(`${apiUrl}/api/services/${service.id}`, {
|
||||
method: 'PATCH',
|
||||
@@ -150,11 +142,9 @@ export default function ServiceView() {
|
||||
body: JSON.stringify({ name: tempName.trim() }),
|
||||
credentials: 'include'
|
||||
});
|
||||
if (!res.ok) throw new Error('Erreur lors de la mise à jour du nom');
|
||||
const updated = await res.json();
|
||||
setService(updated);
|
||||
if (!res.ok) throw new Error('Erreur');
|
||||
setService(await res.json());
|
||||
} else {
|
||||
// En mode éphémère, on met juste à jour l'état local
|
||||
setService({ ...service, name: tempName.trim() });
|
||||
}
|
||||
setIsEditingName(false);
|
||||
@@ -167,6 +157,7 @@ export default function ServiceView() {
|
||||
const handleSave = async () => {
|
||||
if (!service || isSaving) return;
|
||||
setIsSaving(true);
|
||||
setIsTransitioning(true);
|
||||
try {
|
||||
const apiUrl = import.meta.env.VITE_API_URL || 'http://localhost:3001';
|
||||
const res = await fetch(`${apiUrl}/api/services`, {
|
||||
@@ -183,17 +174,14 @@ export default function ServiceView() {
|
||||
|
||||
const savedService = await res.json();
|
||||
setIsSaved(true);
|
||||
// Navigation propre en passant le service dans le 'state' pour que le prochain render le récupère direct
|
||||
setTimeout(() => {
|
||||
setIsSaving(false);
|
||||
navigate(`/service/${savedService.id}`, {
|
||||
replace: true,
|
||||
state: { service: savedService }
|
||||
});
|
||||
navigate(`/service/${savedService.id}`, { replace: true, state: { service: savedService } });
|
||||
}, 1000);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
setIsSaving(false);
|
||||
setIsTransitioning(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -209,76 +197,103 @@ export default function ServiceView() {
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) return <div className="loading-state">Chargement du service...</div>;
|
||||
if (!service) return <div className="loading-state">Service introuvable.</div>;
|
||||
if (loading) return (
|
||||
<div className="flex flex-col items-center justify-center min-h-[60vh] gap-6 fade-in">
|
||||
<div className="relative">
|
||||
<div className="w-12 h-12 border-2 border-primary/20 border-t-primary rounded-full animate-spin"></div>
|
||||
<div className="absolute inset-0 border-2 border-transparent border-b-secondary/50 rounded-full animate-spin [animation-duration:1.5s]"></div>
|
||||
</div>
|
||||
<p className="text-muted-foreground font-medium animate-pulse tracking-wide">Initialisation de la console...</p>
|
||||
</div>
|
||||
);
|
||||
|
||||
if (!service) return (
|
||||
<div className="flex flex-col items-center justify-center min-h-[60vh] gap-6 fade-in text-center p-4">
|
||||
<div className="w-20 h-20 rounded-3xl bg-destructive/5 flex items-center justify-center text-destructive mb-2">
|
||||
<X size={40} />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<h3 className="text-2xl font-bold tracking-tight">Service introuvable</h3>
|
||||
<p className="text-muted-foreground max-w-md">Ce service a peut-être été supprimé ou l'ID est incorrect.</p>
|
||||
</div>
|
||||
<Button variant="outline" asChild className="rounded-xl px-8 h-12">
|
||||
<Link to="/"><ArrowLeft className="mr-2" size={18} /> Retour au tableau de bord</Link>
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="service-view fade-in">
|
||||
<button className="btn btn-back" onClick={() => navigate('/')}>
|
||||
<ArrowLeft size={18} />
|
||||
<div className="max-w-7xl mx-auto px-4 py-8 space-y-6 fade-in">
|
||||
<Button variant="ghost" size="sm" onClick={() => navigate('/')} className="mb-2 text-muted-foreground hover:text-foreground hover:bg-muted/50 rounded-lg">
|
||||
<ArrowLeft size={16} className="mr-2" />
|
||||
Retour à l'accueil
|
||||
</button>
|
||||
</Button>
|
||||
|
||||
<div className="glass-panel service-view-container" style={{ marginTop: '1rem', padding: 0, overflow: 'hidden' }}>
|
||||
<div className="service-view-header">
|
||||
<div className="service-info-main">
|
||||
<div className="service-icon-wrapper">
|
||||
{service.type === 'docker' ? <Server size={24} /> : <HardDrive size={24} />}
|
||||
<Card className="overflow-hidden shadow-sm border-muted/50 rounded-xl">
|
||||
<CardHeader className="bg-muted/10 p-8 space-y-6 flex flex-col md:flex-row items-start md:items-center justify-between gap-6">
|
||||
<div className="flex items-center gap-6">
|
||||
<div className="w-16 h-16 rounded-2xl bg-card border flex items-center justify-center text-primary shadow-sm group-hover:bg-primary/5 transition-colors">
|
||||
{service.type === 'docker' ? <DockerIcon size={32} /> : <Server size={32} />}
|
||||
</div>
|
||||
<div className="service-info-text">
|
||||
{isEditingName ? (
|
||||
<input
|
||||
type="text"
|
||||
className="inline-edit-input"
|
||||
value={tempName}
|
||||
onChange={(e) => setTempName(e.target.value)}
|
||||
onBlur={handleSaveName}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') handleSaveName();
|
||||
if (e.key === 'Escape') setIsEditingName(false);
|
||||
}}
|
||||
autoFocus
|
||||
/>
|
||||
) : (
|
||||
<h3 className="service-title" onClick={() => { setIsEditingName(true); setTempName(service.name); }}>
|
||||
{service.name}
|
||||
<Pencil size={14} className="edit-icon-hint" />
|
||||
</h3>
|
||||
)}
|
||||
<span className="service-subtitle">{service.pathOrContainerId}</span>
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center gap-3 flex-wrap">
|
||||
{isEditingName ? (
|
||||
<Input
|
||||
className="h-10 w-72 text-2xl font-bold bg-card border-primary/30"
|
||||
value={tempName}
|
||||
onChange={(e) => setTempName(e.target.value)}
|
||||
onBlur={handleSaveName}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') handleSaveName();
|
||||
if (e.key === 'Escape') setIsEditingName(false);
|
||||
}}
|
||||
autoFocus
|
||||
/>
|
||||
) : (
|
||||
<h3 className="text-3xl font-bold tracking-tight cursor-pointer hover:text-primary transition-colors flex items-center gap-3 group" onClick={() => { setIsEditingName(true); setTempName(service.name); }}>
|
||||
{service.name}
|
||||
<Pencil size={18} className="opacity-0 group-hover:opacity-40 transition-opacity" />
|
||||
</h3>
|
||||
)}
|
||||
{typeof service.id === 'string' && service.id.startsWith('ephemeral') && !isSaved && (
|
||||
<Badge variant="outline" className="bg-secondary/10 text-secondary border-secondary/20 font-bold px-3 py-1 uppercase tracking-widest text-[10px]">Session Éphémère</Badge>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-3">
|
||||
<p className="text-sm font-mono text-muted-foreground opacity-70 truncate max-w-[200px] md:max-w-md">
|
||||
{service.pathOrContainerId}
|
||||
</p>
|
||||
<Badge variant="outline" className="text-[10px] font-mono opacity-50 uppercase tracking-tighter">ID: {id}</Badge>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="service-actions">
|
||||
{/* Si c'est un service réel (ID numérique) ou qu'on vient de l'enregistrer */}
|
||||
{(typeof service.id === 'number') && (
|
||||
<DeleteWithConfirmation
|
||||
onDelete={handleDelete}
|
||||
serviceName={service.name}
|
||||
/>
|
||||
<div className="flex items-center gap-4 shrink-0 w-full md:w-auto justify-end">
|
||||
{typeof service.id === 'number' && (
|
||||
<DeleteWithConfirmation onDelete={handleDelete} serviceName={service.name} />
|
||||
)}
|
||||
|
||||
{/* Si on est en mode éphémère ET pas encore enregistré */}
|
||||
{typeof service.id === 'string' && service.id.startsWith('ephemeral') && (
|
||||
<>
|
||||
{!isSaved && <span className="ephemeral-badge-large">Mode Éphémère</span>}
|
||||
<button
|
||||
className={`btn ${isSaved ? 'btn-success' : 'btn-primary'} btn-save-action`}
|
||||
onClick={handleSave}
|
||||
disabled={isSaving || isSaved}
|
||||
>
|
||||
{isSaved ? <Check size={18} className="success-check-anim" /> : <Save size={18} />}
|
||||
<span>{isSaved ? 'Enregistré !' : 'Enregistrer'}</span>
|
||||
</button>
|
||||
</>
|
||||
<Button
|
||||
variant={isSaved ? "outline" : "default"}
|
||||
size="lg"
|
||||
className={isSaved ? "border-green-500/50 text-green-500 hover:bg-green-500/5 px-8 rounded-2xl" : "bg-blue-700 hover:bg-blue-800 text-white border-none shadow-lg hover:shadow-blue-500/20 transition-all font-semibold px-8 rounded-2xl"}
|
||||
onClick={handleSave}
|
||||
disabled={isSaving || isSaved}
|
||||
>
|
||||
{isSaved ? <Check size={18} className="mr-2" /> : <Save size={18} className="mr-2" />}
|
||||
{isSaved ? 'Enregistré' : 'Enregistrer le service'}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
|
||||
<div style={{ padding: '1rem' }}>
|
||||
<Separator />
|
||||
|
||||
<CardContent className="p-0 bg-transparent">
|
||||
<LogTerminal service={service} initialLogs={initialLogs} />
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user