Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tag count to tree view #3970

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/src/components/HomeSidebar/TagsSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const TagsSection = (props: Props) => {
</div>
{tagAmounts.length > 0 ? (
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">
{tagAmounts.map(([tag, amount]) => (
Expand Down
22 changes: 15 additions & 7 deletions web/src/components/TagTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,43 @@ import { useMemoFilterStore } from "@/store/v1";
interface Tag {
key: string;
text: string;
amount: number;
subTags: Tag[];
}

interface Props {
tags: string[];
tagAmounts: [tag: string, amount: number][];
}

const TagTree = ({ tags: rawTags }: Props) => {
const TagTree = ({ tagAmounts: rawTagAmounts }: Props) => {
const [tags, setTags] = useState<Tag[]>([]);

useEffect(() => {
const sortedTags = Array.from(rawTags).sort();
const sortedTagAmounts = Array.from(rawTagAmounts).sort();
const root: Tag = {
key: "",
text: "",
amount: 0,
subTags: [],
};

for (const tag of sortedTags) {
const subtags = tag.split("/");
for (const tagAmount of sortedTagAmounts) {
const subtags = tagAmount[0].split("/");
let tempObj = root;
let tagText = "";

for (let i = 0; i < subtags.length; i++) {
const key = subtags[i];
let amount: number = 0;

if (i === 0) {
tagText += key;
} else {
tagText += "/" + key;
}
if (sortedTagAmounts.some(([tag, amount]) => tag === tagText && amount > 1)) {
amount = tagAmount[1];
}

let obj = null;

Expand All @@ -50,6 +57,7 @@ const TagTree = ({ tags: rawTags }: Props) => {
obj = {
key,
text: tagText,
amount: amount,
subTags: [],
};
tempObj.subTags.push(obj);
Expand All @@ -60,7 +68,7 @@ const TagTree = ({ tags: rawTags }: Props) => {
}

setTags(root.subTags as Tag[]);
}, [rawTags]);
}, [rawTagAmounts]);

return (
<div className="flex flex-col justify-start items-start relative w-full h-auto flex-nowrap gap-2 mt-1">
Expand Down Expand Up @@ -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" />
</div>
<span className="truncate cursor-pointer hover:opacity-80" onClick={handleTagClick}>
{tag.key}
{tag.key} {tag.amount > 1 && `(${tag.amount})`}
</span>
</div>
<div className="flex flex-row justify-end items-center">
Expand Down