| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  | import type { FC } from 'react' | 
					
						
							|  |  |  | import { useMemo, useState } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import { useDebounceFn } from 'ahooks' | 
					
						
							| 
									
										
										
										
											2024-06-20 11:05:08 +08:00
										 |  |  | import { RiArrowDownSLine } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   PortalToFollowElem, | 
					
						
							|  |  |  |   PortalToFollowElemContent, | 
					
						
							|  |  |  |   PortalToFollowElemTrigger, | 
					
						
							|  |  |  | } from '@/app/components/base/portal-to-follow-elem' | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import Input from '@/app/components/base/input' | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  | import { Tag03 } from '@/app/components/base/icons/src/vender/line/financeAndECommerce' | 
					
						
							|  |  |  | import Checkbox from '@/app/components/base/checkbox' | 
					
						
							|  |  |  | import type { Label } from '@/app/components/tools/labels/constant' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import { useTags } from '@/app/components/plugins/hooks' | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  | import { noop } from 'lodash-es' | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type LabelSelectorProps = { | 
					
						
							|  |  |  |   value: string[] | 
					
						
							|  |  |  |   onChange: (v: string[]) => void | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | const LabelSelector: FC<LabelSelectorProps> = ({ | 
					
						
							|  |  |  |   value, | 
					
						
							|  |  |  |   onChange, | 
					
						
							|  |  |  | }) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const [open, setOpen] = useState(false) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   const { tags: labelList } = useTags() | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const [keywords, setKeywords] = useState('') | 
					
						
							|  |  |  |   const [searchKeywords, setSearchKeywords] = useState('') | 
					
						
							|  |  |  |   const { run: handleSearch } = useDebounceFn(() => { | 
					
						
							|  |  |  |     setSearchKeywords(keywords) | 
					
						
							|  |  |  |   }, { wait: 500 }) | 
					
						
							|  |  |  |   const handleKeywordsChange = (value: string) => { | 
					
						
							|  |  |  |     setKeywords(value) | 
					
						
							|  |  |  |     handleSearch() | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const filteredLabelList = useMemo(() => { | 
					
						
							|  |  |  |     return labelList.filter(label => label.name.includes(searchKeywords)) | 
					
						
							|  |  |  |   }, [labelList, searchKeywords]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const selectedLabels = useMemo(() => { | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     return value.map(v => labelList.find(l => l.name === v)?.label).join(', ') | 
					
						
							|  |  |  |   }, [value, labelList]) | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const selectLabel = (label: Label) => { | 
					
						
							|  |  |  |     if (value.includes(label.name)) | 
					
						
							|  |  |  |       onChange(value.filter(v => v !== label.name)) | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       onChange([...value, label.name]) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <PortalToFollowElem | 
					
						
							|  |  |  |       open={open} | 
					
						
							|  |  |  |       onOpenChange={setOpen} | 
					
						
							|  |  |  |       placement='bottom-start' | 
					
						
							|  |  |  |       offset={4} | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <div className='relative'> | 
					
						
							|  |  |  |         <PortalToFollowElemTrigger | 
					
						
							|  |  |  |           onClick={() => setOpen(v => !v)} | 
					
						
							|  |  |  |           className='block' | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |           <div className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             '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', | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             open && '!hover:bg-components-input-bg-hover hover:bg-components-input-bg-hover', | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |           )}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             <div title={value.length > 0 ? selectedLabels : ''} className={cn('grow truncate text-[13px] leading-[18px] text-text-secondary', !value.length && '!text-text-quaternary')}> | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |               {!value.length && t('tools.createTool.toolInput.labelPlaceholder')} | 
					
						
							|  |  |  |               {!!value.length && selectedLabels} | 
					
						
							|  |  |  |             </div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             <div className='ml-1 shrink-0 text-text-secondary opacity-60'> | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  |               <RiArrowDownSLine className='h-4 w-4' /> | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |             </div> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </PortalToFollowElemTrigger> | 
					
						
							|  |  |  |         <PortalToFollowElemContent className='z-[1040]'> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <div className='relative w-[591px] rounded-lg border-[0.5px] border-components-panel-border bg-components-panel-bg-blur shadow-lg  backdrop-blur-[5px]'> | 
					
						
							|  |  |  |             <div className='border-b-[0.5px] border-divider-regular p-2'> | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |               <Input | 
					
						
							|  |  |  |                 showLeftIcon | 
					
						
							|  |  |  |                 showClearIcon | 
					
						
							|  |  |  |                 value={keywords} | 
					
						
							|  |  |  |                 onChange={e => handleKeywordsChange(e.target.value)} | 
					
						
							|  |  |  |                 onClear={() => handleKeywordsChange('')} | 
					
						
							|  |  |  |               /> | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |             </div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             <div className='max-h-[264px] overflow-y-auto p-1'> | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |               {filteredLabelList.map(label => ( | 
					
						
							|  |  |  |                 <div | 
					
						
							|  |  |  |                   key={label.name} | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   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' | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |                   onClick={() => selectLabel(label)} | 
					
						
							|  |  |  |                 > | 
					
						
							|  |  |  |                   <Checkbox | 
					
						
							|  |  |  |                     className='shrink-0' | 
					
						
							|  |  |  |                     checked={value.includes(label.name)} | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  |                     onCheck={noop} | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |                   /> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   <div title={label.label} className='grow truncate text-sm leading-5 text-text-secondary'>{label.label}</div> | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |                 </div> | 
					
						
							|  |  |  |               ))} | 
					
						
							|  |  |  |               {!filteredLabelList.length && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <div className='flex flex-col items-center gap-1 p-3'> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                   <Tag03 className='h-6 w-6 text-text-quaternary' /> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   <div className='text-xs leading-[14px] text-text-tertiary'>{t('common.tag.noTag')}</div> | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |                 </div> | 
					
						
							|  |  |  |               )} | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </PortalToFollowElemContent> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </PortalToFollowElem> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default LabelSelector |