@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
package/src/AdminModelList.tsx
CHANGED
|
@@ -8,9 +8,96 @@ import {useAdminConfig} from "./useAdminConfig";
|
|
|
8
8
|
interface AdminModelListProps {
|
|
9
9
|
baseUrl: string;
|
|
10
10
|
api: Api<any, any, any, any>;
|
|
11
|
+
/** Path to navigate to for the configuration screen. When provided, a Configuration card is shown. */
|
|
12
|
+
configurationPath?: string;
|
|
11
13
|
}
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
const ScriptsCard: React.FC<{count: number; onPress: () => void}> = ({count, onPress}) => (
|
|
16
|
+
<Card padding={4} testID="admin-scripts-card">
|
|
17
|
+
<Box
|
|
18
|
+
accessibilityHint="Navigate to admin scripts"
|
|
19
|
+
accessibilityLabel="Scripts"
|
|
20
|
+
gap={2}
|
|
21
|
+
onClick={onPress}
|
|
22
|
+
width={240}
|
|
23
|
+
>
|
|
24
|
+
<Heading size="md">Scripts</Heading>
|
|
25
|
+
<Text color="secondaryDark" size="sm">
|
|
26
|
+
{count} script{count !== 1 ? "s" : ""}
|
|
27
|
+
</Text>
|
|
28
|
+
</Box>
|
|
29
|
+
</Card>
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const ConfigurationCard: React.FC<{onPress: () => void}> = ({onPress}) => (
|
|
33
|
+
<Card padding={4} testID="admin-configuration-card">
|
|
34
|
+
<Box
|
|
35
|
+
accessibilityHint="Navigate to application configuration"
|
|
36
|
+
accessibilityLabel="Configuration"
|
|
37
|
+
gap={2}
|
|
38
|
+
onClick={onPress}
|
|
39
|
+
width={240}
|
|
40
|
+
>
|
|
41
|
+
<Heading size="md">Configuration</Heading>
|
|
42
|
+
<Text color="secondaryDark" size="sm">
|
|
43
|
+
Manage application settings
|
|
44
|
+
</Text>
|
|
45
|
+
</Box>
|
|
46
|
+
</Card>
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const ModelCard: React.FC<{model: AdminModelConfig; onPress: (name: string) => void}> = ({
|
|
50
|
+
model,
|
|
51
|
+
onPress,
|
|
52
|
+
}) => {
|
|
53
|
+
const fieldCount = Object.keys(model.fields).length;
|
|
54
|
+
return (
|
|
55
|
+
<Card key={model.name} padding={4} testID={`admin-model-card-${model.name}`}>
|
|
56
|
+
<Box
|
|
57
|
+
accessibilityHint={`Navigate to ${model.displayName} admin`}
|
|
58
|
+
accessibilityLabel={model.displayName}
|
|
59
|
+
gap={2}
|
|
60
|
+
onClick={() => onPress(model.name)}
|
|
61
|
+
width={240}
|
|
62
|
+
>
|
|
63
|
+
<Heading size="md">{model.displayName}</Heading>
|
|
64
|
+
<Text color="secondaryDark" size="sm">
|
|
65
|
+
{fieldCount} field{fieldCount !== 1 ? "s" : ""}
|
|
66
|
+
</Text>
|
|
67
|
+
</Box>
|
|
68
|
+
</Card>
|
|
69
|
+
);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Admin panel entry screen that displays all available models as clickable cards.
|
|
74
|
+
*
|
|
75
|
+
* Fetches the admin configuration from the backend and renders a grid of model cards.
|
|
76
|
+
* Each card shows the model's display name and field count. Clicking a card navigates
|
|
77
|
+
* to the model's table view.
|
|
78
|
+
*
|
|
79
|
+
* @param props - Component props
|
|
80
|
+
* @param props.baseUrl - Base URL for admin routes (e.g., "/admin")
|
|
81
|
+
* @param props.api - RTK Query API instance for making authenticated requests
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* import {AdminModelList} from "@terreno/admin-frontend";
|
|
86
|
+
* import {api} from "@/store/openApiSdk";
|
|
87
|
+
*
|
|
88
|
+
* function AdminIndexScreen() {
|
|
89
|
+
* return <AdminModelList baseUrl="/admin" api={api} />;
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @see AdminModelTable for the table view that this navigates to
|
|
94
|
+
* @see useAdminConfig for the configuration hook
|
|
95
|
+
*/
|
|
96
|
+
export const AdminModelList: React.FC<AdminModelListProps> = ({
|
|
97
|
+
baseUrl,
|
|
98
|
+
api,
|
|
99
|
+
configurationPath,
|
|
100
|
+
}) => {
|
|
14
101
|
const {config, isLoading, error} = useAdminConfig(api, baseUrl);
|
|
15
102
|
|
|
16
103
|
const handlePress = useCallback(
|
|
@@ -40,28 +127,27 @@ export const AdminModelList: React.FC<AdminModelListProps> = ({baseUrl, api}) =>
|
|
|
40
127
|
);
|
|
41
128
|
}
|
|
42
129
|
|
|
130
|
+
const scripts = config.scripts ?? [];
|
|
131
|
+
const hasToolCards = scripts.length > 0 || !!configurationPath;
|
|
132
|
+
|
|
43
133
|
return (
|
|
44
134
|
<Page maxWidth="100%" scroll title="Admin">
|
|
45
|
-
<Box
|
|
46
|
-
{
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
</Box>
|
|
62
|
-
</Card>
|
|
63
|
-
);
|
|
64
|
-
})}
|
|
135
|
+
<Box gap={4} padding={4}>
|
|
136
|
+
{hasToolCards && (
|
|
137
|
+
<Box direction="row" gap={4} wrap>
|
|
138
|
+
{scripts.length > 0 && (
|
|
139
|
+
<ScriptsCard count={scripts.length} onPress={() => handlePress("__scripts")} />
|
|
140
|
+
)}
|
|
141
|
+
{configurationPath && (
|
|
142
|
+
<ConfigurationCard onPress={() => router.push(configurationPath as any)} />
|
|
143
|
+
)}
|
|
144
|
+
</Box>
|
|
145
|
+
)}
|
|
146
|
+
<Box direction="row" gap={4} wrap>
|
|
147
|
+
{config.models.map((model: AdminModelConfig) => (
|
|
148
|
+
<ModelCard key={model.name} model={model} onPress={handlePress} />
|
|
149
|
+
))}
|
|
150
|
+
</Box>
|
|
65
151
|
</Box>
|
|
66
152
|
</Page>
|
|
67
153
|
);
|
package/src/AdminModelTable.tsx
CHANGED
|
@@ -99,6 +99,86 @@ const AdminLinkCell: React.FC<{column: DataTableColumn; cellData: DataTableCellD
|
|
|
99
99
|
return <Link onClick={() => router.push(href as any)} text={text} />;
|
|
100
100
|
};
|
|
101
101
|
|
|
102
|
+
const AdminActionsCell: React.FC<{column: DataTableColumn; cellData: DataTableCellData}> = ({
|
|
103
|
+
cellData,
|
|
104
|
+
}) => {
|
|
105
|
+
const {id, baseUrl, modelName, onDelete} = cellData.value as {
|
|
106
|
+
id: string;
|
|
107
|
+
baseUrl: string;
|
|
108
|
+
modelName: string;
|
|
109
|
+
onDelete: (id: string) => void;
|
|
110
|
+
};
|
|
111
|
+
const href = `${baseUrl}/${modelName}/${id}`;
|
|
112
|
+
return (
|
|
113
|
+
<Box alignItems="center" direction="row" gap={1} justifyContent="end">
|
|
114
|
+
<IconButton
|
|
115
|
+
accessibilityLabel="View"
|
|
116
|
+
iconName="eye"
|
|
117
|
+
onClick={() => router.push(href as any)}
|
|
118
|
+
tooltipText="View"
|
|
119
|
+
variant="muted"
|
|
120
|
+
/>
|
|
121
|
+
<IconButton
|
|
122
|
+
accessibilityLabel="Edit"
|
|
123
|
+
iconName="pen-to-square"
|
|
124
|
+
onClick={() => router.push(href as any)}
|
|
125
|
+
tooltipText="Edit"
|
|
126
|
+
variant="muted"
|
|
127
|
+
/>
|
|
128
|
+
<IconButton
|
|
129
|
+
accessibilityLabel="Delete"
|
|
130
|
+
confirmationText="Are you sure you want to delete this item?"
|
|
131
|
+
iconName="trash"
|
|
132
|
+
onClick={() => onDelete(id)}
|
|
133
|
+
tooltipText="Delete"
|
|
134
|
+
variant="destructive"
|
|
135
|
+
withConfirmation
|
|
136
|
+
/>
|
|
137
|
+
</Box>
|
|
138
|
+
);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const LoadingContent: React.FC = () => (
|
|
142
|
+
<Box alignItems="center" justifyContent="center" padding={6}>
|
|
143
|
+
<Spinner />
|
|
144
|
+
</Box>
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
const EmptyContent: React.FC = () => (
|
|
148
|
+
<Box alignItems="center" padding={6}>
|
|
149
|
+
<Text color="secondaryDark">No items found.</Text>
|
|
150
|
+
</Box>
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Table view for a specific admin model with pagination, sorting, and CRUD actions.
|
|
155
|
+
*
|
|
156
|
+
* Displays model data in a DataTable with columns from the model's `listFields` configuration.
|
|
157
|
+
* Provides actions for creating new items, editing existing items, and deleting items.
|
|
158
|
+
* Supports pagination, sorting, and reference field rendering as clickable links.
|
|
159
|
+
*
|
|
160
|
+
* @param props - Component props
|
|
161
|
+
* @param props.baseUrl - Base URL for admin routes (e.g., "/admin")
|
|
162
|
+
* @param props.api - RTK Query API instance for making authenticated requests
|
|
163
|
+
* @param props.modelName - Name of the model to display (e.g., "User")
|
|
164
|
+
* @param props.columns - Optional array of field names to display. Defaults to model's listFields.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* import {AdminModelTable} from "@terreno/admin-frontend";
|
|
169
|
+
* import {api} from "@/store/openApiSdk";
|
|
170
|
+
* import {useLocalSearchParams} from "expo-router";
|
|
171
|
+
*
|
|
172
|
+
* function AdminModelScreen() {
|
|
173
|
+
* const {modelName} = useLocalSearchParams();
|
|
174
|
+
* return <AdminModelTable baseUrl="/admin" api={api} modelName={modelName as string} />;
|
|
175
|
+
* }
|
|
176
|
+
* ```
|
|
177
|
+
*
|
|
178
|
+
* @see AdminModelForm for the create/edit form
|
|
179
|
+
* @see AdminModelList for the model list screen
|
|
180
|
+
* @see useAdminApi for the CRUD API hooks
|
|
181
|
+
*/
|
|
102
182
|
export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
103
183
|
baseUrl,
|
|
104
184
|
api,
|
|
@@ -115,13 +195,22 @@ export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
|
115
195
|
[config, modelName]
|
|
116
196
|
);
|
|
117
197
|
|
|
118
|
-
// Set the navigation header title to the model display name
|
|
119
198
|
useEffect(() => {
|
|
120
199
|
if (!modelConfig) {
|
|
121
200
|
return;
|
|
122
201
|
}
|
|
123
|
-
navigation.setOptions({
|
|
124
|
-
|
|
202
|
+
navigation.setOptions({
|
|
203
|
+
headerRight: () => (
|
|
204
|
+
<Button
|
|
205
|
+
onClick={() => router.push(`${baseUrl}/${modelName}/create` as any)}
|
|
206
|
+
testID="admin-create-button"
|
|
207
|
+
text="Create"
|
|
208
|
+
variant="primary"
|
|
209
|
+
/>
|
|
210
|
+
),
|
|
211
|
+
title: modelConfig.displayName,
|
|
212
|
+
});
|
|
213
|
+
}, [navigation, modelConfig, baseUrl, modelName]);
|
|
125
214
|
|
|
126
215
|
const displayFields = useMemo(
|
|
127
216
|
() => columnsProp ?? modelConfig?.listFields ?? [],
|
|
@@ -144,10 +233,6 @@ export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
|
144
233
|
);
|
|
145
234
|
const [deleteItem] = useDeleteMutation();
|
|
146
235
|
|
|
147
|
-
const handleCreate = useCallback(() => {
|
|
148
|
-
router.push(`${baseUrl}/${modelName}/create` as any);
|
|
149
|
-
}, [baseUrl, modelName]);
|
|
150
|
-
|
|
151
236
|
const handleDelete = useCallback(
|
|
152
237
|
async (id: string) => {
|
|
153
238
|
try {
|
|
@@ -159,57 +244,18 @@ export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
|
159
244
|
[deleteItem]
|
|
160
245
|
);
|
|
161
246
|
|
|
162
|
-
const AdminActionsCell: React.FC<{column: DataTableColumn; cellData: DataTableCellData}> =
|
|
163
|
-
useCallback(
|
|
164
|
-
({cellData}: {column: DataTableColumn; cellData: DataTableCellData}) => {
|
|
165
|
-
const {id} = cellData.value as {id: string};
|
|
166
|
-
const viewHref = `${baseUrl}/${modelName}/${id}`;
|
|
167
|
-
const editHref = `${baseUrl}/${modelName}/${id}`;
|
|
168
|
-
return (
|
|
169
|
-
<Box alignItems="center" direction="row" gap={1} justifyContent="end">
|
|
170
|
-
<IconButton
|
|
171
|
-
accessibilityLabel="View"
|
|
172
|
-
iconName="eye"
|
|
173
|
-
onClick={() => router.push(viewHref as any)}
|
|
174
|
-
tooltipText="View"
|
|
175
|
-
variant="muted"
|
|
176
|
-
/>
|
|
177
|
-
<IconButton
|
|
178
|
-
accessibilityLabel="Edit"
|
|
179
|
-
iconName="pen-to-square"
|
|
180
|
-
onClick={() => router.push(editHref as any)}
|
|
181
|
-
tooltipText="Edit"
|
|
182
|
-
variant="muted"
|
|
183
|
-
/>
|
|
184
|
-
<IconButton
|
|
185
|
-
accessibilityLabel="Delete"
|
|
186
|
-
confirmationText="Are you sure you want to delete this item?"
|
|
187
|
-
iconName="trash"
|
|
188
|
-
onClick={() => handleDelete(id)}
|
|
189
|
-
tooltipText="Delete"
|
|
190
|
-
variant="destructive"
|
|
191
|
-
withConfirmation
|
|
192
|
-
/>
|
|
193
|
-
</Box>
|
|
194
|
-
);
|
|
195
|
-
},
|
|
196
|
-
[baseUrl, modelName, handleDelete]
|
|
197
|
-
);
|
|
198
|
-
|
|
199
247
|
const customColumnComponentMap: DataTableCustomComponentMap = useMemo(
|
|
200
248
|
() => ({
|
|
201
249
|
[ACTIONS_COLUMN_TYPE]: AdminActionsCell,
|
|
202
250
|
[LINK_COLUMN_TYPE]: AdminLinkCell,
|
|
203
251
|
}),
|
|
204
|
-
[
|
|
252
|
+
[]
|
|
205
253
|
);
|
|
206
254
|
|
|
207
255
|
if (isConfigLoading || !modelConfig) {
|
|
208
256
|
return (
|
|
209
257
|
<Page maxWidth="100%">
|
|
210
|
-
<
|
|
211
|
-
<Spinner />
|
|
212
|
-
</Box>
|
|
258
|
+
<LoadingContent />
|
|
213
259
|
</Page>
|
|
214
260
|
);
|
|
215
261
|
}
|
|
@@ -263,34 +309,18 @@ export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
|
263
309
|
};
|
|
264
310
|
});
|
|
265
311
|
|
|
266
|
-
const actionsCell = {value: {id: item._id}};
|
|
312
|
+
const actionsCell = {value: {baseUrl, id: item._id, modelName, onDelete: handleDelete}};
|
|
267
313
|
return [...fieldCells, actionsCell];
|
|
268
314
|
});
|
|
269
315
|
|
|
270
316
|
const totalPages = listData ? Math.ceil(listData.total / DEFAULT_LIMIT) : 1;
|
|
271
317
|
|
|
272
318
|
return (
|
|
273
|
-
<Page
|
|
274
|
-
footer={
|
|
275
|
-
<Box direction="row" justifyContent="end" padding={2}>
|
|
276
|
-
<Button
|
|
277
|
-
onClick={handleCreate}
|
|
278
|
-
testID="admin-create-button"
|
|
279
|
-
text="Create"
|
|
280
|
-
variant="primary"
|
|
281
|
-
/>
|
|
282
|
-
</Box>
|
|
283
|
-
}
|
|
284
|
-
maxWidth="100%"
|
|
285
|
-
>
|
|
319
|
+
<Page maxWidth="100%">
|
|
286
320
|
{isListLoading ? (
|
|
287
|
-
<
|
|
288
|
-
<Spinner />
|
|
289
|
-
</Box>
|
|
321
|
+
<LoadingContent />
|
|
290
322
|
) : data.length === 0 ? (
|
|
291
|
-
<
|
|
292
|
-
<Text color="secondaryDark">No items found.</Text>
|
|
293
|
-
</Box>
|
|
323
|
+
<EmptyContent />
|
|
294
324
|
) : (
|
|
295
325
|
<DataTable
|
|
296
326
|
columns={columns}
|
package/src/AdminRefField.tsx
CHANGED
|
@@ -24,6 +24,41 @@ const getDisplayValue = (item: any): string => {
|
|
|
24
24
|
return item._id ?? String(item);
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Select field for choosing a referenced model instance by ID.
|
|
29
|
+
*
|
|
30
|
+
* Fetches all items from the referenced model and displays them in a dropdown.
|
|
31
|
+
* Automatically determines a display label by checking common fields (name, title, email, etc.).
|
|
32
|
+
* Falls back to the item's _id if no suitable display field is found.
|
|
33
|
+
*
|
|
34
|
+
* @param props - Component props
|
|
35
|
+
* @param props.api - RTK Query API instance for fetching reference data
|
|
36
|
+
* @param props.baseUrl - Base URL for admin routes (not currently used, kept for consistency)
|
|
37
|
+
* @param props.routePath - Full route path to the referenced model's list endpoint
|
|
38
|
+
* @param props.refModelName - Name of the referenced model (e.g., "User")
|
|
39
|
+
* @param props.title - Label displayed above the select field
|
|
40
|
+
* @param props.value - Currently selected ID
|
|
41
|
+
* @param props.onChange - Callback when selection changes
|
|
42
|
+
* @param props.errorText - Optional validation error message
|
|
43
|
+
* @param props.helperText - Optional helper text shown below the field
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* <AdminRefField
|
|
48
|
+
* api={api}
|
|
49
|
+
* baseUrl="/admin"
|
|
50
|
+
* routePath="/admin/users"
|
|
51
|
+
* refModelName="User"
|
|
52
|
+
* title="Owner"
|
|
53
|
+
* value={ownerId}
|
|
54
|
+
* onChange={(id) => setOwnerId(id)}
|
|
55
|
+
* helperText="Select the user who owns this item"
|
|
56
|
+
* />
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @see AdminFieldRenderer for field type selection
|
|
60
|
+
* @see useAdminApi for the API hook pattern
|
|
61
|
+
*/
|
|
27
62
|
export const AdminRefField: React.FC<AdminRefFieldProps> = ({
|
|
28
63
|
api,
|
|
29
64
|
routePath,
|
|
@@ -47,7 +82,7 @@ export const AdminRefField: React.FC<AdminRefFieldProps> = ({
|
|
|
47
82
|
}),
|
|
48
83
|
}),
|
|
49
84
|
}),
|
|
50
|
-
overrideExisting:
|
|
85
|
+
overrideExisting: true,
|
|
51
86
|
});
|
|
52
87
|
}, [api, routePath, endpointKey]);
|
|
53
88
|
|
|
@@ -0,0 +1,124 @@
|
|
|
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 {AdminScriptList} from "./AdminScriptList";
|
|
5
|
+
|
|
6
|
+
// Mock useAdminConfig to control returned data
|
|
7
|
+
const mockUseAdminConfig = mock(() => ({
|
|
8
|
+
config: null as any,
|
|
9
|
+
error: null as any,
|
|
10
|
+
isLoading: false,
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
mock.module("./useAdminConfig", () => ({
|
|
14
|
+
useAdminConfig: (...args: any[]) => mockUseAdminConfig(...args),
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
// Mock AdminScriptRunModal since it depends on useAdminScripts
|
|
18
|
+
mock.module("./AdminScriptRunModal", () => ({
|
|
19
|
+
AdminScriptRunModal: () => null,
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
const mockApi = {} as any;
|
|
23
|
+
|
|
24
|
+
describe("AdminScriptList", () => {
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
mockUseAdminConfig.mockClear();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("renders loading state", () => {
|
|
30
|
+
mockUseAdminConfig.mockReturnValue({
|
|
31
|
+
config: null,
|
|
32
|
+
error: null,
|
|
33
|
+
isLoading: true,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const {getByTestId} = renderWithTheme(<AdminScriptList api={mockApi} baseUrl="/admin" />);
|
|
37
|
+
|
|
38
|
+
// Spinner should be rendered during loading
|
|
39
|
+
expect(getByTestId).toBeDefined();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("renders error state when config fails to load", () => {
|
|
43
|
+
mockUseAdminConfig.mockReturnValue({
|
|
44
|
+
config: null,
|
|
45
|
+
error: new Error("Network error"),
|
|
46
|
+
isLoading: false,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const {getByText} = renderWithTheme(<AdminScriptList api={mockApi} baseUrl="/admin" />);
|
|
50
|
+
|
|
51
|
+
expect(getByText("Failed to load admin configuration.")).toBeTruthy();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("renders empty state when no scripts are configured", () => {
|
|
55
|
+
mockUseAdminConfig.mockReturnValue({
|
|
56
|
+
config: {models: [], scripts: []},
|
|
57
|
+
error: null,
|
|
58
|
+
isLoading: false,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const {getByText} = renderWithTheme(<AdminScriptList api={mockApi} baseUrl="/admin" />);
|
|
62
|
+
|
|
63
|
+
expect(getByText("No scripts registered.")).toBeTruthy();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("renders script cards with name and description", () => {
|
|
67
|
+
mockUseAdminConfig.mockReturnValue({
|
|
68
|
+
config: {
|
|
69
|
+
models: [],
|
|
70
|
+
scripts: [
|
|
71
|
+
{description: "Migrate old records", name: "migrate-data"},
|
|
72
|
+
{description: "Remove orphaned documents", name: "cleanup"},
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
error: null,
|
|
76
|
+
isLoading: false,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const {getByText, getByTestId} = renderWithTheme(
|
|
80
|
+
<AdminScriptList api={mockApi} baseUrl="/admin" />
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
expect(getByText("migrate-data")).toBeTruthy();
|
|
84
|
+
expect(getByText("Migrate old records")).toBeTruthy();
|
|
85
|
+
expect(getByText("cleanup")).toBeTruthy();
|
|
86
|
+
expect(getByText("Remove orphaned documents")).toBeTruthy();
|
|
87
|
+
expect(getByTestId("admin-script-card-migrate-data")).toBeTruthy();
|
|
88
|
+
expect(getByTestId("admin-script-card-cleanup")).toBeTruthy();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("renders Run buttons for each script", () => {
|
|
92
|
+
mockUseAdminConfig.mockReturnValue({
|
|
93
|
+
config: {
|
|
94
|
+
models: [],
|
|
95
|
+
scripts: [{description: "Test script", name: "test-script"}],
|
|
96
|
+
},
|
|
97
|
+
error: null,
|
|
98
|
+
isLoading: false,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const {getByTestId} = renderWithTheme(<AdminScriptList api={mockApi} baseUrl="/admin" />);
|
|
102
|
+
|
|
103
|
+
expect(getByTestId("admin-script-run-test-script")).toBeTruthy();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("disables Run button when isAdmin is false", () => {
|
|
107
|
+
mockUseAdminConfig.mockReturnValue({
|
|
108
|
+
config: {
|
|
109
|
+
models: [],
|
|
110
|
+
scripts: [{description: "Test script", name: "test-script"}],
|
|
111
|
+
},
|
|
112
|
+
error: null,
|
|
113
|
+
isLoading: false,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const {getByTestId} = renderWithTheme(
|
|
117
|
+
<AdminScriptList api={mockApi} baseUrl="/admin" isAdmin={false} />
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
const button = getByTestId("admin-script-run-test-script");
|
|
121
|
+
// The button should have aria-disabled or similar when disabled
|
|
122
|
+
expect(button).toBeTruthy();
|
|
123
|
+
});
|
|
124
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type {Api} from "@reduxjs/toolkit/query/react";
|
|
2
|
+
import {Box, Button, Card, Heading, Page, Spinner, Text} from "@terreno/ui";
|
|
3
|
+
import React, {useCallback, useState} from "react";
|
|
4
|
+
import {AdminScriptRunModal} from "./AdminScriptRunModal";
|
|
5
|
+
import type {AdminScriptConfig} from "./types";
|
|
6
|
+
import {useAdminConfig} from "./useAdminConfig";
|
|
7
|
+
|
|
8
|
+
interface AdminScriptListProps {
|
|
9
|
+
baseUrl: string;
|
|
10
|
+
api: Api<any, any, any, any>;
|
|
11
|
+
/** When false, the Run button is disabled. Defaults to true. */
|
|
12
|
+
isAdmin?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const AdminScriptList: React.FC<AdminScriptListProps> = ({baseUrl, api, isAdmin = true}) => {
|
|
16
|
+
const {config, isLoading, error} = useAdminConfig(api, baseUrl);
|
|
17
|
+
const [selectedScript, setSelectedScript] = useState<AdminScriptConfig | null>(null);
|
|
18
|
+
const [modalVisible, setModalVisible] = useState(false);
|
|
19
|
+
|
|
20
|
+
const handleRunScript = useCallback((script: AdminScriptConfig) => {
|
|
21
|
+
setSelectedScript(script);
|
|
22
|
+
setModalVisible(true);
|
|
23
|
+
}, []);
|
|
24
|
+
|
|
25
|
+
const handleDismiss = useCallback(() => {
|
|
26
|
+
setModalVisible(false);
|
|
27
|
+
setSelectedScript(null);
|
|
28
|
+
}, []);
|
|
29
|
+
|
|
30
|
+
if (isLoading) {
|
|
31
|
+
return (
|
|
32
|
+
<Page maxWidth="100%" title="Scripts">
|
|
33
|
+
<Box alignItems="center" justifyContent="center" padding={6}>
|
|
34
|
+
<Spinner />
|
|
35
|
+
</Box>
|
|
36
|
+
</Page>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (error || !config) {
|
|
41
|
+
return (
|
|
42
|
+
<Page maxWidth="100%" title="Scripts">
|
|
43
|
+
<Box padding={4}>
|
|
44
|
+
<Text color="error">Failed to load admin configuration.</Text>
|
|
45
|
+
</Box>
|
|
46
|
+
</Page>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const scripts = config.scripts ?? [];
|
|
51
|
+
|
|
52
|
+
if (scripts.length === 0) {
|
|
53
|
+
return (
|
|
54
|
+
<Page maxWidth="100%" title="Scripts">
|
|
55
|
+
<Box alignItems="center" padding={6}>
|
|
56
|
+
<Text color="secondaryDark">No scripts registered.</Text>
|
|
57
|
+
</Box>
|
|
58
|
+
</Page>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
<Page maxWidth="100%" scroll title="Scripts">
|
|
64
|
+
<Box gap={3} padding={4}>
|
|
65
|
+
{scripts.map((script: AdminScriptConfig) => (
|
|
66
|
+
<Card key={script.name} padding={4} testID={`admin-script-card-${script.name}`}>
|
|
67
|
+
<Box alignItems="center" direction="row" gap={4} justifyContent="between">
|
|
68
|
+
<Box flex="grow" gap={1}>
|
|
69
|
+
<Heading size="sm">{script.name}</Heading>
|
|
70
|
+
<Text color="secondaryDark" size="sm">
|
|
71
|
+
{script.description}
|
|
72
|
+
</Text>
|
|
73
|
+
</Box>
|
|
74
|
+
<Button
|
|
75
|
+
disabled={!isAdmin}
|
|
76
|
+
onClick={() => handleRunScript(script)}
|
|
77
|
+
testID={`admin-script-run-${script.name}`}
|
|
78
|
+
text="Run"
|
|
79
|
+
tooltipText={!isAdmin ? "Only admins can run scripts" : undefined}
|
|
80
|
+
variant="primary"
|
|
81
|
+
/>
|
|
82
|
+
</Box>
|
|
83
|
+
</Card>
|
|
84
|
+
))}
|
|
85
|
+
</Box>
|
|
86
|
+
|
|
87
|
+
<AdminScriptRunModal
|
|
88
|
+
api={api}
|
|
89
|
+
baseUrl={baseUrl}
|
|
90
|
+
onDismiss={handleDismiss}
|
|
91
|
+
scriptName={selectedScript?.name ?? null}
|
|
92
|
+
visible={modalVisible}
|
|
93
|
+
/>
|
|
94
|
+
</Page>
|
|
95
|
+
);
|
|
96
|
+
};
|