Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { Clock, PauseCircle, PlayCircle, Trash2, Zap, Bug, Search, Plus, Edit2, ListChecks } from 'lucide-react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import api from '../../services/api';
import toast from 'react-hot-toast';
import { useOrganizationStore } from '../../store/organizationStore';
function formatDate(dateStr) {
if (!dateStr) return '—';
const d = new Date(dateStr);
const now = new Date();
const diff = now - d;
if (diff < 60000) return 'À l\'instant';
if (diff < 3600000) return `Il y a ${Math.floor(diff / 60000)} min`;
if (diff < 86400000) return `Il y a ${Math.floor(diff / 3600000)}h`;
return d.toLocaleDateString('fr-FR', { day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit' });
}
function describeCron(cron) {
const parts = cron?.split(/\s+/) || [];
if (parts.length !== 5) return cron;
const [min, hour, day, month, week] = parts;
if (min === '0' && hour === '*' && day === '*' && month === '*' && week === '*') return 'Toutes les heures';
if (min === '0' && hour === '*/2' && day === '*' && month === '*' && week === '*') return 'Toutes les 2h';
if (min === '0' && hour === '*/6' && day === '*' && month === '*' && week === '*') return 'Toutes les 6h';
if (min === '0' && hour === '*/12' && day === '*' && month === '*' && week === '*') return 'Toutes les 12h';
if (min === '0' && !hour.includes('/') && day === '*' && month === '*' && week === '*') return `Tous les jours à ${hour}h`;
return cron;
}
export default function Schedules() {
const navigate = useNavigate();
const { currentOrganization } = useOrganizationStore();
const organizationId = currentOrganization?.id;
const queryClient = useQueryClient();
const [searchTerm, setSearchTerm] = useState('');
const { data: schedules, isLoading } = useQuery({
queryKey: ['schedules', organizationId],
queryFn: async () => {
const response = await api.get('/schedules');
return response.data.data || [];
},
enabled: !!organizationId,
});
const { data: crawlers } = useQuery({
queryKey: ['crawlers', organizationId],
queryFn: async () => {
const response = await api.get('/crawlers');
return response.data.data || [];
},
enabled: !!organizationId,
});
const togglePause = useMutation({
mutationFn: async ({ id, isActive }) =>
isActive ? api.post(`/schedules/${id}/pause`) : api.post(`/schedules/${id}/resume`),
onSuccess: () => {
toast.success('Planification mise à jour');
queryClient.invalidateQueries(['schedules', organizationId]);
},
onError: (err) => toast.error(err.response?.data?.message || 'Erreur'),
});
const deleteSchedule = useMutation({
mutationFn: async (id) => api.delete(`/schedules/${id}`),
onSuccess: () => {
toast.success('Planification supprimée');
queryClient.invalidateQueries(['schedules', organizationId]);
},
onError: (err) => toast.error(err.response?.data?.message || 'Erreur de suppression'),
});
const triggerNow = useMutation({
mutationFn: async (id) => api.post(`/schedules/${id}/trigger-now`),
onSuccess: () => toast.success('Crawl déclenché'),
onError: (err) => toast.error(err.response?.data?.message || 'Erreur de déclenchement'),
});
const getCrawlerName = (configId) => {
const c = crawlers?.find(cr => (cr._id || cr.id) === (configId || String(configId)));
return c?.name || configId?.slice(0, 8) + '...' || '—';
};
const filtered = schedules?.filter(s =>
getCrawlerName(s.configId).toLowerCase().includes(searchTerm.toLowerCase())
);
if (isLoading) {
return (
<div className="flex items-center justify-center h-64">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-indigo-600" />
</div>
);
}
return (
<div className="space-y-6">
<div className="flex items-center justify-between">
<div>
<h2 className="text-2xl font-bold text-gray-900">Planifications</h2>
<p className="text-gray-600">Gestion des crawls récurrents programmés</p>
</div>
<button
onClick={() => navigate(`/${organizationId}/backoffice/schedules/new`)}
className="flex items-center px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700"
>
<Plus className="w-5 h-5 mr-2" />
Nouvelle planification
</button>
</div>
<div className="relative">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-5 h-5" />
<input
type="text"
placeholder="Rechercher par crawler..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
/>
</div>
{(!filtered || filtered.length === 0) && (
<div className="text-center py-12 bg-gray-50 rounded-lg">
<Clock className="w-12 h-12 text-gray-400 mx-auto mb-4" />
<p className="text-gray-500">Aucune planification trouvée</p>
<p className="text-sm text-gray-400 mt-1">Créez-en une depuis la page d'édition d'un crawler</p>
<button
onClick={() => navigate(`/${organizationId}/backoffice/crawlers`)}
className="mt-4 text-indigo-600 hover:text-indigo-700 font-medium"
>
Voir les crawlers
</button>
</div>
)}
<div className="space-y-3">
{filtered?.map((s) => (
<div key={s._id} className={`bg-white rounded-lg shadow overflow-hidden border-l-4 ${
s.isActive ? 'border-l-green-500' : 'border-l-gray-300'
}`}>
<div className="p-4">
<div className="flex items-center justify-between">
<div className="flex items-center flex-1">
<Bug className="w-6 h-6 text-indigo-600 mr-3" />
<div>
<h3 className="text-sm font-medium text-gray-900">
{getCrawlerName(s.configId)}
</h3>
<div className="flex items-center gap-2 mt-1">
<span className={`inline-flex items-center px-2 py-0.5 text-xs font-medium rounded-full ${
s.isActive ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-500'
}`}>
{s.isActive ? 'Actif' : 'En pause'}
</span>
<span className="text-xs font-mono text-gray-500 bg-gray-100 px-1.5 py-0.5 rounded">
{s.cronExpression}
</span>
<span className="text-xs text-gray-400">
{describeCron(s.cronExpression)}
</span>
</div>
</div>
</div>
<div className="flex items-center gap-4 text-xs text-gray-500 mr-4">
<div className="text-right">
<p className="font-medium text-gray-700">{s.runCount || 0} exécution(s)</p>
<p>Dernière : {formatDate(s.lastRunAt)}</p>
</div>
</div>
<div className="flex items-center gap-1">
<button
onClick={() => triggerNow.mutate(s._id)}
disabled={triggerNow.isLoading}
className="p-2 text-indigo-600 hover:bg-indigo-50 rounded-lg"
title="Déclencher maintenant"
>
<Zap className="w-4 h-4" />
</button>
<button
onClick={() => togglePause.mutate({ id: s._id, isActive: s.isActive })}
className="p-2 text-gray-600 hover:bg-gray-100 rounded-lg"
title={s.isActive ? 'Mettre en pause' : 'Reprendre'}
>
{s.isActive ? <PauseCircle className="w-4 h-4" /> : <PlayCircle className="w-4 h-4" />}
</button>
<button
onClick={() => navigate(`/${organizationId}/backoffice/jobs?scheduleId=${s._id}`)}
className="p-2 text-gray-600 hover:bg-cyan-50 rounded-lg"
title="Voir les exécutions"
>
<ListChecks className="w-4 h-4" />
</button>
<button
onClick={() => navigate(`/${organizationId}/backoffice/schedules/${s._id}`)}
className="p-2 text-gray-600 hover:bg-indigo-50 rounded-lg"
title="Modifier la planification"
>
<Edit2 className="w-4 h-4" />
</button>
<button
onClick={() => {
if (confirm('Supprimer cette planification ?')) {
deleteSchedule.mutate(s._id);
}
}}
className="p-2 text-red-500 hover:bg-red-50 rounded-lg"
title="Supprimer"
>
<Trash2 className="w-4 h-4" />
</button>
</div>
</div>
</div>
</div>
))}
</div>
</div>
);
}
|