mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-27 14:49:23 +00:00

some of the requested changes warrant discussion so I haven't applied all of them here. Making a draft PR for now, we can discuss the rest later # Description of Changes See Matts todo list, the crossed out items are what were addressed in this PR --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing) for more details. --------- Co-authored-by: James Brunton <jbrunton96@gmail.com>
114 lines
3.9 KiB
TypeScript
114 lines
3.9 KiB
TypeScript
import { useMemo } from 'react';
|
|
|
|
import { SUBCATEGORY_ORDER, SubcategoryId, ToolCategoryId, ToolRegistryEntry } from '../data/toolsTaxonomy';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
type SubcategoryIdMap = {
|
|
[subcategoryId in SubcategoryId]: Array<{ id: string /* FIX ME: Should be ToolId */; tool: ToolRegistryEntry }>;
|
|
}
|
|
|
|
type GroupedTools = {
|
|
[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[];
|
|
};
|
|
|
|
export function useToolSections(filteredTools: [string /* FIX ME: Should be ToolId */, ToolRegistryEntry][]) {
|
|
const { t } = useTranslation();
|
|
|
|
const groupedTools = useMemo(() => {
|
|
const grouped = {} as GroupedTools;
|
|
filteredTools.forEach(([id, tool]) => {
|
|
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 });
|
|
});
|
|
return grouped;
|
|
}, [filteredTools]);
|
|
|
|
const sections: ToolSection[] = useMemo(() => {
|
|
const getOrderIndex = (id: SubcategoryId) => {
|
|
const idx = SUBCATEGORY_ORDER.indexOf(id);
|
|
return idx === -1 ? Number.MAX_SAFE_INTEGER : idx;
|
|
};
|
|
|
|
const quick = {} as SubcategoryIdMap;
|
|
const all = {} as SubcategoryIdMap;
|
|
|
|
Object.entries(groupedTools).forEach(([c, subs]) => {
|
|
const categoryId = c as ToolCategoryId;
|
|
|
|
Object.entries(subs).forEach(([s, tools]) => {
|
|
const subcategoryId = s as SubcategoryId;
|
|
if (!all[subcategoryId]) all[subcategoryId] = [];
|
|
all[subcategoryId].push(...tools);
|
|
});
|
|
|
|
if (categoryId === ToolCategoryId.RECOMMENDED_TOOLS) {
|
|
Object.entries(subs).forEach(([s, tools]) => {
|
|
const subcategoryId = s as SubcategoryId;
|
|
if (!quick[subcategoryId]) quick[subcategoryId] = [];
|
|
// 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);
|
|
});
|
|
}
|
|
});
|
|
|
|
const sortSubs = (obj: SubcategoryIdMap) =>
|
|
Object.entries(obj)
|
|
.sort(([a], [b]) => {
|
|
const aId = a as SubcategoryId;
|
|
const bId = b as SubcategoryId;
|
|
const ai = getOrderIndex(aId);
|
|
const bi = getOrderIndex(bId);
|
|
if (ai !== bi) return ai - bi;
|
|
return aId.localeCompare(bId);
|
|
})
|
|
.map(([subcategoryId, tools]) => ({ subcategoryId, tools } as SubcategoryGroup));
|
|
|
|
const built: ToolSection[] = [
|
|
{ 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]);
|
|
|
|
const searchGroups: SubcategoryGroup[] = useMemo(() => {
|
|
const subMap = {} as SubcategoryIdMap;
|
|
const seen = new Set<string /* FIX ME: Should be ToolId */>();
|
|
filteredTools.forEach(([id, tool]) => {
|
|
const toolId = id as string /* FIX ME: Should be ToolId */;
|
|
if (seen.has(toolId)) return;
|
|
seen.add(toolId);
|
|
const sub = tool.subcategoryId;
|
|
if (!subMap[sub]) subMap[sub] = [];
|
|
subMap[sub].push({ id: toolId, tool });
|
|
});
|
|
return Object.entries(subMap)
|
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
.map(([subcategoryId, tools]) => ({ subcategoryId, tools } as SubcategoryGroup));
|
|
}, [filteredTools]);
|
|
|
|
return { sections, searchGroups };
|
|
}
|
|
|
|
|