@terreno/admin-frontend 0.3.2 → 0.4.1
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.
- package/README.md +5 -0
- package/dist/AdminFieldRenderer.d.ts +36 -0
- package/dist/AdminFieldRenderer.js +41 -1
- package/dist/AdminFieldRenderer.js.map +1 -1
- package/dist/AdminModelForm.d.ts +51 -0
- package/dist/AdminModelForm.js +55 -4
- package/dist/AdminModelForm.js.map +1 -1
- package/dist/AdminModelList.d.ts +26 -0
- package/dist/AdminModelList.js +35 -5
- package/dist/AdminModelList.js.map +1 -1
- package/dist/AdminModelTable.d.ts +29 -0
- package/dist/AdminModelTable.js +45 -16
- package/dist/AdminModelTable.js.map +1 -1
- package/dist/AdminRefField.d.ts +35 -0
- package/dist/AdminRefField.js +36 -1
- package/dist/AdminRefField.js.map +1 -1
- package/dist/AdminScriptList.d.ts +10 -0
- package/dist/AdminScriptList.js +31 -0
- package/dist/AdminScriptList.js.map +1 -0
- package/dist/AdminScriptRunModal.d.ts +11 -0
- package/dist/AdminScriptRunModal.js +87 -0
- package/dist/AdminScriptRunModal.js.map +1 -0
- package/dist/ConfigurationScreen.d.ts +30 -0
- package/dist/ConfigurationScreen.js +128 -0
- package/dist/ConfigurationScreen.js.map +1 -0
- package/dist/DocumentStorageBrowser.d.ts +3 -0
- package/dist/DocumentStorageBrowser.js +317 -0
- package/dist/DocumentStorageBrowser.js.map +1 -0
- package/dist/index.d.ts +8 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +52 -0
- package/dist/types.js.map +1 -1
- package/dist/useAdminApi.d.ts +43 -0
- package/dist/useAdminApi.js +44 -1
- package/dist/useAdminApi.js.map +1 -1
- package/dist/useAdminConfig.d.ts +29 -0
- package/dist/useAdminConfig.js +30 -1
- package/dist/useAdminConfig.js.map +1 -1
- package/dist/useAdminScripts.d.ts +32 -0
- package/dist/useAdminScripts.js +36 -0
- package/dist/useAdminScripts.js.map +1 -0
- package/dist/useConfigurationApi.d.ts +34 -0
- package/dist/useConfigurationApi.js +53 -0
- package/dist/useConfigurationApi.js.map +1 -0
- package/dist/useDocumentStorageApi.d.ts +9 -0
- package/dist/useDocumentStorageApi.js +89 -0
- package/dist/useDocumentStorageApi.js.map +1 -0
- package/package.json +17 -3
- package/src/AdminFieldRenderer.tsx +57 -1
- package/src/AdminModelForm.tsx +69 -16
- package/src/AdminModelList.tsx +107 -21
- package/src/AdminModelTable.tsx +98 -68
- package/src/AdminRefField.tsx +36 -1
- package/src/AdminScriptList.test.tsx +124 -0
- package/src/AdminScriptList.tsx +96 -0
- package/src/AdminScriptRunModal.test.tsx +69 -0
- package/src/AdminScriptRunModal.tsx +188 -0
- package/src/ConfigurationScreen.tsx +319 -0
- package/src/DocumentStorageBrowser.tsx +641 -0
- package/src/index.tsx +12 -0
- package/src/types.ts +59 -0
- package/src/useAdminApi.ts +44 -1
- package/src/useAdminConfig.ts +30 -1
- package/src/useAdminScripts.ts +48 -0
- package/src/useConfigurationApi.ts +70 -0
- package/src/useDocumentStorageApi.ts +96 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {renderWithTheme} from "@terreno/ui/src/test-utils";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import {AdminScriptRunModal} from "./AdminScriptRunModal";
|
|
5
|
+
|
|
6
|
+
// Mock useAdminScripts to control hooks
|
|
7
|
+
const mockRunScript = mock(() => ({unwrap: () => Promise.resolve({taskId: "task-123"})}));
|
|
8
|
+
const mockCancelTask = mock(() => ({unwrap: () => Promise.resolve({message: "cancelled"})}));
|
|
9
|
+
|
|
10
|
+
mock.module("./useAdminScripts", () => ({
|
|
11
|
+
useAdminScripts: () => ({
|
|
12
|
+
useCancelScriptTaskMutation: () => [mockCancelTask, {isLoading: false}],
|
|
13
|
+
useGetScriptTaskQuery: () => ({data: undefined, error: null, isLoading: false}),
|
|
14
|
+
useRunScriptMutation: () => [mockRunScript, {isLoading: false}],
|
|
15
|
+
}),
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
const mockApi = {} as any;
|
|
19
|
+
|
|
20
|
+
describe("AdminScriptRunModal", () => {
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
mockRunScript.mockClear();
|
|
23
|
+
mockCancelTask.mockClear();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("renders without crashing when not visible", () => {
|
|
27
|
+
const {toJSON} = renderWithTheme(
|
|
28
|
+
<AdminScriptRunModal
|
|
29
|
+
api={mockApi}
|
|
30
|
+
baseUrl="/admin"
|
|
31
|
+
onDismiss={() => {}}
|
|
32
|
+
scriptName="test-script"
|
|
33
|
+
visible={false}
|
|
34
|
+
/>
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
expect(toJSON()).toBeDefined();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("renders the modal container when visible and auto-starts", () => {
|
|
41
|
+
const {toJSON} = renderWithTheme(
|
|
42
|
+
<AdminScriptRunModal
|
|
43
|
+
api={mockApi}
|
|
44
|
+
baseUrl="/admin"
|
|
45
|
+
onDismiss={() => {}}
|
|
46
|
+
scriptName="test-script"
|
|
47
|
+
visible={true}
|
|
48
|
+
/>
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
expect(toJSON()).toBeDefined();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("calls onDismiss when provided", () => {
|
|
55
|
+
const onDismiss = mock(() => {});
|
|
56
|
+
|
|
57
|
+
const {toJSON} = renderWithTheme(
|
|
58
|
+
<AdminScriptRunModal
|
|
59
|
+
api={mockApi}
|
|
60
|
+
baseUrl="/admin"
|
|
61
|
+
onDismiss={onDismiss}
|
|
62
|
+
scriptName="test-script"
|
|
63
|
+
visible={true}
|
|
64
|
+
/>
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
expect(toJSON()).toBeDefined();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import type {Api} from "@reduxjs/toolkit/query/react";
|
|
2
|
+
import {Box, Button, Heading, Icon, Modal, Spinner, Text} from "@terreno/ui";
|
|
3
|
+
import React, {useCallback, useEffect, useRef, useState} from "react";
|
|
4
|
+
import type {BackgroundTask} from "./types";
|
|
5
|
+
import {useAdminScripts} from "./useAdminScripts";
|
|
6
|
+
|
|
7
|
+
interface AdminScriptRunModalProps {
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
api: Api<any, any, any, any>;
|
|
10
|
+
scriptName: string | null;
|
|
11
|
+
visible: boolean;
|
|
12
|
+
onDismiss: () => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const POLL_INTERVAL_MS = 2000;
|
|
16
|
+
|
|
17
|
+
const isTerminalStatus = (status?: string): boolean =>
|
|
18
|
+
status === "completed" || status === "failed" || status === "cancelled";
|
|
19
|
+
|
|
20
|
+
export const AdminScriptRunModal: React.FC<AdminScriptRunModalProps> = ({
|
|
21
|
+
baseUrl,
|
|
22
|
+
api,
|
|
23
|
+
scriptName,
|
|
24
|
+
visible,
|
|
25
|
+
onDismiss,
|
|
26
|
+
}) => {
|
|
27
|
+
const [taskId, setTaskId] = useState<string | null>(null);
|
|
28
|
+
const [phase, setPhase] = useState<"running" | "done">("running");
|
|
29
|
+
const [startError, setStartError] = useState<string | null>(null);
|
|
30
|
+
const hasStartedRef = useRef(false);
|
|
31
|
+
|
|
32
|
+
const {useRunScriptMutation, useGetScriptTaskQuery, useCancelScriptTaskMutation} =
|
|
33
|
+
useAdminScripts(api, baseUrl);
|
|
34
|
+
|
|
35
|
+
const [runScript] = useRunScriptMutation();
|
|
36
|
+
const [cancelTask, {isLoading: isCancelling}] = useCancelScriptTaskMutation();
|
|
37
|
+
|
|
38
|
+
const shouldPoll = phase === "running" && taskId !== null;
|
|
39
|
+
const {data: taskData} = useGetScriptTaskQuery(taskId ?? "", {
|
|
40
|
+
pollingInterval: shouldPoll ? POLL_INTERVAL_MS : 0,
|
|
41
|
+
skip: !taskId,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const task: BackgroundTask | undefined = taskData?.task;
|
|
45
|
+
|
|
46
|
+
// Auto-start wet run when modal opens
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (!visible || !scriptName || hasStartedRef.current) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
hasStartedRef.current = true;
|
|
52
|
+
setTaskId(null);
|
|
53
|
+
setPhase("running");
|
|
54
|
+
setStartError(null);
|
|
55
|
+
|
|
56
|
+
void (async () => {
|
|
57
|
+
try {
|
|
58
|
+
const result = await runScript({name: scriptName, wetRun: true}).unwrap();
|
|
59
|
+
setTaskId(result.taskId);
|
|
60
|
+
} catch (err: unknown) {
|
|
61
|
+
const errData = (err as {data?: {title?: string; detail?: string}})?.data;
|
|
62
|
+
const message = errData?.detail ?? errData?.title ?? "Failed to start script";
|
|
63
|
+
setStartError(message);
|
|
64
|
+
setPhase("done");
|
|
65
|
+
hasStartedRef.current = false;
|
|
66
|
+
}
|
|
67
|
+
})();
|
|
68
|
+
}, [visible, scriptName, runScript]);
|
|
69
|
+
|
|
70
|
+
// Reset when modal closes
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
if (!visible) {
|
|
73
|
+
hasStartedRef.current = false;
|
|
74
|
+
}
|
|
75
|
+
}, [visible]);
|
|
76
|
+
|
|
77
|
+
// Transition to done when task reaches terminal status
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (task && isTerminalStatus(task.status)) {
|
|
80
|
+
setPhase("done");
|
|
81
|
+
}
|
|
82
|
+
}, [task?.status]);
|
|
83
|
+
|
|
84
|
+
const handleCancel = useCallback(async () => {
|
|
85
|
+
if (!taskId) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
await cancelTask(taskId).unwrap();
|
|
90
|
+
} catch {
|
|
91
|
+
// Cancellation failed, task may already be done
|
|
92
|
+
}
|
|
93
|
+
}, [taskId, cancelTask]);
|
|
94
|
+
|
|
95
|
+
const renderRunning = (): React.ReactElement => (
|
|
96
|
+
<Box alignItems="center" gap={4} paddingY={4}>
|
|
97
|
+
<Heading align="center" size="md">
|
|
98
|
+
{scriptName}
|
|
99
|
+
</Heading>
|
|
100
|
+
<Spinner size="md" />
|
|
101
|
+
{task?.progress?.message && <Text align="center">{task.progress.message}</Text>}
|
|
102
|
+
{task?.progress?.stage && (
|
|
103
|
+
<Text align="center" color="secondaryDark" size="sm">
|
|
104
|
+
{task.progress.stage}
|
|
105
|
+
</Text>
|
|
106
|
+
)}
|
|
107
|
+
{task?.progress?.percentage !== undefined && task.progress.percentage > 0 && (
|
|
108
|
+
<Text align="center" color="secondaryDark" size="sm">
|
|
109
|
+
{task.progress.percentage}%
|
|
110
|
+
</Text>
|
|
111
|
+
)}
|
|
112
|
+
<Button
|
|
113
|
+
disabled={isCancelling}
|
|
114
|
+
loading={isCancelling}
|
|
115
|
+
onClick={handleCancel}
|
|
116
|
+
testID="admin-script-cancel-button"
|
|
117
|
+
text="Cancel"
|
|
118
|
+
variant="destructive"
|
|
119
|
+
/>
|
|
120
|
+
</Box>
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const renderDone = (): React.ReactElement => {
|
|
124
|
+
const status = task?.status ?? "failed";
|
|
125
|
+
const iconName =
|
|
126
|
+
status === "completed"
|
|
127
|
+
? "circle-check"
|
|
128
|
+
: status === "cancelled"
|
|
129
|
+
? "circle-exclamation"
|
|
130
|
+
: "circle-xmark";
|
|
131
|
+
const iconColor =
|
|
132
|
+
status === "completed" ? "success" : status === "cancelled" ? "warning" : "error";
|
|
133
|
+
const statusLabel =
|
|
134
|
+
status === "completed" ? "Completed" : status === "cancelled" ? "Cancelled" : "Failed";
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<Box gap={4} paddingY={4}>
|
|
138
|
+
<Heading align="center" size="md">
|
|
139
|
+
{scriptName}
|
|
140
|
+
</Heading>
|
|
141
|
+
<Box alignItems="center" gap={2}>
|
|
142
|
+
<Icon color={iconColor} iconName={iconName} size="md" />
|
|
143
|
+
<Text align="center" bold>
|
|
144
|
+
{statusLabel}
|
|
145
|
+
</Text>
|
|
146
|
+
</Box>
|
|
147
|
+
|
|
148
|
+
{(task?.error ?? startError) && (
|
|
149
|
+
<Box color="error" padding={3} rounding="md">
|
|
150
|
+
<Text color="inverted" size="sm">
|
|
151
|
+
{task?.error ?? startError}
|
|
152
|
+
</Text>
|
|
153
|
+
</Box>
|
|
154
|
+
)}
|
|
155
|
+
|
|
156
|
+
{task?.result && task.result.length > 0 && (
|
|
157
|
+
<Box gap={1} maxHeight={300} overflow="scrollY" padding={2}>
|
|
158
|
+
<Text bold size="sm">
|
|
159
|
+
Results ({task.result.length}):
|
|
160
|
+
</Text>
|
|
161
|
+
{task.result.map((line, i) => (
|
|
162
|
+
<Text color="secondaryDark" key={`${i}-${line.slice(0, 20)}`} size="sm">
|
|
163
|
+
{line}
|
|
164
|
+
</Text>
|
|
165
|
+
))}
|
|
166
|
+
</Box>
|
|
167
|
+
)}
|
|
168
|
+
</Box>
|
|
169
|
+
);
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const isComplete = phase === "done";
|
|
173
|
+
|
|
174
|
+
return (
|
|
175
|
+
<Modal
|
|
176
|
+
onDismiss={onDismiss}
|
|
177
|
+
persistOnBackgroundClick={!isComplete}
|
|
178
|
+
primaryButtonOnClick={onDismiss}
|
|
179
|
+
primaryButtonText={isComplete ? "Close" : undefined}
|
|
180
|
+
visible={visible}
|
|
181
|
+
>
|
|
182
|
+
<Box minHeight={200} padding={2}>
|
|
183
|
+
{phase === "running" && renderRunning()}
|
|
184
|
+
{phase === "done" && renderDone()}
|
|
185
|
+
</Box>
|
|
186
|
+
</Modal>
|
|
187
|
+
);
|
|
188
|
+
};
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import type {Api} from "@reduxjs/toolkit/query/react";
|
|
2
|
+
import {
|
|
3
|
+
BooleanField,
|
|
4
|
+
Box,
|
|
5
|
+
Button,
|
|
6
|
+
Card,
|
|
7
|
+
Heading,
|
|
8
|
+
MarkdownEditorField,
|
|
9
|
+
Page,
|
|
10
|
+
Spinner,
|
|
11
|
+
Text,
|
|
12
|
+
TextField,
|
|
13
|
+
useToast,
|
|
14
|
+
} from "@terreno/ui";
|
|
15
|
+
import startCase from "lodash/startCase";
|
|
16
|
+
import React, {useCallback, useMemo, useState} from "react";
|
|
17
|
+
import {useConfigurationApi} from "./useConfigurationApi";
|
|
18
|
+
|
|
19
|
+
interface ConfigFieldMeta {
|
|
20
|
+
type: string;
|
|
21
|
+
required: boolean;
|
|
22
|
+
description?: string;
|
|
23
|
+
enum?: string[];
|
|
24
|
+
default?: any;
|
|
25
|
+
secret?: boolean;
|
|
26
|
+
widget?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface ConfigSectionMeta {
|
|
30
|
+
name: string;
|
|
31
|
+
displayName: string;
|
|
32
|
+
description?: string;
|
|
33
|
+
fields: Record<string, ConfigFieldMeta>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface ConfigurationMetaResponse {
|
|
37
|
+
sections: ConfigSectionMeta[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface ConfigurationScreenProps {
|
|
41
|
+
/** Base URL for configuration routes (e.g., "/configuration"). */
|
|
42
|
+
basePath?: string;
|
|
43
|
+
/** RTK Query API instance. */
|
|
44
|
+
api: Api<any, any, any, any>;
|
|
45
|
+
/** Optional title override. Defaults to "Configuration". */
|
|
46
|
+
title?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Renders a single configuration field based on its metadata.
|
|
51
|
+
*/
|
|
52
|
+
const ConfigField: React.FC<{
|
|
53
|
+
fieldKey: string;
|
|
54
|
+
fieldMeta: ConfigFieldMeta;
|
|
55
|
+
value: any;
|
|
56
|
+
onChange: (value: any) => void;
|
|
57
|
+
sectionName: string;
|
|
58
|
+
}> = ({fieldKey, fieldMeta, value, onChange, sectionName}) => {
|
|
59
|
+
const label = startCase(fieldKey);
|
|
60
|
+
const helperText = fieldMeta.description;
|
|
61
|
+
const testID = `config-${sectionName}-${fieldKey}`;
|
|
62
|
+
|
|
63
|
+
if (fieldMeta.type === "boolean") {
|
|
64
|
+
return (
|
|
65
|
+
<BooleanField
|
|
66
|
+
helperText={helperText}
|
|
67
|
+
onChange={onChange}
|
|
68
|
+
title={label}
|
|
69
|
+
value={value ?? fieldMeta.default ?? false}
|
|
70
|
+
/>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (fieldMeta.type === "number") {
|
|
75
|
+
return (
|
|
76
|
+
<TextField
|
|
77
|
+
helperText={helperText}
|
|
78
|
+
onChange={(text: string) => {
|
|
79
|
+
const num = Number(text);
|
|
80
|
+
onChange(Number.isNaN(num) ? text : num);
|
|
81
|
+
}}
|
|
82
|
+
testID={testID}
|
|
83
|
+
title={label}
|
|
84
|
+
value={value != null ? String(value) : ""}
|
|
85
|
+
/>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (fieldMeta.widget === "markdown") {
|
|
90
|
+
return (
|
|
91
|
+
<MarkdownEditorField
|
|
92
|
+
helperText={helperText}
|
|
93
|
+
onChange={onChange}
|
|
94
|
+
testID={testID}
|
|
95
|
+
title={label}
|
|
96
|
+
value={value ?? ""}
|
|
97
|
+
/>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (fieldMeta.secret) {
|
|
102
|
+
return (
|
|
103
|
+
<TextField
|
|
104
|
+
helperText={helperText ? `${helperText} (secret)` : "Secret value"}
|
|
105
|
+
onChange={onChange}
|
|
106
|
+
testID={testID}
|
|
107
|
+
title={`${label} (Secret)`}
|
|
108
|
+
type="password"
|
|
109
|
+
value={value ?? ""}
|
|
110
|
+
/>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return (
|
|
115
|
+
<TextField
|
|
116
|
+
helperText={helperText}
|
|
117
|
+
onChange={onChange}
|
|
118
|
+
testID={testID}
|
|
119
|
+
title={label}
|
|
120
|
+
value={value ?? ""}
|
|
121
|
+
/>
|
|
122
|
+
);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const SectionDescription: React.FC<{description: string}> = ({description}) => (
|
|
126
|
+
<Text color="secondaryDark" size="sm">
|
|
127
|
+
{description}
|
|
128
|
+
</Text>
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const SectionCard: React.FC<{
|
|
132
|
+
section: ConfigSectionMeta;
|
|
133
|
+
formState: Record<string, any>;
|
|
134
|
+
onFieldChange: (sectionName: string, fieldKey: string, value: any) => void;
|
|
135
|
+
}> = ({section, formState, onFieldChange}) => {
|
|
136
|
+
const sectionValues = section.name === "__root__" ? formState : (formState[section.name] ?? {});
|
|
137
|
+
return (
|
|
138
|
+
<Card padding={4}>
|
|
139
|
+
<Box gap={3}>
|
|
140
|
+
<Box gap={1}>
|
|
141
|
+
<Heading size="md">{section.displayName}</Heading>
|
|
142
|
+
{section.description && <SectionDescription description={section.description} />}
|
|
143
|
+
</Box>
|
|
144
|
+
{Object.entries(section.fields).map(([fieldKey, fieldMeta]) => (
|
|
145
|
+
<ConfigField
|
|
146
|
+
fieldKey={fieldKey}
|
|
147
|
+
fieldMeta={fieldMeta}
|
|
148
|
+
key={fieldKey}
|
|
149
|
+
onChange={(value: any) => onFieldChange(section.name, fieldKey, value)}
|
|
150
|
+
sectionName={section.name}
|
|
151
|
+
value={sectionValues[fieldKey]}
|
|
152
|
+
/>
|
|
153
|
+
))}
|
|
154
|
+
</Box>
|
|
155
|
+
</Card>
|
|
156
|
+
);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const EmptySections: React.FC = () => (
|
|
160
|
+
<Text color="secondaryDark">No configuration sections defined.</Text>
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Configuration management screen that auto-generates a form from the backend
|
|
165
|
+
* configuration model's schema metadata.
|
|
166
|
+
*
|
|
167
|
+
* Fetches metadata from `{basePath}/meta` and current values from `{basePath}`.
|
|
168
|
+
* Each nested subschema in the configuration model renders as a separate card/section.
|
|
169
|
+
* Supports string, number, boolean, and secret field types.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* import {ConfigurationScreen} from "@terreno/admin-frontend";
|
|
174
|
+
* import {api} from "@/store/openApiSdk";
|
|
175
|
+
*
|
|
176
|
+
* export default function ConfigScreen() {
|
|
177
|
+
* return <ConfigurationScreen api={api} />;
|
|
178
|
+
* }
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export const ConfigurationScreen: React.FC<ConfigurationScreenProps> = ({
|
|
182
|
+
basePath = "/configuration",
|
|
183
|
+
api,
|
|
184
|
+
title = "Configuration",
|
|
185
|
+
}) => {
|
|
186
|
+
const {useMetaQuery, useValuesQuery, useUpdateMutation} = useConfigurationApi({api, basePath});
|
|
187
|
+
|
|
188
|
+
const {data: meta, isLoading: isMetaLoading} = useMetaQuery();
|
|
189
|
+
const {data: valuesResponse, isLoading: isValuesLoading} = useValuesQuery();
|
|
190
|
+
const [updateConfig, {isLoading: isSaving}] = useUpdateMutation();
|
|
191
|
+
|
|
192
|
+
// Tracks only the fields the user has explicitly edited. null = no edits yet.
|
|
193
|
+
const [userEdits, setUserEdits] = useState<Record<string, any> | null>(null);
|
|
194
|
+
const [isDirty, setIsDirty] = useState(false);
|
|
195
|
+
|
|
196
|
+
const toast = useToast();
|
|
197
|
+
|
|
198
|
+
const configMeta = meta as ConfigurationMetaResponse | undefined;
|
|
199
|
+
// The baseQuery in emptyApi.ts unwraps {data: ...} responses, so valuesResponse
|
|
200
|
+
// is already the config document — not a wrapper object.
|
|
201
|
+
const configValues = valuesResponse as Record<string, any> | undefined;
|
|
202
|
+
|
|
203
|
+
// Derived synchronously so the form is never blank when data is already cached.
|
|
204
|
+
const serverValues = useMemo((): Record<string, any> => {
|
|
205
|
+
if (!configValues || !configMeta) return {};
|
|
206
|
+
const emptyDefault = (fieldMeta: ConfigFieldMeta) =>
|
|
207
|
+
fieldMeta.type === "boolean" ? false : "";
|
|
208
|
+
const initial: Record<string, any> = {};
|
|
209
|
+
for (const section of configMeta.sections) {
|
|
210
|
+
if (section.name === "__root__") {
|
|
211
|
+
for (const [key, fieldMeta] of Object.entries(section.fields)) {
|
|
212
|
+
initial[key] = configValues[key] ?? fieldMeta.default ?? emptyDefault(fieldMeta);
|
|
213
|
+
}
|
|
214
|
+
} else {
|
|
215
|
+
initial[section.name] = {};
|
|
216
|
+
const sectionValues = (configValues[section.name] as Record<string, any>) ?? {};
|
|
217
|
+
for (const [key, fieldMeta] of Object.entries(section.fields)) {
|
|
218
|
+
initial[section.name][key] =
|
|
219
|
+
sectionValues[key] ?? fieldMeta.default ?? emptyDefault(fieldMeta);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return initial;
|
|
224
|
+
}, [configValues, configMeta]);
|
|
225
|
+
|
|
226
|
+
const formState = userEdits ?? serverValues;
|
|
227
|
+
|
|
228
|
+
const handleFieldChange = useCallback(
|
|
229
|
+
(sectionName: string, fieldKey: string, value: any) => {
|
|
230
|
+
setIsDirty(true);
|
|
231
|
+
setUserEdits((prev) => {
|
|
232
|
+
const base = prev ?? serverValues;
|
|
233
|
+
if (sectionName === "__root__") {
|
|
234
|
+
return {...base, [fieldKey]: value};
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
...base,
|
|
238
|
+
[sectionName]: {
|
|
239
|
+
...(base[sectionName] as Record<string, any>),
|
|
240
|
+
[fieldKey]: value,
|
|
241
|
+
},
|
|
242
|
+
};
|
|
243
|
+
});
|
|
244
|
+
},
|
|
245
|
+
[serverValues]
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
const handleSave = useCallback(async () => {
|
|
249
|
+
try {
|
|
250
|
+
await updateConfig(formState).unwrap();
|
|
251
|
+
// Keep userEdits so the form continues to show the saved values while
|
|
252
|
+
// the cache invalidation refetch runs in the background.
|
|
253
|
+
setIsDirty(false);
|
|
254
|
+
toast.success("Configuration saved");
|
|
255
|
+
} catch (err) {
|
|
256
|
+
toast.catch(err, "Failed to save configuration");
|
|
257
|
+
}
|
|
258
|
+
}, [formState, updateConfig, toast]);
|
|
259
|
+
|
|
260
|
+
const sections = useMemo(() => configMeta?.sections ?? [], [configMeta]);
|
|
261
|
+
|
|
262
|
+
// Only block rendering on the very first load (no cached data yet).
|
|
263
|
+
// Background refetches (e.g. after a save) should not show a spinner.
|
|
264
|
+
const isInitialLoading = (isMetaLoading && !configMeta) || (isValuesLoading && !configValues);
|
|
265
|
+
if (isInitialLoading) {
|
|
266
|
+
return (
|
|
267
|
+
<Page maxWidth="100%" title={title}>
|
|
268
|
+
<Box alignItems="center" justifyContent="center" padding={6}>
|
|
269
|
+
<Spinner />
|
|
270
|
+
</Box>
|
|
271
|
+
</Page>
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (!configMeta) {
|
|
276
|
+
return (
|
|
277
|
+
<Page maxWidth="100%" title={title}>
|
|
278
|
+
<Box padding={4}>
|
|
279
|
+
<Text color="secondaryDark">No configuration metadata available.</Text>
|
|
280
|
+
</Box>
|
|
281
|
+
</Page>
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return (
|
|
286
|
+
<Page
|
|
287
|
+
footer={
|
|
288
|
+
<Box direction="row" justifyContent="end" padding={2}>
|
|
289
|
+
<Button
|
|
290
|
+
disabled={!isDirty}
|
|
291
|
+
loading={isSaving}
|
|
292
|
+
onClick={handleSave}
|
|
293
|
+
testID="config-save-button"
|
|
294
|
+
text="Save Configuration"
|
|
295
|
+
variant="primary"
|
|
296
|
+
/>
|
|
297
|
+
</Box>
|
|
298
|
+
}
|
|
299
|
+
maxWidth={800}
|
|
300
|
+
scroll
|
|
301
|
+
title={title}
|
|
302
|
+
>
|
|
303
|
+
<Box gap={4} padding={4}>
|
|
304
|
+
{sections.length === 0 ? (
|
|
305
|
+
<EmptySections />
|
|
306
|
+
) : (
|
|
307
|
+
sections.map((section) => (
|
|
308
|
+
<SectionCard
|
|
309
|
+
formState={formState}
|
|
310
|
+
key={section.name}
|
|
311
|
+
onFieldChange={handleFieldChange}
|
|
312
|
+
section={section}
|
|
313
|
+
/>
|
|
314
|
+
))
|
|
315
|
+
)}
|
|
316
|
+
</Box>
|
|
317
|
+
</Page>
|
|
318
|
+
);
|
|
319
|
+
};
|