// FIXED version of the dashboard category count code // This correctly uses instance_count and fixes the news query // Make this file a module to allow top-level await export {}; // Get category counts from the categories table const { data: categoryData, error: categoryError } = await supabase .from('categories') .select('id, name, instance_count') // Changed from tool_count to instance_count .order('name'); if (!categoryError && categoryData) { // Option 1: Use the database's instance_count directly categoryData.forEach(cat => { categoryCounts[cat.name] = cat.instance_count || 0; }); /* // Option 2: Calculate counts in the code (remove database values) // Initialize all counts to zero categoryData.forEach(cat => { categoryCounts[cat.name] = 0; }); // Count tools with each category for (const cat of categoryData) { const { count: toolsCount, error: toolsError } = await supabase .from('tools') .select('*', { count: 'exact', head: true }) .eq('category_id', cat.id); if (!toolsError) { categoryCounts[cat.name] += toolsCount || 0; } // Count news with correct column name const { count: newsCount, error: newsError } = await supabase .from('news') .select('*', { count: 'exact', head: true }) .eq('category_id', cat.id); // Fixed: using category_id instead of category if (!newsError) { categoryCounts[cat.name] += newsCount || 0; } // Count blog posts (if applicable) // Check if primary_category_id or category_id is used in your schema const { count: blogCount, error: blogError } = await supabase .from('blog_posts') .select('*', { count: 'exact', head: true }) .eq('category_id', cat.id); if (!blogError) { categoryCounts[cat.name] += blogCount || 0; } } */ }