Skip to content

Commit 8e3c8c5

Browse files
committed
Show usage count per tag
1 parent aa37a01 commit 8e3c8c5

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/container/TagEditor/TagConfig/TagConfig.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { tagIcons, type Tag, type TagIcon } from "../../../interfaces/Tag";
22
import { Tag as TagElement } from "../../../common/components/Tag/Tag";
3-
import { useState } from "react";
3+
import { useEffect, useState } from "react";
44
import { HueSlider } from "../../../common/components/HueSlider/HueSlider";
55
import { tagService } from "../../../scripts/db/TagService";
66
import {
@@ -29,6 +29,7 @@ import type { Editor } from "@tiptap/react";
2929

3030
type Props = {
3131
tag: Tag;
32+
showUsageCount: boolean;
3233
};
3334

3435
const updateHueDebounced = debounce((name: string, hue: number) => {
@@ -47,10 +48,20 @@ const updateIconDebounced = debounce((name: string, icon: TagIcon) => {
4748
}, 1000);
4849

4950
export const TagConfig = (props: Props) => {
50-
const { tag } = props;
51+
const { tag, showUsageCount } = props;
5152
const [hue, setHue] = useState(tag.hue);
5253
const [icon, setIcon] = useState<TagIcon>(tag.icon ?? "hash");
5354
const { toast } = useToast();
55+
const [usageCount, setUsageCount] = useState(0);
56+
57+
useEffect(() => {
58+
if (!showUsageCount) {
59+
return;
60+
}
61+
(async () => {
62+
setUsageCount((await sparkService.find([tag.name])).length);
63+
})();
64+
}, [showUsageCount, tag.name]);
5465

5566
const handleHueChange = (value: number) => {
5667
setHue(value);
@@ -107,7 +118,8 @@ export const TagConfig = (props: Props) => {
107118
};
108119

109120
return (
110-
<div className="grid grid-cols-subgrid col-span-5 border-b border-stone-200 py-2 w-full items-center gap-4">
121+
<div className="grid grid-cols-subgrid col-span-6 border-b border-stone-200 py-2 w-full items-center gap-4">
122+
<div>{showUsageCount ? usageCount : ""}</div>
111123
<div className="justify-self-center">
112124
<TagElement
113125
name={tag.name}

src/container/TagEditor/TagEditor.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@ import { useLiveQuery } from "dexie-react-hooks";
1414
import { tagService } from "../../scripts/db/TagService";
1515
import { TagConfig } from "./TagConfig/TagConfig";
1616
import { Input } from "../../common/components/shadcn/input";
17+
import type React from "react";
1718
import { useState } from "react";
1819
import { matchSorter } from "match-sorter";
1920

2021
export const TagEditor = () => {
22+
const [showUsageCount, setShowUsageCount] = useState(false);
2123
const tags = useLiveQuery(() => {
2224
return tagService.listTags();
2325
});
2426

2527
const [filter, setFilter] = useState("");
2628

29+
const handleFilterChange = (event: React.ChangeEvent<HTMLInputElement>) => {
30+
setFilter(event.target.value);
31+
};
32+
2733
const filteredTags = matchSorter(tags ?? [], filter, {
2834
keys: [
2935
"name",
@@ -62,26 +68,35 @@ export const TagEditor = () => {
6268
</div>
6369
<div className="grid grid-cols-[1fr_minmax(400px,_900px)_1fr] gap-4 p-4">
6470
<div />
65-
<div className="grid grid-cols-[300px_1fr]">
71+
<div className="flex justify-between">
6672
<label className="flex justify-center items-center gap-2">
6773
Filter:
6874
<Input
6975
value={filter}
70-
onChange={(e) => setFilter(e.target.value)}
76+
onChange={handleFilterChange}
7177
/>
7278
</label>
79+
<Button
80+
variant={"outline"}
81+
onClick={(e) =>
82+
setShowUsageCount(!showUsageCount)
83+
}
84+
>
85+
Count usage
86+
</Button>
7387
</div>
7488
<div />
7589
</div>
7690
<div className="grid grid-cols-[1fr_minmax(400px,_900px)_1fr] gap-4 pb-10 overflow-y-auto max-h-96">
7791
{!tags || tags.length <= 0 ? (
7892
<div>No tags yet.</div>
7993
) : (
80-
<div className="col-start-2 grid grid-cols-[200px_1fr_180px_100px_100px]">
94+
<div className="col-start-2 grid grid-cols-[max-content_200px_1fr_180px_100px_100px]">
8195
{filteredTags.map((tag) => (
8296
<TagConfig
8397
key={tag.name}
8498
tag={tag}
99+
showUsageCount={showUsageCount}
85100
/>
86101
))}
87102
</div>

0 commit comments

Comments
 (0)