Skip to content

Commit

Permalink
add search bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-VIGNAU-ESPINE committed Dec 9, 2024
1 parent 2e1e29c commit 0cbd0bb
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/content/series/TagsTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const TagsTree = ({ seriesId }: TagsProps) => {
() => getInstancesOfSeries(seriesId)
);
const [instanceNumber, setInstanceNumber] = useState<number>(1);
const [searchTerm, setSearchTerm] = useState<string>("");

const currentInstanceId =
instanceNumber != null && instances != null
Expand Down Expand Up @@ -46,6 +47,26 @@ const TagsTree = ({ seriesId }: TagsProps) => {
};
}, [header, tags]);

const filteredMetadata = useMemo(() => {
if (!searchTerm) return metadata;
const lowerSearchTerm = searchTerm.toLowerCase();

return Object.entries(metadata).reduce((filtered, [tagAddress, tag]) => {
const tagValue = String(tag.Value).toLowerCase();
const tagName = tag.Name?.toLowerCase();

if (
tagAddress.toLowerCase().includes(lowerSearchTerm) ||
tagName?.includes(lowerSearchTerm) ||
tagValue.includes(lowerSearchTerm)
) {
filtered[tagAddress] = tag;
}

return filtered;
}, {} as Metadata);
}, [metadata, searchTerm]);

const getComponent = (tagAddress: string, tag: TagType) => {
if (Array.isArray(tag.Value)) {
return (
Expand All @@ -66,7 +87,11 @@ const TagsTree = ({ seriesId }: TagsProps) => {
} else {
return (
<li className="ml-4 px-2 list-none" key={tagAddress}>
<Badge variant="success" className="p-1" value={tagAddress + ' - ' +tag.Name +" : "+ (tag.Value as string)}/>
<Badge
variant="success"
className="p-1"
value={`${tagAddress} - ${tag.Name} : ${tag.Value}`}
/>
</li>
);
}
Expand All @@ -83,11 +108,17 @@ const TagsTree = ({ seriesId }: TagsProps) => {
value={instanceNumber ?? 1}
onChange={(event) => setInstanceNumber(Number(event.target?.value))}
/>
<Input
label="Search Metadata"
value={searchTerm}
onChange={(event) => setSearchTerm(event.target?.value)}
placeholder="Search by tag address, name, or value"
/>
<div>
<ul className="list-disc space-y-3">
{Object.entries(metadata).sort().map(([tagAddress, tag]) =>
getComponent(tagAddress, tag)
)}
{Object.entries(filteredMetadata)
.sort()
.map(([tagAddress, tag]) => getComponent(tagAddress, tag))}
</ul>
</div>
</div>
Expand Down

0 comments on commit 0cbd0bb

Please sign in to comment.