feat: add tag count to tree view (#3970)

* Add tag count to tree view

* Only display tag amounts > 1

* Updated tag amount var name to be more descriptive

* - Moved display logic to html in return
- Updated var names to closer match var passed in by TagSection component
pull/3783/head^2
RoccoSmit 8 months ago committed by GitHub
parent 4ab06f5f11
commit f0d43c9577
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -86,7 +86,7 @@ const TagsSection = (props: Props) => {
</div> </div>
{tagAmounts.length > 0 ? ( {tagAmounts.length > 0 ? (
treeMode ? ( treeMode ? (
<TagTree tags={tagAmounts.map((t) => t[0])} /> <TagTree tagAmounts={tagAmounts} />
) : ( ) : (
<div className="w-full flex flex-row justify-start items-center relative flex-wrap gap-x-2 gap-y-1"> <div className="w-full flex flex-row justify-start items-center relative flex-wrap gap-x-2 gap-y-1">
{tagAmounts.map(([tag, amount]) => ( {tagAmounts.map(([tag, amount]) => (

@ -6,36 +6,43 @@ import { useMemoFilterStore } from "@/store/v1";
interface Tag { interface Tag {
key: string; key: string;
text: string; text: string;
amount: number;
subTags: Tag[]; subTags: Tag[];
} }
interface Props { interface Props {
tags: string[]; tagAmounts: [tag: string, amount: number][];
} }
const TagTree = ({ tags: rawTags }: Props) => { const TagTree = ({ tagAmounts: rawTagAmounts }: Props) => {
const [tags, setTags] = useState<Tag[]>([]); const [tags, setTags] = useState<Tag[]>([]);
useEffect(() => { useEffect(() => {
const sortedTags = Array.from(rawTags).sort(); const sortedTagAmounts = Array.from(rawTagAmounts).sort();
const root: Tag = { const root: Tag = {
key: "", key: "",
text: "", text: "",
amount: 0,
subTags: [], subTags: [],
}; };
for (const tag of sortedTags) { for (const tagAmount of sortedTagAmounts) {
const subtags = tag.split("/"); const subtags = tagAmount[0].split("/");
let tempObj = root; let tempObj = root;
let tagText = ""; let tagText = "";
for (let i = 0; i < subtags.length; i++) { for (let i = 0; i < subtags.length; i++) {
const key = subtags[i]; const key = subtags[i];
let amount: number = 0;
if (i === 0) { if (i === 0) {
tagText += key; tagText += key;
} else { } else {
tagText += "/" + key; tagText += "/" + key;
} }
if (sortedTagAmounts.some(([tag, amount]) => tag === tagText && amount > 1)) {
amount = tagAmount[1];
}
let obj = null; let obj = null;
@ -50,6 +57,7 @@ const TagTree = ({ tags: rawTags }: Props) => {
obj = { obj = {
key, key,
text: tagText, text: tagText,
amount: amount,
subTags: [], subTags: [],
}; };
tempObj.subTags.push(obj); tempObj.subTags.push(obj);
@ -60,7 +68,7 @@ const TagTree = ({ tags: rawTags }: Props) => {
} }
setTags(root.subTags as Tag[]); setTags(root.subTags as Tag[]);
}, [rawTags]); }, [rawTagAmounts]);
return ( return (
<div className="flex flex-col justify-start items-start relative w-full h-auto flex-nowrap gap-2 mt-1"> <div className="flex flex-col justify-start items-start relative w-full h-auto flex-nowrap gap-2 mt-1">
@ -111,7 +119,7 @@ const TagItemContainer: React.FC<TagItemContainerProps> = (props: TagItemContain
<HashIcon className="w-4 h-auto shrink-0 mr-1 text-gray-400 dark:text-gray-500" /> <HashIcon className="w-4 h-auto shrink-0 mr-1 text-gray-400 dark:text-gray-500" />
</div> </div>
<span className="truncate cursor-pointer hover:opacity-80" onClick={handleTagClick}> <span className="truncate cursor-pointer hover:opacity-80" onClick={handleTagClick}>
{tag.key} {tag.key} {tag.amount > 1 && `(${tag.amount})`}
</span> </span>
</div> </div>
<div className="flex flex-row justify-end items-center"> <div className="flex flex-row justify-end items-center">

Loading…
Cancel
Save