Fix category display and tool counts in web UI
- Categories API now includes dynamic categories from database (categories used by tools but not in predefined list) - Add total_tools to categories API meta for accurate All Tools count - Fix web routes to use total_tools instead of summing category counts - Dynamic categories get auto-generated descriptions This fixes: - "All Tools" showing 2 instead of 4 - Categories like "code-analysis" and "education" not appearing - Incorrect category counts in sidebar Co-Authored-By: Claude Opus 4.5 <[email protected]>
This commit is contained in:
@@ -850,17 +850,26 @@ def create_app() -> Flask:
|
||||
categories_yaml = get_repo_dir() / "categories" / "categories.yaml"
|
||||
if categories_yaml.exists():
|
||||
categories_payload = yaml.safe_load(categories_yaml.read_text(encoding="utf-8")) or {}
|
||||
categories = (categories_payload or {}).get("categories", [])
|
||||
predefined_categories = (categories_payload or {}).get("categories", [])
|
||||
|
||||
# Get counts for all categories in the database
|
||||
counts = query_all(
|
||||
g.db,
|
||||
"SELECT category, COUNT(DISTINCT owner || '/' || name) AS total FROM tools GROUP BY category",
|
||||
)
|
||||
count_map = {row["category"]: row["total"] for row in counts}
|
||||
|
||||
# Calculate total tools across all categories
|
||||
total_tools = sum(row["total"] for row in counts)
|
||||
|
||||
# Build data from predefined categories
|
||||
predefined_names = set()
|
||||
data = []
|
||||
for cat in categories:
|
||||
for cat in predefined_categories:
|
||||
name = cat.get("name")
|
||||
if not name:
|
||||
continue
|
||||
predefined_names.add(name)
|
||||
data.append({
|
||||
"name": name,
|
||||
"description": cat.get("description"),
|
||||
@@ -868,6 +877,18 @@ def create_app() -> Flask:
|
||||
"tool_count": count_map.get(name, 0),
|
||||
})
|
||||
|
||||
# Add any categories from database that aren't in predefined list
|
||||
for category_name, count in count_map.items():
|
||||
if category_name and category_name not in predefined_names:
|
||||
# Auto-generate display info for dynamic categories
|
||||
display_name = category_name.replace("-", " ").title()
|
||||
data.append({
|
||||
"name": category_name,
|
||||
"description": f"Tools in the {display_name} category",
|
||||
"icon": None,
|
||||
"tool_count": count,
|
||||
})
|
||||
|
||||
reverse = order == "desc"
|
||||
if sort == "tool_count":
|
||||
data.sort(key=lambda item: item["tool_count"], reverse=reverse)
|
||||
@@ -879,7 +900,10 @@ def create_app() -> Flask:
|
||||
end = start + per_page
|
||||
sliced = data[start:end]
|
||||
|
||||
response = jsonify({"data": sliced, "meta": paginate(page, per_page, total)})
|
||||
meta = paginate(page, per_page, total)
|
||||
meta["total_tools"] = total_tools # Add total tools count to meta
|
||||
|
||||
response = jsonify({"data": sliced, "meta": meta})
|
||||
response.headers["Cache-Control"] = "max-age=3600"
|
||||
return response
|
||||
|
||||
|
||||
Reference in New Issue
Block a user