import type { FC } from 'react' import { useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import { useContext } from 'use-context-selector' import { useDebounceFn, useMount } from 'ahooks' import cn from 'classnames' import { useStore as useLabelStore } from './store' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger, } from '@/app/components/base/portal-to-follow-elem' import SearchInput from '@/app/components/base/search-input' import { ChevronDown } from '@/app/components/base/icons/src/vender/line/arrows' 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 { fetchLabelList } from '@/service/tools' import I18n from '@/context/i18n' import { getLanguage } from '@/i18n/language' type LabelSelectorProps = { value: string[] onChange: (v: string[]) => void } const LabelSelector: FC = ({ value, onChange, }) => { const { t } = useTranslation() const { locale } = useContext(I18n) const language = getLanguage(locale) const [open, setOpen] = useState(false) const labelList = useLabelStore(s => s.labelList) const setLabelList = useLabelStore(s => s.setLabelList) 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(() => { 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)) onChange(value.filter(v => v !== label.name)) else onChange([...value, label.name]) } useMount(() => { fetchLabelList().then((res) => { setLabelList(res) }) }) return (
setOpen(v => !v)} className='block' >
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}
{filteredLabelList.map(label => (
selectLabel(label)} > {}} />
{label.label[language]}
))} {!filteredLabelList.length && (
{t('common.tag.noTag')}
)}
) } export default LabelSelector