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

67 lines
1.3 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'
import {
categoryKeys,
tagKeys,
} from './constants'
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
const tags = tagKeys.map((tag) => {
return {
name: tag,
label: t(`pluginTags.tags.${tag}`),
}
})
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 = categoryKeys.map((category) => {
2024-12-12 17:29:25 +08:00
if (category === 'agent') {
return {
name: 'agent_strategy',
2024-12-12 17:29:25 +08:00
label: t(`plugin.category.${category}s`),
}
}
return {
name: category,
label: t(`plugin.category.${category}s`),
}
})
const categoriesMap = categories.reduce((acc, category) => {
acc[category.name] = category
return acc
}, {} as Record<string, Category>)
return {
categories,
categoriesMap,
}
}