mirror of
https://github.com/iib0011/omni-tools.git
synced 2026-11-25 23:24:14 +03:00
feat: add alternativeCategories support to tools and update related functions
This commit is contained in:
1 parent
5f8f4c7b92
commit
ec237461f7
9 files changed
+97
-8
No files matched your search
@@ -10,6 +10,7 @@ export const tool = defineTool('converters', {
|
||||
},
|
||||
path: 'audio-converter',
|
||||
icon: 'mdi:music-note-outline',
|
||||
alternativeCategories: ['audio'],
|
||||
keywords: ['audio', 'converter'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
@@ -11,6 +11,7 @@ export const tool = defineTool('converters', {
|
||||
path: 'convert-to-jpg',
|
||||
icon: 'ph:file-jpg-thin',
|
||||
|
||||
alternativeCategories: ['image-generic'],
|
||||
keywords: ['convert', 'jpg', 'images', 'quality', 'compression'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
@@ -11,6 +11,7 @@ export const tool = defineTool('converters', {
|
||||
path: 'convert-to-webp',
|
||||
icon: 'iconoir:webp-format',
|
||||
|
||||
alternativeCategories: ['image-generic'],
|
||||
keywords: ['convert', 'jpg', 'jpeg', 'png', 'webp', 'compression'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
@@ -4,6 +4,7 @@ import { lazy } from 'react';
|
||||
export const tool = defineTool('converters', {
|
||||
path: 'video-to-gif',
|
||||
icon: 'material-symbols:gif',
|
||||
alternativeCategories: ['gif', 'video'],
|
||||
keywords: ['video', 'gif', 'convert', 'animated', 'image'],
|
||||
component: lazy(() => import('./index')),
|
||||
i18n: {
|
||||
|
||||
@@ -6,6 +6,7 @@ export const meta = defineTool('pdf', {
|
||||
component: lazy(() => import('./index')),
|
||||
keywords: ['pdf', 'image', 'extract'],
|
||||
path: 'extract-image-from-pdf',
|
||||
alternativeCategories: ['image-generic'],
|
||||
i18n: {
|
||||
name: 'pdf:extractImagesFromPdf.title',
|
||||
description: 'pdf:extractImagesFromPdf.description',
|
||||
|
||||
@@ -13,6 +13,7 @@ export const tool = defineTool('pdf', {
|
||||
path: 'pdf-to-png',
|
||||
icon: 'mdi:image-multiple', // Iconify icon ID
|
||||
|
||||
alternativeCategories: ['png'],
|
||||
keywords: ['pdf', 'png', 'convert', 'image', 'extract', 'pages'],
|
||||
component: lazy(() => import('./index'))
|
||||
});
|
||||
@@ -11,6 +11,7 @@ export interface ToolMeta {
|
||||
component: LazyExoticComponent<JSXElementConstructor<ToolComponentProps>>;
|
||||
keywords: string[];
|
||||
icon: IconifyIcon | string;
|
||||
alternativeCategories?: ToolCategory[];
|
||||
i18n: {
|
||||
name: FullI18nKey;
|
||||
description: FullI18nKey;
|
||||
@@ -38,6 +39,7 @@ export type ToolCategory =
|
||||
|
||||
export interface DefinedTool {
|
||||
type: ToolCategory;
|
||||
alternativeCategories?: ToolCategory[];
|
||||
path: string;
|
||||
name: FullI18nKey;
|
||||
description: FullI18nKey;
|
||||
@@ -57,10 +59,12 @@ export const defineTool = (
|
||||
basePath: ToolCategory,
|
||||
options: ToolMeta
|
||||
): DefinedTool => {
|
||||
const { icon, path, keywords, component, i18n } = options;
|
||||
const { icon, path, keywords, component, i18n, alternativeCategories } =
|
||||
options;
|
||||
const Component = component;
|
||||
return {
|
||||
type: basePath,
|
||||
alternativeCategories,
|
||||
path: `${basePath}/${path}`,
|
||||
name: i18n.name,
|
||||
icon,
|
||||
|
||||
+55
-1
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import type { TFunction } from 'i18next';
|
||||
import { filterTools } from './index';
|
||||
import { filterTools, getToolCategories, getToolsByCategory } from './index';
|
||||
import type { DefinedTool } from './defineTool';
|
||||
import type { FullI18nKey, I18nNamespaces } from '../i18n';
|
||||
|
||||
@@ -48,6 +48,19 @@ const otherPdfTool = {
|
||||
component: (() => null) as unknown,
|
||||
userTypes: ['generalUsers']
|
||||
} as unknown as DefinedTool;
|
||||
|
||||
const pdfToolWithAlternativeCategory = {
|
||||
type: 'pdf',
|
||||
alternativeCategories: ['string'],
|
||||
path: 'pdf/pdf-to-text',
|
||||
name: 'pdf:pdfToText.title' as FullI18nKey,
|
||||
description: 'pdf:pdfToText.description' as FullI18nKey,
|
||||
shortDescription: 'pdf:pdfToText.shortDescription' as FullI18nKey,
|
||||
icon: 'icon',
|
||||
keywords: ['pdf', 'text'],
|
||||
component: (() => null) as unknown,
|
||||
userTypes: ['generalUsers']
|
||||
} as unknown as DefinedTool;
|
||||
type NamespacedT = TFunction<I18nNamespaces[]>;
|
||||
|
||||
const enTranslations: Record<string, string> = {
|
||||
@@ -127,3 +140,44 @@ describe('filterTools token-based search', () => {
|
||||
expect(result[0]).toBe(mergePdfTool);
|
||||
});
|
||||
});
|
||||
|
||||
describe('alternative categories', () => {
|
||||
const tEn = makeT(enTranslations);
|
||||
const tools = [mergePdfTool, base64Tool, pdfToolWithAlternativeCategory];
|
||||
|
||||
it('returns the primary category followed by the alternative ones', () => {
|
||||
expect(getToolCategories(base64Tool)).toEqual(['string']);
|
||||
expect(getToolCategories(pdfToolWithAlternativeCategory)).toEqual([
|
||||
'pdf',
|
||||
'string'
|
||||
]);
|
||||
});
|
||||
|
||||
it('lists a tool in the alternative categories it declared', () => {
|
||||
const categories = getToolsByCategory([], tEn, tools);
|
||||
const stringCategory = categories.find(({ type }) => type === 'string');
|
||||
|
||||
expect(stringCategory?.tools).toContain(pdfToolWithAlternativeCategory);
|
||||
});
|
||||
|
||||
it('keeps listing a tool in its primary category', () => {
|
||||
const categories = getToolsByCategory([], tEn, tools);
|
||||
const pdfCategory = categories.find(({ type }) => type === 'pdf');
|
||||
|
||||
expect(pdfCategory?.tools).toContain(pdfToolWithAlternativeCategory);
|
||||
});
|
||||
|
||||
it('does not duplicate a tool declaring its own category as alternative', () => {
|
||||
const duplicatedTool = {
|
||||
...mergePdfTool,
|
||||
alternativeCategories: ['pdf']
|
||||
} as unknown as DefinedTool;
|
||||
|
||||
const categories = getToolsByCategory([], tEn, [duplicatedTool]);
|
||||
const pdfCategory = categories.find(({ type }) => type === 'pdf');
|
||||
|
||||
expect(
|
||||
pdfCategory?.tools.filter((tool) => tool === duplicatedTool)
|
||||
).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
+31
-6
@@ -348,9 +348,17 @@ export const filterTools = (
|
||||
return scored.map(({ tool }) => tool);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns every category a tool belongs to: its primary category first,
|
||||
* followed by the alternative categories it declared.
|
||||
*/
|
||||
export const getToolCategories = (tool: DefinedTool): ToolCategory[] =>
|
||||
Array.from(new Set([tool.type, ...(tool.alternativeCategories ?? [])]));
|
||||
|
||||
export const getToolsByCategory = (
|
||||
userTypes: UserType[] = [],
|
||||
t: TFunction<I18nNamespaces[]>
|
||||
t: TFunction<I18nNamespaces[]>,
|
||||
allTools: DefinedTool[] = tools
|
||||
): {
|
||||
title: string;
|
||||
rawTitle: string;
|
||||
@@ -361,11 +369,28 @@ export const getToolsByCategory = (
|
||||
tools: DefinedTool[];
|
||||
userTypes: UserType[]; // <-- Add this line
|
||||
}[] => {
|
||||
const groupedByType: Partial<Record<ToolCategory, DefinedTool[]>> =
|
||||
Object.groupBy(tools, ({ type }) => type);
|
||||
const groupedByType: Partial<Record<ToolCategory, DefinedTool[]>> = {};
|
||||
|
||||
const addToolToCategory = (category: ToolCategory, tool: DefinedTool) => {
|
||||
groupedByType[category] = groupedByType[category] ?? [];
|
||||
groupedByType[category]!.push(tool);
|
||||
};
|
||||
|
||||
// Native tools come first so the "example" tool of a category stays a native one
|
||||
for (const tool of allTools) {
|
||||
addToolToCategory(tool.type, tool);
|
||||
}
|
||||
|
||||
// Tools are then added to the alternative categories they declared
|
||||
for (const tool of allTools) {
|
||||
for (const category of tool.alternativeCategories ?? []) {
|
||||
if (category === tool.type) continue;
|
||||
addToolToCategory(category, tool);
|
||||
}
|
||||
}
|
||||
|
||||
return (Object.entries(groupedByType) as Entries<typeof groupedByType>)
|
||||
.map(([type, tools]) => {
|
||||
.map(([type, categoryTools]) => {
|
||||
const categoryConfig = categoriesConfig.find(
|
||||
(config) => config.type === type
|
||||
);
|
||||
@@ -373,8 +398,8 @@ export const getToolsByCategory = (
|
||||
// Filter tools by user types if specified
|
||||
const filteredTools =
|
||||
userTypes.length > 0
|
||||
? filterToolsByUserTypes(tools ?? [], userTypes)
|
||||
: tools ?? [];
|
||||
? filterToolsByUserTypes(categoryTools ?? [], userTypes)
|
||||
: categoryTools ?? [];
|
||||
|
||||
// Aggregate unique userTypes from all tools in this category
|
||||
const aggregatedUserTypes = Array.from(
|
||||
|
||||
Reference in new issue
Block a user