From d355e2c631686725ba30c3d4dab92e2ce35f18b1 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 7 May 2024 22:11:46 +0800 Subject: [PATCH] chore: tweak tags section --- .../components/HomeSidebar/TagsSection.tsx | 96 ++----------------- 1 file changed, 10 insertions(+), 86 deletions(-) diff --git a/web/src/components/HomeSidebar/TagsSection.tsx b/web/src/components/HomeSidebar/TagsSection.tsx index c59b5bfec..95ebf5044 100644 --- a/web/src/components/HomeSidebar/TagsSection.tsx +++ b/web/src/components/HomeSidebar/TagsSection.tsx @@ -1,7 +1,5 @@ import { Dropdown, Menu, MenuButton, MenuItem } from "@mui/joy"; -import { useEffect, useState } from "react"; import useDebounce from "react-use/lib/useDebounce"; -import useToggle from "react-use/lib/useToggle"; import { useFilterStore } from "@/store/module"; import { useMemoList, useTagStore } from "@/store/v1"; import { useTranslate } from "@/utils/i18n"; @@ -10,23 +8,13 @@ import { showCommonDialog } from "../Dialog/CommonDialog"; import Icon from "../Icon"; import showRenameTagDialog from "../RenameTagDialog"; -interface KVObject { - [key: string]: T; -} - -interface Tag { - key: string; - text: string; - subTags: Tag[]; -} - const TagsSection = () => { const t = useTranslate(); const filterStore = useFilterStore(); const tagStore = useTagStore(); const memoList = useMemoList(); const filter = filterStore.state; - const [tags, setTags] = useState([]); + const tags = tagStore.tags; useDebounce( () => { @@ -36,42 +24,6 @@ const TagsSection = () => { [memoList.size()], ); - useEffect(() => { - const sortedTags = Array.from(tagStore.getState().tags).sort(); - const root: KVObject = { - subTags: [], - }; - - for (const tag of sortedTags) { - const subtags = tag.split("/"); - let tempObj = root; - let tagText = ""; - - for (let i = 0; i < subtags.length; i++) { - const key = subtags[i]; - if (i === 0) { - tagText += key; - } else { - tagText += "/" + key; - } - - let obj = tempObj.subTags.find((t: Tag) => t.text === tagText); - if (!obj) { - obj = { - key, - text: tagText, - subTags: [], - }; - tempObj.subTags.push(obj); - } - - tempObj = obj; - } - } - - setTags(root.subTags as Tag[]); - }, [tagStore.getState().tags]); - return (
@@ -79,10 +31,10 @@ const TagsSection = () => { {t("common.tags")}
- {tags.length > 0 ? ( + {tags.size > 0 ? (
- {tags.map((t, idx) => ( - + {Array.from(tags).map((tag) => ( + ))}
) : ( @@ -96,7 +48,7 @@ const TagsSection = () => { }; interface TagItemContainerProps { - tag: Tag; + tag: string; tagQuery?: string; } @@ -105,23 +57,16 @@ const TagItemContainer: React.FC = (props: TagItemContain const filterStore = useFilterStore(); const tagStore = useTagStore(); const { tag, tagQuery } = props; - const isActive = tagQuery === tag.text; - const hasSubTags = tag.subTags.length > 0; - const [showSubTags, toggleSubTags] = useToggle(false); + const isActive = tagQuery === tag; const handleTagClick = () => { if (isActive) { filterStore.setTagFilter(undefined); } else { - filterStore.setTagFilter(tag.text); + filterStore.setTagFilter(tag); } }; - const handleToggleBtnClick = (event: React.MouseEvent) => { - event.stopPropagation(); - toggleSubTags(); - }; - const handleDeleteTag = async () => { showCommonDialog({ title: t("tag.delete-tag"), @@ -129,7 +74,7 @@ const TagItemContainer: React.FC = (props: TagItemContain style: "danger", dialogName: "delete-tag-dialog", onConfirm: async () => { - await tagStore.deleteTag(tag.text); + await tagStore.deleteTag(tag); tagStore.fetchTags({ skipCache: true }); }, }); @@ -151,7 +96,7 @@ const TagItemContainer: React.FC = (props: TagItemContain
- showRenameTagDialog({ tag: tag.text })}> + showRenameTagDialog({ tag: tag })}> {t("common.rename")} @@ -162,31 +107,10 @@ const TagItemContainer: React.FC = (props: TagItemContain - {tag.key} + {tag} -
- {hasSubTags ? ( - - - - ) : null} -
- {hasSubTags ? ( -
- {tag.subTags.map((st, idx) => ( - - ))} -
- ) : null} ); };