|
1 | | -import React, { useMemo } from "react"; |
| 1 | +import React, { useMemo } from 'react'; |
2 | 2 |
|
3 | | -import $RefParser from '@apidevtools/json-schema-ref-parser'; |
4 | | -import pkg from 'fhir-react'; |
5 | | -const {FhirResource, fhirVersions} = pkg; |
6 | 3 | interface FHIRSchemaViewerProps { |
7 | 4 | structureDefinition: any; |
8 | 5 | title?: string; |
9 | 6 | } |
10 | 7 |
|
11 | 8 | interface FhirElementNode { |
| 9 | + key: string; |
12 | 10 | id: string; |
13 | 11 | path: string; |
14 | 12 | name: string; |
15 | 13 | min: number; |
16 | 14 | max: string; |
17 | | - types: string [] |
| 15 | + types: string[]; |
18 | 16 | sliceName?: string; |
| 17 | + |
| 18 | + short?: string; |
| 19 | + definition?: string; |
| 20 | + comment?: string; |
| 21 | + alias?: string[]; |
| 22 | + binding?: { |
| 23 | + strength?: string; |
| 24 | + valueSet?: string; |
| 25 | + }; |
| 26 | + mapping?: { |
| 27 | + identity: string; |
| 28 | + map: string; |
| 29 | + }[]; |
| 30 | + |
19 | 31 | children: FhirElementNode[]; |
20 | 32 | } |
21 | | - |
22 | 33 | /** |
23 | 34 | * Take snapshot.element[] and transform the flat list |
24 | 35 | * into a nested tree based on the element paths. |
25 | 36 | */ |
26 | 37 | function buildElementTree(elements: any[]): FhirElementNode[] { |
27 | | - const nodeMap = new Map<string, FhirElementNode>(); |
28 | 38 | const roots: FhirElementNode[] = []; |
29 | | - console.log(elements.length); |
30 | | - // First pass: build nodes |
31 | | - elements.forEach((el: any) => { |
32 | | - const path = el.path; |
33 | | - const parts = path.split("."); |
34 | | - const name = el.sliceName ?? parts[parts.length - 1]; |
| 39 | + const stack: { depth: number; node: FhirElementNode }[] = []; |
| 40 | + |
| 41 | + elements.forEach((el: any, index: number) => { |
| 42 | + const depth = el.path.split('.').length; |
| 43 | + |
35 | 44 | const node: FhirElementNode = { |
36 | | - id: el.id, // FHIR ID (with slice names) |
37 | | - path, // hierarchical path |
38 | | - name, // display name |
| 45 | + key: `${index}`, |
| 46 | + id: el.id, |
| 47 | + path: el.path, |
| 48 | + name: el.name, |
39 | 49 | min: el.min ?? 0, |
40 | | - max: el.max ?? "1", |
41 | | - types: Array.isArray(el.type) |
42 | | - ? el.type.map((t: any) => t.code) |
43 | | - : [], |
| 50 | + max: el.max ?? '1', |
| 51 | + types: Array.isArray(el.type) ? el.type.map((t: any) => t.code) : [], |
44 | 52 | sliceName: el.sliceName, |
45 | | - children: [], |
46 | | - }; |
47 | 53 |
|
48 | | - nodeMap.set(path, node); |
49 | | - }); |
| 54 | + short: el.short, |
| 55 | + definition: el.definition, |
| 56 | + comment: el.comment, |
| 57 | + alias: el.alias, |
| 58 | + binding: el.binding |
| 59 | + ? { |
| 60 | + strength: el.binding.strength, |
| 61 | + valueSet: el.binding.valueSet, |
| 62 | + } |
| 63 | + : undefined, |
| 64 | + mapping: el.mapping, |
50 | 65 |
|
51 | | - // Second pass: connect nodes into a tree |
52 | | - elements.forEach((el: any) => { |
53 | | - const path = el.path; |
54 | | - const parts = path.split("."); |
55 | | - const parentPath = parts.slice(0, -1).join("."); |
| 66 | + children: [], |
| 67 | + }; |
56 | 68 |
|
57 | | - const node = nodeMap.get(path); |
| 69 | + // Pop stack until we find the correct parent depth |
| 70 | + while (stack.length && stack[stack.length - 1].depth >= depth) { |
| 71 | + stack.pop(); |
| 72 | + } |
58 | 73 |
|
59 | | - if (parentPath && nodeMap.has(parentPath)) { |
60 | | - nodeMap.get(parentPath)!.children.push(node!); |
| 74 | + if (stack.length === 0) { |
| 75 | + roots.push(node); |
61 | 76 | } else { |
62 | | - // no parent => top-level root |
63 | | - roots.push(node!); |
| 77 | + stack[stack.length - 1].node.children.push(node); |
64 | 78 | } |
| 79 | + |
| 80 | + stack.push({ depth, node }); |
65 | 81 | }); |
66 | 82 |
|
67 | 83 | return roots; |
68 | 84 | } |
69 | | - |
70 | 85 | /** |
71 | 86 | * Simple recursive UI renderer for FHIR element tree. |
72 | 87 | */ |
73 | | -import { useState } from "react"; |
| 88 | +import { useState } from 'react'; |
| 89 | + |
| 90 | +function ElementDetails({ node }: { node: FhirElementNode }) { |
| 91 | + return ( |
| 92 | + <div |
| 93 | + style={{ |
| 94 | + marginTop: 6, |
| 95 | + padding: '10px 14px', |
| 96 | + background: '#f9fafb', |
| 97 | + borderLeft: '3px solid #3b82f6', |
| 98 | + fontSize: '0.85em', |
| 99 | + borderRadius: 4, |
| 100 | + }} |
| 101 | + > |
| 102 | + {node.short && ( |
| 103 | + <div> |
| 104 | + <strong>Short description</strong> |
| 105 | + <br /> |
| 106 | + {node.short} |
| 107 | + </div> |
| 108 | + )} |
| 109 | + |
| 110 | + {node.alias?.length && ( |
| 111 | + <div style={{ marginTop: 6 }}> |
| 112 | + <strong>Alternate names</strong> |
| 113 | + <br /> |
| 114 | + {node.alias.join(', ')} |
| 115 | + </div> |
| 116 | + )} |
| 117 | + |
| 118 | + {node.definition && ( |
| 119 | + <div style={{ marginTop: 6 }}> |
| 120 | + <strong>Definition</strong> |
| 121 | + <br /> |
| 122 | + {node.definition} |
| 123 | + </div> |
| 124 | + )} |
| 125 | + |
| 126 | + {node.comment && ( |
| 127 | + <div style={{ marginTop: 6 }}> |
| 128 | + <strong>Comments</strong> |
| 129 | + <br /> |
| 130 | + {node.comment} |
| 131 | + </div> |
| 132 | + )} |
| 133 | + |
| 134 | + {node.binding && ( |
| 135 | + <div style={{ marginTop: 6 }}> |
| 136 | + <strong>Binding</strong> |
| 137 | + <br /> |
| 138 | + {node.binding.strength} —{' '} |
| 139 | + <a href={node.binding.valueSet} target="_blank" rel="noreferrer"> |
| 140 | + {node.binding.valueSet} |
| 141 | + </a> |
| 142 | + </div> |
| 143 | + )} |
| 144 | + |
| 145 | + {node.mapping?.length && ( |
| 146 | + <div style={{ marginTop: 6 }}> |
| 147 | + <strong>Mappings</strong> |
| 148 | + <ul style={{ marginLeft: 16 }}> |
| 149 | + {node.mapping.map((m, i) => ( |
| 150 | + <li key={i}> |
| 151 | + <strong>{m.identity}:</strong> {m.map} |
| 152 | + </li> |
| 153 | + ))} |
| 154 | + </ul> |
| 155 | + </div> |
| 156 | + )} |
| 157 | + </div> |
| 158 | + ); |
| 159 | +} |
74 | 160 |
|
75 | 161 | function ElementNodeView({ node, depth }: { node: FhirElementNode; depth: number }) { |
76 | | - const [expanded, setExpanded] = useState(depth === 0); // root expanded by default |
| 162 | + const [expanded, setExpanded] = useState(depth === 0); |
| 163 | + const [selected, setSelected] = useState(false); |
77 | 164 |
|
78 | | - const hasChildren = node.children && node.children.length > 0; |
| 165 | + const hasChildren = node.children.length > 0; |
79 | 166 |
|
80 | 167 | return ( |
81 | 168 | <div style={{ marginLeft: depth * 18 }}> |
82 | 169 | <div |
83 | 170 | style={{ |
84 | | - display: "flex", |
85 | | - alignItems: "center", |
86 | | - cursor: hasChildren ? "pointer" : "default", |
87 | | - lineHeight: "1.5em", |
| 171 | + display: 'flex', |
| 172 | + alignItems: 'center', |
| 173 | + cursor: 'pointer', |
| 174 | + lineHeight: '1.6em', |
| 175 | + padding: '2px 4px', |
| 176 | + borderRadius: 4, |
| 177 | + }} |
| 178 | + onMouseEnter={(e) => { |
| 179 | + e.currentTarget.style.background = '#f3f4f6'; |
| 180 | + }} |
| 181 | + onMouseLeave={(e) => { |
| 182 | + e.currentTarget.style.background = 'transparent'; |
| 183 | + }} |
| 184 | + onClick={(e) => { |
| 185 | + e.stopPropagation(); // 🔑 critical |
| 186 | + setSelected(!selected); |
88 | 187 | }} |
89 | | - onClick={() => hasChildren && setExpanded(!expanded)} |
90 | 188 | > |
91 | | - {/* Expand / collapse triangle */} |
92 | 189 | {hasChildren ? ( |
93 | | - <span style={{ width: 14, display: "inline-block", fontSize: 12 }}> |
94 | | - {expanded ? "▼" : "▶"} |
| 190 | + <span |
| 191 | + onClick={(e) => { |
| 192 | + e.stopPropagation(); |
| 193 | + setExpanded(!expanded); |
| 194 | + }} |
| 195 | + style={{ width: 14, display: 'inline-block', fontSize: 12 }} |
| 196 | + > |
| 197 | + {expanded ? '▼' : '▶'} |
95 | 198 | </span> |
96 | 199 | ) : ( |
97 | | - <span style={{ width: 14, display: "inline-block" }} /> |
| 200 | + <span style={{ width: 14 }} /> |
98 | 201 | )} |
99 | 202 |
|
100 | | - {/* Element path */} |
101 | | - <span style={{ fontWeight: depth === 0 ? "bold" : "normal" }}> |
102 | | - {node.path} |
103 | | - </span> |
| 203 | + <span style={{ fontWeight: depth === 0 ? 'bold' : 'normal' }}>{node.path}</span> |
104 | 204 |
|
105 | | - {/* Min/max */} |
106 | 205 | <span style={{ opacity: 0.6, marginLeft: 6 }}> |
107 | 206 | ({node.min}..{node.max}) |
108 | 207 | </span> |
109 | 208 |
|
110 | | - {/* Type info */} |
111 | | - {(node.types?.length ?? 0) > 0 && ( |
112 | | - <span style={{ marginLeft: 8, fontSize: "0.9em", opacity: 0.8 }}> |
113 | | - : {node.types!.join(" | ")} |
| 209 | + {node.types.length > 0 && ( |
| 210 | + <span style={{ marginLeft: 8 }}> |
| 211 | + : |
| 212 | + {node.types.map((t, i) => ( |
| 213 | + <a |
| 214 | + key={t} |
| 215 | + href={`/fhir/datatypes/${t}`} |
| 216 | + style={{ |
| 217 | + marginLeft: 4, |
| 218 | + color: '#2563eb', |
| 219 | + textDecoration: 'none', |
| 220 | + fontSize: '0.9em', |
| 221 | + }} |
| 222 | + > |
| 223 | + {t} |
| 224 | + {i < node.types.length - 1 ? ' |' : ''} |
| 225 | + </a> |
| 226 | + ))} |
114 | 227 | </span> |
115 | 228 | )} |
116 | 229 | </div> |
117 | 230 |
|
118 | | - {/* Child nodes */} |
119 | | - {expanded && |
120 | | - hasChildren && |
121 | | - node.children.map((child) => ( |
122 | | - <ElementNodeView key={child.path} node={child} depth={depth + 1} /> |
123 | | - ))} |
| 231 | + {selected && <ElementDetails node={node} />} |
| 232 | + |
| 233 | + {expanded && node.children.map((child) => <ElementNodeView key={child.key} node={child} depth={depth + 1} />)} |
124 | 234 | </div> |
125 | 235 | ); |
126 | 236 | } |
127 | 237 | /** |
128 | 238 | * Main viewer component. |
129 | 239 | */ |
130 | 240 | export default function FHIRSchemaViewer({ structureDefinition, title }: FHIRSchemaViewerProps) { |
131 | | - |
132 | | - |
133 | 241 | if (!structureDefinition) { |
134 | 242 | return <div>No StructureDefinition provided.</div>; |
135 | 243 | } |
136 | 244 |
|
137 | 245 | return ( |
138 | | - <div style={{ padding: "1rem", fontFamily: "Arial" }}> |
139 | | - <h3 style={{ marginBottom: "0.5rem" }}> |
140 | | - {title ?? structureDefinition.title ?? structureDefinition.name} |
141 | | - </h3> |
| 246 | + <div style={{ padding: '1rem', fontFamily: 'Arial' }}> |
| 247 | + <h3 style={{ marginBottom: '0.5rem' }}>{title ?? structureDefinition.title ?? structureDefinition.name}</h3> |
142 | 248 |
|
143 | | - <div style={{ fontSize: "0.9rem", color: "#555", marginBottom: "1rem" }}> |
144 | | - {structureDefinition.description} |
145 | | - </div> |
| 249 | + <div style={{ fontSize: '0.9rem', color: '#555', marginBottom: '1rem' }}>{structureDefinition.description}</div> |
146 | 250 |
|
147 | 251 | {/* Render root elements */} |
148 | | - |
149 | | - <FhirResource |
150 | | - fhirResource={structureDefinition} |
151 | | - fhirVersion={fhirVersions.R4} |
152 | | - /> |
153 | | - |
| 252 | + {buildElementTree(structureDefinition.snapshot.element).map((rootNode) => ( |
| 253 | + <ElementNodeView key={rootNode.path} node={rootNode} depth={0} /> |
| 254 | + ))} |
154 | 255 | </div> |
155 | 256 | ); |
156 | 257 | } |
0 commit comments