Skip to content

Commit

Permalink
:feat: adding expend list
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-VIGNAU-ESPINE committed Nov 26, 2024
1 parent 8cf9cea commit f915ea8
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 deletions src/content/series/Tags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@ import { getInstancesOfSeries } from "../../services/orthanc";
import { Input, Spinner } from "../../ui";
import { useCustomQuery } from "../../utils";
import { instanceHeader, instanceTags } from "../../services/instances";
import { Metadata, Tag } from "../../utils/types";
import { Meta } from "@storybook/react";

type TagsProps = {
seriesId: string;
};

const Tags = ({ seriesId }: TagsProps) => {
const { data: instances } = useCustomQuery(
["series", seriesId, "instances"],
() => getInstancesOfSeries(seriesId)
);
const [instanceNumber, setInstanceNumber] = useState<number>(1);
const [expanded, setExpanded] = useState<{ [key: string]: boolean }>({});

const currentInstanceId =
instanceNumber != null && instances != null
? instances[instanceNumber - 1].id
: null;

const { data: header } = useCustomQuery(
const { data: header } = useCustomQuery<Metadata>(
["instances", currentInstanceId, "metadata"],
() => instanceHeader(currentInstanceId),
{
enabled: currentInstanceId !== null,
}
);

const { data: tags } = useCustomQuery(
const { data: tags } = useCustomQuery<Metadata>(
["instances", currentInstanceId, "tags"],
() => instanceTags(currentInstanceId),
{
Expand All @@ -43,21 +47,43 @@ const Tags = ({ seriesId }: TagsProps) => {
};
}, [header, tags]);

const getComponent = (
tagName: string,
tag: string | Record<string, string>[]
) => {
if (Array.isArray(tag)) {
return tag.map((tagItem) => {
return Object.entries(tagItem).map(([key, tag]) => {
return getComponent(key, tag);
});
});
const toggleExpand = (key: string) => {
setExpanded((prev) => ({
...prev,
[key]: !prev[key],
}));
};

const getComponent = (tagAddress: string, tag: Tag) => {
if (Array.isArray(tag.Value)) {
return (
<li key={tagAddress} className="ml-4">
<button
onClick={() => toggleExpand(tagAddress)}
className="text-blue-600 underline"
>
{tag.Name} {expanded[tagAddress] ? "▲" : "▼"}
</button>
{expanded[tagAddress] && (
<ul className="list-disc pl-4">
{tag.Value.map((metadata, index) => (
<li key={`${tagAddress}-${index}`}>
<ul>
{Object.entries(metadata).map(([addressTag, tag]) =>
getComponent(addressTag, tag)
)}
</ul>
</li>
))}
</ul>
)}
</li>
);
} else {
return (
<span>
<strong>{tagName}:</strong> {tag}
</span>
<li className="ml-4 px-2" key={tagAddress}>
{tag.Name} - {tag.Value}
</li>
);
}
};
Expand All @@ -73,11 +99,13 @@ const Tags = ({ seriesId }: TagsProps) => {
value={instanceNumber ?? 1}
onChange={(event) => setInstanceNumber(Number(event.target?.value))}
/>
<ul>
{Object.entries(metadata).map(([key, tag]) => (
<li key={key}>{getComponent(key, tag)}</li>
))}
</ul>
<div>
<ul className="list-disc">
{Object.entries(metadata).map(([tagAddress, tag]) =>
getComponent(tagAddress, tag)
)}
</ul>
</div>
</>
);
};
Expand Down

0 comments on commit f915ea8

Please sign in to comment.