dify/web/app/components/plugins/hooks.ts

129 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-11-05 15:07:24 +08:00
import { useTranslation } from 'react-i18next'
2024-11-06 11:55:19 +08:00
import type { TFunction } from 'i18next'
2024-11-05 15:07:24 +08:00
2024-11-05 17:35:18 +08:00
type Tag = {
name: string
label: string
}
2024-11-06 11:55:19 +08:00
export const useTags = (translateFromOut?: TFunction) => {
const { t: translation } = useTranslation()
const t = translateFromOut || translation
2024-11-05 15:07:24 +08:00
2024-11-05 17:35:18 +08:00
const tags = [
2024-11-05 15:07:24 +08:00
{
name: 'search',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.search'),
2024-11-05 15:07:24 +08:00
},
{
name: 'image',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.image'),
2024-11-05 15:07:24 +08:00
},
{
name: 'videos',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.videos'),
2024-11-05 15:07:24 +08:00
},
{
name: 'weather',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.weather'),
2024-11-05 15:07:24 +08:00
},
{
name: 'finance',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.finance'),
2024-11-05 15:07:24 +08:00
},
{
name: 'design',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.design'),
2024-11-05 15:07:24 +08:00
},
{
name: 'travel',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.travel'),
2024-11-05 15:07:24 +08:00
},
{
name: 'social',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.social'),
2024-11-05 15:07:24 +08:00
},
{
name: 'news',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.news'),
2024-11-05 15:07:24 +08:00
},
{
name: 'medical',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.medical'),
2024-11-05 15:07:24 +08:00
},
{
name: 'productivity',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.productivity'),
2024-11-05 15:07:24 +08:00
},
{
name: 'education',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.education'),
2024-11-05 15:07:24 +08:00
},
{
name: 'business',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.business'),
2024-11-05 15:07:24 +08:00
},
{
name: 'entertainment',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.entertainment'),
2024-11-05 15:07:24 +08:00
},
{
name: 'utilities',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.utilities'),
2024-11-05 15:07:24 +08:00
},
{
name: 'other',
2024-11-05 17:35:18 +08:00
label: t('pluginTags.tags.other'),
2024-11-05 15:07:24 +08:00
},
]
2024-11-05 17:35:18 +08:00
const tagsMap = tags.reduce((acc, tag) => {
acc[tag.name] = tag
return acc
}, {} as Record<string, Tag>)
return {
tags,
tagsMap,
}
2024-11-05 15:07:24 +08:00
}
type Category = {
name: string
label: string
}
export const useCategories = (translateFromOut?: TFunction) => {
const { t: translation } = useTranslation()
const t = translateFromOut || translation
const categories = [
{
name: 'model',
label: t('plugin.category.models'),
},
{
name: 'tool',
label: t('plugin.category.tools'),
},
{
name: 'extension',
label: t('plugin.category.extensions'),
},
{
name: 'bundle',
label: t('plugin.category.bundles'),
},
]
const categoriesMap = categories.reduce((acc, category) => {
acc[category.name] = category
return acc
}, {} as Record<string, Category>)
return {
categories,
categoriesMap,
}
}