@epcare/esm-report-builder 0.1.1-pre.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/.eslintignore +5 -0
  2. package/.eslintrc.js +19 -0
  3. package/.prettierrc +8 -0
  4. package/LICENSE +401 -0
  5. package/README.md +317 -0
  6. package/dist/184.js +2 -0
  7. package/dist/184.js.LICENSE.txt +14 -0
  8. package/dist/184.js.map +1 -0
  9. package/dist/211.js +1 -0
  10. package/dist/211.js.map +1 -0
  11. package/dist/300.js +1 -0
  12. package/dist/311.js +1 -0
  13. package/dist/311.js.map +1 -0
  14. package/dist/44.js +2 -0
  15. package/dist/44.js.LICENSE.txt +30 -0
  16. package/dist/44.js.map +1 -0
  17. package/dist/478.js +2 -0
  18. package/dist/478.js.LICENSE.txt +9 -0
  19. package/dist/478.js.map +1 -0
  20. package/dist/535.js +1 -0
  21. package/dist/535.js.map +1 -0
  22. package/dist/540.js +2 -0
  23. package/dist/540.js.LICENSE.txt +9 -0
  24. package/dist/540.js.map +1 -0
  25. package/dist/710.js +1 -0
  26. package/dist/710.js.map +1 -0
  27. package/dist/729.js +1 -0
  28. package/dist/729.js.map +1 -0
  29. package/dist/872.js +1 -0
  30. package/dist/872.js.map +1 -0
  31. package/dist/917.js +1 -0
  32. package/dist/917.js.map +1 -0
  33. package/dist/961.js +2 -0
  34. package/dist/961.js.LICENSE.txt +19 -0
  35. package/dist/961.js.map +1 -0
  36. package/dist/972.js +2 -0
  37. package/dist/972.js.LICENSE.txt +21 -0
  38. package/dist/972.js.map +1 -0
  39. package/dist/main.js +1 -0
  40. package/dist/main.js.map +1 -0
  41. package/dist/openmrs-esm-report-builder.js +1 -0
  42. package/dist/openmrs-esm-report-builder.js.buildmanifest.json +482 -0
  43. package/dist/openmrs-esm-report-builder.js.map +1 -0
  44. package/dist/routes.json +1 -0
  45. package/e2e/core/env.ts +5 -0
  46. package/e2e/core/global-setup.ts +34 -0
  47. package/e2e/core/test.ts +21 -0
  48. package/e2e/pages/app-shell.page.ts +34 -0
  49. package/e2e/pages/etl-sources.page.ts +35 -0
  50. package/e2e/pages/report-sections.page.ts +20 -0
  51. package/e2e/pages/section-preview.modal.ts +29 -0
  52. package/e2e/specs/etl-sources.spec.ts +51 -0
  53. package/e2e/specs/navigation.spec.ts +13 -0
  54. package/e2e/specs/section-preview-validation.spec.ts +38 -0
  55. package/e2e/specs/section-preview.spec.ts +94 -0
  56. package/e2e/tsconfig.json +11 -0
  57. package/example.env +5 -0
  58. package/jest.config.js +28 -0
  59. package/package.json +102 -0
  60. package/playwright.config.ts +28 -0
  61. package/src/admin-card-link.extension.module.scss +55 -0
  62. package/src/admin-card-link.extension.tsx +24 -0
  63. package/src/components/admin/admin-page.component.tsx +31 -0
  64. package/src/components/admin/age-categories-page.component.tsx +109 -0
  65. package/src/components/admin/age-groups-page.component.tsx +146 -0
  66. package/src/components/admin/etl-sources-page.component.tsx +344 -0
  67. package/src/components/admin/report-categories-page.component.tsx +256 -0
  68. package/src/components/admin/report-library-page.component.tsx +540 -0
  69. package/src/components/ai-support/ai-assist-button.component.tsx +30 -0
  70. package/src/components/ai-support/ai-assist-modal.component.tsx +81 -0
  71. package/src/components/ai-support/ai.service.ts +21 -0
  72. package/src/components/ai-support/ai.types.ts +12 -0
  73. package/src/components/app-shell/report-builder-shell.component.tsx +120 -0
  74. package/src/components/app-shell/report-builder-shell.scss +15 -0
  75. package/src/components/data-themes/data-theme-form.component.tsx +59 -0
  76. package/src/components/data-themes/data-theme-modal.component.tsx +349 -0
  77. package/src/components/data-themes/data-themes-page.component.tsx +132 -0
  78. package/src/components/data-themes/data-themes-table.component.tsx +91 -0
  79. package/src/components/data-themes/icon-registry.ts +35 -0
  80. package/src/components/data-themes/sections/data-theme-basics.section.tsx +63 -0
  81. package/src/components/data-themes/sections/data-theme-condition-columns.section.tsx +397 -0
  82. package/src/components/data-themes/sections/data-theme-fields-editor.section.tsx +125 -0
  83. package/src/components/data-themes/sections/data-theme-metadata.section.tsx +245 -0
  84. package/src/components/data-themes/sections/data-theme-preview.section.tsx +115 -0
  85. package/src/components/data-themes/sections/data-theme-source.section.tsx +184 -0
  86. package/src/components/indicators/README.md +24 -0
  87. package/src/components/indicators/base-indicator-basic-fields.component.tsx +18 -0
  88. package/src/components/indicators/base-indicator-basics.section.tsx +54 -0
  89. package/src/components/indicators/base-indicator-countby.section.tsx +25 -0
  90. package/src/components/indicators/base-indicator-criteria.section.tsx +83 -0
  91. package/src/components/indicators/base-indicator-details-form.component.tsx +80 -0
  92. package/src/components/indicators/base-indicator-theme-unit-fields.component.tsx +39 -0
  93. package/src/components/indicators/create-base-indicator-modal.component.tsx +501 -0
  94. package/src/components/indicators/create-composite-base-indicator-modal.component.tsx +297 -0
  95. package/src/components/indicators/create-final-indicator-modal.component.tsx +260 -0
  96. package/src/components/indicators/diagnosis-filters-form.component.tsx +101 -0
  97. package/src/components/indicators/disaggregation-panel.component.tsx +93 -0
  98. package/src/components/indicators/handler/concept-search-multiselect.component.tsx +153 -0
  99. package/src/components/indicators/handler/question-answer-concept-search.component.tsx +336 -0
  100. package/src/components/indicators/indicator-status-tag.component.tsx +9 -0
  101. package/src/components/indicators/indicators-page.component.tsx +481 -0
  102. package/src/components/indicators/indicators-page.scss +121 -0
  103. package/src/components/indicators/indicators-table.component.tsx +166 -0
  104. package/src/components/indicators/preview-table.component.tsx +31 -0
  105. package/src/components/indicators/sections/composite-indicator-basics.section.tsx +36 -0
  106. package/src/components/indicators/sections/composite-indicator-picker.section.tsx +161 -0
  107. package/src/components/indicators/sections/composite-indicator-sql-preview.section.tsx +31 -0
  108. package/src/components/indicators/sections/final-indicator-basics.section.tsx +46 -0
  109. package/src/components/indicators/sections/final-indicator-disaggregation.section.tsx +47 -0
  110. package/src/components/indicators/sections/final-indicator-picker.section.tsx +92 -0
  111. package/src/components/indicators/sections/final-indicator-preview.section.tsx +20 -0
  112. package/src/components/indicators/sections/final-indicator-results-preview.section.tsx +65 -0
  113. package/src/components/indicators/sections/indicator-basics.section.tsx +40 -0
  114. package/src/components/indicators/sections/indicator-conditions.section.tsx +194 -0
  115. package/src/components/indicators/sections/indicator-sql-preview.section.tsx +26 -0
  116. package/src/components/indicators/sections/indicator-theme.section.tsx +48 -0
  117. package/src/components/indicators/sql-preview.component.tsx +19 -0
  118. package/src/components/indicators/types/base-indicator-form.types.ts +17 -0
  119. package/src/components/indicators/types/base-indicator-theme.constants.ts +8 -0
  120. package/src/components/indicators/types/composite-indicator.types.ts +21 -0
  121. package/src/components/indicators/types/condition-ui.types.ts +11 -0
  122. package/src/components/indicators/types/data-theme-config.types.ts +88 -0
  123. package/src/components/indicators/types/indicator-types.ts +56 -0
  124. package/src/components/indicators/types/sql-builders.ts +43 -0
  125. package/src/components/indicators/utils/composite-indicator-sql.utils.ts +206 -0
  126. package/src/components/indicators/utils/concept-preload.utils.ts +21 -0
  127. package/src/components/indicators/utils/final-indicator-sql.utils.ts +124 -0
  128. package/src/components/indicators/utils/indicator-conditions-hydration.utils.ts +254 -0
  129. package/src/components/indicators/utils/indicator-sql.utils.ts +185 -0
  130. package/src/components/landing/landing-action-item.component.tsx +50 -0
  131. package/src/components/landing/landing-action-list.component.tsx +12 -0
  132. package/src/components/landing/landing-activity-list.component.tsx +47 -0
  133. package/src/components/landing/landing-footer-grid.component.tsx +45 -0
  134. package/src/components/landing/landing-hero.component.tsx +18 -0
  135. package/src/components/landing/landing-section-card.component.tsx +33 -0
  136. package/src/components/landing/report-builder-landing-page.component.tsx +103 -0
  137. package/src/components/landing/report-builder-landing-page.scss +131 -0
  138. package/src/components/report/dashboard/reports-table.component.tsx +145 -0
  139. package/src/components/report/definition/panels/properties-panel.component.tsx +38 -0
  140. package/src/components/report/definition/panels/report-structure-panel.component.tsx +61 -0
  141. package/src/components/report/definition/panels/selection-panel.component.tsx +58 -0
  142. package/src/components/report/definition/report-definition-editor.component.tsx +99 -0
  143. package/src/components/report/definition/report-definition.types.ts +66 -0
  144. package/src/components/report/design/panels/IndicatorPropertiesPanel.scss +33 -0
  145. package/src/components/report/design/panels/IndicatorPropertiesPanel.tsx +389 -0
  146. package/src/components/report/design/panels/JsonPreviewPanel.scss +32 -0
  147. package/src/components/report/design/panels/JsonPreviewPanel.tsx +104 -0
  148. package/src/components/report/design/panels/mapping-preview-panel.component.tsx +82 -0
  149. package/src/components/report/design/panels/mapping-preview-panel.scss +55 -0
  150. package/src/components/report/design/panels/template-structure-panel.component.tsx +296 -0
  151. package/src/components/report/design/panels/template-structure-panel.scss +81 -0
  152. package/src/components/report/design/report-design-editor.component.tsx +894 -0
  153. package/src/components/report/design/report-design.types.ts +43 -0
  154. package/src/components/report/design/report-design.utils.ts +86 -0
  155. package/src/components/report/panels/report-detail.component.tsx +71 -0
  156. package/src/components/report/panels/report-panel-shell.component.tsx +60 -0
  157. package/src/components/report/report-dashboard.page.component.tsx +75 -0
  158. package/src/components/report/report-editor.page.component.tsx +466 -0
  159. package/src/components/report/report.routes.tsx +30 -0
  160. package/src/components/report-sections/create-report-section-modal.component.tsx +278 -0
  161. package/src/components/report-sections/hooks/useAgeCategories.ts +25 -0
  162. package/src/components/report-sections/hooks/useSectionEditorState.ts +306 -0
  163. package/src/components/report-sections/panels/Dhis2MappingPanel.tsx +157 -0
  164. package/src/components/report-sections/panels/DisaggregationPanel.tsx +97 -0
  165. package/src/components/report-sections/panels/IndicatorPickerPanel.tsx +143 -0
  166. package/src/components/report-sections/panels/SectionBasicsPanel.tsx +27 -0
  167. package/src/components/report-sections/report-section-preview-modal.component.tsx +418 -0
  168. package/src/components/report-sections/report-sections-page.component.tsx +313 -0
  169. package/src/components/report-sections/section-types.ts +55 -0
  170. package/src/components/report-sections/section-utils.ts +46 -0
  171. package/src/components/report-sections/sections-page.scss +79 -0
  172. package/src/components/run-reports/run-reports-page.component.tsx +23 -0
  173. package/src/components/shared/header/builder-header-actions.component.tsx +38 -0
  174. package/src/components/shared/header/header.component.tsx +85 -0
  175. package/src/components/shared/header/header.scss +150 -0
  176. package/src/components/shared/header/illustration.component.tsx +34 -0
  177. package/src/components/shared/indicator-properties-panel/IndicatorPropertiesPanel.scss +33 -0
  178. package/src/components/shared/indicator-properties-panel/IndicatorPropertiesPanel.tsx +389 -0
  179. package/src/components/shared/json-preview-panel/JsonPreviewPanel.scss +32 -0
  180. package/src/components/shared/json-preview-panel/JsonPreviewPanel.tsx +104 -0
  181. package/src/components/shared/mapping-preview-panel/mapping-preview-panel.component.tsx +82 -0
  182. package/src/components/shared/mapping-preview-panel/mapping-preview-panel.scss +55 -0
  183. package/src/components/shared/preview/query-results-preview.component.tsx +204 -0
  184. package/src/components/shared/template-structure-panel/template-structure-panel.component.tsx +296 -0
  185. package/src/components/shared/template-structure-panel/template-structure-panel.scss +81 -0
  186. package/src/config-schema.ts +23 -0
  187. package/src/globals.d.ts +14 -0
  188. package/src/hooks/theme/useDataThemes.ts +28 -0
  189. package/src/hooks/theme/useMambaTableMeta.ts +34 -0
  190. package/src/hooks/theme/useMambaTables.ts +63 -0
  191. package/src/index.ts +23 -0
  192. package/src/resources/agegroup/agegroups.api.ts +58 -0
  193. package/src/resources/concepts/concept-mapper.ts +21 -0
  194. package/src/resources/concepts/concept-types.ts +31 -0
  195. package/src/resources/concepts/concepts.resource.ts +44 -0
  196. package/src/resources/concepts/useConceptSearch.ts +41 -0
  197. package/src/resources/etl-source/etl-source.api.ts +65 -0
  198. package/src/resources/indicator/indicators.api.ts +125 -0
  199. package/src/resources/openmrs-api.ts +24 -0
  200. package/src/resources/preview/sql-preview.api.ts +54 -0
  201. package/src/resources/report/reports.api.ts +117 -0
  202. package/src/resources/report-category/report-category.api.ts +41 -0
  203. package/src/resources/report-library/report-library.api.ts +63 -0
  204. package/src/resources/report-section/report-sections.api.ts +73 -0
  205. package/src/resources/report-section/section-preview.api.ts +21 -0
  206. package/src/resources/theme/data-theme.api.ts +88 -0
  207. package/src/resources/theme/mamba-schema.api.ts +31 -0
  208. package/src/resources/theme/mamba-table-meta.api.ts +45 -0
  209. package/src/root.component.tsx +20 -0
  210. package/src/routes/report-builder.routes.tsx +66 -0
  211. package/src/routes/report-builder.scss +56 -0
  212. package/src/routes.json +21 -0
  213. package/src/sample-template.ts +121 -0
  214. package/src/template-utils.ts +110 -0
  215. package/src/types/carbon-react-inert.d.ts +13 -0
  216. package/src/types/condition-operators.ts +49 -0
  217. package/src/types/theme/data-theme.types.ts +88 -0
  218. package/src/utils/html-entities.utils.ts +23 -0
  219. package/tools/setup-tests.ts +1 -0
  220. package/translations/en.json +4 -0
  221. package/tsconfig.json +43 -0
  222. package/webpack.config.js +14 -0
@@ -0,0 +1,418 @@
1
+ import React from 'react';
2
+ import {
3
+ Modal,
4
+ Stack,
5
+ Button,
6
+ InlineLoading,
7
+ InlineNotification,
8
+ TableContainer,
9
+ DataTable,
10
+ Table,
11
+ TableBody,
12
+ TableCell,
13
+ TableHead,
14
+ TableHeader,
15
+ TableRow,
16
+ TextInput,
17
+ } from '@carbon/react';
18
+ import { Play } from '@carbon/icons-react';
19
+
20
+ import { previewSection } from '../../resources/report-section/section-preview.api';
21
+ import type { ReportSectionDto } from '../../resources/report-section/report-sections.api';
22
+
23
+ type Props = {
24
+ open: boolean;
25
+ onClose: () => void;
26
+ section: ReportSectionDto | null;
27
+ };
28
+
29
+ type PreviewResult = {
30
+ indicatorUuid: string;
31
+ kind?: string;
32
+ name?: string;
33
+ code?: string;
34
+ columns: string[];
35
+ rows: any[][];
36
+ rowCount: number;
37
+ truncated: boolean;
38
+ error?: string | null;
39
+ };
40
+
41
+ type SectionPreviewResponse = {
42
+ sectionUuid: string;
43
+ results: PreviewResult[];
44
+ };
45
+
46
+ function normalizeKey(s: string) {
47
+ return String(s ?? '').trim().toLowerCase();
48
+ }
49
+
50
+ function decodeHtmlEntities(s: string) {
51
+ if (!s) return s;
52
+ return s
53
+ .replace(/&/g, '&')
54
+ .replace(/&lt;/g, '<')
55
+ .replace(/&gt;/g, '>')
56
+ .replace(/&quot;/g, '"')
57
+ .replace(/&#39;/g, "'");
58
+ }
59
+
60
+ function isMatrixCompatible(r: PreviewResult) {
61
+ const cols = (r.columns ?? []).map(normalizeKey);
62
+ return cols.includes('age_group') && cols.includes('gender') && cols.includes('value');
63
+ }
64
+
65
+ /**
66
+ * Sort age groups by "start age" in DAYS, not by first number.
67
+ * Handles labels like:
68
+ * - 0-28d
69
+ * - 29d-4yrs
70
+ * - 5-9yrs
71
+ * - 10-19yrs
72
+ * - 20yrs+
73
+ */
74
+ function startAgeInDays(label: string): number {
75
+ const s = String(label ?? '').trim().toLowerCase();
76
+
77
+ const startPart = s.split('-')[0].replace('+', '').trim();
78
+
79
+ const m = startPart.match(/^(\d+)\s*(d|day|days|m|mo|mos|month|months|y|yr|yrs|year|years)?$/i);
80
+ if (!m) {
81
+ const n = startPart.match(/\d+/);
82
+ return n ? Number(n[0]) * 365 : Number.POSITIVE_INFINITY;
83
+ }
84
+
85
+ const value = Number(m[1]);
86
+ const unit = (m[2] ?? 'y').toLowerCase();
87
+
88
+ if (unit === 'd' || unit === 'day' || unit === 'days') return value;
89
+ if (unit === 'm' || unit === 'mo' || unit === 'mos' || unit === 'month' || unit === 'months') return value * 30;
90
+
91
+ return value * 365;
92
+ }
93
+
94
+ function buildMatrix(results: PreviewResult[]) {
95
+ const matrixResults = results.filter((r) => !r.error && isMatrixCompatible(r));
96
+
97
+ const getIdx = (r: PreviewResult, key: string) => (r.columns ?? []).findIndex((c) => normalizeKey(c) === key);
98
+
99
+ const ageSet = new Set<string>();
100
+ const genderSet = new Set<string>();
101
+
102
+ for (const r of matrixResults) {
103
+ const ai = getIdx(r, 'age_group');
104
+ const gi = getIdx(r, 'gender');
105
+ if (ai < 0 || gi < 0) continue;
106
+
107
+ for (const row of r.rows ?? []) {
108
+ const ag = row?.[ai];
109
+ const g = row?.[gi];
110
+ if (ag != null) ageSet.add(String(ag));
111
+ if (g != null) genderSet.add(String(g));
112
+ }
113
+ }
114
+
115
+ const genders = Array.from(genderSet);
116
+ genders.sort((a, b) => {
117
+ const A = a.toUpperCase();
118
+ const B = b.toUpperCase();
119
+ const rank = (x: string) => (x === 'F' ? 0 : x === 'M' ? 1 : 2);
120
+ const ra = rank(A);
121
+ const rb = rank(B);
122
+ if (ra !== rb) return ra - rb;
123
+ return A.localeCompare(B);
124
+ });
125
+
126
+ const ageGroups = Array.from(ageSet);
127
+ ageGroups.sort((a, b) => {
128
+ const da = startAgeInDays(a);
129
+ const db = startAgeInDays(b);
130
+ if (da !== db) return da - db;
131
+ return a.localeCompare(b);
132
+ });
133
+
134
+ const disaggCols: Array<{ key: string; header: string; age: string; gender: string }> = [];
135
+ for (const ag of ageGroups) {
136
+ for (const g of genders) {
137
+ const key = `${ag}__${g}`;
138
+ disaggCols.push({ key, header: `${ag} ${g}`, age: ag, gender: g });
139
+ }
140
+ }
141
+
142
+ const rows = results.map((r, idx) => {
143
+ const base: any = {
144
+ id: r.indicatorUuid || String(idx),
145
+ indicator: decodeHtmlEntities(r.name || r.code || r.indicatorUuid),
146
+ kind: r.kind ?? '',
147
+ error: r.error ?? '',
148
+ };
149
+
150
+ for (const c of disaggCols) base[c.key] = 0;
151
+
152
+ if (!r.error && isMatrixCompatible(r)) {
153
+ const ai = getIdx(r, 'age_group');
154
+ const gi = getIdx(r, 'gender');
155
+ const vi = getIdx(r, 'value');
156
+
157
+ for (const row of r.rows ?? []) {
158
+ const ag = String(row?.[ai] ?? '');
159
+ const g = String(row?.[gi] ?? '');
160
+ const v = row?.[vi];
161
+
162
+ const key = `${ag}__${g}`;
163
+ if (key in base) base[key] = v ?? 0;
164
+ }
165
+ }
166
+
167
+ return base;
168
+ });
169
+
170
+ const headers = [
171
+ { key: 'indicator', header: 'Indicator' },
172
+ { key: 'kind', header: 'Type' },
173
+ ...disaggCols.map((c) => ({ key: c.key, header: c.header })),
174
+ { key: 'error', header: 'Error' },
175
+ ];
176
+
177
+ return { headers, rows, hasAnyDisagg: disaggCols.length > 0 };
178
+ }
179
+
180
+ function toGenericTableModel(columns: string[], rows: any[][]) {
181
+ const headers = columns.map((c, i) => ({ key: `${i}`, header: c }));
182
+ const tableRows = (rows ?? []).map((r, idx) => {
183
+ const obj: any = { id: String(idx) };
184
+ columns.forEach((_, i) => {
185
+ obj[`${i}`] = r?.[i];
186
+ });
187
+ return obj;
188
+ });
189
+ return { headers, tableRows };
190
+ }
191
+
192
+ export default function ReportSectionPreviewModal({ open, onClose, section }: Props) {
193
+ const [startDate, setStartDate] = React.useState<string>('');
194
+ const [endDate, setEndDate] = React.useState<string>('');
195
+ const [indicatorUuid, setIndicatorUuid] = React.useState<string>('');
196
+ const [maxRows, setMaxRows] = React.useState<number>(200);
197
+
198
+ const [loading, setLoading] = React.useState(false);
199
+ const [err, setErr] = React.useState<string | null>(null);
200
+ const [data, setData] = React.useState<SectionPreviewResponse | null>(null);
201
+
202
+ React.useEffect(() => {
203
+ if (!open) return;
204
+ setStartDate('');
205
+ setEndDate('');
206
+ setIndicatorUuid('');
207
+ setMaxRows(200);
208
+ setErr(null);
209
+ setData(null);
210
+ setLoading(false);
211
+ }, [open]);
212
+
213
+ const canRun = Boolean(section?.uuid) && Boolean(startDate) && Boolean(endDate) && !loading;
214
+
215
+ const run = async () => {
216
+ if (!section?.uuid) return;
217
+
218
+ if (!startDate || !endDate) {
219
+ setErr('Start date and End date are required.');
220
+ return;
221
+ }
222
+
223
+ const ac = new AbortController();
224
+ setLoading(true);
225
+ setErr(null);
226
+ setData(null);
227
+
228
+ try {
229
+ const resp = await previewSection(
230
+ {
231
+ sectionUuid: section.uuid,
232
+ indicatorUuid: indicatorUuid.trim() || undefined,
233
+ startDate,
234
+ endDate,
235
+ maxRows,
236
+ params: {},
237
+ },
238
+ ac.signal,
239
+ );
240
+
241
+ setData(resp);
242
+ } catch (e: any) {
243
+ setErr(e?.message ?? 'Failed to preview section');
244
+ } finally {
245
+ setLoading(false);
246
+ }
247
+ };
248
+
249
+ const results: PreviewResult[] = data?.results ?? [];
250
+ const matrix = buildMatrix(results);
251
+
252
+ const nonMatrix = results.filter((r) => r.error || !isMatrixCompatible(r));
253
+
254
+ return (
255
+ <Modal
256
+ open={open}
257
+ onRequestClose={onClose}
258
+ modalHeading={section ? `Preview Section: ${section.name}` : 'Preview Section'}
259
+ primaryButtonText="Close"
260
+ secondaryButtonText="Cancel"
261
+ onRequestSubmit={onClose}
262
+ size="lg"
263
+ >
264
+ <Stack gap={5}>
265
+ {!section ? (
266
+ <InlineNotification kind="warning" lowContrast title="No section" subtitle="Select a section first." />
267
+ ) : null}
268
+
269
+ <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '0.75rem' }}>
270
+ <TextInput
271
+ id="section-preview-startdate"
272
+ data-testid="section-preview-startdate"
273
+ labelText="Start date"
274
+ type="date"
275
+ value={startDate}
276
+ onChange={(e) => setStartDate((e.target as HTMLInputElement).value)}
277
+ disabled={!section}
278
+ />
279
+ <TextInput
280
+ id="section-preview-enddate"
281
+ data-testid="section-preview-enddate"
282
+ labelText="End date"
283
+ type="date"
284
+ value={endDate}
285
+ onChange={(e) => setEndDate((e.target as HTMLInputElement).value)}
286
+ disabled={!section}
287
+ />
288
+ </div>
289
+
290
+ <TextInput
291
+ id="section-preview-indicatoruuid"
292
+ data-testid="section-preview-indicatoruuid"
293
+ labelText="Indicator UUID (optional)"
294
+ value={indicatorUuid}
295
+ onChange={(e) => setIndicatorUuid((e.target as HTMLInputElement).value)}
296
+ disabled={!section}
297
+ />
298
+
299
+ <div style={{ display: 'grid', gridTemplateColumns: '1fr auto', gap: '0.75rem', alignItems: 'end' }}>
300
+ <TextInput
301
+ id="section-preview-maxrows"
302
+ data-testid="section-preview-maxrows"
303
+ labelText="Max rows"
304
+ value={String(maxRows)}
305
+ onChange={(e) => {
306
+ const v = Number((e.target as HTMLInputElement).value);
307
+ setMaxRows(Number.isFinite(v) && v > 0 ? v : 200);
308
+ }}
309
+ disabled={!section}
310
+ />
311
+
312
+ <Button data-testid="section-preview-run" kind="primary" renderIcon={Play} disabled={!canRun} onClick={run}>
313
+ Preview Section
314
+ </Button>
315
+ </div>
316
+
317
+ {loading ? <InlineLoading description="Running section preview…" /> : null}
318
+ {err ? <InlineNotification kind="error" lowContrast title="Preview" subtitle={err} /> : null}
319
+
320
+ {data ? (
321
+ matrix.hasAnyDisagg ? (
322
+ <TableContainer
323
+ data-testid="section-preview-matrix"
324
+ title="Section preview matrix"
325
+ description="Indicators are rows; disaggregations are columns. Missing values show as 0."
326
+ >
327
+ <DataTable rows={matrix.rows} headers={matrix.headers} size="md" useZebraStyles>
328
+ {({ rows, headers, getHeaderProps, getRowProps, getTableProps }) => (
329
+ <Table {...getTableProps()}>
330
+ <TableHead>
331
+ <TableRow>
332
+ {headers.map((h) => (
333
+ <TableHeader key={h.key} {...getHeaderProps({ header: h })}>
334
+ {h.header}
335
+ </TableHeader>
336
+ ))}
337
+ </TableRow>
338
+ </TableHead>
339
+ <TableBody>
340
+ {rows.map((row) => (
341
+ <TableRow key={row.id} {...getRowProps({ row })}>
342
+ {row.cells.map((cell) => (
343
+ <TableCell key={cell.id}>{cell.value as any}</TableCell>
344
+ ))}
345
+ </TableRow>
346
+ ))}
347
+ </TableBody>
348
+ </Table>
349
+ )}
350
+ </DataTable>
351
+ </TableContainer>
352
+ ) : (
353
+ <InlineNotification
354
+ kind="info"
355
+ lowContrast
356
+ title="No disaggregated results"
357
+ subtitle="No indicator returned (age_group, gender, value) rows for this preview."
358
+ />
359
+ )
360
+ ) : null}
361
+
362
+ {data && nonMatrix.length ? (
363
+ <Stack gap={4}>
364
+ <div style={{ fontWeight: 600, marginTop: '0.5rem' }}>Other results</div>
365
+
366
+ {nonMatrix.map((r, idx) => {
367
+ if (r.error) {
368
+ return (
369
+ <InlineNotification
370
+ key={`${r.indicatorUuid}-${idx}`}
371
+ kind="error"
372
+ lowContrast
373
+ title={`Indicator error: ${decodeHtmlEntities(r.name || r.code || r.indicatorUuid)}`}
374
+ subtitle={r.error}
375
+ />
376
+ );
377
+ }
378
+
379
+ const { headers, tableRows } = toGenericTableModel(r.columns ?? [], r.rows ?? []);
380
+ return (
381
+ <TableContainer
382
+ key={`${r.indicatorUuid}-${idx}`}
383
+ title={decodeHtmlEntities(r.name || r.code || r.indicatorUuid)}
384
+ description={`Returned columns: ${(r.columns ?? []).join(', ')}`}
385
+ >
386
+ <DataTable rows={tableRows} headers={headers} size="md" useZebraStyles>
387
+ {({ rows, headers, getHeaderProps, getRowProps, getTableProps }) => (
388
+ <Table {...getTableProps()}>
389
+ <TableHead>
390
+ <TableRow>
391
+ {headers.map((h) => (
392
+ <TableHeader key={h.key} {...getHeaderProps({ header: h })}>
393
+ {h.header}
394
+ </TableHeader>
395
+ ))}
396
+ </TableRow>
397
+ </TableHead>
398
+ <TableBody>
399
+ {rows.map((row) => (
400
+ <TableRow key={row.id} {...getRowProps({ row })}>
401
+ {row.cells.map((cell) => (
402
+ <TableCell key={cell.id}>{cell.value as any}</TableCell>
403
+ ))}
404
+ </TableRow>
405
+ ))}
406
+ </TableBody>
407
+ </Table>
408
+ )}
409
+ </DataTable>
410
+ </TableContainer>
411
+ );
412
+ })}
413
+ </Stack>
414
+ ) : null}
415
+ </Stack>
416
+ </Modal>
417
+ );
418
+ }