You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import type { FC } from 'react'
  2. import { useMemo, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useDebounceFn } from 'ahooks'
  5. import { RiArrowDownSLine } from '@remixicon/react'
  6. import cn from '@/utils/classnames'
  7. import {
  8. PortalToFollowElem,
  9. PortalToFollowElemContent,
  10. PortalToFollowElemTrigger,
  11. } from '@/app/components/base/portal-to-follow-elem'
  12. import Input from '@/app/components/base/input'
  13. import { Tag03 } from '@/app/components/base/icons/src/vender/line/financeAndECommerce'
  14. import Checkbox from '@/app/components/base/checkbox'
  15. import type { Label } from '@/app/components/tools/labels/constant'
  16. import { useTags } from '@/app/components/plugins/hooks'
  17. import { noop } from 'lodash-es'
  18. type LabelSelectorProps = {
  19. value: string[]
  20. onChange: (v: string[]) => void
  21. }
  22. const LabelSelector: FC<LabelSelectorProps> = ({
  23. value,
  24. onChange,
  25. }) => {
  26. const { t } = useTranslation()
  27. const [open, setOpen] = useState(false)
  28. const { tags: labelList } = useTags()
  29. const [keywords, setKeywords] = useState('')
  30. const [searchKeywords, setSearchKeywords] = useState('')
  31. const { run: handleSearch } = useDebounceFn(() => {
  32. setSearchKeywords(keywords)
  33. }, { wait: 500 })
  34. const handleKeywordsChange = (value: string) => {
  35. setKeywords(value)
  36. handleSearch()
  37. }
  38. const filteredLabelList = useMemo(() => {
  39. return labelList.filter(label => label.name.includes(searchKeywords))
  40. }, [labelList, searchKeywords])
  41. const selectedLabels = useMemo(() => {
  42. return value.map(v => labelList.find(l => l.name === v)?.label).join(', ')
  43. }, [value, labelList])
  44. const selectLabel = (label: Label) => {
  45. if (value.includes(label.name))
  46. onChange(value.filter(v => v !== label.name))
  47. else
  48. onChange([...value, label.name])
  49. }
  50. return (
  51. <PortalToFollowElem
  52. open={open}
  53. onOpenChange={setOpen}
  54. placement='bottom-start'
  55. offset={4}
  56. >
  57. <div className='relative'>
  58. <PortalToFollowElemTrigger
  59. onClick={() => setOpen(v => !v)}
  60. className='block'
  61. >
  62. <div className={cn(
  63. 'flex h-10 cursor-pointer items-center gap-1 rounded-lg border-[0.5px] border-transparent bg-components-input-bg-normal px-3 hover:bg-components-input-bg-hover',
  64. open && '!hover:bg-components-input-bg-hover hover:bg-components-input-bg-hover',
  65. )}>
  66. <div title={value.length > 0 ? selectedLabels : ''} className={cn('grow truncate text-[13px] leading-[18px] text-text-secondary', !value.length && '!text-text-quaternary')}>
  67. {!value.length && t('tools.createTool.toolInput.labelPlaceholder')}
  68. {!!value.length && selectedLabels}
  69. </div>
  70. <div className='ml-1 shrink-0 text-text-secondary opacity-60'>
  71. <RiArrowDownSLine className='h-4 w-4' />
  72. </div>
  73. </div>
  74. </PortalToFollowElemTrigger>
  75. <PortalToFollowElemContent className='z-[1040]'>
  76. <div className='relative w-[591px] rounded-lg border-[0.5px] border-components-panel-border bg-components-panel-bg-blur shadow-lg backdrop-blur-[5px]'>
  77. <div className='border-b-[0.5px] border-divider-regular p-2'>
  78. <Input
  79. showLeftIcon
  80. showClearIcon
  81. value={keywords}
  82. onChange={e => handleKeywordsChange(e.target.value)}
  83. onClear={() => handleKeywordsChange('')}
  84. />
  85. </div>
  86. <div className='max-h-[264px] overflow-y-auto p-1'>
  87. {filteredLabelList.map(label => (
  88. <div
  89. key={label.name}
  90. className='flex cursor-pointer items-center gap-2 rounded-lg py-[6px] pl-3 pr-2 hover:bg-components-panel-on-panel-item-bg-hover'
  91. onClick={() => selectLabel(label)}
  92. >
  93. <Checkbox
  94. className='shrink-0'
  95. checked={value.includes(label.name)}
  96. onCheck={noop}
  97. />
  98. <div title={label.label} className='grow truncate text-sm leading-5 text-text-secondary'>{label.label}</div>
  99. </div>
  100. ))}
  101. {!filteredLabelList.length && (
  102. <div className='flex flex-col items-center gap-1 p-3'>
  103. <Tag03 className='h-6 w-6 text-text-quaternary' />
  104. <div className='text-xs leading-[14px] text-text-tertiary'>{t('common.tag.noTag')}</div>
  105. </div>
  106. )}
  107. </div>
  108. </div>
  109. </PortalToFollowElemContent>
  110. </div>
  111. </PortalToFollowElem>
  112. )
  113. }
  114. export default LabelSelector