category.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use client'
  2. import { useRef } from 'react'
  3. import cn from 'classnames'
  4. import { useTranslation } from 'react-i18next'
  5. import { useContext } from 'use-context-selector'
  6. import { useMount } from 'ahooks'
  7. import { Apps02 } from '@/app/components/base/icons/src/vender/line/others'
  8. import I18n from '@/context/i18n'
  9. import { getLanguage } from '@/i18n/language'
  10. import { useStore as useLabelStore } from '@/app/components/tools/labels/store'
  11. import { fetchLabelList } from '@/service/tools'
  12. type Props = {
  13. value: string
  14. onSelect: (type: string) => void
  15. }
  16. const Icon = ({ svgString, active }: { svgString: string; active: boolean }) => {
  17. const svgRef = useRef<SVGSVGElement | null>(null)
  18. const SVGParsor = (svg: string) => {
  19. if (!svg)
  20. return null
  21. const parser = new DOMParser()
  22. const doc = parser.parseFromString(svg, 'image/svg+xml')
  23. console.log(doc.documentElement)
  24. return doc.documentElement
  25. }
  26. useMount(() => {
  27. const svgElement = SVGParsor(svgString)
  28. if (svgRef.current && svgElement)
  29. svgRef.current.appendChild(svgElement)
  30. })
  31. return <svg className={cn('w-4 h-4 text-gray-700', active && '!text-primary-600')} ref={svgRef} />
  32. }
  33. const Category = ({
  34. value,
  35. onSelect,
  36. }: Props) => {
  37. const { t } = useTranslation()
  38. const { locale } = useContext(I18n)
  39. const language = getLanguage(locale)
  40. const labelList = useLabelStore(s => s.labelList)
  41. const setLabelList = useLabelStore(s => s.setLabelList)
  42. useMount(() => {
  43. fetchLabelList().then((res) => {
  44. setLabelList(res)
  45. })
  46. })
  47. return (
  48. <div className='mb-3'>
  49. <div className='px-3 py-0.5 text-gray-500 text-xs leading-[18px] font-medium'>{t('tools.addToolModal.category').toLocaleUpperCase()}</div>
  50. <div className={cn('mb-0.5 p-1 pl-3 flex items-center cursor-pointer text-gray-700 text-sm leading-5 rounded-lg hover:bg-white', value === '' && '!bg-white !text-primary-600 font-medium')} onClick={() => onSelect('')}>
  51. <Apps02 className='shrink-0 w-4 h-4 mr-2' />
  52. {t('tools.type.all')}
  53. </div>
  54. {labelList.map(label => (
  55. <div key={label.name} title={label.label[language]} className={cn('mb-0.5 p-1 pl-3 flex items-center cursor-pointer text-gray-700 text-sm leading-5 rounded-lg hover:bg-white truncate overflow-hidden', value === label.name && '!bg-white !text-primary-600 font-medium')} onClick={() => onSelect(label.name)}>
  56. <div className='shrink-0 w-4 h-4 mr-2'>
  57. <Icon active={value === label.name} svgString={label.icon} />
  58. </div>
  59. {label.label[language]}
  60. </div>
  61. ))}
  62. </div>
  63. )
  64. }
  65. export default Category