@terreno/admin-frontend 0.7.1 → 0.8.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.
- package/dist/AdminFieldRenderer.d.ts +2 -0
- package/dist/AdminFieldRenderer.js +96 -3
- package/dist/AdminFieldRenderer.js.map +1 -1
- package/dist/AdminModelForm.d.ts +11 -0
- package/dist/AdminModelForm.js +95 -24
- package/dist/AdminModelForm.js.map +1 -1
- package/dist/AdminModelList.d.ts +4 -0
- package/dist/AdminModelList.js +10 -4
- package/dist/AdminModelList.js.map +1 -1
- package/dist/AdminModelTable.js +33 -33
- package/dist/AdminModelTable.js.map +1 -1
- package/dist/AdminNestedArrayField.d.ts +26 -0
- package/dist/AdminNestedArrayField.js +118 -0
- package/dist/AdminNestedArrayField.js.map +1 -0
- package/dist/CheckboxListEditor.d.ts +15 -0
- package/dist/CheckboxListEditor.js +21 -0
- package/dist/CheckboxListEditor.js.map +1 -0
- package/dist/ConsentFormEditor.d.ts +12 -0
- package/dist/ConsentFormEditor.js +299 -0
- package/dist/ConsentFormEditor.js.map +1 -0
- package/dist/ConsentFormList.d.ts +9 -0
- package/dist/ConsentFormList.js +70 -0
- package/dist/ConsentFormList.js.map +1 -0
- package/dist/ConsentResponseViewer.d.ts +8 -0
- package/dist/ConsentResponseViewer.js +61 -0
- package/dist/ConsentResponseViewer.js.map +1 -0
- package/dist/LocaleContentEditor.d.ts +10 -0
- package/dist/LocaleContentEditor.js +48 -0
- package/dist/LocaleContentEditor.js.map +1 -0
- package/dist/generateConsentPdf.d.ts +26 -0
- package/dist/generateConsentPdf.js +210 -0
- package/dist/generateConsentPdf.js.map +1 -0
- package/dist/index.d.ts +7 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +4 -0
- package/dist/types.js.map +1 -1
- package/dist/useAdminApi.js +2 -1
- package/dist/useAdminApi.js.map +1 -1
- package/package.json +3 -2
- package/src/AdminFieldRenderer.tsx +190 -2
- package/src/AdminModelForm.tsx +134 -40
- package/src/AdminModelList.tsx +35 -19
- package/src/AdminModelTable.tsx +40 -36
- package/src/AdminNestedArrayField.tsx +228 -0
- package/src/CheckboxListEditor.tsx +107 -0
- package/src/ConsentFormEditor.tsx +677 -0
- package/src/ConsentFormList.tsx +164 -0
- package/src/ConsentResponseViewer.tsx +228 -0
- package/src/LocaleContentEditor.tsx +151 -0
- package/src/generateConsentPdf.ts +264 -0
- package/src/index.tsx +7 -0
- package/src/types.ts +4 -0
- package/src/useAdminApi.ts +2 -1
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Box,
|
|
3
|
+
Button,
|
|
4
|
+
type ColumnSortInterface,
|
|
5
|
+
DataTable,
|
|
6
|
+
type DataTableCellData,
|
|
7
|
+
type DataTableColumn,
|
|
8
|
+
type DataTableCustomComponentMap,
|
|
9
|
+
IconButton,
|
|
10
|
+
Page,
|
|
11
|
+
Spinner,
|
|
12
|
+
Text,
|
|
13
|
+
} from "@terreno/ui";
|
|
14
|
+
import React, {useCallback, useMemo, useState} from "react";
|
|
15
|
+
import {useAdminApi} from "./useAdminApi";
|
|
16
|
+
|
|
17
|
+
interface ConsentFormListProps {
|
|
18
|
+
baseUrl: string;
|
|
19
|
+
api: any;
|
|
20
|
+
onCreateNew?: () => void;
|
|
21
|
+
onRowClick?: (id: string) => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const DEFAULT_LIMIT = 20;
|
|
25
|
+
|
|
26
|
+
const ACTIONS_COLUMN_TYPE = "consentFormActions";
|
|
27
|
+
|
|
28
|
+
const DATA_COLUMNS: DataTableColumn[] = [
|
|
29
|
+
{columnType: "text", sortable: true, title: "Title", width: 250},
|
|
30
|
+
{columnType: "text", sortable: true, title: "Type", width: 120},
|
|
31
|
+
{columnType: "number", sortable: true, title: "Version", width: 100},
|
|
32
|
+
{columnType: "boolean", sortable: true, title: "Active", width: 100},
|
|
33
|
+
{columnType: "number", sortable: true, title: "Order", width: 100},
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const DATA_COLUMN_KEYS = ["title", "type", "version", "active", "order"];
|
|
37
|
+
|
|
38
|
+
const buildSortString = (sort: ColumnSortInterface | undefined): string | undefined => {
|
|
39
|
+
if (!sort) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
const fieldKey = DATA_COLUMN_KEYS[sort.column];
|
|
43
|
+
if (!fieldKey) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
return sort.direction === "desc" ? `-${fieldKey}` : fieldKey;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const ConsentFormList: React.FC<ConsentFormListProps> = ({
|
|
50
|
+
baseUrl,
|
|
51
|
+
api,
|
|
52
|
+
onCreateNew,
|
|
53
|
+
onRowClick,
|
|
54
|
+
}) => {
|
|
55
|
+
const [page, setPage] = useState(1);
|
|
56
|
+
const [sortColumn, setSortColumn] = useState<ColumnSortInterface | undefined>();
|
|
57
|
+
|
|
58
|
+
const routePath = `${baseUrl}/consent-forms`;
|
|
59
|
+
const {useListQuery} = useAdminApi(api, routePath, "ConsentForm");
|
|
60
|
+
|
|
61
|
+
const sortString = buildSortString(sortColumn) ?? "-created";
|
|
62
|
+
|
|
63
|
+
const {
|
|
64
|
+
data: listData,
|
|
65
|
+
isLoading,
|
|
66
|
+
error,
|
|
67
|
+
} = useListQuery({
|
|
68
|
+
limit: DEFAULT_LIMIT,
|
|
69
|
+
page,
|
|
70
|
+
sort: sortString,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const ActionsCell: React.FC<{column: DataTableColumn; cellData: DataTableCellData}> = useCallback(
|
|
74
|
+
({cellData}: {column: DataTableColumn; cellData: DataTableCellData}) => {
|
|
75
|
+
const {id} = cellData.value as {id: string};
|
|
76
|
+
if (!onRowClick) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
return (
|
|
80
|
+
<Box alignItems="center" direction="row" gap={1} justifyContent="end">
|
|
81
|
+
<IconButton
|
|
82
|
+
accessibilityLabel="Edit"
|
|
83
|
+
iconName="pen-to-square"
|
|
84
|
+
onClick={() => onRowClick(id)}
|
|
85
|
+
tooltipText="Edit"
|
|
86
|
+
variant="muted"
|
|
87
|
+
/>
|
|
88
|
+
</Box>
|
|
89
|
+
);
|
|
90
|
+
},
|
|
91
|
+
[onRowClick]
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const customColumnComponentMap: DataTableCustomComponentMap = useMemo(
|
|
95
|
+
() => ({[ACTIONS_COLUMN_TYPE]: ActionsCell}),
|
|
96
|
+
[ActionsCell]
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const columns: DataTableColumn[] = [
|
|
100
|
+
...DATA_COLUMNS,
|
|
101
|
+
...(onRowClick
|
|
102
|
+
? [{columnType: ACTIONS_COLUMN_TYPE, sortable: false, title: "", width: 60}]
|
|
103
|
+
: []),
|
|
104
|
+
];
|
|
105
|
+
|
|
106
|
+
const rows = (listData?.data ?? []).map((item: any) => {
|
|
107
|
+
const dataCells = DATA_COLUMN_KEYS.map((key) => {
|
|
108
|
+
const value = item[key];
|
|
109
|
+
if (key === "active") {
|
|
110
|
+
return {value: Boolean(value)};
|
|
111
|
+
}
|
|
112
|
+
if (key === "version" || key === "order") {
|
|
113
|
+
return {value: typeof value === "number" ? String(value) : (value ?? "0")};
|
|
114
|
+
}
|
|
115
|
+
return {value: value ?? ""};
|
|
116
|
+
});
|
|
117
|
+
if (onRowClick) {
|
|
118
|
+
dataCells.push({value: {id: item._id}});
|
|
119
|
+
}
|
|
120
|
+
return dataCells;
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const totalPages = listData ? Math.ceil(listData.total / DEFAULT_LIMIT) : 1;
|
|
124
|
+
|
|
125
|
+
return (
|
|
126
|
+
<Page maxWidth="100%">
|
|
127
|
+
<Box alignItems="center" direction="row" justifyContent="between" padding={3}>
|
|
128
|
+
<Text size="lg">Consent Forms</Text>
|
|
129
|
+
{onCreateNew && (
|
|
130
|
+
<Button
|
|
131
|
+
onClick={onCreateNew}
|
|
132
|
+
testID="consent-form-list-create-button"
|
|
133
|
+
text="Create New"
|
|
134
|
+
variant="primary"
|
|
135
|
+
/>
|
|
136
|
+
)}
|
|
137
|
+
</Box>
|
|
138
|
+
{isLoading ? (
|
|
139
|
+
<Box alignItems="center" justifyContent="center" padding={6}>
|
|
140
|
+
<Spinner />
|
|
141
|
+
</Box>
|
|
142
|
+
) : error ? (
|
|
143
|
+
<Box alignItems="center" padding={6}>
|
|
144
|
+
<Text color="error">Failed to load consent forms.</Text>
|
|
145
|
+
</Box>
|
|
146
|
+
) : rows.length === 0 ? (
|
|
147
|
+
<Box alignItems="center" padding={6}>
|
|
148
|
+
<Text color="secondaryDark">No consent forms found.</Text>
|
|
149
|
+
</Box>
|
|
150
|
+
) : (
|
|
151
|
+
<DataTable
|
|
152
|
+
columns={columns}
|
|
153
|
+
customColumnComponentMap={customColumnComponentMap}
|
|
154
|
+
data={rows}
|
|
155
|
+
page={page}
|
|
156
|
+
setPage={setPage}
|
|
157
|
+
setSortColumn={setSortColumn}
|
|
158
|
+
sortColumn={sortColumn}
|
|
159
|
+
totalPages={totalPages}
|
|
160
|
+
/>
|
|
161
|
+
)}
|
|
162
|
+
</Page>
|
|
163
|
+
);
|
|
164
|
+
};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import {Badge, Box, Button, Card, Heading, MarkdownView, Page, Spinner, Text} from "@terreno/ui";
|
|
2
|
+
import {DateTime} from "luxon";
|
|
3
|
+
import React, {useCallback, useState} from "react";
|
|
4
|
+
import {Image} from "react-native";
|
|
5
|
+
import {generateConsentPdf} from "./generateConsentPdf";
|
|
6
|
+
import {useAdminApi} from "./useAdminApi";
|
|
7
|
+
|
|
8
|
+
interface ConsentResponseViewerProps {
|
|
9
|
+
baseUrl: string;
|
|
10
|
+
api: any;
|
|
11
|
+
id: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const formatDate = (value: any): string => {
|
|
15
|
+
if (!value) {
|
|
16
|
+
return "";
|
|
17
|
+
}
|
|
18
|
+
const dt = DateTime.fromISO(String(value));
|
|
19
|
+
if (!dt.isValid) {
|
|
20
|
+
return String(value);
|
|
21
|
+
}
|
|
22
|
+
return dt.toLocaleString(DateTime.DATETIME_FULL);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const ConsentResponseViewer: React.FC<ConsentResponseViewerProps> = ({baseUrl, api, id}) => {
|
|
26
|
+
const routePath = `${baseUrl}/consent-responses`;
|
|
27
|
+
const {useReadQuery} = useAdminApi(api, routePath, "ConsentResponse");
|
|
28
|
+
|
|
29
|
+
const {data: response, isLoading} = useReadQuery(id, {skip: !id});
|
|
30
|
+
|
|
31
|
+
if (isLoading) {
|
|
32
|
+
return (
|
|
33
|
+
<Page maxWidth="100%">
|
|
34
|
+
<Box alignItems="center" justifyContent="center" padding={6}>
|
|
35
|
+
<Spinner />
|
|
36
|
+
</Box>
|
|
37
|
+
</Page>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!response) {
|
|
42
|
+
return (
|
|
43
|
+
<Page maxWidth="100%">
|
|
44
|
+
<Box alignItems="center" padding={6}>
|
|
45
|
+
<Text color="secondaryDark">Response not found.</Text>
|
|
46
|
+
</Box>
|
|
47
|
+
</Page>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const formTitle =
|
|
52
|
+
typeof response.consentFormId === "object"
|
|
53
|
+
? (response.consentFormId?.title ?? "Unknown Form")
|
|
54
|
+
: "Unknown Form";
|
|
55
|
+
|
|
56
|
+
const userId =
|
|
57
|
+
typeof response.userId === "object"
|
|
58
|
+
? (response.userId?._id ?? String(response.userId))
|
|
59
|
+
: String(response.userId ?? "");
|
|
60
|
+
|
|
61
|
+
// checkboxValues is stored as a Map serialized to {index: boolean} by the backend
|
|
62
|
+
const checkboxEntries: Array<{index: string; checked: boolean}> =
|
|
63
|
+
response.checkboxValues && typeof response.checkboxValues === "object"
|
|
64
|
+
? Object.entries(response.checkboxValues as Record<string, boolean>).map(
|
|
65
|
+
([index, checked]) => ({
|
|
66
|
+
checked,
|
|
67
|
+
index,
|
|
68
|
+
})
|
|
69
|
+
)
|
|
70
|
+
: [];
|
|
71
|
+
|
|
72
|
+
const hasAuditTrail =
|
|
73
|
+
response.ipAddress ||
|
|
74
|
+
response.userAgent ||
|
|
75
|
+
response.contentSnapshot ||
|
|
76
|
+
response.formVersionSnapshot;
|
|
77
|
+
|
|
78
|
+
const [isDownloading, setIsDownloading] = useState(false);
|
|
79
|
+
|
|
80
|
+
const handleDownloadPdf = useCallback(async () => {
|
|
81
|
+
setIsDownloading(true);
|
|
82
|
+
try {
|
|
83
|
+
await generateConsentPdf(response);
|
|
84
|
+
} catch (err) {
|
|
85
|
+
console.error("Failed to generate PDF", err);
|
|
86
|
+
} finally {
|
|
87
|
+
setIsDownloading(false);
|
|
88
|
+
}
|
|
89
|
+
}, [response]);
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<Page maxWidth="100%" scroll>
|
|
93
|
+
<Box gap={4} padding={4}>
|
|
94
|
+
<Box alignItems="center" direction="row" justifyContent="between">
|
|
95
|
+
<Heading size="lg">Consent Response</Heading>
|
|
96
|
+
<Button
|
|
97
|
+
iconName="download"
|
|
98
|
+
loading={isDownloading}
|
|
99
|
+
onClick={handleDownloadPdf}
|
|
100
|
+
text="Download PDF"
|
|
101
|
+
variant="outline"
|
|
102
|
+
/>
|
|
103
|
+
</Box>
|
|
104
|
+
|
|
105
|
+
{/* Core Fields */}
|
|
106
|
+
<Card padding={4}>
|
|
107
|
+
<Box gap={3}>
|
|
108
|
+
<Heading size="sm">Response Details</Heading>
|
|
109
|
+
|
|
110
|
+
<Box alignItems="center" direction="row" gap={2}>
|
|
111
|
+
<Text color="secondaryDark">Form:</Text>
|
|
112
|
+
<Text>{formTitle}</Text>
|
|
113
|
+
</Box>
|
|
114
|
+
|
|
115
|
+
<Box alignItems="center" direction="row" gap={2}>
|
|
116
|
+
<Text color="secondaryDark">User ID:</Text>
|
|
117
|
+
<Text>{userId}</Text>
|
|
118
|
+
</Box>
|
|
119
|
+
|
|
120
|
+
<Box alignItems="center" direction="row" gap={2}>
|
|
121
|
+
<Text color="secondaryDark">Decision:</Text>
|
|
122
|
+
<Badge
|
|
123
|
+
status={response.agreed ? "success" : "error"}
|
|
124
|
+
value={response.agreed ? "Agreed" : "Declined"}
|
|
125
|
+
/>
|
|
126
|
+
</Box>
|
|
127
|
+
|
|
128
|
+
{response.agreedAt && (
|
|
129
|
+
<Box alignItems="center" direction="row" gap={2}>
|
|
130
|
+
<Text color="secondaryDark">Agreed At:</Text>
|
|
131
|
+
<Text>{formatDate(response.agreedAt)}</Text>
|
|
132
|
+
</Box>
|
|
133
|
+
)}
|
|
134
|
+
|
|
135
|
+
{response.locale && (
|
|
136
|
+
<Box alignItems="center" direction="row" gap={2}>
|
|
137
|
+
<Text color="secondaryDark">Locale:</Text>
|
|
138
|
+
<Text>{response.locale}</Text>
|
|
139
|
+
</Box>
|
|
140
|
+
)}
|
|
141
|
+
</Box>
|
|
142
|
+
</Card>
|
|
143
|
+
|
|
144
|
+
{/* Checkbox Values */}
|
|
145
|
+
{checkboxEntries.length > 0 && (
|
|
146
|
+
<Card padding={4}>
|
|
147
|
+
<Box gap={3}>
|
|
148
|
+
<Heading size="sm">Checkbox Responses</Heading>
|
|
149
|
+
{checkboxEntries.map((cb) => (
|
|
150
|
+
<Box
|
|
151
|
+
alignItems="center"
|
|
152
|
+
direction="row"
|
|
153
|
+
gap={2}
|
|
154
|
+
key={cb.index}
|
|
155
|
+
testID={`consent-response-checkbox-${cb.index}`}
|
|
156
|
+
>
|
|
157
|
+
<Badge
|
|
158
|
+
status={cb.checked ? "success" : "neutral"}
|
|
159
|
+
value={cb.checked ? "Checked" : "Unchecked"}
|
|
160
|
+
/>
|
|
161
|
+
<Text>Checkbox {cb.index}</Text>
|
|
162
|
+
</Box>
|
|
163
|
+
))}
|
|
164
|
+
</Box>
|
|
165
|
+
</Card>
|
|
166
|
+
)}
|
|
167
|
+
|
|
168
|
+
{/* Signature */}
|
|
169
|
+
{response.signature && (
|
|
170
|
+
<Card padding={4}>
|
|
171
|
+
<Box gap={3}>
|
|
172
|
+
<Heading size="sm">Signature</Heading>
|
|
173
|
+
<Image
|
|
174
|
+
accessibilityLabel="User signature"
|
|
175
|
+
accessible
|
|
176
|
+
resizeMode="contain"
|
|
177
|
+
source={{uri: response.signature}}
|
|
178
|
+
style={{height: 120, width: "100%"}}
|
|
179
|
+
testID="consent-response-signature"
|
|
180
|
+
/>
|
|
181
|
+
</Box>
|
|
182
|
+
</Card>
|
|
183
|
+
)}
|
|
184
|
+
|
|
185
|
+
{/* Audit Trail */}
|
|
186
|
+
{hasAuditTrail && (
|
|
187
|
+
<Card padding={4}>
|
|
188
|
+
<Box gap={3}>
|
|
189
|
+
<Heading size="sm">Audit Trail</Heading>
|
|
190
|
+
|
|
191
|
+
{response.ipAddress && (
|
|
192
|
+
<Box alignItems="center" direction="row" gap={2}>
|
|
193
|
+
<Text color="secondaryDark">IP Address:</Text>
|
|
194
|
+
<Text>{response.ipAddress}</Text>
|
|
195
|
+
</Box>
|
|
196
|
+
)}
|
|
197
|
+
|
|
198
|
+
{response.userAgent && (
|
|
199
|
+
<Box alignItems="center" direction="row" gap={2}>
|
|
200
|
+
<Text color="secondaryDark">User Agent:</Text>
|
|
201
|
+
<Text>{response.userAgent}</Text>
|
|
202
|
+
</Box>
|
|
203
|
+
)}
|
|
204
|
+
|
|
205
|
+
{response.contentSnapshot && (
|
|
206
|
+
<Box gap={2}>
|
|
207
|
+
<Text color="secondaryDark">Content Snapshot:</Text>
|
|
208
|
+
<Box border="default" padding={3} rounding="sm">
|
|
209
|
+
<MarkdownView>{response.contentSnapshot}</MarkdownView>
|
|
210
|
+
</Box>
|
|
211
|
+
</Box>
|
|
212
|
+
)}
|
|
213
|
+
|
|
214
|
+
{response.formVersionSnapshot && (
|
|
215
|
+
<Box gap={2}>
|
|
216
|
+
<Text color="secondaryDark">Form Version Snapshot:</Text>
|
|
217
|
+
<Box border="default" padding={3} rounding="sm">
|
|
218
|
+
<Text>{JSON.stringify(response.formVersionSnapshot, null, 2)}</Text>
|
|
219
|
+
</Box>
|
|
220
|
+
</Box>
|
|
221
|
+
)}
|
|
222
|
+
</Box>
|
|
223
|
+
</Card>
|
|
224
|
+
)}
|
|
225
|
+
</Box>
|
|
226
|
+
</Page>
|
|
227
|
+
);
|
|
228
|
+
};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import {Box, Button, MarkdownEditorField, SelectField, Text} from "@terreno/ui";
|
|
2
|
+
import React, {useCallback, useMemo, useState} from "react";
|
|
3
|
+
|
|
4
|
+
interface LocaleContentEditorProps {
|
|
5
|
+
value: Record<string, string>;
|
|
6
|
+
onChange: (value: Record<string, string>) => void;
|
|
7
|
+
title?: string;
|
|
8
|
+
helperText?: string;
|
|
9
|
+
errorText?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const COMMON_LOCALES = [
|
|
13
|
+
{label: "English", value: "en"},
|
|
14
|
+
{label: "Spanish", value: "es"},
|
|
15
|
+
{label: "French", value: "fr"},
|
|
16
|
+
{label: "German", value: "de"},
|
|
17
|
+
{label: "Portuguese", value: "pt"},
|
|
18
|
+
{label: "Chinese", value: "zh"},
|
|
19
|
+
{label: "Japanese", value: "ja"},
|
|
20
|
+
{label: "Korean", value: "ko"},
|
|
21
|
+
{label: "Arabic", value: "ar"},
|
|
22
|
+
{label: "Hindi", value: "hi"},
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
export const LocaleContentEditor: React.FC<LocaleContentEditorProps> = ({
|
|
26
|
+
value = {},
|
|
27
|
+
onChange,
|
|
28
|
+
title,
|
|
29
|
+
helperText,
|
|
30
|
+
errorText,
|
|
31
|
+
}) => {
|
|
32
|
+
const entries = useMemo(
|
|
33
|
+
() => (typeof value === "object" && value !== null ? value : {}),
|
|
34
|
+
[value]
|
|
35
|
+
);
|
|
36
|
+
const locales = useMemo(() => Object.keys(entries), [entries]);
|
|
37
|
+
const [activeLocale, setActiveLocale] = useState<string>(locales[0] ?? "en");
|
|
38
|
+
const [newLocale, setNewLocale] = useState("");
|
|
39
|
+
|
|
40
|
+
const availableLocales = useMemo(
|
|
41
|
+
() => COMMON_LOCALES.filter((l) => !locales.includes(l.value)),
|
|
42
|
+
[locales]
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const handleContentChange = useCallback(
|
|
46
|
+
(content: string) => {
|
|
47
|
+
onChange({...entries, [activeLocale]: content});
|
|
48
|
+
},
|
|
49
|
+
[entries, activeLocale, onChange]
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const handleAddLocale = useCallback(() => {
|
|
53
|
+
if (!newLocale || locales.includes(newLocale)) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
onChange({...entries, [newLocale]: ""});
|
|
57
|
+
setActiveLocale(newLocale);
|
|
58
|
+
setNewLocale("");
|
|
59
|
+
}, [newLocale, locales, entries, onChange]);
|
|
60
|
+
|
|
61
|
+
const handleRemoveLocale = useCallback(
|
|
62
|
+
(locale: string) => {
|
|
63
|
+
const updated = {...entries};
|
|
64
|
+
delete updated[locale];
|
|
65
|
+
onChange(updated);
|
|
66
|
+
if (activeLocale === locale) {
|
|
67
|
+
setActiveLocale(Object.keys(updated)[0] ?? "en");
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
[entries, activeLocale, onChange]
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<Box gap={2}>
|
|
75
|
+
{title && (
|
|
76
|
+
<Text bold size="md">
|
|
77
|
+
{title}
|
|
78
|
+
</Text>
|
|
79
|
+
)}
|
|
80
|
+
{helperText && (
|
|
81
|
+
<Text color="secondaryDark" size="sm">
|
|
82
|
+
{helperText}
|
|
83
|
+
</Text>
|
|
84
|
+
)}
|
|
85
|
+
|
|
86
|
+
{locales.length > 0 && (
|
|
87
|
+
<Box direction="row" gap={1} wrap>
|
|
88
|
+
{locales.map((locale) => (
|
|
89
|
+
<Button
|
|
90
|
+
key={locale}
|
|
91
|
+
onClick={() => setActiveLocale(locale)}
|
|
92
|
+
text={COMMON_LOCALES.find((l) => l.value === locale)?.label ?? locale.toUpperCase()}
|
|
93
|
+
variant={locale === activeLocale ? "primary" : "outline"}
|
|
94
|
+
/>
|
|
95
|
+
))}
|
|
96
|
+
</Box>
|
|
97
|
+
)}
|
|
98
|
+
|
|
99
|
+
{locales.length > 0 && entries[activeLocale] !== undefined && (
|
|
100
|
+
<Box gap={1}>
|
|
101
|
+
<Box alignItems="center" direction="row" justifyContent="between">
|
|
102
|
+
<Text color="secondaryDark" size="sm">
|
|
103
|
+
Editing: {activeLocale}
|
|
104
|
+
</Text>
|
|
105
|
+
{locales.length > 1 && (
|
|
106
|
+
<Button
|
|
107
|
+
onClick={() => handleRemoveLocale(activeLocale)}
|
|
108
|
+
text={`Remove ${activeLocale}`}
|
|
109
|
+
variant="destructive"
|
|
110
|
+
/>
|
|
111
|
+
)}
|
|
112
|
+
</Box>
|
|
113
|
+
<MarkdownEditorField
|
|
114
|
+
onChange={handleContentChange}
|
|
115
|
+
testID={`locale-content-${activeLocale}`}
|
|
116
|
+
value={entries[activeLocale] ?? ""}
|
|
117
|
+
/>
|
|
118
|
+
</Box>
|
|
119
|
+
)}
|
|
120
|
+
|
|
121
|
+
{locales.length === 0 && (
|
|
122
|
+
<Text color="secondaryDark" size="sm">
|
|
123
|
+
No content yet. Add a locale to get started.
|
|
124
|
+
</Text>
|
|
125
|
+
)}
|
|
126
|
+
|
|
127
|
+
<Box alignItems="end" direction="row" gap={2}>
|
|
128
|
+
<Box flex="grow">
|
|
129
|
+
<SelectField
|
|
130
|
+
onChange={setNewLocale}
|
|
131
|
+
options={availableLocales}
|
|
132
|
+
placeholder="Select locale..."
|
|
133
|
+
value={newLocale}
|
|
134
|
+
/>
|
|
135
|
+
</Box>
|
|
136
|
+
<Button
|
|
137
|
+
disabled={!newLocale}
|
|
138
|
+
onClick={handleAddLocale}
|
|
139
|
+
text="Add Locale"
|
|
140
|
+
variant="outline"
|
|
141
|
+
/>
|
|
142
|
+
</Box>
|
|
143
|
+
|
|
144
|
+
{errorText && (
|
|
145
|
+
<Text color="error" size="sm">
|
|
146
|
+
{errorText}
|
|
147
|
+
</Text>
|
|
148
|
+
)}
|
|
149
|
+
</Box>
|
|
150
|
+
);
|
|
151
|
+
};
|