Skip to content

Commit

Permalink
try refac
Browse files Browse the repository at this point in the history
  • Loading branch information
salimkanoun committed Oct 8, 2024
1 parent f8040a1 commit e6e76d6
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 91 deletions.
139 changes: 73 additions & 66 deletions src/content/series/Tags.tsx
Original file line number Diff line number Diff line change
@@ -1,78 +1,85 @@
import { useState } from "react"
import { getInstancesOfSeries } from "../../services/orthanc"
import { Input, Spinner } from "../../ui"
import { useCustomQuery } from "../../utils"
import { instanceHeader, instanceTags } from "../../services/instances"
import { useMemo, useState } from "react";
import { getInstancesOfSeries } from "../../services/orthanc";
import { Input, Spinner } from "../../ui";
import { useCustomQuery } from "../../utils";
import { instanceHeader, instanceTags } from "../../services/instances";

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

const { data: instances } = useCustomQuery(['series', seriesId, 'instances'], () => getInstancesOfSeries(seriesId))
const [instanceNumber, setInstanceNumber] = useState<number>(1)
const currentInstanceId =
instanceNumber != null && instances != null
? instances[instanceNumber - 1].id
: null;

const currentInstanceId = (instanceNumber != null && instances != null) ? instances[instanceNumber - 1].id : null
const { data: header } = useCustomQuery(
["instances", currentInstanceId, "metadata"],
() => instanceHeader(currentInstanceId),
{
enabled: currentInstanceId !== null,
}
);

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

const { data: tags } = useCustomQuery(
['instances', currentInstanceId, 'tags'],
() => instanceTags(currentInstanceId),
{
enabled: (currentInstanceId !== null)
}
)
const metadata = useMemo(() => {
if (!header || !tags) return {};
return {
...header,
...tags,
};
}, [header, tags]);

const json = JSON.parse(JSON.stringify(header, null, 2))
const json_values = Object.values(json)
var names_list = []
var values_list = []
for (var index = 0; index < json_values.length; index++) {
names_list.push(json_values[index]["Name"])
values_list.push(json_values[index]["Value"])
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);
});
});
} else {
return (
<span>
<strong>{tagName}:</strong> {tag}
</span>
);
}
const jsonNameValues = Object.fromEntries(names_list.map((key, index) => [key, values_list[index]]));
};

const json2 = JSON.parse(JSON.stringify(tags, null, 2))
const json_values2 = Object.values(json2)
var names_list2 = []
var values_list2 = []
for (var index = 0; index < json_values2.length; index++) {
names_list2.push(json_values2[index]["Name"])
values_list2.push(json_values2[index]["Value"])
}
const jsonNameValues2 = Object.fromEntries(names_list2.map((key, index) => [key, values_list2[index]]));
if (!instances) return <Spinner />;

if (!instances) return <Spinner />
return (
<>
<Input label="Instance Number" min={1} max={instances.length} value={instanceNumber ?? 1} onChange={(event) => setInstanceNumber(Number(event.target?.value))} />
<pre>
{Object.entries(jsonNameValues).map(([key, value]) => (
<div key={key}>
<strong>{key}:</strong> {JSON.stringify(value)}
<hr/>
</div>
))}
<hr/><hr/><hr/><hr/><hr/>
{Object.entries(jsonNameValues2).map(([key, value]) => (
<div key={key}>
<strong>{key}:</strong> {JSON.stringify(value)}
<hr/>
</div>
))}
{/* {JSON.stringify(header, null, 2)} */}
{/* {JSON.stringify(tags, null, 2)} */}
</pre>
</>
)
}
return (
<>
<Input
label="Instance Number"
min={1}
max={instances.length}
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>
</>
);
};

export default Tags
export default Tags;
24 changes: 17 additions & 7 deletions src/services/instances.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "./axios";
import { OrthancImportDicom } from "../utils/types";
import { OrthancImportDicom, Tags } from "../utils/types";

export const sendDicom = (payload: Uint8Array): Promise<OrthancImportDicom> => {
return axios
Expand Down Expand Up @@ -71,24 +71,34 @@ export const previewFrame = (
});
};

export const instanceTags = (instanceId: string): Promise<any> => {
export const instanceTags = (
instanceId: string
): Promise<Tags> => {
return axios
.get("/api/instances/" + instanceId + "/tags")
.get("/api/instances/" + instanceId + "/tags?simplfy")
.then((response) => {
return response.data;
})
.catch((error) => {
console.error(error);
if (error.response) {
throw error.response;
}
throw error;
});
};

export const instanceHeader = (instanceId: string): Promise<any> => {
export const instanceHeader = (
instanceId: string
): Promise<Tags> => {
return axios
.get("/api/instances/" + instanceId + "/header")
.get("/api/instances/" + instanceId + "/header?simplfy")
.then((response) => {
return response.data;
})
.catch((error) => {
console.error(error);
if (error.response) {
throw error.response;
}
throw error;
});
};
38 changes: 20 additions & 18 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export type ProcessingJob = {
progress: number;
state: string;
id: string;
results : Record<string,any>
results: Record<string, any>;
};

export type Peer = {
Expand Down Expand Up @@ -302,7 +302,7 @@ export type Instances = {
instanceCreationTime: string | null;
instanceNumber: string | null;
sopInstanceUID: string | null;
numberOfFrames: string|null;
numberOfFrames: string | null;
};
parentSeries: string;
type: string;
Expand Down Expand Up @@ -343,7 +343,7 @@ export type PatientModifyPayload = {
force: boolean;
synchronous: boolean;
keepSource: boolean;
keep : string[];
keep: string[];
};

export type OrthancResponse = {
Expand All @@ -364,19 +364,19 @@ export type Study = {
};

export type AnonStudy = {
newPatientName : string,
newPatientId : string,
newStudyDescription : string,
newAccessionNumber : string,
originalStudy : Study;
}
newPatientName: string;
newPatientId: string;
newStudyDescription: string;
newAccessionNumber: string;
originalStudy: Study;
};

export type StudyModifyPayload = {
replace: Partial<StudyMainDicomTags&PatientMainDicomTags>;
replace: Partial<StudyMainDicomTags & PatientMainDicomTags>;
remove: string[];
removePrivateTags: boolean;
force: boolean;
keep : string[];
keep: string[];
synchronous: boolean;
keepSource: boolean;
};
Expand All @@ -388,13 +388,15 @@ export type SeriesModifyPayload = {
keepSource: boolean;
force: boolean;
synchronous: boolean;
keep : string[];
keep: string[];
};

export type Queue = {
progress : number
state : string
id : string
results : Record<string,any>
userId : number
}
progress: number;
state: string;
id: string;
results: Record<string, any>;
userId: number;
};

export type Tags = Record<string, string | Record<string, string>[]>;

0 comments on commit e6e76d6

Please sign in to comment.