dify/web/app/components/explore/category.tsx

62 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-05-25 16:59:47 +08:00
'use client'
import type { FC } from 'react'
import React from 'react'
2023-05-25 16:59:47 +08:00
import { useTranslation } from 'react-i18next'
import cn from 'classnames'
2024-02-23 14:31:06 +08:00
import exploreI18n from '@/i18n/en-US/explore'
2023-08-28 19:48:53 +08:00
import type { AppCategory } from '@/models/explore'
2023-05-25 16:59:47 +08:00
const categoryI18n = exploreI18n.category
export type ICategoryProps = {
2023-05-25 16:59:47 +08:00
className?: string
2023-08-28 19:48:53 +08:00
list: AppCategory[]
2023-05-25 16:59:47 +08:00
value: string
onChange: (value: AppCategory | string) => void
/**
* default value for searchparam 'category' in en
*/
allCategoriesEn: string
2023-05-25 16:59:47 +08:00
}
const Category: FC<ICategoryProps> = ({
className,
list,
value,
onChange,
allCategoriesEn,
2023-05-25 16:59:47 +08:00
}) => {
const { t } = useTranslation()
const isAllCategories = !list.includes(value)
const itemClassName = (isSelected: boolean) =>
cn(
isSelected
? 'bg-white text-primary-600 border-gray-200 font-semibold shadow-[0px_1px_2px_rgba(16,24,40,0.05)]'
: 'border-transparent font-medium',
'flex items-center h-7 px-3 border cursor-pointer rounded-lg',
)
2023-05-25 16:59:47 +08:00
return (
<div className={cn(className, 'flex space-x-1 text-[13px] flex-wrap')}>
<div
className={itemClassName(isAllCategories)}
onClick={() => onChange(allCategoriesEn)}
>
{t('explore.apps.allCategories')}
</div>
2023-05-25 16:59:47 +08:00
{list.map(name => (
<div
2023-05-25 16:59:47 +08:00
key={name}
className={itemClassName(name === value)}
onClick={() => onChange(name)}
>
2023-08-28 19:48:53 +08:00
{categoryI18n[name] ? t(`explore.category.${name}`) : name}
2023-05-25 16:59:47 +08:00
</div>
))}
</div>
)
}
2023-05-25 16:59:47 +08:00
export default React.memo(Category)