Skip to content

Commit c7c4b42

Browse files
committed
improve child information
1 parent 5adfbf7 commit c7c4b42

File tree

2 files changed

+88
-57
lines changed

2 files changed

+88
-57
lines changed

src/api.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ export async function getProviderLinks(baseUrl) {
7575
export async function getCustomInfo({ baseUrl }) {
7676
try {
7777
const json = await fetchWithCorsFallback(`${baseUrl}/info/`);
78-
return { meta: json.meta ?? {} };
79-
} catch {
80-
return { meta: {} };
78+
if (!json || !json.meta) {
79+
throw new Error("Invalid OPTIMADE response");
80+
}
81+
return { meta: json.meta };
82+
} catch (error) {
83+
throw error;
8184
}
8285
}
8386

Lines changed: 82 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,106 @@
1-
import { useState, useEffect } from "react";
1+
import { useQuery } from "@tanstack/react-query";
22
import { containerStyle } from "../../styles/containerStyles";
33
import { textNormal, textTiny, textHyperlink } from "../../styles/textStyles";
44
import { getCustomInfo } from "../../api";
55

66
export default function OptimadeChildInfo({ child }) {
7-
const [providerMeta, setProviderMeta] = useState(null);
8-
97
const isCustom = child?.id === "__custom__";
108

11-
useEffect(() => {
12-
if (!isCustom) return;
13-
14-
async function fetchCustom() {
15-
const result = await getCustomInfo({ baseUrl: child.base_url });
16-
if (result?.meta?.provider) {
17-
setProviderMeta(result.meta.provider);
18-
}
19-
}
20-
21-
fetchCustom();
22-
23-
return () => {};
24-
}, [child, isCustom]);
9+
const {
10+
data: customData,
11+
isLoading,
12+
isError,
13+
} = useQuery({
14+
queryKey: ["custom-provider-info", child?.base_url],
15+
queryFn: () => getCustomInfo({ baseUrl: child.base_url }),
16+
enabled: !!(isCustom && child?.base_url),
17+
staleTime: 1000 * 60 * 60,
18+
retry: 0,
19+
});
2520

2621
if (!child) return null;
2722

28-
// Decide which data to render
29-
const data = isCustom ? providerMeta : child;
23+
const providerMeta = isCustom ? customData?.meta?.provider : child;
3024

31-
if (isCustom && !providerMeta) {
25+
if (isCustom && isLoading) {
3226
return (
33-
<div className={`${containerStyle} h-35 overflow-auto ${textTiny}`}>
34-
<h3 className={`${textNormal} pb-1.5`}>Waiting...</h3>
27+
<div
28+
className={`${containerStyle} ${textTiny} h-35 flex items-center justify-center`}
29+
>
30+
<div className="w-5 h-5 border-2 border-slate-300 border-t-blue-500 rounded-full animate-spin mr-2" />
31+
<p>Fetching provider info...</p>
3532
</div>
3633
);
3734
}
3835

39-
// Destructure and render
40-
const { name, description, homepage, base_url, ...otherAttrs } = data;
41-
const baseUrl = isCustom ? child.base_url : base_url;
36+
if (isCustom && isError) {
37+
return (
38+
<div className={`${containerStyle} ${textTiny} h-25 overflow-auto`}>
39+
<p className="pb-1">
40+
Could not retrieve information. Please check if the base URL is
41+
correct and OPTIMADE-compliant, it's unlikely any queries will
42+
preceed...
43+
</p>
44+
45+
{child?.base_url && (
46+
<p>
47+
Submitted url:{" "}
48+
<a
49+
href={child.base_url}
50+
target="_blank"
51+
rel="noopener noreferrer"
52+
className={textHyperlink}
53+
>
54+
{child.base_url}
55+
</a>
56+
</p>
57+
)}
58+
</div>
59+
);
60+
}
61+
62+
if (isCustom && !providerMeta) return null;
63+
64+
const { name, description, homepage, base_url } = providerMeta || {};
65+
const displayBaseUrl = isCustom ? child.base_url : base_url;
4266

4367
return (
4468
<div className={`${containerStyle} ${textTiny} h-35 overflow-auto`}>
45-
<h3
46-
className={`${textNormal} pb-1.5`}
47-
>{`Provider information: ${name}`}</h3>
69+
<h3 className={`${textNormal} pb-1.5`}>
70+
{isCustom ? `Provider information: ${name || "Unknown"}` : `${name}`}
71+
</h3>
72+
4873
{description && <p className="pb-1">{description}</p>}
49-
{homepage && (
50-
<p>
51-
Homepage:{" "}
52-
<a
53-
href={homepage}
54-
target="_blank"
55-
rel="noopener noreferrer"
56-
className={textHyperlink}
57-
>
58-
{homepage}
59-
</a>
60-
</p>
61-
)}
6274

63-
{baseUrl && (
64-
<p>
65-
Base url:{" "}
66-
<a
67-
href={baseUrl}
68-
target="_blank"
69-
rel="noopener noreferrer"
70-
className={textHyperlink}
71-
>
72-
{baseUrl}
73-
</a>
74-
</p>
75-
)}
75+
<div className="space-y-1">
76+
{homepage && (
77+
<p>
78+
Homepage:{" "}
79+
<a
80+
href={homepage}
81+
target="_blank"
82+
rel="noopener noreferrer"
83+
className={textHyperlink}
84+
>
85+
{homepage}
86+
</a>
87+
</p>
88+
)}
89+
90+
{displayBaseUrl && (
91+
<p>
92+
{isCustom ? "Database base url" : "Base url"}:{" "}
93+
<a
94+
href={displayBaseUrl}
95+
target="_blank"
96+
rel="noopener noreferrer"
97+
className={textHyperlink}
98+
>
99+
{displayBaseUrl}
100+
</a>
101+
</p>
102+
)}
103+
</div>
76104
</div>
77105
);
78106
}

0 commit comments

Comments
 (0)