![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.ca/private_html/src/pages/admin/ |
import React, { useState, useEffect } from 'react';
import { useSession } from 'next-auth/react';
import { useRouter } from 'next/router';
import Head from 'next/head';
import LayoutWithSidebar from '../../components/LayoutWithSidebar';
import { motion } from 'framer-motion';
import {
Play, Square, RefreshCw, Database, Users,
Clock, CheckCircle, AlertCircle, TrendingUp,
Shield, FileText, Globe, Settings
} from 'lucide-react';
import toast from 'react-hot-toast';
interface ScrapingProgress {
totalPages: number;
currentPage: number;
totalLawyers: number;
importedLawyers: number;
errors: number;
startTime: number;
}
const BarreauScraperPage: React.FC = () => {
const { data: session, status } = useSession();
const router = useRouter();
const [isRunning, setIsRunning] = useState(false);
const [progress, setProgress] = useState<ScrapingProgress | null>(null);
const [isLoading, setIsLoading] = useState(false);
// Check authentication and admin permissions
useEffect(() => {
if (status === 'loading') return;
if (!session || !['ADMIN', 'SUPERADMIN'].includes(session.user?.role || '')) {
router.push('/auth/login');
}
}, [session, status, router]);
// Poll for progress updates
useEffect(() => {
if (!isRunning) return;
const interval = setInterval(async () => {
try {
const response = await fetch('/api/admin/scrape-barreau');
const data = await response.json();
if (data.success && data.progress) {
setProgress(data.progress);
// Check if scraping is complete
if (data.progress.currentPage >= data.progress.totalPages && data.progress.totalPages > 0) {
setIsRunning(false);
toast.success('🎉 Barreau scraping completed successfully!');
}
}
} catch (error) {
console.error('Error fetching progress:', error);
}
}, 2000);
return () => clearInterval(interval);
}, [isRunning]);
const startScraping = async () => {
setIsLoading(true);
try {
const response = await fetch('/api/admin/scrape-barreau', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: 'start' })
});
const data = await response.json();
if (data.success) {
setIsRunning(true);
toast.success('🚀 Barreau scraping started!');
} else {
toast.error(data.error || 'Failed to start scraping');
}
} catch (error) {
console.error('Error starting scraping:', error);
toast.error('Failed to start scraping');
} finally {
setIsLoading(false);
}
};
const stopScraping = async () => {
setIsLoading(true);
try {
const response = await fetch('/api/admin/scrape-barreau', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: 'stop' })
});
const data = await response.json();
if (data.success) {
setIsRunning(false);
toast.success('🛑 Scraping stopped');
} else {
toast.error(data.error || 'Failed to stop scraping');
}
} catch (error) {
console.error('Error stopping scraping:', error);
toast.error('Failed to stop scraping');
} finally {
setIsLoading(false);
}
};
const getProgressPercentage = () => {
if (!progress || progress.totalPages === 0) return 0;
return Math.round((progress.currentPage / progress.totalPages) * 100);
};
const getDuration = () => {
if (!progress || !progress.startTime) return '0m 0s';
const duration = Date.now() - progress.startTime;
const minutes = Math.floor(duration / 60000);
const seconds = Math.floor((duration % 60000) / 1000);
return `${minutes}m ${seconds}s`;
};
const getSuccessRate = () => {
if (!progress || (progress.importedLawyers + progress.errors) === 0) return 0;
return Math.round((progress.importedLawyers / (progress.importedLawyers + progress.errors)) * 100);
};
if (status === 'loading') {
return (
<LayoutWithSidebar>
<div className="flex items-center justify-center min-h-screen">
<RefreshCw className="h-8 w-8 animate-spin text-blue-600" />
</div>
</LayoutWithSidebar>
);
}
if (!session || !['ADMIN', 'SUPERADMIN'].includes(session.user?.role || '')) {
return null;
}
return (
<LayoutWithSidebar>
<Head>
<title>Barreau Scraper - Admin Dashboard</title>
<meta name="description" content="Control the Barreau du Québec lawyer directory scraper" />
</Head>
<div className="max-w-6xl mx-auto px-4 py-8">
{/* Header */}
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
className="mb-8"
>
<div className="flex items-center gap-3 mb-4">
<div className="p-3 bg-blue-100 dark:bg-blue-900/30 rounded-lg">
<Database className="h-8 w-8 text-blue-600" />
</div>
<div>
<h1 className="text-3xl font-bold text-gray-900 dark:text-white">
Barreau Directory Scraper
</h1>
<p className="text-gray-600 dark:text-gray-400">
Import the entire Barreau du Québec lawyer directory
</p>
</div>
</div>
</motion.div>
{/* Control Panel */}
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.1 }}
className="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6 mb-8"
>
<h2 className="text-xl font-semibold text-gray-900 dark:text-white mb-4 flex items-center">
<Settings className="h-5 w-5 mr-2" />
Control Panel
</h2>
<div className="flex gap-4">
<button
onClick={startScraping}
disabled={isRunning || isLoading}
className="flex items-center gap-2 bg-green-600 hover:bg-green-700 disabled:bg-gray-400 text-white px-6 py-3 rounded-lg font-medium transition-colors"
>
<Play className="h-5 w-5" />
{isLoading ? 'Starting...' : 'Start Scraping'}
</button>
<button
onClick={stopScraping}
disabled={!isRunning || isLoading}
className="flex items-center gap-2 bg-red-600 hover:bg-red-700 disabled:bg-gray-400 text-white px-6 py-3 rounded-lg font-medium transition-colors"
>
<Square className="h-5 w-5" />
{isLoading ? 'Stopping...' : 'Stop Scraping'}
</button>
</div>
<div className="mt-4 p-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg">
<h3 className="font-medium text-blue-800 dark:text-blue-200 mb-2">
⚠️ Important Notes:
</h3>
<ul className="text-sm text-blue-700 dark:text-blue-300 space-y-1">
<li>• Scraping may take several hours depending on the directory size</li>
<li>• The scraper respects rate limits to avoid overwhelming the server</li>
<li>• Existing lawyers will be updated with new information</li>
<li>• New lawyers will be created with temporary passwords</li>
<li>• All imported lawyers will have Barreau verification status</li>
</ul>
</div>
</motion.div>
{/* Progress Dashboard */}
{progress && (
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.2 }}
className="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6 mb-8"
>
<h2 className="text-xl font-semibold text-gray-900 dark:text-white mb-4 flex items-center">
<TrendingUp className="h-5 w-5 mr-2" />
Progress Dashboard
</h2>
{/* Progress Bar */}
<div className="mb-6">
<div className="flex justify-between text-sm text-gray-600 dark:text-gray-400 mb-2">
<span>Overall Progress</span>
<span>{getProgressPercentage()}%</span>
</div>
<div className="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-3">
<div
className="bg-gradient-to-r from-blue-500 to-green-500 h-3 rounded-full transition-all duration-500"
style={{ width: `${getProgressPercentage()}%` }}
></div>
</div>
</div>
{/* Stats Grid */}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
<div className="bg-blue-50 dark:bg-blue-900/20 rounded-lg p-4">
<div className="flex items-center">
<FileText className="h-8 w-8 text-blue-600 mr-3" />
<div>
<p className="text-sm text-blue-600 dark:text-blue-400">Pages</p>
<p className="text-2xl font-bold text-blue-800 dark:text-blue-200">
{progress.currentPage}/{progress.totalPages}
</p>
</div>
</div>
</div>
<div className="bg-green-50 dark:bg-green-900/20 rounded-lg p-4">
<div className="flex items-center">
<Users className="h-8 w-8 text-green-600 mr-3" />
<div>
<p className="text-sm text-green-600 dark:text-green-400">Imported</p>
<p className="text-2xl font-bold text-green-800 dark:text-green-200">
{progress.importedLawyers}
</p>
</div>
</div>
</div>
<div className="bg-red-50 dark:bg-red-900/20 rounded-lg p-4">
<div className="flex items-center">
<AlertCircle className="h-8 w-8 text-red-600 mr-3" />
<div>
<p className="text-sm text-red-600 dark:text-red-400">Errors</p>
<p className="text-2xl font-bold text-red-800 dark:text-red-200">
{progress.errors}
</p>
</div>
</div>
</div>
<div className="bg-purple-50 dark:bg-purple-900/20 rounded-lg p-4">
<div className="flex items-center">
<Clock className="h-8 w-8 text-purple-600 mr-3" />
<div>
<p className="text-sm text-purple-600 dark:text-purple-400">Duration</p>
<p className="text-2xl font-bold text-purple-800 dark:text-purple-200">
{getDuration()}
</p>
</div>
</div>
</div>
</div>
{/* Success Rate */}
<div className="bg-gray-50 dark:bg-gray-700 rounded-lg p-4">
<div className="flex items-center justify-between">
<div className="flex items-center">
<CheckCircle className="h-6 w-6 text-green-600 mr-2" />
<span className="font-medium text-gray-900 dark:text-white">Success Rate</span>
</div>
<span className="text-2xl font-bold text-green-600">{getSuccessRate()}%</span>
</div>
</div>
</motion.div>
)}
{/* Status Indicator */}
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.3 }}
className="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6"
>
<h2 className="text-xl font-semibold text-gray-900 dark:text-white mb-4 flex items-center">
<Shield className="h-5 w-5 mr-2" />
Scraper Status
</h2>
<div className="flex items-center gap-3">
<div className={`w-4 h-4 rounded-full ${isRunning ? 'bg-green-500 animate-pulse' : 'bg-gray-400'}`}></div>
<span className="font-medium text-gray-900 dark:text-white">
{isRunning ? 'Running' : 'Stopped'}
</span>
{isRunning && (
<span className="text-sm text-gray-600 dark:text-gray-400">
• Scraping in progress...
</span>
)}
</div>
{!progress && (
<p className="mt-4 text-gray-600 dark:text-gray-400">
No scraping session has been started yet. Click "Start Scraping" to begin importing the Barreau directory.
</p>
)}
</motion.div>
</div>
</LayoutWithSidebar>
);
};
export default BarreauScraperPage;