AI Resource Pro / website / fix-dashboard-code.md
Updating Dashboard Code to Use instance_count
Since the database column has been renamed from tool_count to instance_count, you need to update your dashboard code to use the new column name. Here are the specific changes needed:
1. Update the Dashboard Query in src/app/admin/dashboard/page.tsx
Around line 295, change this:
// Get category counts from the categories table (tools)
const { data: categoryData, error: categoryError } = await supabase
.from('categories')
.select('id, name, tool_count') // OLD: using tool_count
.order('name');
if (!categoryError && categoryData) {
// Initialize count for each category
categoryData.forEach(cat => {
categoryCounts[cat.name] = cat.tool_count || 0; // OLD: using tool_count
});
// Count blog posts with each category
for (const cat of categoryData) {
// ...more code that adds additional counts
}
}
To this:
// Get category counts from the categories table
const { data: categoryData, error: categoryError } = await supabase
.from('categories')
.select('id, name, instance_count') // NEW: using instance_count
.order('name');
if (!categoryError && categoryData) {
// Initialize count for each category
categoryData.forEach(cat => {
categoryCounts[cat.name] = cat.instance_count || 0; // NEW: using instance_count
});
// REMOVE all these additional counting lines to avoid double counting
// If the code below was originally intended to supplement the count,
// you should ensure the SQL update script counts all instances properly instead
/*
// Count blog posts with each category
for (const cat of categoryData) {
// Remove this entire for loop
}
*/
}
2. Update Any Other References to tool_count
Search for any other instances of tool_count in your code and replace them with instance_count. Common places to check:
src/components/admin/ClientCategoryManager.tsx- API routes that add or remove tools/news items
3. Fix the News Query Issue
If you still need to count news items in your dashboard page (though this is not recommended if the database already has the correct counts), use the correct column name:
// Count news items with this category
const { count: newsCount, error: newsCountError } = await supabase
.from('news')
.select('*', { count: 'exact', head: true })
.eq('category_id', cat.id); // NEW: using category_id instead of category
Testing
After making these changes:
- Run the database script (
fix-instance-count.sql) to ensure the database counts are correct - Update the code as described above
- Restart your development server
- Visit the admin dashboard to confirm the counts appear correctly
- Add a new tool or news item and verify the count increments properly
- Remove a tool or news item and verify the count decrements properly
Make sure your API routes that add/remove items also update the instance_count column in the categories table.