import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { supabase } from '@/lib/supabaseClient'; import { Building2, AlertCircle, CheckCircle2 } from 'lucide-react'; import { useToast } from '@/hooks/use-toast'; export function CompanyDataBackfill() { const [isRunning, setIsRunning] = useState(false); const [result, setResult] = useState<{ success: boolean; companies_updated: number; headquarters_added: number; website_added: number; founded_year_added: number; description_added: number; logo_added: number; } | null>(null); const [error, setError] = useState(null); const { toast } = useToast(); const handleBackfill = async () => { setIsRunning(true); setError(null); setResult(null); try { const { data, error: invokeError } = await supabase.functions.invoke( 'backfill-company-data' ); if (invokeError) throw invokeError; setResult(data); const updates: string[] = []; if (data.headquarters_added > 0) updates.push(`${data.headquarters_added} headquarters`); if (data.website_added > 0) updates.push(`${data.website_added} websites`); if (data.founded_year_added > 0) updates.push(`${data.founded_year_added} founding years`); if (data.description_added > 0) updates.push(`${data.description_added} descriptions`); if (data.logo_added > 0) updates.push(`${data.logo_added} logos`); toast({ title: 'Backfill Complete', description: `Updated ${data.companies_updated} companies: ${updates.join(', ')}`, }); } catch (err: any) { const errorMessage = err.message || 'Failed to run backfill'; setError(errorMessage); toast({ title: 'Backfill Failed', description: errorMessage, variant: 'destructive', }); } finally { setIsRunning(false); } }; return ( Company Data Backfill Backfill missing headquarters, website, founding year, description, and logo data for companies from their submission data This tool will find companies (operators, manufacturers, designers) missing basic information and populate them using data from their approved submissions. Useful for fixing companies that were approved before all fields were properly handled. {result && (
Backfill completed successfully!
Companies updated: {result.companies_updated}
Headquarters added: {result.headquarters_added}
Websites added: {result.website_added}
Founding years added: {result.founded_year_added}
Descriptions added: {result.description_added}
Logos added: {result.logo_added}
)} {error && ( {error} )}
); }