sanity-plugin-internationalized-array 2.0.0 → 2.0.1-canary.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/lib/index.d.mts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.esm.js +108 -89
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +108 -90
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +108 -89
- package/lib/index.mjs.map +1 -1
- package/package.json +20 -26
- package/src/components/DocumentAddButtons.tsx +53 -52
- package/src/components/InternationalizedArray.tsx +3 -1
- package/src/components/InternationalizedArrayContext.tsx +4 -5
- package/src/fieldActions/index.ts +8 -2
- package/src/schema/array.ts +7 -1
- package/src/schema/object.ts +1 -1
- package/src/types.ts +1 -1
- package/src/utils/createAddLanguagePatches.ts +18 -10
- package/src/utils/getDocumentsToTranslate.ts +66 -0
package/lib/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { isSanityDocument, setIfMissing, insert, PatchEvent, useClient, useWorks
|
|
|
5
5
|
import { useLanguageFilterStudioContext } from "@sanity/language-filter";
|
|
6
6
|
import { Grid, Button, useToast, Stack, Box, Text, Card, Code, Label, MenuButton, Menu, MenuItem, Flex, Spinner } from "@sanity/ui";
|
|
7
7
|
import equal from "fast-deep-equal";
|
|
8
|
-
import {
|
|
8
|
+
import { useCallback, createContext, useContext, useDeferredValue, useMemo, memo, useRef, useEffect, createElement } from "react";
|
|
9
9
|
import { useDocumentPane } from "sanity/structure";
|
|
10
10
|
import { AddIcon, TranslateIcon, RemoveCircleIcon } from "@sanity/icons";
|
|
11
11
|
import get from "lodash/get.js";
|
|
@@ -17,10 +17,35 @@ const namespace = "sanity-plugin-internationalized-array", version = "v0", prelo
|
|
|
17
17
|
apiVersion: "2022-11-27",
|
|
18
18
|
buttonLocations: ["field"],
|
|
19
19
|
buttonAddAll: !0
|
|
20
|
+
}, getDocumentsToTranslate = (value, rootPath = []) => {
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
const arrayRootPath = [...rootPath], internationalizedValues = value.filter((item) => {
|
|
23
|
+
if (Array.isArray(item)) return !1;
|
|
24
|
+
if (typeof item == "object") {
|
|
25
|
+
const type = item?._type;
|
|
26
|
+
return type?.startsWith("internationalizedArray") && type?.endsWith("Value");
|
|
27
|
+
}
|
|
28
|
+
return !1;
|
|
29
|
+
});
|
|
30
|
+
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => ({
|
|
31
|
+
...internationalizedValue,
|
|
32
|
+
path: arrayRootPath,
|
|
33
|
+
pathString: arrayRootPath.join(".")
|
|
34
|
+
})) : value.length > 0 ? value.map(
|
|
35
|
+
(item, index) => getDocumentsToTranslate(item, [...arrayRootPath, index])
|
|
36
|
+
).flat() : [];
|
|
37
|
+
}
|
|
38
|
+
if (typeof value == "object" && value) {
|
|
39
|
+
const startsWithUnderscoreRegex = /^_/;
|
|
40
|
+
return Object.keys(value).filter(
|
|
41
|
+
(key) => !key.match(startsWithUnderscoreRegex)
|
|
42
|
+
).map((item) => {
|
|
43
|
+
const selectedValue = value[item], path = [...rootPath, item];
|
|
44
|
+
return getDocumentsToTranslate(selectedValue, path);
|
|
45
|
+
}).flat();
|
|
46
|
+
}
|
|
47
|
+
return [];
|
|
20
48
|
};
|
|
21
|
-
function createValueSchemaTypeName(schemaType) {
|
|
22
|
-
return `${schemaType.name}Value`;
|
|
23
|
-
}
|
|
24
49
|
function AddButtons(props) {
|
|
25
50
|
const { languages, readOnly, value, onClick } = props;
|
|
26
51
|
return languages.length > 0 ? /* @__PURE__ */ jsx(Grid, { columns: Math.min(languages.length, MAX_COLUMNS), gap: 2, children: languages.map((language) => /* @__PURE__ */ jsx(
|
|
@@ -29,7 +54,7 @@ function AddButtons(props) {
|
|
|
29
54
|
tone: "primary",
|
|
30
55
|
mode: "ghost",
|
|
31
56
|
fontSize: 1,
|
|
32
|
-
disabled: readOnly || !!
|
|
57
|
+
disabled: readOnly || !!value?.find((item) => item._key === language.id),
|
|
33
58
|
text: language.id.toUpperCase(),
|
|
34
59
|
icon: languages.length > MAX_COLUMNS ? void 0 : AddIcon,
|
|
35
60
|
value: language.id,
|
|
@@ -39,13 +64,8 @@ function AddButtons(props) {
|
|
|
39
64
|
)) }) : null;
|
|
40
65
|
}
|
|
41
66
|
function DocumentAddButtons(props) {
|
|
42
|
-
const { filteredLanguages } = useInternationalizedArrayContext(),
|
|
43
|
-
() =>
|
|
44
|
-
(field) => field.type.name.startsWith("internationalizedArray")
|
|
45
|
-
),
|
|
46
|
-
[fields]
|
|
47
|
-
), handleDocumentButtonClick = useCallback(
|
|
48
|
-
(event) => {
|
|
67
|
+
const { filteredLanguages } = useInternationalizedArrayContext(), value = isSanityDocument(props.value) ? props.value : void 0, toast = useToast(), { onChange } = useDocumentPane(), documentsToTranslation = getDocumentsToTranslate(value, []), handleDocumentButtonClick = useCallback(
|
|
68
|
+
async (event) => {
|
|
49
69
|
const languageId = event.currentTarget.value;
|
|
50
70
|
if (!languageId) {
|
|
51
71
|
toast.push({
|
|
@@ -54,37 +74,38 @@ function DocumentAddButtons(props) {
|
|
|
54
74
|
});
|
|
55
75
|
return;
|
|
56
76
|
}
|
|
57
|
-
|
|
77
|
+
const alreadyTranslated = documentsToTranslation.filter(
|
|
78
|
+
(translation) => translation?._key === languageId
|
|
79
|
+
), removeDuplicates = documentsToTranslation.reduce((filteredTranslations, translation) => alreadyTranslated.filter(
|
|
80
|
+
(alreadyTranslation) => alreadyTranslation.pathString === translation.pathString
|
|
81
|
+
).length > 0 || filteredTranslations.filter(
|
|
82
|
+
(filteredTranslation) => filteredTranslation.path === translation.path
|
|
83
|
+
).length > 0 ? filteredTranslations : [...filteredTranslations, translation], []);
|
|
84
|
+
if (removeDuplicates.length === 0) {
|
|
58
85
|
toast.push({
|
|
59
86
|
status: "error",
|
|
60
87
|
title: "No internationalizedArray fields found in document root"
|
|
61
88
|
});
|
|
62
89
|
return;
|
|
63
90
|
}
|
|
64
|
-
const patches =
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
"after",
|
|
81
|
-
[fieldKey, -1]
|
|
82
|
-
)
|
|
83
|
-
];
|
|
84
|
-
}).flat();
|
|
85
|
-
onChange(PatchEvent.from(patches));
|
|
91
|
+
const patches = [];
|
|
92
|
+
for (const toTranslate of removeDuplicates) {
|
|
93
|
+
const path = toTranslate.path, ifMissing = setIfMissing([], path), insertValue = insert(
|
|
94
|
+
[
|
|
95
|
+
{
|
|
96
|
+
_key: languageId,
|
|
97
|
+
_type: toTranslate._type,
|
|
98
|
+
value: void 0
|
|
99
|
+
}
|
|
100
|
+
],
|
|
101
|
+
"after",
|
|
102
|
+
[...path, -1]
|
|
103
|
+
);
|
|
104
|
+
patches.push(ifMissing), patches.push(insertValue);
|
|
105
|
+
}
|
|
106
|
+
onChange(PatchEvent.from(patches.flat()));
|
|
86
107
|
},
|
|
87
|
-
[
|
|
108
|
+
[documentsToTranslation, onChange, toast]
|
|
88
109
|
);
|
|
89
110
|
return /* @__PURE__ */ jsxs(Stack, { space: 3, children: [
|
|
90
111
|
/* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsx(Text, { size: 1, weight: "semibold", children: "Add translation to internationalized fields" }) }),
|
|
@@ -106,7 +127,7 @@ const getSelectedValue = (select, document) => {
|
|
|
106
127
|
for (const [key, path] of Object.entries(selection)) {
|
|
107
128
|
let value = get(document, path);
|
|
108
129
|
Array.isArray(value) && (value = value.filter(
|
|
109
|
-
(item) => typeof item == "object" ?
|
|
130
|
+
(item) => typeof item == "object" ? item?._type === "reference" && "_ref" in item : !0
|
|
110
131
|
)), selectedValue[key] = value;
|
|
111
132
|
}
|
|
112
133
|
return selectedValue;
|
|
@@ -119,7 +140,9 @@ function useInternationalizedArrayContext() {
|
|
|
119
140
|
return useContext(InternationalizedArrayContext);
|
|
120
141
|
}
|
|
121
142
|
function InternationalizedArrayProvider(props) {
|
|
122
|
-
const { internationalizedArray: internationalizedArray2 } = props, client = useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = useWorkspace(), { value: document } = useFormBuilder()
|
|
143
|
+
const { internationalizedArray: internationalizedArray2 } = props, client = useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = useWorkspace(), { value: document } = useFormBuilder();
|
|
144
|
+
console.log({ document }, useFormBuilder());
|
|
145
|
+
const deferredDocument = useDeferredValue(document), selectedValue = useMemo(
|
|
123
146
|
() => getSelectedValue(internationalizedArray2.select, deferredDocument),
|
|
124
147
|
[internationalizedArray2.select, deferredDocument]
|
|
125
148
|
), languages = Array.isArray(internationalizedArray2.languages) ? internationalizedArray2.languages : suspend$1(
|
|
@@ -133,7 +156,7 @@ function InternationalizedArrayProvider(props) {
|
|
|
133
156
|
(language) => selectedLanguageIds.includes(language.id)
|
|
134
157
|
) : languages;
|
|
135
158
|
}, [deferredDocument, languageFilterOptions, languages, selectedLanguageIds]), showDocumentButtons = internationalizedArray2.buttonLocations.includes("document");
|
|
136
|
-
return /* @__PURE__ */ jsx(
|
|
159
|
+
return console.log({ showDocumentButtons }), /* @__PURE__ */ jsx(
|
|
137
160
|
InternationalizedArrayContext.Provider,
|
|
138
161
|
{
|
|
139
162
|
value: {
|
|
@@ -142,13 +165,7 @@ function InternationalizedArrayProvider(props) {
|
|
|
142
165
|
filteredLanguages
|
|
143
166
|
},
|
|
144
167
|
children: showDocumentButtons ? /* @__PURE__ */ jsxs(Stack, { space: 5, children: [
|
|
145
|
-
/* @__PURE__ */ jsx(
|
|
146
|
-
DocumentAddButtons,
|
|
147
|
-
{
|
|
148
|
-
schemaType: props.schemaType,
|
|
149
|
-
value: props.value
|
|
150
|
-
}
|
|
151
|
-
),
|
|
168
|
+
/* @__PURE__ */ jsx(DocumentAddButtons, { value: props.value }),
|
|
152
169
|
props.renderDefault(props)
|
|
153
170
|
] }) : props.renderDefault(props)
|
|
154
171
|
}
|
|
@@ -165,7 +182,10 @@ function checkAllLanguagesArePresent(languages, value) {
|
|
|
165
182
|
return languagesInUseIds.length === filteredLanguageIds.length && languagesInUseIds.every((l) => filteredLanguageIds.includes(l));
|
|
166
183
|
}
|
|
167
184
|
function createAddAllTitle(value, languages) {
|
|
168
|
-
return value
|
|
185
|
+
return value?.length ? `Add missing ${languages.length - value.length === 1 ? "language" : "languages"}` : languages.length === 1 ? `Add ${languages[0].title} Field` : "Add all languages";
|
|
186
|
+
}
|
|
187
|
+
function createValueSchemaTypeName(schemaType) {
|
|
188
|
+
return `${schemaType.name}Value`;
|
|
169
189
|
}
|
|
170
190
|
function createAddLanguagePatches(config) {
|
|
171
191
|
const {
|
|
@@ -175,15 +195,15 @@ function createAddLanguagePatches(config) {
|
|
|
175
195
|
filteredLanguages,
|
|
176
196
|
value,
|
|
177
197
|
path = []
|
|
178
|
-
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? (
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
) : (
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
), languagesInUse = value
|
|
198
|
+
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => ({
|
|
199
|
+
...itemBase,
|
|
200
|
+
_key: id
|
|
201
|
+
})) : filteredLanguages.filter(
|
|
202
|
+
(language) => value?.length ? !value.find((v) => v._key === language.id) : !0
|
|
203
|
+
).map((language) => ({
|
|
204
|
+
...itemBase,
|
|
205
|
+
_key: language.id
|
|
206
|
+
})), languagesInUse = value?.length ? value.map((v) => v) : [];
|
|
187
207
|
return newItems.map((item) => {
|
|
188
208
|
const languageIndex = languages.findIndex((l) => item._key === l.id), remainingLanguages = languages.slice(languageIndex + 1), nextLanguageIndex = languagesInUse.findIndex(
|
|
189
209
|
(l) => (
|
|
@@ -201,7 +221,7 @@ function createAddLanguagePatches(config) {
|
|
|
201
221
|
});
|
|
202
222
|
}
|
|
203
223
|
const createTranslateFieldActions = (fieldActionProps, { languages, filteredLanguages }) => languages.map((language) => {
|
|
204
|
-
const value = useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!
|
|
224
|
+
const value = useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!value?.find((item) => item._key === language.id) : !1, hidden = !filteredLanguages.some((f) => f.id === language.id), { onChange } = useDocumentPane(), onAction = useCallback(() => {
|
|
205
225
|
const { schemaType, path } = fieldActionProps, addLanguageKeys = [language.id], patches = createAddLanguagePatches({
|
|
206
226
|
addLanguageKeys,
|
|
207
227
|
schemaType,
|
|
@@ -243,8 +263,7 @@ const createTranslateFieldActions = (fieldActionProps, { languages, filteredLang
|
|
|
243
263
|
}, internationalizedArrayFieldAction = defineDocumentFieldAction({
|
|
244
264
|
name: "internationalizedArray",
|
|
245
265
|
useAction(fieldActionProps) {
|
|
246
|
-
|
|
247
|
-
const isInternationalizedArrayField = (_b = (_a = fieldActionProps == null ? void 0 : fieldActionProps.schemaType) == null ? void 0 : _a.type) == null ? void 0 : _b.name.startsWith(
|
|
266
|
+
const isInternationalizedArrayField = fieldActionProps?.schemaType?.type?.name.startsWith(
|
|
248
267
|
"internationalizedArray"
|
|
249
268
|
), { languages, filteredLanguages } = useInternationalizedArrayContext(), translateFieldActions = createTranslateFieldActions(
|
|
250
269
|
fieldActionProps,
|
|
@@ -320,11 +339,10 @@ function InternationalizedArray(props) {
|
|
|
320
339
|
}) : members,
|
|
321
340
|
[languageFilterEnabled, members, languageFilterOptions, selectedLanguageIds]
|
|
322
341
|
), handleAddLanguage = useCallback(
|
|
323
|
-
(param) => {
|
|
324
|
-
|
|
325
|
-
if (!(filteredLanguages != null && filteredLanguages.length))
|
|
342
|
+
async (param) => {
|
|
343
|
+
if (!filteredLanguages?.length)
|
|
326
344
|
return;
|
|
327
|
-
const addLanguageKeys = Array.isArray(param) ? param : [
|
|
345
|
+
const addLanguageKeys = Array.isArray(param) ? param : [param?.currentTarget?.value].filter(Boolean), patches = createAddLanguagePatches({
|
|
328
346
|
addLanguageKeys,
|
|
329
347
|
schemaType,
|
|
330
348
|
languages,
|
|
@@ -342,23 +360,23 @@ function InternationalizedArray(props) {
|
|
|
342
360
|
};
|
|
343
361
|
}, [defaultLanguages, documentCreatedAt, handleAddLanguage, value]);
|
|
344
362
|
const handleRestoreOrder = useCallback(() => {
|
|
345
|
-
if (!
|
|
363
|
+
if (!value?.length || !languages?.length)
|
|
346
364
|
return;
|
|
347
365
|
const updatedValue = value.reduce((acc, v) => {
|
|
348
|
-
const newIndex = languages.findIndex((l) => l.id ===
|
|
366
|
+
const newIndex = languages.findIndex((l) => l.id === v?._key);
|
|
349
367
|
return newIndex > -1 && (acc[newIndex] = v), acc;
|
|
350
368
|
}, []).filter(Boolean);
|
|
351
|
-
|
|
369
|
+
value?.length !== updatedValue.length && toast.push({
|
|
352
370
|
title: "There was an error reordering languages",
|
|
353
371
|
status: "warning"
|
|
354
372
|
}), onChange(set(updatedValue));
|
|
355
|
-
}, [toast, languages, onChange, value]), allKeysAreLanguages = useMemo(() => !
|
|
356
|
-
() => languages && languages.length > 1 ? languages.filter((l) => value
|
|
373
|
+
}, [toast, languages, onChange, value]), allKeysAreLanguages = useMemo(() => !value?.length || !languages?.length ? !0 : value?.every((v) => languages.find((l) => l?.id === v?._key)), [value, languages]), languagesInUse = useMemo(
|
|
374
|
+
() => languages && languages.length > 1 ? languages.filter((l) => value?.find((v) => v._key === l.id)) : [],
|
|
357
375
|
[languages, value]
|
|
358
|
-
), languagesOutOfOrder = useMemo(() => !
|
|
376
|
+
), languagesOutOfOrder = useMemo(() => !value?.length || !languagesInUse.length ? [] : value.map(
|
|
359
377
|
(v, vIndex) => vIndex === languagesInUse.findIndex((l) => l.id === v._key) ? null : v
|
|
360
378
|
).filter(Boolean), [value, languagesInUse]), languagesAreValid = useMemo(
|
|
361
|
-
() => !
|
|
379
|
+
() => !languages?.length || languages?.length && languages.every((item) => item.id && item.title),
|
|
362
380
|
[languages]
|
|
363
381
|
);
|
|
364
382
|
useEffect(() => {
|
|
@@ -373,9 +391,9 @@ function InternationalizedArray(props) {
|
|
|
373
391
|
const addButtonsAreVisible = (
|
|
374
392
|
// Plugin was configured to display buttons here (default!)
|
|
375
393
|
buttonLocations.includes("field") && // There's at least one language visible
|
|
376
|
-
|
|
394
|
+
filteredLanguages?.length > 0 && // Not every language has a value yet
|
|
377
395
|
!allLanguagesArePresent
|
|
378
|
-
), fieldHasMembers =
|
|
396
|
+
), fieldHasMembers = members?.length > 0;
|
|
379
397
|
return /* @__PURE__ */ jsxs(Stack, { space: 2, children: [
|
|
380
398
|
fieldHasMembers ? /* @__PURE__ */ jsx(Fragment, { children: filteredMembers.map((member) => member.kind === "item" ? /* @__PURE__ */ createElement(
|
|
381
399
|
ArrayOfObjectsItem,
|
|
@@ -411,8 +429,7 @@ function InternationalizedArray(props) {
|
|
|
411
429
|
] });
|
|
412
430
|
}
|
|
413
431
|
function getLanguagesFieldOption(schemaType) {
|
|
414
|
-
|
|
415
|
-
return schemaType ? ((_a = schemaType.options) == null ? void 0 : _a.languages) || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
432
|
+
return schemaType ? schemaType.options?.languages || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
416
433
|
}
|
|
417
434
|
var array = (config) => {
|
|
418
435
|
const { apiVersion, select, languages, type } = config, typeName = typeof type == "string" ? type : type.name, arrayName = createFieldName(typeName), objectName = createFieldName(typeName, !0);
|
|
@@ -424,7 +441,12 @@ var array = (config) => {
|
|
|
424
441
|
input: InternationalizedArray
|
|
425
442
|
},
|
|
426
443
|
// These options are required for validation rules – not the custom input component
|
|
427
|
-
options: {
|
|
444
|
+
options: {
|
|
445
|
+
// @ts-expect-error - find out why it fails
|
|
446
|
+
apiVersion,
|
|
447
|
+
select,
|
|
448
|
+
languages
|
|
449
|
+
},
|
|
428
450
|
of: [
|
|
429
451
|
defineField({
|
|
430
452
|
...typeof type == "string" ? {} : type,
|
|
@@ -432,15 +454,16 @@ var array = (config) => {
|
|
|
432
454
|
type: objectName
|
|
433
455
|
})
|
|
434
456
|
],
|
|
457
|
+
// @ts-expect-error - find out why it fails
|
|
435
458
|
validation: (rule) => rule.custom(async (value, context) => {
|
|
436
459
|
if (!value)
|
|
437
460
|
return !0;
|
|
438
461
|
const selectedValue = getSelectedValue(select, context.document), client = context.getClient({ apiVersion });
|
|
439
462
|
let contextLanguages = [];
|
|
440
|
-
const languagesFieldOption = getLanguagesFieldOption(context
|
|
463
|
+
const languagesFieldOption = getLanguagesFieldOption(context?.type);
|
|
441
464
|
if (Array.isArray(languagesFieldOption) ? contextLanguages = languagesFieldOption : Array.isArray(peek(selectedValue)) ? contextLanguages = peek(selectedValue) || [] : typeof languagesFieldOption == "function" && (contextLanguages = await languagesFieldOption(client, selectedValue)), value && value.length > contextLanguages.length)
|
|
442
465
|
return `Cannot be more than ${contextLanguages.length === 1 ? "1 item" : `${contextLanguages.length} items`}`;
|
|
443
|
-
const nonLanguageKeys = value
|
|
466
|
+
const nonLanguageKeys = value?.length ? value.filter(
|
|
444
467
|
(item) => !contextLanguages.find((language) => item._key === language.id)
|
|
445
468
|
) : [];
|
|
446
469
|
if (nonLanguageKeys.length)
|
|
@@ -448,10 +471,10 @@ var array = (config) => {
|
|
|
448
471
|
message: "Array item keys must be valid languages registered to the field type",
|
|
449
472
|
paths: nonLanguageKeys.map((item) => [{ _key: item._key }])
|
|
450
473
|
};
|
|
451
|
-
const valuesByLanguage = value
|
|
474
|
+
const valuesByLanguage = value?.length ? value.filter((item) => !!item?._key).reduce((acc, cur) => acc[cur._key] ? { ...acc, [cur._key]: [...acc[cur._key], cur] } : {
|
|
452
475
|
...acc,
|
|
453
476
|
[cur._key]: [cur]
|
|
454
|
-
}, {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) =>
|
|
477
|
+
}, {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) => item?.length > 1).flat();
|
|
455
478
|
return duplicateValues.length ? {
|
|
456
479
|
message: "There can only be one field per language",
|
|
457
480
|
paths: duplicateValues.map((item) => [{ _key: item._key }])
|
|
@@ -467,7 +490,7 @@ function InternationalizedField(props) {
|
|
|
467
490
|
}) : props.children;
|
|
468
491
|
}
|
|
469
492
|
function getToneFromValidation(validations) {
|
|
470
|
-
if (!
|
|
493
|
+
if (!validations?.length)
|
|
471
494
|
return;
|
|
472
495
|
const validationLevels = validations.map((v) => v.level);
|
|
473
496
|
if (validationLevels.includes("error"))
|
|
@@ -488,16 +511,12 @@ function InternationalizedInput(props) {
|
|
|
488
511
|
// TODO: Remove this as it shouldn't be necessary?
|
|
489
512
|
value: props.value
|
|
490
513
|
}, { validation, value, onChange, readOnly } = inlineProps, { languages } = useInternationalizedArrayContext(), languageKeysInUse = useMemo(
|
|
491
|
-
() =>
|
|
492
|
-
var _a;
|
|
493
|
-
return (_a = parentValue == null ? void 0 : parentValue.map((v) => v._key)) != null ? _a : [];
|
|
494
|
-
},
|
|
514
|
+
() => parentValue?.map((v) => v._key) ?? [],
|
|
495
515
|
[parentValue]
|
|
496
|
-
), keyIsValid = languages
|
|
516
|
+
), keyIsValid = languages?.length ? languages.find((l) => l.id === value._key) : !1, handleKeyChange = useCallback(
|
|
497
517
|
(event) => {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
!value || !(languages != null && languages.length) || !languages.find((l) => l.id === languageId) || onChange([set(languageId, ["_key"])]);
|
|
518
|
+
const languageId = event?.currentTarget?.value;
|
|
519
|
+
!value || !languages?.length || !languages.find((l) => l.id === languageId) || onChange([set(languageId, ["_key"])]);
|
|
501
520
|
},
|
|
502
521
|
[onChange, value, languages]
|
|
503
522
|
), handleUnset = useCallback(() => {
|
|
@@ -545,9 +564,9 @@ var object = (config) => {
|
|
|
545
564
|
title: `Internationalized array ${type}`,
|
|
546
565
|
type: "object",
|
|
547
566
|
components: {
|
|
567
|
+
// @ts-expect-error - find out why it fails
|
|
548
568
|
item: InternationalizedInput
|
|
549
569
|
},
|
|
550
|
-
// @ts-expect-error - Address this typing issue with the inner object
|
|
551
570
|
fields: [
|
|
552
571
|
typeof type == "string" ? (
|
|
553
572
|
// Define a simple field if all we have is the name as a string
|