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.js
CHANGED
|
@@ -5,8 +5,7 @@ function _interopDefaultCompat(e) {
|
|
|
5
5
|
return e && typeof e == "object" && "default" in e ? e : { default: e };
|
|
6
6
|
}
|
|
7
7
|
function _interopNamespaceCompat(e) {
|
|
8
|
-
if (e && typeof e == "object" && "default" in e)
|
|
9
|
-
return e;
|
|
8
|
+
if (e && typeof e == "object" && "default" in e) return e;
|
|
10
9
|
var n = /* @__PURE__ */ Object.create(null);
|
|
11
10
|
return e && Object.keys(e).forEach(function(k) {
|
|
12
11
|
if (k !== "default") {
|
|
@@ -29,10 +28,35 @@ const namespace = "sanity-plugin-internationalized-array", version = "v0", prelo
|
|
|
29
28
|
apiVersion: "2022-11-27",
|
|
30
29
|
buttonLocations: ["field"],
|
|
31
30
|
buttonAddAll: !0
|
|
31
|
+
}, getDocumentsToTranslate = (value, rootPath = []) => {
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
const arrayRootPath = [...rootPath], internationalizedValues = value.filter((item) => {
|
|
34
|
+
if (Array.isArray(item)) return !1;
|
|
35
|
+
if (typeof item == "object") {
|
|
36
|
+
const type = item?._type;
|
|
37
|
+
return type?.startsWith("internationalizedArray") && type?.endsWith("Value");
|
|
38
|
+
}
|
|
39
|
+
return !1;
|
|
40
|
+
});
|
|
41
|
+
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => ({
|
|
42
|
+
...internationalizedValue,
|
|
43
|
+
path: arrayRootPath,
|
|
44
|
+
pathString: arrayRootPath.join(".")
|
|
45
|
+
})) : value.length > 0 ? value.map(
|
|
46
|
+
(item, index) => getDocumentsToTranslate(item, [...arrayRootPath, index])
|
|
47
|
+
).flat() : [];
|
|
48
|
+
}
|
|
49
|
+
if (typeof value == "object" && value) {
|
|
50
|
+
const startsWithUnderscoreRegex = /^_/;
|
|
51
|
+
return Object.keys(value).filter(
|
|
52
|
+
(key) => !key.match(startsWithUnderscoreRegex)
|
|
53
|
+
).map((item) => {
|
|
54
|
+
const selectedValue = value[item], path = [...rootPath, item];
|
|
55
|
+
return getDocumentsToTranslate(selectedValue, path);
|
|
56
|
+
}).flat();
|
|
57
|
+
}
|
|
58
|
+
return [];
|
|
32
59
|
};
|
|
33
|
-
function createValueSchemaTypeName(schemaType) {
|
|
34
|
-
return `${schemaType.name}Value`;
|
|
35
|
-
}
|
|
36
60
|
function AddButtons(props) {
|
|
37
61
|
const { languages, readOnly, value, onClick } = props;
|
|
38
62
|
return languages.length > 0 ? /* @__PURE__ */ jsxRuntime.jsx(ui.Grid, { columns: Math.min(languages.length, MAX_COLUMNS), gap: 2, children: languages.map((language) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
@@ -41,7 +65,7 @@ function AddButtons(props) {
|
|
|
41
65
|
tone: "primary",
|
|
42
66
|
mode: "ghost",
|
|
43
67
|
fontSize: 1,
|
|
44
|
-
disabled: readOnly || !!
|
|
68
|
+
disabled: readOnly || !!value?.find((item) => item._key === language.id),
|
|
45
69
|
text: language.id.toUpperCase(),
|
|
46
70
|
icon: languages.length > MAX_COLUMNS ? void 0 : icons.AddIcon,
|
|
47
71
|
value: language.id,
|
|
@@ -51,13 +75,8 @@ function AddButtons(props) {
|
|
|
51
75
|
)) }) : null;
|
|
52
76
|
}
|
|
53
77
|
function DocumentAddButtons(props) {
|
|
54
|
-
const { filteredLanguages } = useInternationalizedArrayContext(),
|
|
55
|
-
() =>
|
|
56
|
-
(field) => field.type.name.startsWith("internationalizedArray")
|
|
57
|
-
),
|
|
58
|
-
[fields]
|
|
59
|
-
), handleDocumentButtonClick = react.useCallback(
|
|
60
|
-
(event) => {
|
|
78
|
+
const { filteredLanguages } = useInternationalizedArrayContext(), value = sanity.isSanityDocument(props.value) ? props.value : void 0, toast = ui.useToast(), { onChange } = structure.useDocumentPane(), documentsToTranslation = getDocumentsToTranslate(value, []), handleDocumentButtonClick = react.useCallback(
|
|
79
|
+
async (event) => {
|
|
61
80
|
const languageId = event.currentTarget.value;
|
|
62
81
|
if (!languageId) {
|
|
63
82
|
toast.push({
|
|
@@ -66,37 +85,38 @@ function DocumentAddButtons(props) {
|
|
|
66
85
|
});
|
|
67
86
|
return;
|
|
68
87
|
}
|
|
69
|
-
|
|
88
|
+
const alreadyTranslated = documentsToTranslation.filter(
|
|
89
|
+
(translation) => translation?._key === languageId
|
|
90
|
+
), removeDuplicates = documentsToTranslation.reduce((filteredTranslations, translation) => alreadyTranslated.filter(
|
|
91
|
+
(alreadyTranslation) => alreadyTranslation.pathString === translation.pathString
|
|
92
|
+
).length > 0 || filteredTranslations.filter(
|
|
93
|
+
(filteredTranslation) => filteredTranslation.path === translation.path
|
|
94
|
+
).length > 0 ? filteredTranslations : [...filteredTranslations, translation], []);
|
|
95
|
+
if (removeDuplicates.length === 0) {
|
|
70
96
|
toast.push({
|
|
71
97
|
status: "error",
|
|
72
98
|
title: "No internationalizedArray fields found in document root"
|
|
73
99
|
});
|
|
74
100
|
return;
|
|
75
101
|
}
|
|
76
|
-
const patches =
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
"after",
|
|
93
|
-
[fieldKey, -1]
|
|
94
|
-
)
|
|
95
|
-
];
|
|
96
|
-
}).flat();
|
|
97
|
-
onChange(sanity.PatchEvent.from(patches));
|
|
102
|
+
const patches = [];
|
|
103
|
+
for (const toTranslate of removeDuplicates) {
|
|
104
|
+
const path = toTranslate.path, ifMissing = sanity.setIfMissing([], path), insertValue = sanity.insert(
|
|
105
|
+
[
|
|
106
|
+
{
|
|
107
|
+
_key: languageId,
|
|
108
|
+
_type: toTranslate._type,
|
|
109
|
+
value: void 0
|
|
110
|
+
}
|
|
111
|
+
],
|
|
112
|
+
"after",
|
|
113
|
+
[...path, -1]
|
|
114
|
+
);
|
|
115
|
+
patches.push(ifMissing), patches.push(insertValue);
|
|
116
|
+
}
|
|
117
|
+
onChange(sanity.PatchEvent.from(patches.flat()));
|
|
98
118
|
},
|
|
99
|
-
[
|
|
119
|
+
[documentsToTranslation, onChange, toast]
|
|
100
120
|
);
|
|
101
121
|
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
|
|
102
122
|
/* @__PURE__ */ jsxRuntime.jsx(ui.Box, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: 1, weight: "semibold", children: "Add translation to internationalized fields" }) }),
|
|
@@ -118,7 +138,7 @@ const getSelectedValue = (select, document) => {
|
|
|
118
138
|
for (const [key, path] of Object.entries(selection)) {
|
|
119
139
|
let value = get__default.default(document, path);
|
|
120
140
|
Array.isArray(value) && (value = value.filter(
|
|
121
|
-
(item) => typeof item == "object" ?
|
|
141
|
+
(item) => typeof item == "object" ? item?._type === "reference" && "_ref" in item : !0
|
|
122
142
|
)), selectedValue[key] = value;
|
|
123
143
|
}
|
|
124
144
|
return selectedValue;
|
|
@@ -131,7 +151,9 @@ function useInternationalizedArrayContext() {
|
|
|
131
151
|
return react.useContext(InternationalizedArrayContext);
|
|
132
152
|
}
|
|
133
153
|
function InternationalizedArrayProvider(props) {
|
|
134
|
-
const { internationalizedArray: internationalizedArray2 } = props, client = sanity.useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = sanity.useWorkspace(), { value: document } = sanity.useFormBuilder()
|
|
154
|
+
const { internationalizedArray: internationalizedArray2 } = props, client = sanity.useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = sanity.useWorkspace(), { value: document } = sanity.useFormBuilder();
|
|
155
|
+
console.log({ document }, sanity.useFormBuilder());
|
|
156
|
+
const deferredDocument = react.useDeferredValue(document), selectedValue = react.useMemo(
|
|
135
157
|
() => getSelectedValue(internationalizedArray2.select, deferredDocument),
|
|
136
158
|
[internationalizedArray2.select, deferredDocument]
|
|
137
159
|
), languages = Array.isArray(internationalizedArray2.languages) ? internationalizedArray2.languages : suspend.suspend(
|
|
@@ -145,7 +167,7 @@ function InternationalizedArrayProvider(props) {
|
|
|
145
167
|
(language) => selectedLanguageIds.includes(language.id)
|
|
146
168
|
) : languages;
|
|
147
169
|
}, [deferredDocument, languageFilterOptions, languages, selectedLanguageIds]), showDocumentButtons = internationalizedArray2.buttonLocations.includes("document");
|
|
148
|
-
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
170
|
+
return console.log({ showDocumentButtons }), /* @__PURE__ */ jsxRuntime.jsx(
|
|
149
171
|
InternationalizedArrayContext.Provider,
|
|
150
172
|
{
|
|
151
173
|
value: {
|
|
@@ -154,13 +176,7 @@ function InternationalizedArrayProvider(props) {
|
|
|
154
176
|
filteredLanguages
|
|
155
177
|
},
|
|
156
178
|
children: showDocumentButtons ? /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 5, children: [
|
|
157
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
158
|
-
DocumentAddButtons,
|
|
159
|
-
{
|
|
160
|
-
schemaType: props.schemaType,
|
|
161
|
-
value: props.value
|
|
162
|
-
}
|
|
163
|
-
),
|
|
179
|
+
/* @__PURE__ */ jsxRuntime.jsx(DocumentAddButtons, { value: props.value }),
|
|
164
180
|
props.renderDefault(props)
|
|
165
181
|
] }) : props.renderDefault(props)
|
|
166
182
|
}
|
|
@@ -177,7 +193,10 @@ function checkAllLanguagesArePresent(languages, value) {
|
|
|
177
193
|
return languagesInUseIds.length === filteredLanguageIds.length && languagesInUseIds.every((l) => filteredLanguageIds.includes(l));
|
|
178
194
|
}
|
|
179
195
|
function createAddAllTitle(value, languages) {
|
|
180
|
-
return value
|
|
196
|
+
return value?.length ? `Add missing ${languages.length - value.length === 1 ? "language" : "languages"}` : languages.length === 1 ? `Add ${languages[0].title} Field` : "Add all languages";
|
|
197
|
+
}
|
|
198
|
+
function createValueSchemaTypeName(schemaType) {
|
|
199
|
+
return `${schemaType.name}Value`;
|
|
181
200
|
}
|
|
182
201
|
function createAddLanguagePatches(config) {
|
|
183
202
|
const {
|
|
@@ -187,15 +206,15 @@ function createAddLanguagePatches(config) {
|
|
|
187
206
|
filteredLanguages,
|
|
188
207
|
value,
|
|
189
208
|
path = []
|
|
190
|
-
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? (
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
) : (
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
), languagesInUse = value
|
|
209
|
+
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => ({
|
|
210
|
+
...itemBase,
|
|
211
|
+
_key: id
|
|
212
|
+
})) : filteredLanguages.filter(
|
|
213
|
+
(language) => value?.length ? !value.find((v) => v._key === language.id) : !0
|
|
214
|
+
).map((language) => ({
|
|
215
|
+
...itemBase,
|
|
216
|
+
_key: language.id
|
|
217
|
+
})), languagesInUse = value?.length ? value.map((v) => v) : [];
|
|
199
218
|
return newItems.map((item) => {
|
|
200
219
|
const languageIndex = languages.findIndex((l) => item._key === l.id), remainingLanguages = languages.slice(languageIndex + 1), nextLanguageIndex = languagesInUse.findIndex(
|
|
201
220
|
(l) => (
|
|
@@ -213,7 +232,7 @@ function createAddLanguagePatches(config) {
|
|
|
213
232
|
});
|
|
214
233
|
}
|
|
215
234
|
const createTranslateFieldActions = (fieldActionProps, { languages, filteredLanguages }) => languages.map((language) => {
|
|
216
|
-
const value = sanity.useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!
|
|
235
|
+
const value = sanity.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 } = structure.useDocumentPane(), onAction = react.useCallback(() => {
|
|
217
236
|
const { schemaType, path } = fieldActionProps, addLanguageKeys = [language.id], patches = createAddLanguagePatches({
|
|
218
237
|
addLanguageKeys,
|
|
219
238
|
schemaType,
|
|
@@ -255,8 +274,7 @@ const createTranslateFieldActions = (fieldActionProps, { languages, filteredLang
|
|
|
255
274
|
}, internationalizedArrayFieldAction = sanity.defineDocumentFieldAction({
|
|
256
275
|
name: "internationalizedArray",
|
|
257
276
|
useAction(fieldActionProps) {
|
|
258
|
-
|
|
259
|
-
const isInternationalizedArrayField = (_b = (_a = fieldActionProps == null ? void 0 : fieldActionProps.schemaType) == null ? void 0 : _a.type) == null ? void 0 : _b.name.startsWith(
|
|
277
|
+
const isInternationalizedArrayField = fieldActionProps?.schemaType?.type?.name.startsWith(
|
|
260
278
|
"internationalizedArray"
|
|
261
279
|
), { languages, filteredLanguages } = useInternationalizedArrayContext(), translateFieldActions = createTranslateFieldActions(
|
|
262
280
|
fieldActionProps,
|
|
@@ -332,11 +350,10 @@ function InternationalizedArray(props) {
|
|
|
332
350
|
}) : members,
|
|
333
351
|
[languageFilterEnabled, members, languageFilterOptions, selectedLanguageIds]
|
|
334
352
|
), handleAddLanguage = react.useCallback(
|
|
335
|
-
(param) => {
|
|
336
|
-
|
|
337
|
-
if (!(filteredLanguages != null && filteredLanguages.length))
|
|
353
|
+
async (param) => {
|
|
354
|
+
if (!filteredLanguages?.length)
|
|
338
355
|
return;
|
|
339
|
-
const addLanguageKeys = Array.isArray(param) ? param : [
|
|
356
|
+
const addLanguageKeys = Array.isArray(param) ? param : [param?.currentTarget?.value].filter(Boolean), patches = createAddLanguagePatches({
|
|
340
357
|
addLanguageKeys,
|
|
341
358
|
schemaType,
|
|
342
359
|
languages,
|
|
@@ -354,23 +371,23 @@ function InternationalizedArray(props) {
|
|
|
354
371
|
};
|
|
355
372
|
}, [defaultLanguages, documentCreatedAt, handleAddLanguage, value]);
|
|
356
373
|
const handleRestoreOrder = react.useCallback(() => {
|
|
357
|
-
if (!
|
|
374
|
+
if (!value?.length || !languages?.length)
|
|
358
375
|
return;
|
|
359
376
|
const updatedValue = value.reduce((acc, v) => {
|
|
360
|
-
const newIndex = languages.findIndex((l) => l.id ===
|
|
377
|
+
const newIndex = languages.findIndex((l) => l.id === v?._key);
|
|
361
378
|
return newIndex > -1 && (acc[newIndex] = v), acc;
|
|
362
379
|
}, []).filter(Boolean);
|
|
363
|
-
|
|
380
|
+
value?.length !== updatedValue.length && toast.push({
|
|
364
381
|
title: "There was an error reordering languages",
|
|
365
382
|
status: "warning"
|
|
366
383
|
}), onChange(sanity.set(updatedValue));
|
|
367
|
-
}, [toast, languages, onChange, value]), allKeysAreLanguages = react.useMemo(() => !
|
|
368
|
-
() => languages && languages.length > 1 ? languages.filter((l) => value
|
|
384
|
+
}, [toast, languages, onChange, value]), allKeysAreLanguages = react.useMemo(() => !value?.length || !languages?.length ? !0 : value?.every((v) => languages.find((l) => l?.id === v?._key)), [value, languages]), languagesInUse = react.useMemo(
|
|
385
|
+
() => languages && languages.length > 1 ? languages.filter((l) => value?.find((v) => v._key === l.id)) : [],
|
|
369
386
|
[languages, value]
|
|
370
|
-
), languagesOutOfOrder = react.useMemo(() => !
|
|
387
|
+
), languagesOutOfOrder = react.useMemo(() => !value?.length || !languagesInUse.length ? [] : value.map(
|
|
371
388
|
(v, vIndex) => vIndex === languagesInUse.findIndex((l) => l.id === v._key) ? null : v
|
|
372
389
|
).filter(Boolean), [value, languagesInUse]), languagesAreValid = react.useMemo(
|
|
373
|
-
() => !
|
|
390
|
+
() => !languages?.length || languages?.length && languages.every((item) => item.id && item.title),
|
|
374
391
|
[languages]
|
|
375
392
|
);
|
|
376
393
|
react.useEffect(() => {
|
|
@@ -385,9 +402,9 @@ function InternationalizedArray(props) {
|
|
|
385
402
|
const addButtonsAreVisible = (
|
|
386
403
|
// Plugin was configured to display buttons here (default!)
|
|
387
404
|
buttonLocations.includes("field") && // There's at least one language visible
|
|
388
|
-
|
|
405
|
+
filteredLanguages?.length > 0 && // Not every language has a value yet
|
|
389
406
|
!allLanguagesArePresent
|
|
390
|
-
), fieldHasMembers =
|
|
407
|
+
), fieldHasMembers = members?.length > 0;
|
|
391
408
|
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 2, children: [
|
|
392
409
|
fieldHasMembers ? /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: filteredMembers.map((member) => member.kind === "item" ? /* @__PURE__ */ react.createElement(
|
|
393
410
|
sanity.ArrayOfObjectsItem,
|
|
@@ -423,8 +440,7 @@ function InternationalizedArray(props) {
|
|
|
423
440
|
] });
|
|
424
441
|
}
|
|
425
442
|
function getLanguagesFieldOption(schemaType) {
|
|
426
|
-
|
|
427
|
-
return schemaType ? ((_a = schemaType.options) == null ? void 0 : _a.languages) || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
443
|
+
return schemaType ? schemaType.options?.languages || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
428
444
|
}
|
|
429
445
|
var array = (config) => {
|
|
430
446
|
const { apiVersion, select, languages, type } = config, typeName = typeof type == "string" ? type : type.name, arrayName = createFieldName(typeName), objectName = createFieldName(typeName, !0);
|
|
@@ -436,7 +452,12 @@ var array = (config) => {
|
|
|
436
452
|
input: InternationalizedArray
|
|
437
453
|
},
|
|
438
454
|
// These options are required for validation rules – not the custom input component
|
|
439
|
-
options: {
|
|
455
|
+
options: {
|
|
456
|
+
// @ts-expect-error - find out why it fails
|
|
457
|
+
apiVersion,
|
|
458
|
+
select,
|
|
459
|
+
languages
|
|
460
|
+
},
|
|
440
461
|
of: [
|
|
441
462
|
sanity.defineField({
|
|
442
463
|
...typeof type == "string" ? {} : type,
|
|
@@ -444,15 +465,16 @@ var array = (config) => {
|
|
|
444
465
|
type: objectName
|
|
445
466
|
})
|
|
446
467
|
],
|
|
468
|
+
// @ts-expect-error - find out why it fails
|
|
447
469
|
validation: (rule) => rule.custom(async (value, context) => {
|
|
448
470
|
if (!value)
|
|
449
471
|
return !0;
|
|
450
472
|
const selectedValue = getSelectedValue(select, context.document), client = context.getClient({ apiVersion });
|
|
451
473
|
let contextLanguages = [];
|
|
452
|
-
const languagesFieldOption = getLanguagesFieldOption(context
|
|
474
|
+
const languagesFieldOption = getLanguagesFieldOption(context?.type);
|
|
453
475
|
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)
|
|
454
476
|
return `Cannot be more than ${contextLanguages.length === 1 ? "1 item" : `${contextLanguages.length} items`}`;
|
|
455
|
-
const nonLanguageKeys = value
|
|
477
|
+
const nonLanguageKeys = value?.length ? value.filter(
|
|
456
478
|
(item) => !contextLanguages.find((language) => item._key === language.id)
|
|
457
479
|
) : [];
|
|
458
480
|
if (nonLanguageKeys.length)
|
|
@@ -460,10 +482,10 @@ var array = (config) => {
|
|
|
460
482
|
message: "Array item keys must be valid languages registered to the field type",
|
|
461
483
|
paths: nonLanguageKeys.map((item) => [{ _key: item._key }])
|
|
462
484
|
};
|
|
463
|
-
const valuesByLanguage = value
|
|
485
|
+
const valuesByLanguage = value?.length ? value.filter((item) => !!item?._key).reduce((acc, cur) => acc[cur._key] ? { ...acc, [cur._key]: [...acc[cur._key], cur] } : {
|
|
464
486
|
...acc,
|
|
465
487
|
[cur._key]: [cur]
|
|
466
|
-
}, {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) =>
|
|
488
|
+
}, {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) => item?.length > 1).flat();
|
|
467
489
|
return duplicateValues.length ? {
|
|
468
490
|
message: "There can only be one field per language",
|
|
469
491
|
paths: duplicateValues.map((item) => [{ _key: item._key }])
|
|
@@ -479,7 +501,7 @@ function InternationalizedField(props) {
|
|
|
479
501
|
}) : props.children;
|
|
480
502
|
}
|
|
481
503
|
function getToneFromValidation(validations) {
|
|
482
|
-
if (!
|
|
504
|
+
if (!validations?.length)
|
|
483
505
|
return;
|
|
484
506
|
const validationLevels = validations.map((v) => v.level);
|
|
485
507
|
if (validationLevels.includes("error"))
|
|
@@ -500,16 +522,12 @@ function InternationalizedInput(props) {
|
|
|
500
522
|
// TODO: Remove this as it shouldn't be necessary?
|
|
501
523
|
value: props.value
|
|
502
524
|
}, { validation, value, onChange, readOnly } = inlineProps, { languages } = useInternationalizedArrayContext(), languageKeysInUse = react.useMemo(
|
|
503
|
-
() =>
|
|
504
|
-
var _a;
|
|
505
|
-
return (_a = parentValue == null ? void 0 : parentValue.map((v) => v._key)) != null ? _a : [];
|
|
506
|
-
},
|
|
525
|
+
() => parentValue?.map((v) => v._key) ?? [],
|
|
507
526
|
[parentValue]
|
|
508
|
-
), keyIsValid = languages
|
|
527
|
+
), keyIsValid = languages?.length ? languages.find((l) => l.id === value._key) : !1, handleKeyChange = react.useCallback(
|
|
509
528
|
(event) => {
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
!value || !(languages != null && languages.length) || !languages.find((l) => l.id === languageId) || onChange([sanity.set(languageId, ["_key"])]);
|
|
529
|
+
const languageId = event?.currentTarget?.value;
|
|
530
|
+
!value || !languages?.length || !languages.find((l) => l.id === languageId) || onChange([sanity.set(languageId, ["_key"])]);
|
|
513
531
|
},
|
|
514
532
|
[onChange, value, languages]
|
|
515
533
|
), handleUnset = react.useCallback(() => {
|
|
@@ -557,9 +575,9 @@ var object = (config) => {
|
|
|
557
575
|
title: `Internationalized array ${type}`,
|
|
558
576
|
type: "object",
|
|
559
577
|
components: {
|
|
578
|
+
// @ts-expect-error - find out why it fails
|
|
560
579
|
item: InternationalizedInput
|
|
561
580
|
},
|
|
562
|
-
// @ts-expect-error - Address this typing issue with the inner object
|
|
563
581
|
fields: [
|
|
564
582
|
typeof type == "string" ? (
|
|
565
583
|
// Define a simple field if all we have is the name as a string
|