|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | + |
| 3 | +interface FHIRSchemaViewerProps { |
| 4 | + structureDefinition: any; |
| 5 | + title?: string; |
| 6 | +} |
| 7 | + |
| 8 | +interface FhirElementNode { |
| 9 | + key: string; |
| 10 | + id: string; |
| 11 | + path: string; |
| 12 | + name: string; |
| 13 | + min: number; |
| 14 | + max: string; |
| 15 | + types: string[]; |
| 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 | + |
| 31 | + children: FhirElementNode[]; |
| 32 | +} |
| 33 | +/** |
| 34 | + * Take snapshot.element[] and transform the flat list |
| 35 | + * into a nested tree based on the element paths. |
| 36 | + */ |
| 37 | +function buildElementTree(elements: any[]): FhirElementNode[] { |
| 38 | + const roots: FhirElementNode[] = []; |
| 39 | + const stack: { depth: number; node: FhirElementNode }[] = []; |
| 40 | + |
| 41 | + elements.forEach((el: any, index: number) => { |
| 42 | + const depth = el.path.split('.').length; |
| 43 | + |
| 44 | + const node: FhirElementNode = { |
| 45 | + key: `${index}`, |
| 46 | + id: el.id, |
| 47 | + path: el.path, |
| 48 | + name: el.name, |
| 49 | + min: el.min ?? 0, |
| 50 | + max: el.max ?? '1', |
| 51 | + types: Array.isArray(el.type) ? el.type.map((t: any) => t.code) : [], |
| 52 | + sliceName: el.sliceName, |
| 53 | + |
| 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, |
| 65 | + |
| 66 | + children: [], |
| 67 | + }; |
| 68 | + |
| 69 | + // Pop stack until we find the correct parent depth |
| 70 | + while (stack.length && stack[stack.length - 1].depth >= depth) { |
| 71 | + stack.pop(); |
| 72 | + } |
| 73 | + |
| 74 | + if (stack.length === 0) { |
| 75 | + roots.push(node); |
| 76 | + } else { |
| 77 | + stack[stack.length - 1].node.children.push(node); |
| 78 | + } |
| 79 | + |
| 80 | + stack.push({ depth, node }); |
| 81 | + }); |
| 82 | + |
| 83 | + return roots; |
| 84 | +} |
| 85 | +/** |
| 86 | + * Simple recursive UI renderer for FHIR element tree. |
| 87 | + */ |
| 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 | +} |
| 160 | + |
| 161 | +function ElementNodeView({ node, depth }: { node: FhirElementNode; depth: number }) { |
| 162 | + const [expanded, setExpanded] = useState(depth === 0); |
| 163 | + const [selected, setSelected] = useState(false); |
| 164 | + |
| 165 | + const hasChildren = node.children.length > 0; |
| 166 | + |
| 167 | + return ( |
| 168 | + <div style={{ marginLeft: depth * 18 }}> |
| 169 | + <div |
| 170 | + style={{ |
| 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); |
| 187 | + }} |
| 188 | + > |
| 189 | + {hasChildren ? ( |
| 190 | + <span |
| 191 | + onClick={(e) => { |
| 192 | + e.stopPropagation(); |
| 193 | + setExpanded(!expanded); |
| 194 | + }} |
| 195 | + style={{ width: 14, display: 'inline-block', fontSize: 12 }} |
| 196 | + > |
| 197 | + {expanded ? '▼' : '▶'} |
| 198 | + </span> |
| 199 | + ) : ( |
| 200 | + <span style={{ width: 14 }} /> |
| 201 | + )} |
| 202 | + |
| 203 | + <span style={{ fontWeight: depth === 0 ? 'bold' : 'normal' }}>{node.path}</span> |
| 204 | + |
| 205 | + <span style={{ opacity: 0.6, marginLeft: 6 }}> |
| 206 | + ({node.min}..{node.max}) |
| 207 | + </span> |
| 208 | + |
| 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 | + ))} |
| 227 | + </span> |
| 228 | + )} |
| 229 | + </div> |
| 230 | + |
| 231 | + {selected && <ElementDetails node={node} />} |
| 232 | + |
| 233 | + {expanded && node.children.map((child) => <ElementNodeView key={child.key} node={child} depth={depth + 1} />)} |
| 234 | + </div> |
| 235 | + ); |
| 236 | +} |
| 237 | +/** |
| 238 | + * Main viewer component. |
| 239 | + */ |
| 240 | +export default function FHIRSchemaViewer({ structureDefinition, title }: FHIRSchemaViewerProps) { |
| 241 | + if (!structureDefinition) { |
| 242 | + return <div>No StructureDefinition provided.</div>; |
| 243 | + } |
| 244 | + |
| 245 | + return ( |
| 246 | + <div style={{ padding: '1rem', fontFamily: 'Arial' }}> |
| 247 | + <h3 style={{ marginBottom: '0.5rem' }}>{title ?? structureDefinition.title ?? structureDefinition.name}</h3> |
| 248 | + |
| 249 | + <div style={{ fontSize: '0.9rem', color: '#555', marginBottom: '1rem' }}>{structureDefinition.description}</div> |
| 250 | + |
| 251 | + {/* Render root elements */} |
| 252 | + {buildElementTree(structureDefinition.snapshot.element).map((rootNode) => ( |
| 253 | + <ElementNodeView key={rootNode.path} node={rootNode} depth={0} /> |
| 254 | + ))} |
| 255 | + </div> |
| 256 | + ); |
| 257 | +} |
0 commit comments