2025-08-19 13:31:09 +01:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
|
2025-08-22 13:53:06 +01:00
|
|
|
import { SUBCATEGORY_ORDER, SubcategoryId, ToolCategoryId, ToolRegistryEntry } from '../data/toolsTaxonomy';
|
2025-08-19 13:31:09 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
2025-08-22 13:53:06 +01:00
|
|
|
type SubcategoryIdMap = {
|
|
|
|
[subcategoryId in SubcategoryId]: Array<{ id: string /* FIX ME: Should be ToolId */; tool: ToolRegistryEntry }>;
|
|
|
|
}
|
|
|
|
|
2025-08-19 13:31:09 +01:00
|
|
|
type GroupedTools = {
|
2025-08-22 13:53:06 +01:00
|
|
|
[categoryId in ToolCategoryId]: SubcategoryIdMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface SubcategoryGroup {
|
|
|
|
subcategoryId: SubcategoryId;
|
|
|
|
tools: {
|
|
|
|
id: string /* FIX ME: Should be ToolId */;
|
|
|
|
tool: ToolRegistryEntry;
|
|
|
|
}[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ToolSectionKey = 'quick' | 'all';
|
|
|
|
|
|
|
|
export interface ToolSection {
|
|
|
|
key: ToolSectionKey;
|
|
|
|
title: string;
|
|
|
|
subcategories: SubcategoryGroup[];
|
2025-08-19 13:31:09 +01:00
|
|
|
};
|
|
|
|
|
2025-08-22 13:53:06 +01:00
|
|
|
export function useToolSections(filteredTools: [string /* FIX ME: Should be ToolId */, ToolRegistryEntry][]) {
|
2025-08-19 13:31:09 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
const groupedTools = useMemo(() => {
|
2025-08-22 13:53:06 +01:00
|
|
|
const grouped = {} as GroupedTools;
|
2025-08-19 13:31:09 +01:00
|
|
|
filteredTools.forEach(([id, tool]) => {
|
2025-08-22 13:53:06 +01:00
|
|
|
const categoryId = tool.categoryId;
|
|
|
|
const subcategoryId = tool.subcategoryId;
|
|
|
|
if (!grouped[categoryId]) grouped[categoryId] = {} as SubcategoryIdMap;
|
|
|
|
if (!grouped[categoryId][subcategoryId]) grouped[categoryId][subcategoryId] = [];
|
|
|
|
grouped[categoryId][subcategoryId].push({ id, tool });
|
2025-08-19 13:31:09 +01:00
|
|
|
});
|
|
|
|
return grouped;
|
|
|
|
}, [filteredTools]);
|
|
|
|
|
2025-08-22 13:53:06 +01:00
|
|
|
const sections: ToolSection[] = useMemo(() => {
|
|
|
|
const getOrderIndex = (id: SubcategoryId) => {
|
|
|
|
const idx = SUBCATEGORY_ORDER.indexOf(id);
|
2025-08-19 13:31:09 +01:00
|
|
|
return idx === -1 ? Number.MAX_SAFE_INTEGER : idx;
|
|
|
|
};
|
|
|
|
|
2025-08-22 13:53:06 +01:00
|
|
|
const quick = {} as SubcategoryIdMap;
|
|
|
|
const all = {} as SubcategoryIdMap;
|
2025-08-19 13:31:09 +01:00
|
|
|
|
2025-08-22 13:53:06 +01:00
|
|
|
Object.entries(groupedTools).forEach(([c, subs]) => {
|
|
|
|
const categoryId = c as ToolCategoryId;
|
2025-08-19 13:31:09 +01:00
|
|
|
|
2025-08-22 13:53:06 +01:00
|
|
|
Object.entries(subs).forEach(([s, tools]) => {
|
|
|
|
const subcategoryId = s as SubcategoryId;
|
|
|
|
if (!all[subcategoryId]) all[subcategoryId] = [];
|
|
|
|
all[subcategoryId].push(...tools);
|
2025-08-19 13:31:09 +01:00
|
|
|
});
|
|
|
|
|
2025-08-22 13:53:06 +01:00
|
|
|
if (categoryId === ToolCategoryId.RECOMMENDED_TOOLS) {
|
|
|
|
Object.entries(subs).forEach(([s, tools]) => {
|
|
|
|
const subcategoryId = s as SubcategoryId;
|
|
|
|
if (!quick[subcategoryId]) quick[subcategoryId] = [];
|
2025-08-26 16:31:20 +01:00
|
|
|
// Only include ready tools (have a component or external link) in Quick Access
|
|
|
|
const readyTools = tools.filter(({ tool }) => tool.component !== null || !!tool.link);
|
|
|
|
quick[subcategoryId].push(...readyTools);
|
2025-08-19 13:31:09 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-08-22 13:53:06 +01:00
|
|
|
const sortSubs = (obj: SubcategoryIdMap) =>
|
2025-08-19 13:31:09 +01:00
|
|
|
Object.entries(obj)
|
|
|
|
.sort(([a], [b]) => {
|
2025-08-22 13:53:06 +01:00
|
|
|
const aId = a as SubcategoryId;
|
|
|
|
const bId = b as SubcategoryId;
|
|
|
|
const ai = getOrderIndex(aId);
|
|
|
|
const bi = getOrderIndex(bId);
|
2025-08-19 13:31:09 +01:00
|
|
|
if (ai !== bi) return ai - bi;
|
2025-08-22 13:53:06 +01:00
|
|
|
return aId.localeCompare(bId);
|
2025-08-19 13:31:09 +01:00
|
|
|
})
|
2025-08-22 13:53:06 +01:00
|
|
|
.map(([subcategoryId, tools]) => ({ subcategoryId, tools } as SubcategoryGroup));
|
2025-08-19 13:31:09 +01:00
|
|
|
|
2025-08-22 13:53:06 +01:00
|
|
|
const built: ToolSection[] = [
|
2025-08-19 13:31:09 +01:00
|
|
|
{ key: 'quick', title: t('toolPicker.quickAccess', 'QUICK ACCESS'), subcategories: sortSubs(quick) },
|
|
|
|
{ key: 'all', title: t('toolPicker.allTools', 'ALL TOOLS'), subcategories: sortSubs(all) }
|
|
|
|
];
|
|
|
|
|
|
|
|
return built.filter(section => section.subcategories.some(sc => sc.tools.length > 0));
|
|
|
|
}, [groupedTools]);
|
|
|
|
|
2025-08-22 13:53:06 +01:00
|
|
|
const searchGroups: SubcategoryGroup[] = useMemo(() => {
|
|
|
|
const subMap = {} as SubcategoryIdMap;
|
|
|
|
const seen = new Set<string /* FIX ME: Should be ToolId */>();
|
2025-08-19 13:31:09 +01:00
|
|
|
filteredTools.forEach(([id, tool]) => {
|
2025-08-22 13:53:06 +01:00
|
|
|
const toolId = id as string /* FIX ME: Should be ToolId */;
|
|
|
|
if (seen.has(toolId)) return;
|
|
|
|
seen.add(toolId);
|
|
|
|
const sub = tool.subcategoryId;
|
2025-08-19 13:31:09 +01:00
|
|
|
if (!subMap[sub]) subMap[sub] = [];
|
2025-08-22 13:53:06 +01:00
|
|
|
subMap[sub].push({ id: toolId, tool });
|
2025-08-19 13:31:09 +01:00
|
|
|
});
|
|
|
|
return Object.entries(subMap)
|
|
|
|
.sort(([a], [b]) => a.localeCompare(b))
|
2025-08-22 13:53:06 +01:00
|
|
|
.map(([subcategoryId, tools]) => ({ subcategoryId, tools } as SubcategoryGroup));
|
2025-08-19 13:31:09 +01:00
|
|
|
}, [filteredTools]);
|
|
|
|
|
|
|
|
return { sections, searchGroups };
|
|
|
|
}
|
|
|
|
|
|
|
|
|