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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 | import { useState, useEffect } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import {
Play,
Plus,
Edit2,
Trash2,
Save,
X,
Globe,
ChevronDown,
ChevronRight,
Search,
Route,
Brain,
Clock,
AlertCircle,
CheckCircle
} 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';
export default function CrawlerConfig() {
const { id } = useParams();
const { currentOrganization } = useOrganizationStore();
const organizationId = currentOrganization?.id;
const navigate = useNavigate();
const queryClient = useQueryClient();
const isEditing = id && id !== 'new';
const [searchTerm, setSearchTerm] = useState('');
const [showForm, setShowForm] = useState(isEditing);
const [startUrl, setStartUrl] = useState('');
const [showStartModal, setShowStartModal] = useState(false);
const [fieldErrors, setFieldErrors] = useState({});
const [formData, setFormData] = useState({
name: '',
description: '',
config: {
maxPages: 10,
maxDepth: 3,
pageThreshold: 60,
linkThreshold: 40,
crawlDelay: 100,
sameDomain: false,
enableSynthesis: true,
synthetiser: { mode: 'fact', spec: {} },
router: [],
}
});
const { data: crawlers, isLoading } = useQuery({
queryKey: ['crawlers', organizationId],
queryFn: async () => {
const response = await api.get('/crawlers');
return response.data.data || [];
}
});
const { data: routers } = useQuery({
queryKey: ['routers', organizationId],
queryFn: async () => {
const response = await api.get('/knowledge-routers');
return response.data.data || [];
}
});
const { data: currentCrawler, isLoading: isLoadingCrawler } = useQuery({
queryKey: ['crawler', id, organizationId],
queryFn: async () => {
const response = await api.get(`/crawlers/${id}`);
return response.data.data;
},
enabled: isEditing && !!id
});
useEffect(() => {
if (currentCrawler && isEditing) {
setFormData({
name: currentCrawler.name || '',
description: currentCrawler.description || '',
config: {
maxPages: currentCrawler.config?.maxPages || 10,
maxDepth: currentCrawler.config?.maxDepth || 3,
pageThreshold: currentCrawler.config?.pageThreshold || 60,
linkThreshold: currentCrawler.config?.linkThreshold || 40,
crawlDelay: currentCrawler.config?.crawlDelay || 100,
sameDomain: currentCrawler.config?.sameDomain || false,
enableSynthesis: currentCrawler.config?.enableSynthesis !== false,
synthetiser: currentCrawler.config?.synthetiser || { mode: 'fact', spec: {} },
router: currentCrawler.config?.router || [],
}
});
}
}, [currentCrawler, isEditing]);
const createMutation = useMutation({
mutationFn: async (data) => {
return api.post('/crawlers', { ...data, organizationId });
},
onSuccess: (response) => {
toast.success('Crawler créé');
queryClient.invalidateQueries(['crawlers']);
const newId = response.data.data?._id || response.data.data?.id;
if (newId) {
navigate(`/${organizationId}/backoffice/crawlers/${newId}`);
} else {
navigate(`/${organizationId}/backoffice/crawlers`);
}
},
onError: (error) => {
toast.error(error.response?.data?.message || 'Erreur lors de la création');
}
});
const updateMutation = useMutation({
mutationFn: async (data) => {
return api.put(`/crawlers/${id}`, { ...data, organizationId });
},
onSuccess: () => {
toast.success('Crawler mis à jour');
queryClient.invalidateQueries(['crawlers']);
queryClient.invalidateQueries(['crawler', id]);
setShowForm(false);
},
onError: (error) => {
toast.error(error.response?.data?.message || 'Erreur lors de la mise à jour');
}
});
const deleteMutation = useMutation({
mutationFn: async (crawlerId) => {
return api.delete(`/crawlers/${crawlerId}`);
},
onSuccess: () => {
toast.success('Crawler supprimé');
queryClient.invalidateQueries(['crawlers']);
navigate(`/${organizationId}/backoffice/crawlers`);
},
onError: (error) => {
toast.error(error.response?.data?.message || 'Erreur lors de la suppression');
}
});
const startJobMutation = useMutation({
mutationFn: async ({ configId, startUrl }) => {
return api.post(`/crawlers/${configId}/start`, { startUrl });
},
onSuccess: () => {
toast.success('Job de crawl démarré');
queryClient.invalidateQueries(['crawler-jobs']);
setShowStartModal(false);
setStartUrl('');
navigate(`/${organizationId}/backoffice/crawlers-jobs`);
},
onError: (error) => {
toast.error(error.response?.data?.message || 'Erreur lors du démarrage du crawl');
}
});
const handleSubmit = (e) => {
e.preventDefault();
if (isEditing) {
updateMutation.mutate(formData);
} else {
createMutation.mutate(formData);
}
};
const handleStartCrawl = () => {
if (!startUrl.trim()) {
toast.error('Veuillez entrer une URL de départ');
return;
}
const configId = currentCrawler?._id || currentCrawler?.id || id;
startJobMutation.mutate({ configId, startUrl });
};
const filteredCrawlers = crawlers?.filter(crawler =>
crawler.name?.toLowerCase().includes(searchTerm.toLowerCase()) ||
crawler.description?.toLowerCase().includes(searchTerm.toLowerCase())
);
if (isLoading || (isEditing && isLoadingCrawler)) {
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>
</div>
);
}
return (
<div className="space-y-6">
<div className="flex items-center justify-between">
<div>
<h2 className="text-2xl font-bold text-gray-900">Crawler Config</h2>
<p className="text-gray-600">Configuration des crawlers d'exploration web</p>
</div>
{!showForm && (
<button
onClick={() => setShowForm(true)}
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" />
Nouveau crawler
</button>
)}
</div>
{showForm ? (
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center justify-between mb-4">
<h3 className="text-lg font-medium text-gray-900">
{isEditing ? 'Modifier le crawler' : 'Nouveau crawler'}
</h3>
<button
onClick={() => {
setShowForm(false);
if (!isEditing) navigate(`/${organizationId}/backoffice/crawlers`);
}}
className="text-gray-400 hover:text-gray-500"
>
<X className="w-6 h-6" />
</button>
</div>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">Nom</label>
<input
type="text"
value={formData.name}
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
className="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
required
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">Max pages</label>
<input
type="number"
value={formData.config.maxPages}
onChange={(e) => setFormData({
...formData,
config: { ...formData.config, maxPages: parseInt(e.target.value) || 10 }
})}
min="1"
max="10000"
className="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
/>
</div>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">Description</label>
<textarea
value={formData.description}
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
className="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
rows={2}
/>
</div>
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">Profondeur max</label>
<input
type="number"
value={formData.config.maxDepth}
onChange={(e) => setFormData({
...formData,
config: { ...formData.config, maxDepth: parseInt(e.target.value) || 3 }
})}
min="1"
max="10"
className="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">Page threshold (%)</label>
<input
type="number"
value={formData.config.pageThreshold}
onChange={(e) => setFormData({
...formData,
config: { ...formData.config, pageThreshold: parseInt(e.target.value) || 60 }
})}
min="0"
max="100"
className="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
/>
</div>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">Link threshold (%)</label>
<input
type="number"
value={formData.config.linkThreshold}
onChange={(e) => setFormData({
...formData,
config: { ...formData.config, linkThreshold: parseInt(e.target.value) || 40 }
})}
min="0"
max="100"
className="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
/>
</div>
<div className="flex items-center space-x-6">
<div className="flex items-center">
<input
type="checkbox"
id="enableSynthesis"
checked={formData.config.enableSynthesis}
onChange={(e) => setFormData({
...formData,
config: { ...formData.config, enableSynthesis: e.target.checked }
})}
className="w-4 h-4 text-indigo-600 border-gray-300 rounded focus:ring-indigo-500"
/>
<label htmlFor="enableSynthesis" className="ml-2 text-sm text-gray-700">
Activer synthèse
</label>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">Crawl delay (ms)</label>
<input
type="number"
value={formData.config.crawlDelay || 100}
onChange={(e) => setFormData({
...formData,
config: { ...formData.config, crawlDelay: parseInt(e.target.value) || 100 }
})}
className="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
min="100"
max="60000"
/>
</div>
</div>
<div className="flex items-center justify-between bg-gray-50 rounded-lg px-4 py-3">
<div className="flex flex-col">
<span className="text-sm font-medium text-gray-700">Même domaine uniquement</span>
<span className="text-xs text-gray-500">Limiter le crawl aux pages du même domaine que l'URL de départ</span>
</div>
<button
type="button"
onClick={() => setFormData({
...formData,
config: { ...formData.config, sameDomain: !formData.config.sameDomain }
})}
className={`relative inline-flex h-6 w-11 items-center rounded-full transition-colors ${formData.config.sameDomain ? 'bg-indigo-600' : 'bg-gray-300'}`}
>
<span className={`inline-block h-4 w-4 transform rounded-full bg-white transition-transform ${formData.config.sameDomain ? 'translate-x-6' : 'translate-x-1'}`} />
</button>
</div>
{formData.config.enableSynthesis && (
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">Mode synthèse</label>
<select
value={formData.config.synthetiser?.mode || 'fact'}
onChange={(e) => setFormData({
...formData,
config: {
...formData.config,
synthetiser: { ...formData.config.synthetiser, mode: e.target.value }
}
})}
className="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
>
<option value="fact">Faits</option>
<option value="data">Données</option>
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">Routers</label>
<select
value={formData.config.router?.[0]?.id || ''}
onChange={(e) => setFormData({
...formData,
config: {
...formData.config,
router: e.target.value ? [e.target.value] : []
}
})}
className="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
>
<option value="">Aucun router</option>
{routers?.map((router) => (
<option key={router._id} value={router._id}>{router.name}</option>
))}
</select>
</div>
</div>
)}
<div className="flex justify-end space-x-4 pt-4">
<button
type="button"
onClick={() => {
setShowForm(false);
if (!isEditing) navigate(`/${organizationId}/backoffice/crawlers`);
}}
className="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50"
>
Annuler
</button>
<button
type="submit"
disabled={createMutation.isLoading || updateMutation.isLoading}
className="flex items-center px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 disabled:opacity-50"
>
<Save className="w-4 h-4 mr-2" />
{createMutation.isLoading || updateMutation.isLoading ? 'Sauvegarde...' : 'Sauvegarder'}
</button>
</div>
</form>
</div>
) : (
<>
<div className="flex gap-4">
<div className="relative flex-1">
<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 un 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>
</div>
<div className="bg-indigo-50 border border-indigo-200 rounded-lg p-4">
<h4 className="font-medium text-indigo-900 mb-2">À propos des crawlers</h4>
<p className="text-sm text-indigo-700">
Les crawlers explorent automatiquement le web à partir d'URLs sources.
Créez d'abord un crawler, puis lancez un job avec une URL de départ.
</p>
</div>
<div className="space-y-4">
{filteredCrawlers?.map((crawler) => {
const crawlerId = crawler._id || crawler.id;
return (
<div key={crawlerId} className="bg-white rounded-lg shadow overflow-hidden">
<div className="p-4 flex items-center justify-between">
<div className="flex items-center">
<Globe className="w-8 h-8 text-indigo-600" />
<div className="ml-4">
<h3 className="text-lg font-medium text-gray-900">{crawler.name}</h3>
{crawler.description ? (
<p className="text-sm text-gray-600 mt-1">{crawler.description}</p>
) : (
<p className="text-sm text-gray-400 italic mt-1">Aucune description</p>
)}
<div className="flex items-center mt-2 space-x-2">
<span className="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-blue-100 text-blue-700">
<Settings className="w-3 h-3 mr-1" />
Max: {crawler.config?.maxPages || 10}
</span>
<span className="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-green-100 text-green-700">
Depth: {crawler.config?.maxDepth || 3}
</span>
{crawler.runCount > 0 && (
<span className="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-gray-100 text-gray-700">
<Clock className="w-3 h-3 mr-1" />
{crawler.runCount} runs
</span>
)}
</div>
</div>
</div>
<div className="flex items-center space-x-2">
<button
onClick={() => {
setFormData({
name: crawler.name,
description: crawler.description || '',
config: crawler.config || {}
});
navigate(`/${organizationId}/backoffice/crawlers/${crawlerId}`);
setShowForm(true);
}}
className="flex items-center px-3 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 text-sm"
>
<Play className="w-4 h-4 mr-1" />
Lancer
</button>
<button
onClick={() => {
setFormData({
name: crawler.name,
description: crawler.description || '',
config: crawler.config || {}
});
navigate(`/${organizationId}/backoffice/crawlers/${crawlerId}`);
setShowForm(true);
}}
className="p-2 text-indigo-600 hover:bg-indigo-50 rounded-lg"
>
<Edit2 className="w-5 h-5" />
</button>
<button
onClick={() => {
if (confirm('Êtes-vous sûr de vouloir supprimer ce crawler ?')) {
deleteMutation.mutate(crawlerId);
}
}}
className="p-2 text-red-600 hover:bg-red-50 rounded-lg"
>
<Trash2 className="w-5 h-5" />
</button>
</div>
</div>
</div>
);
})}
{filteredCrawlers?.length === 0 && (
<div className="text-center py-12 bg-gray-50 rounded-lg">
<Globe className="w-12 h-12 text-gray-400 mx-auto mb-4" />
<p className="text-gray-500">Aucun crawler trouvé</p>
<button
onClick={() => setShowForm(true)}
className="mt-4 text-indigo-600 hover:text-indigo-700 font-medium"
>
Créer votre premier crawler
</button>
</div>
)}
</div>
</>
)}
{showStartModal && (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
<div className="bg-white rounded-lg p-6 w-full max-w-md">
<h3 className="text-lg font-medium text-gray-900 mb-4">Lancer un crawl</h3>
<p className="text-sm text-gray-600 mb-4">
Entrez l'URL de départ pour {formData.name}
</p>
<input
type="url"
value={startUrl}
onChange={(e) => setStartUrl(e.target.value)}
placeholder="https://example.com"
className="w-full p-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 mb-4"
/>
<div className="flex justify-end space-x-4">
<button
onClick={() => {
setShowStartModal(false);
setStartUrl('');
}}
className="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50"
>
Annuler
</button>
<button
onClick={handleStartCrawl}
disabled={startJobMutation.isLoading}
className="flex items-center px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 disabled:opacity-50"
>
<Play className="w-4 h-4 mr-2" />
{startJobMutation.isLoading ? 'Démarrage...' : 'Démarrer'}
</button>
</div>
</div>
</div>
)}
</div>
);
} |