refactor: Simplify tag label retrieval in hooks and update related components

This commit is contained in:
WTW0313 2025-09-04 17:32:15 +08:00 committed by twwu
parent d790d2bc89
commit a061215e42
3 changed files with 12 additions and 5 deletions

View File

@ -26,9 +26,16 @@ export const useTags = (translateFromOut?: TFunction) => {
return acc
}, {} as Record<string, Tag>)
const getTagLabel = (name: string) => {
if (!tagsMap[name])
return name
return tagsMap[name].label
}
return {
tags,
tagsMap,
getTagLabel,
}
}

View File

@ -29,7 +29,7 @@ const CardWrapper = ({
setFalse: hideInstallFromMarketplace,
}] = useBoolean(false)
const { locale: localeFromLocale } = useI18N()
const { tagsMap } = useTags(t)
const { getTagLabel } = useTags(t)
if (showInstallButton) {
return (
@ -43,7 +43,7 @@ const CardWrapper = ({
footer={
<CardMoreInfo
downloadCount={plugin.install_count}
tags={plugin.tags.map(tag => tagsMap[tag.name].label)}
tags={plugin.tags.map(tag => getTagLabel(tag.name))}
/>
}
/>
@ -92,7 +92,7 @@ const CardWrapper = ({
footer={
<CardMoreInfo
downloadCount={plugin.install_count}
tags={plugin.tags.map(tag => tagsMap[tag.name].label)}
tags={plugin.tags.map(tag => getTagLabel(tag.name))}
/>
}
/>

View File

@ -27,7 +27,7 @@ const TagsFilter = ({
const { t } = useTranslation()
const [open, setOpen] = useState(false)
const [searchText, setSearchText] = useState('')
const { tags: options, tagsMap } = useTags()
const { tags: options, getTagLabel } = useTags()
const filteredOptions = options.filter(option => option.name.toLowerCase().includes(searchText.toLowerCase()))
const handleCheck = (id: string) => {
if (value.includes(id))
@ -59,7 +59,7 @@ const TagsFilter = ({
!selectedTagsLength && t('pluginTags.allTags')
}
{
!!selectedTagsLength && value.map(val => tagsMap[val].label).slice(0, 2).join(',')
!!selectedTagsLength && value.map(val => getTagLabel(val)).slice(0, 2).join(',')
}
{
selectedTagsLength > 2 && (