import { useState, useEffect } from "react"; import BrandLoader from "./BrandLoader"; import { Language, translations } from "../lib/translations"; import { clearDatabase, fetchDetailedDbStats, syncDatabase } from "../lib/api"; type Props = { onFilterClick?: (type: "sector" | "region" | "buyer", value: string) => void; lang: Language; }; export default function DBManager({ onFilterClick, lang }: Props) { const t = translations[lang]; const [stats, setStats] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isActionInProgress, setIsActionInProgress] = useState(false); const [message, setMessage] = useState<{ type: 'success' | 'error', text: string } | null>(null); const loadStats = async () => { setIsLoading(true); const data = await fetchDetailedDbStats(); setStats(data); setIsLoading(false); }; useEffect(() => { loadStats(); }, []); const handleSync = async () => { setIsActionInProgress(true); setMessage(null); try { const result = await syncDatabase(); setMessage({ type: 'success', text: `Sync complete! New: ${result.tenders?.new || 0} tenders, ${result.purchase_orders?.new || 0} OCs.` }); await loadStats(); } catch (e) { setMessage({ type: 'error', text: 'Synchronization failed.' }); } finally { setIsActionInProgress(false); } }; const handleClear = async () => { if (!confirm("Are you sure you want to delete ALL local tenders and purchase orders? This cannot be undone.")) return; setIsActionInProgress(true); setMessage(null); try { await clearDatabase(); setMessage({ type: 'success', text: 'Local database cleared successfully.' }); await loadStats(); } catch (e) { setMessage({ type: 'error', text: 'Failed to clear database.' }); } finally { setIsActionInProgress(false); } }; if (isLoading) return (
); return (
{isActionInProgress && }

{t.databaseTitle}

{t.databaseDesc}

{message && (
{message.text}
)} {/* Stats Grid */}
📄

{t.tenderCount}

{stats?.total_records || 0}

{lang === 'es' ? 'Sincronización:' : 'Last Sync:'} {stats?.last_sync ? new Date(stats.last_sync).toLocaleString() : (lang === 'es' ? 'Nunca' : 'Never')}

🛒

{lang === 'es' ? 'Órdenes de Compra' : 'Purchase Orders'}

{stats?.total_ocs || 0}

{lang === 'es' ? 'Seguimiento en tiempo real' : 'Real-time local tracking'}

🧠

{t.analysisCount}

{stats?.total_analysis || 0}

{lang === 'es' ? 'Inteligencia de IA persistente' : 'AI Intelligence persistence'}

{/* Top Buyers List */}

🏛️ {lang === 'es' ? 'Instituciones Top Locales' : 'Top Local Institutions'}

{stats?.top_buyers?.map((buyer: any, idx: number) => ( ))} {(!stats?.top_buyers || stats.top_buyers.length === 0) && (

No institutions found in local database.

)}

💡 Persistence Insights

Local Mode Active

System is prioritizing local database for faster search. Global sync updates the local cache with the latest Mercado Público data.

Integrity Check

All nested data (attachments, items, criteria) is successfully serialized as JSON in the SQLite storage.

); }