AI Resource Pro / website / scripts / fix-dashboard-counts.tsx.example
| 1 | // FIXED version of the dashboard category count code |
| 2 | // This correctly uses instance_count and fixes the news query |
| 3 | |
| 4 | // Make this file a module to allow top-level await |
| 5 | export {}; |
| 6 | |
| 7 | // Get category counts from the categories table |
| 8 | const { data: categoryData, error: categoryError } = await supabase |
| 9 | .from('categories') |
| 10 | .select('id, name, instance_count') // Changed from tool_count to instance_count |
| 11 | .order('name'); |
| 12 | |
| 13 | if (!categoryError && categoryData) { |
| 14 | // Option 1: Use the database's instance_count directly |
| 15 | categoryData.forEach(cat => { |
| 16 | categoryCounts[cat.name] = cat.instance_count || 0; |
| 17 | }); |
| 18 | |
| 19 | /* |
| 20 | // Option 2: Calculate counts in the code (remove database values) |
| 21 | // Initialize all counts to zero |
| 22 | categoryData.forEach(cat => { |
| 23 | categoryCounts[cat.name] = 0; |
| 24 | }); |
| 25 | |
| 26 | // Count tools with each category |
| 27 | for (const cat of categoryData) { |
| 28 | const { count: toolsCount, error: toolsError } = await supabase |
| 29 | .from('tools') |
| 30 | .select('*', { count: 'exact', head: true }) |
| 31 | .eq('category_id', cat.id); |
| 32 | |
| 33 | if (!toolsError) { |
| 34 | categoryCounts[cat.name] += toolsCount || 0; |
| 35 | } |
| 36 | |
| 37 | // Count news with correct column name |
| 38 | const { count: newsCount, error: newsError } = await supabase |
| 39 | .from('news') |
| 40 | .select('*', { count: 'exact', head: true }) |
| 41 | .eq('category_id', cat.id); // Fixed: using category_id instead of category |
| 42 | |
| 43 | if (!newsError) { |
| 44 | categoryCounts[cat.name] += newsCount || 0; |
| 45 | } |
| 46 | |
| 47 | // Count blog posts (if applicable) |
| 48 | // Check if primary_category_id or category_id is used in your schema |
| 49 | const { count: blogCount, error: blogError } = await supabase |
| 50 | .from('blog_posts') |
| 51 | .select('*', { count: 'exact', head: true }) |
| 52 | .eq('category_id', cat.id); |
| 53 | |
| 54 | if (!blogError) { |
| 55 | categoryCounts[cat.name] += blogCount || 0; |
| 56 | } |
| 57 | } |
| 58 | */ |
| 59 | } |