@terreno/admin-frontend 0.20.1 → 0.21.0

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 (117) hide show
  1. package/.ai/guidelines/core.md +10 -0
  2. package/dist/AdminBreadcrumbs.d.ts +13 -0
  3. package/dist/AdminBreadcrumbs.js +22 -0
  4. package/dist/AdminBreadcrumbs.js.map +1 -0
  5. package/dist/AdminFieldRenderer.js +3 -3
  6. package/dist/AdminFieldRenderer.js.map +1 -1
  7. package/dist/AdminFieldRendererCore.d.ts +2 -0
  8. package/dist/AdminFieldRendererCore.js +28 -18
  9. package/dist/AdminFieldRendererCore.js.map +1 -1
  10. package/dist/AdminHome.d.ts +22 -0
  11. package/dist/AdminHome.js +189 -0
  12. package/dist/AdminHome.js.map +1 -0
  13. package/dist/AdminModelForm.d.ts +24 -1
  14. package/dist/AdminModelForm.js +185 -14
  15. package/dist/AdminModelForm.js.map +1 -1
  16. package/dist/AdminModelList.d.ts +4 -0
  17. package/dist/AdminModelList.js +19 -4
  18. package/dist/AdminModelList.js.map +1 -1
  19. package/dist/AdminModelTable.d.ts +2 -27
  20. package/dist/AdminModelTable.js +316 -60
  21. package/dist/AdminModelTable.js.map +1 -1
  22. package/dist/AdminNestedArrayField.d.ts +1 -0
  23. package/dist/AdminNestedArrayField.js +26 -6
  24. package/dist/AdminNestedArrayField.js.map +1 -1
  25. package/dist/AdminObjectPicker.d.ts +1 -0
  26. package/dist/AdminObjectPicker.js +6 -2
  27. package/dist/AdminObjectPicker.js.map +1 -1
  28. package/dist/AdminPrimitiveArrayField.d.ts +1 -0
  29. package/dist/AdminPrimitiveArrayField.js +9 -8
  30. package/dist/AdminPrimitiveArrayField.js.map +1 -1
  31. package/dist/AdminRefField.js +2 -2
  32. package/dist/AdminRefField.js.map +1 -1
  33. package/dist/AdminScriptList.js +114 -7
  34. package/dist/AdminScriptList.js.map +1 -1
  35. package/dist/AdminScriptRunModal.d.ts +5 -0
  36. package/dist/AdminScriptRunModal.js +136 -30
  37. package/dist/AdminScriptRunModal.js.map +1 -1
  38. package/dist/AdminShell.d.ts +39 -0
  39. package/dist/AdminShell.js +80 -0
  40. package/dist/AdminShell.js.map +1 -0
  41. package/dist/AdminShellLayout.d.ts +23 -0
  42. package/dist/AdminShellLayout.js +32 -0
  43. package/dist/AdminShellLayout.js.map +1 -0
  44. package/dist/AdminVersionConfig.d.ts +5 -0
  45. package/dist/AdminVersionConfig.js +15 -5
  46. package/dist/AdminVersionConfig.js.map +1 -1
  47. package/dist/ConfigurationScreen.js +3 -3
  48. package/dist/ConfigurationScreen.js.map +1 -1
  49. package/dist/ConsentFormEditor.js +2 -2
  50. package/dist/ConsentFormEditor.js.map +1 -1
  51. package/dist/ConsentFormList.js +1 -1
  52. package/dist/ConsentFormList.js.map +1 -1
  53. package/dist/ConsentResponseViewer.js +3 -3
  54. package/dist/ConsentResponseViewer.js.map +1 -1
  55. package/dist/DocumentStorageBrowser.js +1 -1
  56. package/dist/DocumentStorageBrowser.js.map +1 -1
  57. package/dist/adminModelListQueryParams.d.ts +14 -0
  58. package/dist/adminModelListQueryParams.js +51 -0
  59. package/dist/adminModelListQueryParams.js.map +1 -0
  60. package/dist/adminShellNav.d.ts +10 -0
  61. package/dist/adminShellNav.js +29 -0
  62. package/dist/adminShellNav.js.map +1 -0
  63. package/dist/index.d.ts +6 -1
  64. package/dist/index.js +5 -0
  65. package/dist/index.js.map +1 -1
  66. package/dist/scriptRunUtils.d.ts +36 -0
  67. package/dist/scriptRunUtils.js +121 -0
  68. package/dist/scriptRunUtils.js.map +1 -0
  69. package/dist/types.d.ts +70 -0
  70. package/dist/types.js.map +1 -1
  71. package/dist/useAdminApi.d.ts +1 -0
  72. package/dist/useAdminApi.js +11 -0
  73. package/dist/useAdminApi.js.map +1 -1
  74. package/dist/useAdminBackgroundTask.d.ts +12 -0
  75. package/dist/useAdminBackgroundTask.js +26 -0
  76. package/dist/useAdminBackgroundTask.js.map +1 -0
  77. package/dist/useAdminScripts.d.ts +19 -1
  78. package/dist/useAdminScripts.js +33 -4
  79. package/dist/useAdminScripts.js.map +1 -1
  80. package/package.json +4 -3
  81. package/src/AdminBreadcrumbs.test.tsx +27 -0
  82. package/src/AdminBreadcrumbs.tsx +63 -0
  83. package/src/AdminFieldRenderer.tsx +3 -0
  84. package/src/AdminFieldRendererCore.tsx +59 -1
  85. package/src/AdminHome.test.tsx +208 -0
  86. package/src/AdminHome.tsx +474 -0
  87. package/src/AdminModelForm.test.tsx +81 -1
  88. package/src/AdminModelForm.tsx +282 -34
  89. package/src/AdminModelList.test.tsx +1 -1
  90. package/src/AdminModelList.tsx +72 -39
  91. package/src/AdminModelTable.test.tsx +11 -0
  92. package/src/AdminModelTable.tsx +530 -92
  93. package/src/AdminNestedArrayField.tsx +59 -9
  94. package/src/AdminObjectPicker.tsx +19 -1
  95. package/src/AdminPrimitiveArrayField.tsx +30 -15
  96. package/src/AdminRefField.tsx +2 -0
  97. package/src/AdminScriptList.tsx +307 -30
  98. package/src/AdminScriptRunModal.tsx +410 -95
  99. package/src/AdminShell.tsx +296 -0
  100. package/src/AdminShellLayout.tsx +34 -0
  101. package/src/AdminVersionConfig.tsx +120 -76
  102. package/src/ConfigurationScreen.tsx +4 -2
  103. package/src/ConsentFormEditor.tsx +3 -1
  104. package/src/ConsentFormList.tsx +1 -1
  105. package/src/ConsentResponseViewer.tsx +3 -3
  106. package/src/DocumentStorageBrowser.tsx +1 -1
  107. package/src/adminModelListQueryParams.ts +61 -0
  108. package/src/adminShellNav.test.ts +29 -0
  109. package/src/adminShellNav.ts +33 -0
  110. package/src/index.tsx +6 -1
  111. package/src/isolated/AdminScriptRunModal.isolated.tsx +152 -1
  112. package/src/scriptRunUtils.test.ts +146 -0
  113. package/src/scriptRunUtils.ts +149 -0
  114. package/src/types.ts +69 -0
  115. package/src/useAdminApi.ts +11 -0
  116. package/src/useAdminBackgroundTask.ts +40 -0
  117. package/src/useAdminScripts.ts +56 -5
@@ -1,8 +1,28 @@
1
- import {Box, Button, Card, Heading, Page, Spinner, Text} from "@terreno/ui";
2
- import React, {useCallback, useState} from "react";
1
+ import {
2
+ Badge,
3
+ Box,
4
+ Button,
5
+ Card,
6
+ Heading,
7
+ Icon,
8
+ Page,
9
+ SegmentedControl,
10
+ Spinner,
11
+ Text,
12
+ } from "@terreno/ui";
13
+ import React, {useCallback, useEffect, useMemo, useRef, useState} from "react";
3
14
  import {AdminScriptRunModal} from "./AdminScriptRunModal";
4
- import {type AdminApi, type AdminScriptConfig, resolveAdminBases} from "./types";
15
+ import {
16
+ formatDuration,
17
+ latestRunByScript,
18
+ relativeTime,
19
+ shortId,
20
+ statusMeta,
21
+ summarizeOutput,
22
+ } from "./scriptRunUtils";
23
+ import {type AdminApi, type AdminScriptConfig, resolveAdminBases, type ScriptRun} from "./types";
5
24
  import {useAdminConfig} from "./useAdminConfig";
25
+ import {useAdminScripts} from "./useAdminScripts";
6
26
 
7
27
  interface AdminScriptListProps {
8
28
  /** @deprecated Use `apiBase`/`routeBase`. Kept as a backward-compatible alias. */
@@ -16,6 +36,16 @@ interface AdminScriptListProps {
16
36
  isAdmin?: boolean;
17
37
  }
18
38
 
39
+ const HISTORY_PAGE_SIZE = 25;
40
+
41
+ type Tab = "scripts" | "history";
42
+
43
+ interface SelectedRun {
44
+ description?: string;
45
+ scriptName: string;
46
+ taskId: string;
47
+ }
48
+
19
49
  export const AdminScriptList: React.FC<AdminScriptListProps> = ({
20
50
  baseUrl,
21
51
  apiBase,
@@ -29,22 +59,104 @@ export const AdminScriptList: React.FC<AdminScriptListProps> = ({
29
59
  routeBase,
30
60
  });
31
61
  const {config, isLoading, error} = useAdminConfig(api, resolvedApiBase);
62
+ const {useListScriptRunsQuery} = useAdminScripts(api, resolvedApiBase);
63
+
64
+ const [tab, setTab] = useState<Tab>("scripts");
65
+ const [historyPage, setHistoryPage] = useState(1);
66
+ const [historyScriptName, setHistoryScriptName] = useState<string | null>(null);
67
+ const [historyRuns, setHistoryRuns] = useState<ScriptRun[]>([]);
68
+ const [historyRunsKey, setHistoryRunsKey] = useState<string | null>(null);
69
+ const historyKeyRef = useRef<string | null>(null);
32
70
  const [selectedScript, setSelectedScript] = useState<AdminScriptConfig | null>(null);
71
+ const [selectedRun, setSelectedRun] = useState<SelectedRun | null>(null);
33
72
  const [modalVisible, setModalVisible] = useState(false);
34
73
 
74
+ // Unfiltered most-recent runs power the per-script "Last run" badges on the Scripts tab.
75
+ const {data: latestRunsData} = useListScriptRunsQuery({limit: HISTORY_PAGE_SIZE, page: 1});
76
+ const latestRuns: ScriptRun[] = useMemo(() => latestRunsData?.data ?? [], [latestRunsData]);
77
+ const lastRuns = useMemo(() => latestRunByScript(latestRuns), [latestRuns]);
78
+
79
+ // Paginated history, optionally scoped to a single script via the per-script History link.
80
+ const {data: historyData} = useListScriptRunsQuery({
81
+ limit: HISTORY_PAGE_SIZE,
82
+ name: historyScriptName ?? undefined,
83
+ page: historyPage,
84
+ });
85
+
86
+ // Accumulate each loaded history page (de-duplicated by id) so "Load more" advances through
87
+ // pages instead of re-requesting a server-capped first page. Keying on the active filter lets
88
+ // a filter change replace the list (rather than blanking state and risking an empty render when
89
+ // RTK Query serves a cached, same-reference page).
90
+ useEffect(() => {
91
+ if (!historyData?.data) {
92
+ return;
93
+ }
94
+ const filterKey = historyScriptName ?? "__all__";
95
+ setHistoryRuns((prev) => {
96
+ const base = historyKeyRef.current === filterKey ? prev : [];
97
+ const byId = new Map(base.map((run) => [run._id, run]));
98
+ for (const run of historyData.data ?? []) {
99
+ byId.set(run._id, run);
100
+ }
101
+ return Array.from(byId.values());
102
+ });
103
+ historyKeyRef.current = filterKey;
104
+ setHistoryRunsKey(filterKey);
105
+ }, [historyData, historyScriptName]);
106
+
107
+ // The accumulated list belongs to whichever filter last loaded. While a newly-selected filter
108
+ // is still fetching, the list does not yet match the active scope; render a spinner instead of
109
+ // stale runs from the previous filter.
110
+ const activeFilterKey = historyScriptName ?? "__all__";
111
+ const historyMatchesFilter = historyRunsKey === activeFilterKey;
112
+
35
113
  const handleRunScript = useCallback((script: AdminScriptConfig) => {
114
+ setSelectedRun(null);
36
115
  setSelectedScript(script);
37
116
  setModalVisible(true);
38
117
  }, []);
39
118
 
119
+ const handleOpenRun = useCallback((run: ScriptRun, description?: string) => {
120
+ setSelectedScript(null);
121
+ setSelectedRun({description, scriptName: run.taskType, taskId: run._id});
122
+ setModalVisible(true);
123
+ }, []);
124
+
125
+ const handleViewHistory = useCallback((scriptName?: string) => {
126
+ setHistoryScriptName(scriptName ?? null);
127
+ setHistoryPage(1);
128
+ setTab("history");
129
+ }, []);
130
+
131
+ const handleLoadMore = useCallback(() => {
132
+ setHistoryPage((page) => page + 1);
133
+ }, []);
134
+
135
+ const handleClearHistoryFilter = useCallback(() => {
136
+ setHistoryScriptName(null);
137
+ setHistoryPage(1);
138
+ }, []);
139
+
40
140
  const handleDismiss = useCallback(() => {
41
141
  setModalVisible(false);
42
142
  setSelectedScript(null);
143
+ setSelectedRun(null);
144
+ }, []);
145
+
146
+ const handleTabChange = useCallback((index: number) => {
147
+ if (index === 1) {
148
+ // Selecting the global history tab clears any per-script filter.
149
+ setHistoryScriptName(null);
150
+ setHistoryPage(1);
151
+ setTab("history");
152
+ return;
153
+ }
154
+ setTab("scripts");
43
155
  }, []);
44
156
 
45
157
  if (isLoading) {
46
158
  return (
47
- <Page maxWidth="100%" title="Scripts">
159
+ <Page color="transparent" maxWidth="100%" padding={0} title="Scripts">
48
160
  <Box alignItems="center" justifyContent="center" padding={6}>
49
161
  <Spinner />
50
162
  </Box>
@@ -54,7 +166,7 @@ export const AdminScriptList: React.FC<AdminScriptListProps> = ({
54
166
 
55
167
  if (error || !config) {
56
168
  return (
57
- <Page maxWidth="100%" title="Scripts">
169
+ <Page color="transparent" maxWidth="100%" padding={0} title="Scripts">
58
170
  <Box padding={4}>
59
171
  <Text color="error">Failed to load admin configuration.</Text>
60
172
  </Box>
@@ -66,7 +178,7 @@ export const AdminScriptList: React.FC<AdminScriptListProps> = ({
66
178
 
67
179
  if (scripts.length === 0) {
68
180
  return (
69
- <Page maxWidth="100%" title="Scripts">
181
+ <Page color="transparent" maxWidth="100%" padding={0} title="Scripts">
70
182
  <Box alignItems="center" padding={6}>
71
183
  <Text color="secondaryDark">No scripts registered.</Text>
72
184
  </Box>
@@ -74,38 +186,203 @@ export const AdminScriptList: React.FC<AdminScriptListProps> = ({
74
186
  );
75
187
  }
76
188
 
77
- return (
78
- <Page maxWidth="100%" scroll title="Scripts">
79
- <Box gap={3} padding={4}>
80
- {scripts.map((script: AdminScriptConfig) => (
81
- <Card key={script.name} padding={4} testID={`admin-script-card-${script.name}`}>
82
- <Box alignItems="center" direction="row" gap={4} justifyContent="between">
83
- <Box flex="grow" gap={1}>
84
- <Heading size="sm">{script.name}</Heading>
85
- <Text color="secondaryDark" size="sm">
86
- {script.description}
87
- </Text>
88
- </Box>
89
- <Button
90
- disabled={!isAdmin}
91
- onClick={() => handleRunScript(script)}
92
- testID={`admin-script-run-${script.name}`}
93
- text="Run"
94
- tooltipText={!isAdmin ? "Only admins can run scripts" : undefined}
95
- variant="primary"
96
- />
189
+ const descriptionFor = (name: string): string | undefined =>
190
+ scripts.find((s) => s.name === name)?.description;
191
+
192
+ const renderLastRun = (script: AdminScriptConfig): React.ReactElement | null => {
193
+ const run = lastRuns[script.name];
194
+ if (!run) {
195
+ return null;
196
+ }
197
+ const meta = statusMeta(run.status);
198
+ const iconColor =
199
+ meta.badgeStatus === "success" ||
200
+ meta.badgeStatus === "error" ||
201
+ meta.badgeStatus === "warning"
202
+ ? meta.badgeStatus
203
+ : "secondaryDark";
204
+ return (
205
+ <Box alignItems="center" direction="row" gap={2} marginTop={1} wrap>
206
+ <Icon color={iconColor} iconName={meta.iconName} size="sm" />
207
+ <Text color="secondaryDark" size="sm">
208
+ Last run {relativeTime(run.created)} — {run.isDryRun ? "dry run" : "live run"}{" "}
209
+ {meta.label.toLowerCase()}
210
+ </Text>
211
+ <Box
212
+ accessibilityHint={`Opens run history for ${script.name}`}
213
+ accessibilityLabel={`View run history for ${script.name}`}
214
+ onClick={() => handleViewHistory(script.name)}
215
+ testID={`admin-script-history-${script.name}`}
216
+ >
217
+ <Text color="link" size="sm">
218
+ History
219
+ </Text>
220
+ </Box>
221
+ </Box>
222
+ );
223
+ };
224
+
225
+ const renderScripts = (): React.ReactElement => (
226
+ <Box gap={3} padding={4}>
227
+ {scripts.map((script: AdminScriptConfig) => (
228
+ <Card key={script.name} padding={4} testID={`admin-script-card-${script.name}`}>
229
+ <Box alignItems="center" direction="row" gap={4} justifyContent="between">
230
+ <Box flex="grow" gap={1}>
231
+ <Heading size="sm">{script.name}</Heading>
232
+ <Text color="secondaryDark" size="sm">
233
+ {script.description}
234
+ </Text>
235
+ {renderLastRun(script)}
97
236
  </Box>
98
- </Card>
99
- ))}
237
+ <Button
238
+ disabled={!isAdmin}
239
+ iconName="play"
240
+ onClick={() => handleRunScript(script)}
241
+ testID={`admin-script-run-${script.name}`}
242
+ text="Run"
243
+ tooltipText={!isAdmin ? "Only admins can run scripts" : undefined}
244
+ variant="primary"
245
+ />
246
+ </Box>
247
+ </Card>
248
+ ))}
249
+ <Box alignItems="center" direction="row" gap={2} justifyContent="center" paddingY={2}>
250
+ <Icon color="secondaryDark" iconName="circle-info" size="sm" />
251
+ <Text color="secondaryDark" size="sm">
252
+ Each Run opens the runner. Start with a dry run to preview changes before running live.
253
+ </Text>
254
+ </Box>
255
+ </Box>
256
+ );
257
+
258
+ const renderRunRow = (run: ScriptRun): React.ReactElement => {
259
+ const meta = statusMeta(run.status);
260
+ const summary = summarizeOutput(run);
261
+ const duration = formatDuration(run.startedAt, run.completedAt);
262
+ return (
263
+ <Box
264
+ accessibilityHint="Opens this run's output"
265
+ accessibilityLabel={`Open run ${run.taskType}`}
266
+ alignItems="center"
267
+ border="default"
268
+ direction="row"
269
+ gap={3}
270
+ key={run._id}
271
+ onClick={() => handleOpenRun(run, descriptionFor(run.taskType))}
272
+ padding={3}
273
+ rounding="md"
274
+ wrap
275
+ >
276
+ <Box flex="grow" gap={1} minWidth={180}>
277
+ <Box alignItems="baseline" direction="row" gap={2} wrap>
278
+ <Text bold>{run.taskType}</Text>
279
+ <Text color="secondaryDark" size="sm">
280
+ #{shortId(run._id)}
281
+ </Text>
282
+ </Box>
283
+ <Text color="secondaryDark" size="sm">
284
+ {summary.total} line{summary.total === 1 ? "" : "s"}
285
+ {summary.errorCount > 0
286
+ ? ` · ${summary.errorCount} error${summary.errorCount === 1 ? "" : "s"}`
287
+ : ""}
288
+ {duration ? ` · ${duration}` : ""}
289
+ </Text>
290
+ </Box>
291
+ <Box gap={1} minWidth={110}>
292
+ <Text size="sm">{relativeTime(run.created)}</Text>
293
+ {run.createdByName ? (
294
+ <Text color="secondaryDark" size="sm">
295
+ {run.createdByName}
296
+ </Text>
297
+ ) : null}
298
+ </Box>
299
+ <Badge status={run.isDryRun ? "info" : "warning"} value={run.isDryRun ? "DRY" : "LIVE"} />
300
+ <Badge
301
+ iconName={meta.iconName}
302
+ secondary
303
+ status={meta.badgeStatus === "neutral" ? "info" : meta.badgeStatus}
304
+ value={meta.label}
305
+ />
306
+ <Button
307
+ iconName="arrow-right"
308
+ onClick={() => handleOpenRun(run, descriptionFor(run.taskType))}
309
+ text="Open"
310
+ variant="outline"
311
+ />
312
+ </Box>
313
+ );
314
+ };
315
+
316
+ const renderHistory = (): React.ReactElement => (
317
+ <Box gap={3} padding={4}>
318
+ <Box gap={1}>
319
+ <Heading size="sm">Run history</Heading>
320
+ <Text color="secondaryDark" size="sm">
321
+ {historyScriptName
322
+ ? `Showing runs for ${historyScriptName}. Open a run to review its output.`
323
+ : "Every dry and live run is logged. Open a run to review its output."}
324
+ </Text>
325
+ {historyScriptName ? (
326
+ <Box
327
+ accessibilityHint="Shows runs for every script"
328
+ accessibilityLabel="Show all scripts"
329
+ onClick={handleClearHistoryFilter}
330
+ testID="admin-script-history-clear-filter"
331
+ >
332
+ <Text color="link" size="sm">
333
+ Show all scripts
334
+ </Text>
335
+ </Box>
336
+ ) : null}
100
337
  </Box>
338
+ {!historyMatchesFilter ? (
339
+ <Box alignItems="center" justifyContent="center" padding={6}>
340
+ <Spinner />
341
+ </Box>
342
+ ) : historyRuns.length === 0 ? (
343
+ <Box alignItems="center" padding={6}>
344
+ <Text color="secondaryDark">
345
+ {historyScriptName ? "No runs logged for this script yet." : "No runs logged yet."}
346
+ </Text>
347
+ </Box>
348
+ ) : (
349
+ <Box gap={2}>
350
+ {historyRuns.map((run) => renderRunRow(run))}
351
+ {historyData?.more && (
352
+ <Box alignItems="center" paddingY={2}>
353
+ <Button onClick={handleLoadMore} text="Load more" variant="muted" />
354
+ </Box>
355
+ )}
356
+ </Box>
357
+ )}
358
+ </Box>
359
+ );
360
+
361
+ return (
362
+ <Page color="transparent" maxWidth="100%" padding={0} scroll title="Scripts">
363
+ <Box paddingX={4} paddingY={4}>
364
+ <SegmentedControl
365
+ items={[
366
+ "Scripts",
367
+ (latestRunsData?.total ?? latestRuns.length) > 0
368
+ ? `Run history (${latestRunsData?.total ?? latestRuns.length})`
369
+ : "Run history",
370
+ ]}
371
+ onChange={handleTabChange}
372
+ selectedIndex={tab === "history" ? 1 : 0}
373
+ />
374
+ </Box>
375
+
376
+ {tab === "scripts" ? renderScripts() : renderHistory()}
101
377
 
102
378
  <AdminScriptRunModal
103
379
  api={api}
104
380
  apiBase={resolvedApiBase}
381
+ historyTaskId={selectedRun?.taskId}
105
382
  onDismiss={handleDismiss}
106
383
  routeBase={resolvedRouteBase}
107
- scriptDescription={selectedScript?.description}
108
- scriptName={selectedScript?.name ?? null}
384
+ scriptDescription={selectedRun?.description ?? selectedScript?.description}
385
+ scriptName={selectedRun?.scriptName ?? selectedScript?.name ?? null}
109
386
  visible={modalVisible}
110
387
  />
111
388
  </Page>