Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.5 KB
Raw Blame
'use client'
import { cn } from '@langgenius/dify-ui/cn'
import { RadioGroup, RadioItem } from '@langgenius/dify-ui/radio-group'
import { useTranslation } from 'react-i18next'
import exploreI18n from '@/i18n/locales/en-US/explore.json'

type ICategoryProps = {
  className?: string
  list: string[]
  value: string
  onChange: (value: string) => void
  /**
   * default value for search param 'category' in en
   */
  allCategoriesEn: string
}

function Category({ className, list, value, onChange, allCategoriesEn }: ICategoryProps) {
  const { t } = useTranslation(['explore'])
  const isAllCategories = !list.includes(value) || value === allCategoriesEn
  const selectedCategory = isAllCategories ? allCategoriesEn : value

  const renderCategoryName = (name: string) => {
    const categoryKey = `category.${name}` as Extract<
      keyof typeof exploreI18n,
      `category.${string}`
    >
    return categoryKey in exploreI18n ? t(($) => $[categoryKey], { ns: 'explore' }) : name
  }

  const handleValueChange = (nextCategory: string) => {
    if (nextCategory) onChange(nextCategory)
  }

  return (
    <RadioGroup
      aria-label={t(($) => $['tryApp.category'], { ns: 'explore' })}
      className={cn(
        className,
        'flex max-w-full flex-wrap items-start gap-1 overflow-visible rounded-none bg-transparent p-0 text-[13px]',
      )}
      value={selectedCategory}
      onValueChange={handleValueChange}
    >
      {[
        {
          name: allCategoriesEn,
          label: t(($) => $['apps.allCategories'], { ns: 'explore' }),
          isAll: true,
        },
        ...list
          .filter((name) => name !== allCategoriesEn)
          .map((name) => ({
            name,
            label: renderCategoryName(name),
            isAll: false,
          })),
      ].map((item) => (
        <RadioItem
          key={item.isAll ? 'all' : item.name}
          value={item.name}
          nativeButton
          render={<button type="button" />}
          className="h-8 min-w-12 shrink-0 cursor-pointer touch-manipulation rounded-lg border-0 px-2.5 py-2 text-center system-sm-medium text-text-tertiary shadow-none hover:bg-state-base-hover focus-visible:ring-2 focus-visible:ring-state-accent-solid focus-visible:outline-hidden data-checked:border-0 data-checked:bg-state-base-active data-checked:system-sm-semibold data-checked:text-text-secondary data-checked:shadow-none"
        >
          {item.label}
        </RadioItem>
      ))}
    </RadioGroup>
  )
}

export default Category