import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { RefreshCw, Database, Zap, Info, AlertCircle, CheckCircle, Clock } from 'lucide-react'; import { api } from '../../services/api'; import toast from 'react-hot-toast'; import LoadingSpinner from '../ui/LoadingSpinner'; import PerformanceIndicator from '../ui/PerformanceIndicator'; import LicenseManager from '../license/LicenseManager'; interface CacheStats { cache: { table_exists: boolean; row_count: number; table_size_mb: number; index_count: number; last_sync: string | null; }; health: { status: 'HEALTHY' | 'WARNING' | 'CRITICAL'; cache_count: number; actual_count: number; missing_count: number; orphaned_count: number; }; sync: { is_running: boolean; processed: number; total: number; last_error: string | null; }; performance: { avg_query_time_ms: number; cache_hit_rate: number; variations_per_second: number; }; system: { wordpress_version: string; woocommerce_version: string; php_version: string; php_memory_limit: string; mysql_version: string; table_engine: string; }; } export default function SettingsTab() { const { t, i18n } = useTranslation(); const [stats, setStats] = useState(null); const [loading, setLoading] = useState(true); const [resyncing, setResyncing] = useState(false); const [testing, setTesting] = useState(false); useEffect(() => { loadStats(); }, []); const loadStats = async () => { try { setLoading(true); const response = await api.get('/cache/stats'); setStats(response.data.data); } catch (error) { toast.error(t('settings_load_failed')); console.error(error); } finally { setLoading(false); } }; const handleResync = async () => { if (!confirm(t('settings_confirm_resync'))) { return; } try { setResyncing(true); const response = await api.post('/cache/resync'); toast.success(response.data.message); // Reload stats after 3 seconds setTimeout(() => { loadStats(); }, 3000); } catch (error: any) { toast.error(error.response?.data?.message || t('settings_resync_failed')); console.error(error); } finally { setResyncing(false); } }; const handlePerformanceTest = async () => { try { setTesting(true); const start = performance.now(); const response = await api.get('/cache/test'); const totalTime = performance.now() - start; toast.success( `Query: ${response.data.data.query_time_ms}ms | Total: ${totalTime.toFixed(2)}ms`, { duration: 4000 } ); } catch (error) { toast.error(t('settings_test_failed')); console.error(error); } finally { setTesting(false); } }; if (loading || !stats) { return (
); } const getHealthColor = (status: string) => { switch (status) { case 'HEALTHY': return 'text-green-600 bg-green-50 border-green-200'; case 'WARNING': return 'text-yellow-600 bg-yellow-50 border-yellow-200'; case 'CRITICAL': return 'text-red-600 bg-red-50 border-red-200'; default: return 'text-gray-600 bg-gray-50 border-gray-200'; } }; const getHealthIcon = (status: string) => { switch (status) { case 'HEALTHY': return ; case 'WARNING': return ; case 'CRITICAL': return ; default: return ; } }; // Get current locale for number formatting const locale = i18n.language === 'hu' ? 'hu-HU' : 'en-US'; return (
{/* Header with Action Buttons */}

{t('settings_title')}

{t('settings_description')}

{/* Sync Status Banner */} {stats.sync.is_running && (

{t('settings_sync_in_progress')}

{t('settings_variations_processed', { processed: (stats.sync.processed || 0).toLocaleString(locale), total: (stats.sync.total || 0).toLocaleString(locale) })}

{stats.sync.total > 0 ? Math.round(((stats.sync.processed || 0) / stats.sync.total) * 100) : 0}%
)} {/* Main Layout: 1/2 - 1/2 Grid */}
{/* LEFT COLUMN (1/2): System Statistics & Monitoring */}
{/* Cache Statistics Card */}

{t('settings_cache_statistics')}

{t('settings_table_status')}

{stats.cache.table_exists ? ( ✓ {t('settings_exists')} ) : ( ✗ {t('settings_missing')} )}

{t('settings_row_count')}

{(stats.cache.row_count || 0).toLocaleString(locale)}

{t('settings_table_size')}

{(stats.cache.table_size_mb || 0).toFixed(2)} MB

{t('settings_indexes')}

{stats.cache.index_count || 0}

{t('settings_last_sync')}: {stats.cache.last_sync || t('settings_never_synced')}
{/* Health Status Card */}

{t('settings_health_check')}

{getHealthIcon(stats.health.status)} {stats.health.status}

{t('settings_cache_variations')}

{(stats.health.cache_count || 0).toLocaleString(locale)}

{t('settings_woo_variations')}

{(stats.health.actual_count || 0).toLocaleString(locale)}

{t('settings_missing_cache')}

0 ? 'text-yellow-600' : 'text-green-600'}`}> {stats.health.missing_count || 0}

{t('settings_orphaned_cache')}

0 ? 'text-yellow-600' : 'text-green-600'}`}> {stats.health.orphaned_count || 0}

{/* Performance Metrics Card */}

{t('settings_performance_metrics')}

{t('settings_avg_query')}

{(stats.performance.avg_query_time_ms || 0).toFixed(1)} ms

{t('settings_cache_hit')}

{(stats.performance.cache_hit_rate || 0).toFixed(1)} %

{t('settings_throughput')}

{((stats.performance.variations_per_second || 0) / 1000).toFixed(0)} k/s

{/* Performance Indicator Progress Bar */}
{/* System Information Card */}

{t('settings_system_info')}

{t('settings_wordpress_version')}

{stats.system.wordpress_version}

{t('settings_woocommerce_version')}

{stats.system.woocommerce_version}

{t('settings_php_version')}

{stats.system.php_version}

{t('settings_php_memory_limit')}

{stats.system.php_memory_limit || 'N/A'}

{t('settings_mysql_version')}

{stats.system.mysql_version}

{t('settings_table_engine')}

{stats.system.table_engine}

{/* Last Error (if any) - moved inside left column */} {stats.sync.last_error && (

{t('settings_last_sync_error')}:

{stats.sync.last_error}

)}
{/* End of Left Column */} {/* RIGHT COLUMN (1/2): License Management */}
{/* End of Main Layout Grid */}
); }