| | |
| | | import type { FC } from 'react' |
| | | import { useMemo, useState } from 'react' |
| | | import { useTranslation } from 'react-i18next' |
| | | import { useDebounceFn } from 'ahooks' |
| | | import { useContext } from 'use-context-selector' |
| | | import { useDebounceFn, useMount } from 'ahooks' |
| | | import { RiArrowDownSLine } from '@remixicon/react' |
| | | import { useStore as useLabelStore } from './store' |
| | | import cn from '@/utils/classnames' |
| | | import { |
| | | PortalToFollowElem, |
| | |
| | | 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' |
| | | import { useTags } from '@/app/components/plugins/hooks' |
| | | import { noop } from 'lodash-es' |
| | | import { fetchLabelList } from '@/service/tools' |
| | | import I18n from '@/context/i18n' |
| | | import { getLanguage } from '@/i18n/language' |
| | | |
| | | type LabelSelectorProps = { |
| | | value: string[] |
| | |
| | | onChange, |
| | | }) => { |
| | | const { t } = useTranslation() |
| | | const { locale } = useContext(I18n) |
| | | const language = getLanguage(locale) |
| | | const [open, setOpen] = useState(false) |
| | | |
| | | const { tags: labelList } = useTags() |
| | | const labelList = useLabelStore(s => s.labelList) |
| | | const setLabelList = useLabelStore(s => s.setLabelList) |
| | | |
| | | const [keywords, setKeywords] = useState('') |
| | | const [searchKeywords, setSearchKeywords] = useState('') |
| | |
| | | }, [labelList, searchKeywords]) |
| | | |
| | | const selectedLabels = useMemo(() => { |
| | | return value.map(v => labelList.find(l => l.name === v)?.label).join(', ') |
| | | }, [value, labelList]) |
| | | return value.map(v => labelList.find(l => l.name === v)?.label[language]).join(', ') |
| | | }, [value, labelList, language]) |
| | | |
| | | const selectLabel = (label: Label) => { |
| | | if (value.includes(label.name)) |
| | |
| | | else |
| | | onChange([...value, label.name]) |
| | | } |
| | | |
| | | useMount(() => { |
| | | fetchLabelList().then((res) => { |
| | | setLabelList(res) |
| | | }) |
| | | }) |
| | | |
| | | return ( |
| | | <PortalToFollowElem |
| | |
| | | className='block' |
| | | > |
| | | <div className={cn( |
| | | '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', |
| | | open && '!hover:bg-components-input-bg-hover hover:bg-components-input-bg-hover', |
| | | 'flex items-center gap-1 px-3 h-10 rounded-lg border-[0.5px] border-transparent bg-gray-100 cursor-pointer hover:bg-gray-200', |
| | | open && '!bg-gray-200 hover:bg-gray-200', |
| | | )}> |
| | | <div title={value.length > 0 ? selectedLabels : ''} className={cn('grow truncate text-[13px] leading-[18px] text-text-secondary', !value.length && '!text-text-quaternary')}> |
| | | <div title={value.length > 0 ? selectedLabels : ''} className={cn('grow text-[13px] leading-[18px] text-gray-700 truncate', !value.length && '!text-gray-400')}> |
| | | {!value.length && t('tools.createTool.toolInput.labelPlaceholder')} |
| | | {!!value.length && selectedLabels} |
| | | </div> |
| | | <div className='ml-1 shrink-0 text-text-secondary opacity-60'> |
| | | <div className='shrink-0 ml-1 text-gray-700 opacity-60'> |
| | | <RiArrowDownSLine className='h-4 w-4' /> |
| | | </div> |
| | | </div> |
| | | </PortalToFollowElemTrigger> |
| | | <PortalToFollowElemContent className='z-[1040]'> |
| | | <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'> |
| | | <div className='relative w-[591px] bg-white rounded-lg border-[0.5px] border-gray-200 shadow-lg'> |
| | | <div className='p-2 border-b-[0.5px] border-black/5'> |
| | | <Input |
| | | showLeftIcon |
| | | showClearIcon |
| | |
| | | onClear={() => handleKeywordsChange('')} |
| | | /> |
| | | </div> |
| | | <div className='max-h-[264px] overflow-y-auto p-1'> |
| | | <div className='p-1 max-h-[264px] overflow-y-auto'> |
| | | {filteredLabelList.map(label => ( |
| | | <div |
| | | key={label.name} |
| | | 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' |
| | | className='flex items-center gap-2 pl-3 py-[6px] pr-2 rounded-lg cursor-pointer hover:bg-gray-100' |
| | | onClick={() => selectLabel(label)} |
| | | > |
| | | <Checkbox |
| | | className='shrink-0' |
| | | checked={value.includes(label.name)} |
| | | onCheck={noop} |
| | | onCheck={() => { }} |
| | | /> |
| | | <div title={label.label} className='grow truncate text-sm leading-5 text-text-secondary'>{label.label}</div> |
| | | <div title={label.label[language]} className='grow text-sm text-gray-700 leading-5 truncate'>{label.label[language]}</div> |
| | | </div> |
| | | ))} |
| | | {!filteredLabelList.length && ( |
| | | <div className='flex flex-col items-center gap-1 p-3'> |
| | | <Tag03 className='h-6 w-6 text-text-quaternary' /> |
| | | <div className='text-xs leading-[14px] text-text-tertiary'>{t('common.tag.noTag')}</div> |
| | | <div className='p-3 flex flex-col items-center gap-1'> |
| | | <Tag03 className='h-6 w-6 text-gray-300' /> |
| | | <div className='text-gray-500 text-xs leading-[14px]'>{t('common.tag.noTag')}</div> |
| | | </div> |
| | | )} |
| | | </div> |