category.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import cn from 'classnames'
  6. import exploreI18n from '@/i18n/en-US/explore'
  7. import type { AppCategory } from '@/models/explore'
  8. const categoryI18n = exploreI18n.category
  9. export type ICategoryProps = {
  10. className?: string
  11. list: AppCategory[]
  12. value: string
  13. onChange: (value: AppCategory | string) => void
  14. /**
  15. * default value for searchparam 'category' in en
  16. */
  17. allCategoriesEn: string
  18. }
  19. const Category: FC<ICategoryProps> = ({
  20. className,
  21. list,
  22. value,
  23. onChange,
  24. allCategoriesEn,
  25. }) => {
  26. const { t } = useTranslation()
  27. const isAllCategories = !list.includes(value)
  28. const itemClassName = (isSelected: boolean) =>
  29. cn(
  30. isSelected
  31. ? 'bg-white text-primary-600 border-gray-200 font-semibold shadow-[0px_1px_2px_rgba(16,24,40,0.05)]'
  32. : 'border-transparent font-medium',
  33. 'flex items-center h-7 px-3 border cursor-pointer rounded-lg',
  34. )
  35. return (
  36. <div className={cn(className, 'flex space-x-1 text-[13px] flex-wrap')}>
  37. <div
  38. className={itemClassName(isAllCategories)}
  39. onClick={() => onChange(allCategoriesEn)}
  40. >
  41. {t('explore.apps.allCategories')}
  42. </div>
  43. {list.map(name => (
  44. <div
  45. key={name}
  46. className={itemClassName(name === value)}
  47. onClick={() => onChange(name)}
  48. >
  49. {categoryI18n[name] ? t(`explore.category.${name}`) : name}
  50. </div>
  51. ))}
  52. </div>
  53. )
  54. }
  55. export default React.memo(Category)