|
1 | | -import { useState, useEffect } from "react"; |
| 1 | +import { useQuery } from "@tanstack/react-query"; |
2 | 2 | import { containerStyle } from "../../styles/containerStyles"; |
3 | 3 | import { textNormal, textTiny, textHyperlink } from "../../styles/textStyles"; |
4 | 4 | import { getCustomInfo } from "../../api"; |
5 | 5 |
|
6 | 6 | export default function OptimadeChildInfo({ child }) { |
7 | | - const [providerMeta, setProviderMeta] = useState(null); |
8 | | - |
9 | 7 | const isCustom = child?.id === "__custom__"; |
10 | 8 |
|
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 | + }); |
25 | 20 |
|
26 | 21 | if (!child) return null; |
27 | 22 |
|
28 | | - // Decide which data to render |
29 | | - const data = isCustom ? providerMeta : child; |
| 23 | + const providerMeta = isCustom ? customData?.meta?.provider : child; |
30 | 24 |
|
31 | | - if (isCustom && !providerMeta) { |
| 25 | + if (isCustom && isLoading) { |
32 | 26 | 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> |
35 | 32 | </div> |
36 | 33 | ); |
37 | 34 | } |
38 | 35 |
|
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; |
42 | 66 |
|
43 | 67 | return ( |
44 | 68 | <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 | + |
48 | 73 | {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 | | - )} |
62 | 74 |
|
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> |
76 | 104 | </div> |
77 | 105 | ); |
78 | 106 | } |
0 commit comments