gt-sanity 4.0.14 → 4.0.15
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/index.js +118 -266
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/adapter/{api.test.ts → __tests__/api.test.ts} +75 -59
- package/src/adapter/api.ts +22 -221
- package/src/serialization/data.ts +2 -22
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { defaultBaseUrl, unwrapApiResult, decode
|
|
3
|
-
import {
|
|
1
|
+
import { GT, getLocaleProperties } from "generaltranslation";
|
|
2
|
+
import { createGtApiAdapter, defaultBaseUrl, unwrapApiResult, decode, libraryDefaultLocale, createDiagnosticMessage, encode } from "generaltranslation/internal";
|
|
3
|
+
import { downloadFile, getTranslationStatus } from "generaltranslation/api";
|
|
4
4
|
import { useClient as useClient$1, useSchema, Preview, definePlugin } from "sanity";
|
|
5
5
|
import { useIntentLink, Link, route } from "sanity/router";
|
|
6
6
|
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
@@ -29,109 +29,38 @@ import { internationalizedArray } from "sanity-plugin-internationalized-array";
|
|
|
29
29
|
import { internationalizedArray as internationalizedArray2, internationalizedArrayLanguageFilter, isInternationalizedArrayItemType } from "sanity-plugin-internationalized-array";
|
|
30
30
|
import { documentInternationalization } from "@sanity/document-internationalization";
|
|
31
31
|
import { DocumentInternationalizationMenu, documentInternationalization as documentInternationalization2, useDeleteTranslationAction, useDocumentInternationalizationContext, useDuplicateWithTranslationsAction } from "@sanity/document-internationalization";
|
|
32
|
-
|
|
32
|
+
const {
|
|
33
|
+
configure: configureSharedApi,
|
|
34
|
+
getClient,
|
|
35
|
+
getClientConfig: _getClientConfig,
|
|
36
|
+
...sharedApi
|
|
37
|
+
} = createGtApiAdapter({ baseUrl: defaultBaseUrl });
|
|
33
38
|
function configureApiClient(config) {
|
|
34
|
-
|
|
35
|
-
client = createApiClient({
|
|
36
|
-
baseUrl: defaultBaseUrl,
|
|
37
|
-
...clientConfig
|
|
38
|
-
}), customMapping = mapping;
|
|
39
|
+
configureSharedApi({ baseUrl: defaultBaseUrl, ...config });
|
|
39
40
|
}
|
|
40
41
|
const api = {
|
|
41
|
-
|
|
42
|
-
const sourceLocale = resolveCanonicalLocale(
|
|
43
|
-
options.sourceLocale,
|
|
44
|
-
customMapping
|
|
45
|
-
);
|
|
46
|
-
return { uploadedFiles: await processBatches(files, async (batch) => unwrapApiResult(
|
|
47
|
-
await uploadSourceFiles({
|
|
48
|
-
body: {
|
|
49
|
-
data: batch.map(({ source }) => ({
|
|
50
|
-
source: {
|
|
51
|
-
...source,
|
|
52
|
-
content: encodeFileContent(source.content, source.fileFormat),
|
|
53
|
-
locale: resolveCanonicalLocale(source.locale, customMapping)
|
|
54
|
-
}
|
|
55
|
-
})),
|
|
56
|
-
sourceLocale
|
|
57
|
-
},
|
|
58
|
-
client
|
|
59
|
-
})
|
|
60
|
-
).uploadedFiles) };
|
|
61
|
-
},
|
|
62
|
-
async uploadTranslations(files, options) {
|
|
63
|
-
return { uploadedFiles: await processBatches(files, async (batch) => unwrapApiResult(
|
|
64
|
-
await uploadTranslations$1({
|
|
65
|
-
body: {
|
|
66
|
-
data: batch.map(({ source, translations }) => ({
|
|
67
|
-
source: {
|
|
68
|
-
...source,
|
|
69
|
-
content: encodeFileContent(source.content, source.fileFormat)
|
|
70
|
-
},
|
|
71
|
-
translations: translations.map((translation) => ({
|
|
72
|
-
...translation,
|
|
73
|
-
content: encodeFileContent(
|
|
74
|
-
translation.content,
|
|
75
|
-
translation.fileFormat
|
|
76
|
-
),
|
|
77
|
-
locale: resolveCanonicalLocale(
|
|
78
|
-
translation.locale,
|
|
79
|
-
customMapping
|
|
80
|
-
)
|
|
81
|
-
}))
|
|
82
|
-
})),
|
|
83
|
-
sourceLocale: resolveCanonicalLocale(
|
|
84
|
-
options.sourceLocale,
|
|
85
|
-
customMapping
|
|
86
|
-
)
|
|
87
|
-
},
|
|
88
|
-
client
|
|
89
|
-
})
|
|
90
|
-
).uploadedFiles) };
|
|
91
|
-
},
|
|
92
|
-
async enqueueFiles(files, options) {
|
|
93
|
-
const sourceLocale = options.sourceLocale ? resolveCanonicalLocale(options.sourceLocale, customMapping) : void 0, targetLocales = options.targetLocales.map(
|
|
94
|
-
(locale) => resolveCanonicalLocale(locale, customMapping)
|
|
95
|
-
), result = await processBatches(files, async (batch) => {
|
|
96
|
-
const response = unwrapApiResult(
|
|
97
|
-
await enqueueFileTranslations({
|
|
98
|
-
body: {
|
|
99
|
-
files: batch,
|
|
100
|
-
sourceLocale,
|
|
101
|
-
targetLocales,
|
|
102
|
-
force: options.force
|
|
103
|
-
},
|
|
104
|
-
client
|
|
105
|
-
})
|
|
106
|
-
);
|
|
107
|
-
return Object.entries(
|
|
108
|
-
"jobData" in response ? response.jobData : response.data
|
|
109
|
-
);
|
|
110
|
-
});
|
|
111
|
-
return { jobData: Object.fromEntries(result), locales: targetLocales };
|
|
112
|
-
},
|
|
42
|
+
...sharedApi,
|
|
113
43
|
async querySourceFile(query) {
|
|
114
44
|
const { fileId, ...queryParams } = query, result = unwrapApiResult(
|
|
115
45
|
await getTranslationStatus({
|
|
116
46
|
path: { fileId },
|
|
117
47
|
query: queryParams,
|
|
118
|
-
client
|
|
48
|
+
client: getClient()
|
|
119
49
|
})
|
|
120
50
|
);
|
|
121
51
|
return {
|
|
122
52
|
...result,
|
|
123
53
|
translations: result.translations.map((translation) => ({
|
|
124
54
|
...translation,
|
|
125
|
-
locale: resolveAliasLocale(translation.locale
|
|
55
|
+
locale: sharedApi.resolveAliasLocale(translation.locale)
|
|
126
56
|
})),
|
|
127
57
|
sourceFile: {
|
|
128
58
|
...result.sourceFile,
|
|
129
|
-
sourceLocale: resolveAliasLocale(
|
|
130
|
-
result.sourceFile.sourceLocale
|
|
131
|
-
customMapping
|
|
59
|
+
sourceLocale: sharedApi.resolveAliasLocale(
|
|
60
|
+
result.sourceFile.sourceLocale
|
|
132
61
|
),
|
|
133
62
|
locales: result.sourceFile.locales.map(
|
|
134
|
-
(locale) => resolveAliasLocale(locale
|
|
63
|
+
(locale) => sharedApi.resolveAliasLocale(locale)
|
|
135
64
|
)
|
|
136
65
|
}
|
|
137
66
|
};
|
|
@@ -142,80 +71,16 @@ const api = {
|
|
|
142
71
|
path: { fileId },
|
|
143
72
|
query: {
|
|
144
73
|
...queryParams,
|
|
145
|
-
locale: locale ? resolveCanonicalLocale(locale
|
|
74
|
+
locale: locale ? sharedApi.resolveCanonicalLocale(locale) : void 0
|
|
146
75
|
},
|
|
147
|
-
client
|
|
76
|
+
client: getClient()
|
|
148
77
|
})
|
|
149
78
|
);
|
|
150
|
-
return decode
|
|
79
|
+
return decode(response.data);
|
|
151
80
|
},
|
|
152
81
|
async downloadFileBatch(files) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
await downloadFiles({
|
|
156
|
-
body: batch.map((file) => ({
|
|
157
|
-
...file,
|
|
158
|
-
locale: file.locale ? resolveCanonicalLocale(file.locale, customMapping) : void 0
|
|
159
|
-
})),
|
|
160
|
-
client
|
|
161
|
-
})
|
|
162
|
-
), responses = await processBatches(files, async (batch) => [
|
|
163
|
-
await request(batch)
|
|
164
|
-
]);
|
|
165
|
-
return {
|
|
166
|
-
files: responses.flatMap(
|
|
167
|
-
(response) => response.files.map((file) => ({
|
|
168
|
-
...file,
|
|
169
|
-
...file.locale && {
|
|
170
|
-
locale: resolveAliasLocale(file.locale, customMapping)
|
|
171
|
-
},
|
|
172
|
-
data: decodeFileContent(file.data, file.fileFormat),
|
|
173
|
-
// OpenAPI currently types metadata as an open object; the API emits
|
|
174
|
-
// JSON values here, matching DownloadedFile's public contract.
|
|
175
|
-
metadata: file.metadata
|
|
176
|
-
}))
|
|
177
|
-
),
|
|
178
|
-
count: responses.reduce((count, response) => count + response.count, 0)
|
|
179
|
-
};
|
|
180
|
-
},
|
|
181
|
-
async setupProject(files, options = {}) {
|
|
182
|
-
return unwrapApiResult(
|
|
183
|
-
await generateProjectContext({ body: { files, ...options }, client })
|
|
184
|
-
);
|
|
185
|
-
},
|
|
186
|
-
async awaitJobs(jobIds, options) {
|
|
187
|
-
return awaitJobs(client, jobIds, options);
|
|
188
|
-
},
|
|
189
|
-
async queryFileData(body) {
|
|
190
|
-
const result = unwrapApiResult(
|
|
191
|
-
await getFileInfo({
|
|
192
|
-
body: {
|
|
193
|
-
...body,
|
|
194
|
-
translatedFiles: body.translatedFiles?.map((file) => ({
|
|
195
|
-
...file,
|
|
196
|
-
locale: resolveCanonicalLocale(file.locale, customMapping)
|
|
197
|
-
}))
|
|
198
|
-
},
|
|
199
|
-
client
|
|
200
|
-
})
|
|
201
|
-
);
|
|
202
|
-
return {
|
|
203
|
-
...result,
|
|
204
|
-
translatedFiles: result.translatedFiles.map((file) => ({
|
|
205
|
-
...file,
|
|
206
|
-
locale: resolveAliasLocale(file.locale, customMapping)
|
|
207
|
-
})),
|
|
208
|
-
sourceFiles: result.sourceFiles.map((file) => ({
|
|
209
|
-
...file,
|
|
210
|
-
sourceLocale: resolveAliasLocale(file.sourceLocale, customMapping),
|
|
211
|
-
locales: file.locales.map(
|
|
212
|
-
(locale) => resolveAliasLocale(locale, customMapping)
|
|
213
|
-
)
|
|
214
|
-
}))
|
|
215
|
-
};
|
|
216
|
-
},
|
|
217
|
-
async createBranch(body) {
|
|
218
|
-
return unwrapApiResult(await createBranch({ body, client }));
|
|
82
|
+
const { pending: _pending, ...result } = await sharedApi.downloadFileBatch(files);
|
|
83
|
+
return result;
|
|
219
84
|
}
|
|
220
85
|
}, SECRETS_NAMESPACE = "generaltranslation.secrets", SANITY_API_VERSION = "2025-09-15", gt = new GT(), DEFAULT_TRANSLATION_PREFERENCES = {
|
|
221
86
|
autoRefresh: !0,
|
|
@@ -358,10 +223,10 @@ function formatLocalePropertiesLabel(code, properties, mapping) {
|
|
|
358
223
|
}
|
|
359
224
|
const useClient = () => useClient$1({ apiVersion: SANITY_API_VERSION });
|
|
360
225
|
function useSecrets(id) {
|
|
361
|
-
const [loading, setLoading] = useState(!0), [secrets, setSecrets] = useState(null),
|
|
226
|
+
const [loading, setLoading] = useState(!0), [secrets, setSecrets] = useState(null), client = useClient();
|
|
362
227
|
return useEffect(() => {
|
|
363
228
|
function fetchData() {
|
|
364
|
-
|
|
229
|
+
client.fetch("* [_id == $id][0]", { id }).then((doc) => {
|
|
365
230
|
const result = {};
|
|
366
231
|
for (const key in doc)
|
|
367
232
|
key[0] !== "_" && (result[key] = doc[key]);
|
|
@@ -369,7 +234,7 @@ function useSecrets(id) {
|
|
|
369
234
|
});
|
|
370
235
|
}
|
|
371
236
|
fetchData();
|
|
372
|
-
}, [id,
|
|
237
|
+
}, [id, client]), { loading, secrets };
|
|
373
238
|
}
|
|
374
239
|
const gtRadius = [0, 4, 6, 8, 12, 16, 24], baseTheme = buildTheme(), theme = {
|
|
375
240
|
...baseTheme,
|
|
@@ -478,18 +343,18 @@ function createStableTranslationKey(branchId, documentId, localeId) {
|
|
|
478
343
|
const publishedId = getPublishedId(documentId);
|
|
479
344
|
return branchId ? `${branchId}:${publishedId}:${localeId}` : `${publishedId}:${localeId}`;
|
|
480
345
|
}
|
|
481
|
-
const findLatestDraft = (documentId,
|
|
346
|
+
const findLatestDraft = (documentId, client) => {
|
|
482
347
|
const publishedId = getPublishedId(documentId), query = "*[_id == $id || _id == $draftId]", params = { id: publishedId, draftId: `drafts.${publishedId}` };
|
|
483
|
-
return
|
|
348
|
+
return client.fetch(query, params).then(
|
|
484
349
|
(docs) => docs.find((doc) => doc._id.startsWith("drafts.")) ?? docs[0]
|
|
485
350
|
);
|
|
486
|
-
}, findDocumentAtRevision = async (documentId, rev,
|
|
487
|
-
const baseUrl = `/data/history/${
|
|
351
|
+
}, findDocumentAtRevision = async (documentId, rev, client) => {
|
|
352
|
+
const baseUrl = `/data/history/${client.config().dataset}/documents/${documentId}?revision=${rev}`, url = client.getUrl(baseUrl);
|
|
488
353
|
return await fetch(url, { credentials: "include" }).then((req) => req.json()).then((req) => req.documents && req.documents.length ? req.documents[0] : null);
|
|
489
354
|
};
|
|
490
|
-
async function requireSourceDocument(documentId, versionId,
|
|
355
|
+
async function requireSourceDocument(documentId, versionId, client) {
|
|
491
356
|
let doc = null;
|
|
492
|
-
if (documentId && versionId && (doc = await findDocumentAtRevision(documentId, versionId,
|
|
357
|
+
if (documentId && versionId && (doc = await findDocumentAtRevision(documentId, versionId, client)), doc || (doc = await findLatestDraft(documentId, client)), !doc)
|
|
493
358
|
throw new Error(missingDocumentDiagnostic(documentId));
|
|
494
359
|
return doc;
|
|
495
360
|
}
|
|
@@ -589,7 +454,7 @@ function metadataTranslations(languageCondition, extraCondition) {
|
|
|
589
454
|
function metadataTranslationRef(languageExpression) {
|
|
590
455
|
return `${metadataTranslations(`== ${languageExpression}`)}[0].value._ref`;
|
|
591
456
|
}
|
|
592
|
-
async function createI18nDocAndPatchMetadata(sourceDocument, translatedDoc, localeId,
|
|
457
|
+
async function createI18nDocAndPatchMetadata(sourceDocument, translatedDoc, localeId, client, translationMetadata, sourceDocumentId, languageField = "language") {
|
|
593
458
|
const publishedSourceDocumentId = getPublishedId(sourceDocumentId);
|
|
594
459
|
translatedDoc[languageField] = localeId;
|
|
595
460
|
const existingLocaleKey = translationMetadata.translations.find(
|
|
@@ -609,19 +474,19 @@ async function createI18nDocAndPatchMetadata(sourceDocument, translatedDoc, loca
|
|
|
609
474
|
publishedSourceDocumentId,
|
|
610
475
|
localeId
|
|
611
476
|
);
|
|
612
|
-
createDocumentPromise =
|
|
477
|
+
createDocumentPromise = client.create({
|
|
613
478
|
...appliedDocument,
|
|
614
479
|
_type: rest._type,
|
|
615
480
|
_id: `drafts.${translatedDocId}`
|
|
616
481
|
});
|
|
617
482
|
} else
|
|
618
|
-
createDocumentPromise =
|
|
483
|
+
createDocumentPromise = client.create({
|
|
619
484
|
...appliedDocument,
|
|
620
485
|
_type: rest._type,
|
|
621
486
|
_id: "drafts."
|
|
622
487
|
});
|
|
623
488
|
const doc = await createDocumentPromise, _ref = getPublishedId(doc._id);
|
|
624
|
-
await
|
|
489
|
+
await client.transaction().patch(
|
|
625
490
|
translationMetadata._id,
|
|
626
491
|
(p) => p.insert(operation, location, [
|
|
627
492
|
{
|
|
@@ -640,8 +505,8 @@ async function createI18nDocAndPatchMetadata(sourceDocument, translatedDoc, loca
|
|
|
640
505
|
])
|
|
641
506
|
).commit();
|
|
642
507
|
}
|
|
643
|
-
const getOrCreateTranslationMetadata = async (documentId, baseDocument,
|
|
644
|
-
const publishedId = getPublishedId(documentId), existingMetadata = await
|
|
508
|
+
const getOrCreateTranslationMetadata = async (documentId, baseDocument, client, baseLanguage) => {
|
|
509
|
+
const publishedId = getPublishedId(documentId), existingMetadata = await client.fetch(
|
|
645
510
|
`*[
|
|
646
511
|
_type == '${TRANSLATION_METADATA_TYPE}' &&
|
|
647
512
|
${metadataTranslationRef("$baseLanguage")} == $id
|
|
@@ -669,13 +534,13 @@ const getOrCreateTranslationMetadata = async (documentId, baseDocument, client2,
|
|
|
669
534
|
}
|
|
670
535
|
});
|
|
671
536
|
try {
|
|
672
|
-
return await
|
|
537
|
+
return await client.createIfNotExists({
|
|
673
538
|
_id: translationMetadataId(publishedId),
|
|
674
539
|
_type: TRANSLATION_METADATA_TYPE,
|
|
675
540
|
translations: [baseLangEntry]
|
|
676
541
|
});
|
|
677
542
|
} catch (error) {
|
|
678
|
-
const metadata = await
|
|
543
|
+
const metadata = await client.fetch(
|
|
679
544
|
`*[
|
|
680
545
|
_type == '${TRANSLATION_METADATA_TYPE}' &&
|
|
681
546
|
${metadataTranslationRef("$baseLanguage")} == $id
|
|
@@ -687,7 +552,7 @@ const getOrCreateTranslationMetadata = async (documentId, baseDocument, client2,
|
|
|
687
552
|
throw error;
|
|
688
553
|
}
|
|
689
554
|
}, SYSTEM_FIELDS = ["_id", "_rev", "_updatedAt", "language"], isSystemField = (field) => SYSTEM_FIELDS.includes(field);
|
|
690
|
-
async function patchI18nDoc(sourceDocumentId, i18nDocId, sourceDocument, mergedDocument, translatedFields,
|
|
555
|
+
async function patchI18nDoc(sourceDocumentId, i18nDocId, sourceDocument, mergedDocument, translatedFields, client, existingDocument) {
|
|
691
556
|
const cleanedMerge = {};
|
|
692
557
|
Object.entries(mergedDocument).forEach(([key, value]) => {
|
|
693
558
|
key in translatedFields && //don't overwrite any existing system values on the i18n doc
|
|
@@ -712,36 +577,36 @@ async function patchI18nDoc(sourceDocumentId, i18nDocId, sourceDocument, mergedD
|
|
|
712
577
|
JSONPointer.set(appliedDocument, result.pointer, result.value);
|
|
713
578
|
}
|
|
714
579
|
), i18nDocId.startsWith("drafts.")) {
|
|
715
|
-
await
|
|
580
|
+
await client.patch(i18nDocId, { set: appliedDocument }).commit();
|
|
716
581
|
return;
|
|
717
582
|
}
|
|
718
|
-
const seed = existingDocument ?? await
|
|
583
|
+
const seed = existingDocument ?? await client.getDocument(i18nDocId);
|
|
719
584
|
if (!seed) {
|
|
720
|
-
await
|
|
585
|
+
await client.patch(i18nDocId, { set: appliedDocument }).commit();
|
|
721
586
|
return;
|
|
722
587
|
}
|
|
723
588
|
const draftId = `drafts.${getPublishedId(i18nDocId)}`;
|
|
724
|
-
await
|
|
589
|
+
await client.transaction().createIfNotExists({ ...seed, _id: draftId }).patch(draftId, (patch) => patch.set(appliedDocument)).commit();
|
|
725
590
|
}
|
|
726
|
-
const documentLevelPatch = async (docInfo, translatedFields, localeId,
|
|
591
|
+
const documentLevelPatch = async (docInfo, translatedFields, localeId, client, languageField = "language", mergeWithTargetLocale = !1) => {
|
|
727
592
|
const baseLanguage = pluginConfig.getSourceLocale();
|
|
728
593
|
let i18nDoc, baseDoc = await requireSourceDocument(
|
|
729
594
|
docInfo.documentId,
|
|
730
595
|
docInfo.versionId,
|
|
731
|
-
|
|
596
|
+
client
|
|
732
597
|
);
|
|
733
598
|
const i18nDocId = (await getOrCreateTranslationMetadata(
|
|
734
599
|
docInfo.documentId,
|
|
735
600
|
baseDoc,
|
|
736
|
-
|
|
601
|
+
client,
|
|
737
602
|
baseLanguage
|
|
738
603
|
)).translations.find(
|
|
739
604
|
(translation) => translation.language === localeId
|
|
740
605
|
)?.value?._ref;
|
|
741
|
-
i18nDocId && (i18nDoc = await findLatestDraft(i18nDocId,
|
|
606
|
+
i18nDocId && (i18nDoc = await findLatestDraft(i18nDocId, client)), mergeWithTargetLocale && i18nDoc ? baseDoc = i18nDoc : docInfo.documentId && docInfo.versionId && (baseDoc = await requireSourceDocument(
|
|
742
607
|
docInfo.documentId,
|
|
743
608
|
docInfo.versionId,
|
|
744
|
-
|
|
609
|
+
client
|
|
745
610
|
));
|
|
746
611
|
const merged = BaseDocumentMerger.documentLevelMerge(
|
|
747
612
|
translatedFields,
|
|
@@ -754,18 +619,18 @@ const documentLevelPatch = async (docInfo, translatedFields, localeId, client2,
|
|
|
754
619
|
baseDoc,
|
|
755
620
|
merged,
|
|
756
621
|
translatedFields,
|
|
757
|
-
|
|
622
|
+
client,
|
|
758
623
|
i18nDoc
|
|
759
624
|
);
|
|
760
625
|
else {
|
|
761
626
|
const freshTranslationMetadata = await getOrCreateTranslationMetadata(
|
|
762
627
|
docInfo.documentId,
|
|
763
628
|
baseDoc,
|
|
764
|
-
|
|
629
|
+
client,
|
|
765
630
|
baseLanguage
|
|
766
631
|
), freshI18nDocId = freshTranslationMetadata.translations.find(
|
|
767
632
|
(translation) => translation.language === localeId
|
|
768
|
-
)?.value?._ref, freshI18nDoc = freshI18nDocId ? await findLatestDraft(freshI18nDocId,
|
|
633
|
+
)?.value?._ref, freshI18nDoc = freshI18nDocId ? await findLatestDraft(freshI18nDocId, client) : void 0;
|
|
769
634
|
if (freshI18nDoc) {
|
|
770
635
|
await patchI18nDoc(
|
|
771
636
|
docInfo.documentId,
|
|
@@ -773,7 +638,7 @@ const documentLevelPatch = async (docInfo, translatedFields, localeId, client2,
|
|
|
773
638
|
baseDoc,
|
|
774
639
|
merged,
|
|
775
640
|
translatedFields,
|
|
776
|
-
|
|
641
|
+
client,
|
|
777
642
|
freshI18nDoc
|
|
778
643
|
);
|
|
779
644
|
return;
|
|
@@ -782,7 +647,7 @@ const documentLevelPatch = async (docInfo, translatedFields, localeId, client2,
|
|
|
782
647
|
baseDoc,
|
|
783
648
|
merged,
|
|
784
649
|
localeId,
|
|
785
|
-
|
|
650
|
+
client,
|
|
786
651
|
freshTranslationMetadata,
|
|
787
652
|
docInfo.documentId,
|
|
788
653
|
languageField
|
|
@@ -872,8 +737,8 @@ function mergeInternationalizedArrays(baseDoc, translatedFields, targetLocale, s
|
|
|
872
737
|
}
|
|
873
738
|
return changes;
|
|
874
739
|
}
|
|
875
|
-
const internationalizedArrayPatch = async (docInfo, translatedFields, localeId,
|
|
876
|
-
const sourceLocale = pluginConfig.getSourceLocale(), baseDoc = await findLatestDraft(docInfo.documentId,
|
|
740
|
+
const internationalizedArrayPatch = async (docInfo, translatedFields, localeId, client) => {
|
|
741
|
+
const sourceLocale = pluginConfig.getSourceLocale(), baseDoc = await findLatestDraft(docInfo.documentId, client);
|
|
877
742
|
if (!baseDoc)
|
|
878
743
|
return;
|
|
879
744
|
const changes = mergeInternationalizedArrays(
|
|
@@ -885,11 +750,11 @@ const internationalizedArrayPatch = async (docInfo, translatedFields, localeId,
|
|
|
885
750
|
if (Object.keys(changes).length === 0)
|
|
886
751
|
return;
|
|
887
752
|
if (baseDoc._id.startsWith("drafts.")) {
|
|
888
|
-
await
|
|
753
|
+
await client.patch(baseDoc._id).set(changes).commit();
|
|
889
754
|
return;
|
|
890
755
|
}
|
|
891
756
|
const draftId = `drafts.${getPublishedId(baseDoc._id)}`;
|
|
892
|
-
await
|
|
757
|
+
await client.transaction().createIfNotExists({ ...baseDoc, _id: draftId }).patch(draftId, (patch) => patch.set(changes)).commit();
|
|
893
758
|
}, defaultSchema = Schema.compile({
|
|
894
759
|
name: "default",
|
|
895
760
|
types: [
|
|
@@ -939,19 +804,6 @@ function detachGTData(html) {
|
|
|
939
804
|
data: extractedData
|
|
940
805
|
};
|
|
941
806
|
}
|
|
942
|
-
function encode(data) {
|
|
943
|
-
const bytes = new TextEncoder().encode(data);
|
|
944
|
-
let binary = "";
|
|
945
|
-
for (let i = 0; i < bytes.length; i++)
|
|
946
|
-
binary += String.fromCharCode(bytes[i]);
|
|
947
|
-
return btoa(binary);
|
|
948
|
-
}
|
|
949
|
-
function decode(base64) {
|
|
950
|
-
const binary = atob(base64), bytes = new Uint8Array(binary.length);
|
|
951
|
-
for (let i = 0; i < binary.length; i++)
|
|
952
|
-
bytes[i] = binary.charCodeAt(i);
|
|
953
|
-
return new TextDecoder().decode(bytes);
|
|
954
|
-
}
|
|
955
807
|
const isRecord$3 = (value) => typeof value == "object" && value !== null && !Array.isArray(value), defaultStopTypes = [
|
|
956
808
|
"reference",
|
|
957
809
|
"date",
|
|
@@ -1487,18 +1339,18 @@ function stripIgnoredFields(document2, ignoreFields, dedupeFields) {
|
|
|
1487
1339
|
const documentAdapter = {
|
|
1488
1340
|
level: "document",
|
|
1489
1341
|
serialize: (document2, schema, baseLanguage) => serializeDocument(document2, schema, baseLanguage, "document"),
|
|
1490
|
-
patch: (docInfo, deserialized, localeId,
|
|
1342
|
+
patch: (docInfo, deserialized, localeId, client, mergeWithTargetLocale) => documentLevelPatch(
|
|
1491
1343
|
docInfo,
|
|
1492
1344
|
deserialized,
|
|
1493
1345
|
localeId,
|
|
1494
|
-
|
|
1346
|
+
client,
|
|
1495
1347
|
pluginConfig.getLanguageField(),
|
|
1496
1348
|
mergeWithTargetLocale
|
|
1497
1349
|
)
|
|
1498
1350
|
}, internationalizedArrayAdapter = {
|
|
1499
1351
|
level: "internationalizedArray",
|
|
1500
1352
|
serialize: (document2, schema, baseLanguage) => serializeDocument(document2, schema, baseLanguage, "internationalizedArray"),
|
|
1501
|
-
patch: (docInfo, deserialized, localeId,
|
|
1353
|
+
patch: (docInfo, deserialized, localeId, client) => internationalizedArrayPatch(docInfo, deserialized, localeId, client)
|
|
1502
1354
|
};
|
|
1503
1355
|
function matchesFieldLevel(type) {
|
|
1504
1356
|
return type ? pluginConfig.getFieldLevelDocuments().some((filter) => filter.type === type) : !1;
|
|
@@ -1552,7 +1404,7 @@ async function createJobs(uploadResult, localeIds, secrets, force = !1) {
|
|
|
1552
1404
|
force
|
|
1553
1405
|
});
|
|
1554
1406
|
}
|
|
1555
|
-
async function collectExistingTranslations(documents, targetLocaleIds, { client
|
|
1407
|
+
async function collectExistingTranslations(documents, targetLocaleIds, { client, schema }) {
|
|
1556
1408
|
const existing = /* @__PURE__ */ new Map();
|
|
1557
1409
|
if (documents.length === 0 || targetLocaleIds.length === 0)
|
|
1558
1410
|
return existing;
|
|
@@ -1586,7 +1438,7 @@ async function collectExistingTranslations(documents, targetLocaleIds, { client:
|
|
|
1586
1438
|
localeIds,
|
|
1587
1439
|
sourceLocale,
|
|
1588
1440
|
languageField,
|
|
1589
|
-
|
|
1441
|
+
client,
|
|
1590
1442
|
schema
|
|
1591
1443
|
);
|
|
1592
1444
|
for (const [sourceDocId, translations] of documentLevel)
|
|
@@ -1594,7 +1446,7 @@ async function collectExistingTranslations(documents, targetLocaleIds, { client:
|
|
|
1594
1446
|
}
|
|
1595
1447
|
return existing;
|
|
1596
1448
|
}
|
|
1597
|
-
async function collectDocumentLevelTranslations(documents, localeIds, sourceLocale, languageField,
|
|
1449
|
+
async function collectDocumentLevelTranslations(documents, localeIds, sourceLocale, languageField, client, schema) {
|
|
1598
1450
|
const existing = /* @__PURE__ */ new Map(), sourceDocIds = documents.map(getDocumentPublishedId), query = `*[
|
|
1599
1451
|
_type == '${TRANSLATION_METADATA_TYPE}' &&
|
|
1600
1452
|
${metadataTranslationRef("$sourceLocale")} in $sourceDocIds
|
|
@@ -1604,7 +1456,7 @@ async function collectDocumentLevelTranslations(documents, localeIds, sourceLoca
|
|
|
1604
1456
|
language,
|
|
1605
1457
|
'docId': value._ref
|
|
1606
1458
|
}
|
|
1607
|
-
}`, metadataRows = await
|
|
1459
|
+
}`, metadataRows = await client.fetch(query, {
|
|
1608
1460
|
sourceLocale,
|
|
1609
1461
|
sourceDocIds,
|
|
1610
1462
|
localeIds
|
|
@@ -1625,7 +1477,7 @@ async function collectDocumentLevelTranslations(documents, localeIds, sourceLoca
|
|
|
1625
1477
|
);
|
|
1626
1478
|
if (translatedDocIds.length === 0)
|
|
1627
1479
|
return existing;
|
|
1628
|
-
const translatedDocs = await
|
|
1480
|
+
const translatedDocs = await client.fetch(
|
|
1629
1481
|
"*[_id in $ids || _id in $draftIds]",
|
|
1630
1482
|
{
|
|
1631
1483
|
ids: translatedDocIds,
|
|
@@ -1787,30 +1639,30 @@ async function checkTranslationStatus(fileQueryData, downloadStatus, secrets) {
|
|
|
1787
1639
|
}
|
|
1788
1640
|
}
|
|
1789
1641
|
async function importDocument(docInfo, localeId, document2, context, mergeWithTargetLocale = !1) {
|
|
1790
|
-
const { client
|
|
1642
|
+
const { client } = context, deserialized = deserializeDocument(document2);
|
|
1791
1643
|
return getTranslationStrategyForType(
|
|
1792
1644
|
deserialized._type
|
|
1793
1645
|
).patch(
|
|
1794
1646
|
docInfo,
|
|
1795
1647
|
deserialized,
|
|
1796
1648
|
localeId,
|
|
1797
|
-
|
|
1649
|
+
client,
|
|
1798
1650
|
mergeWithTargetLocale
|
|
1799
1651
|
);
|
|
1800
1652
|
}
|
|
1801
1653
|
const isRecord = (value) => typeof value == "object" && value !== null && !Array.isArray(value), isReference = (value) => isRecord(value) && value._type === "reference" && typeof value._ref == "string";
|
|
1802
|
-
async function resolveRefs(doc, locale,
|
|
1654
|
+
async function resolveRefs(doc, locale, client) {
|
|
1803
1655
|
const references = findReferences(doc);
|
|
1804
1656
|
if (references.length === 0)
|
|
1805
1657
|
return doc;
|
|
1806
1658
|
const translatedRefs = await resolveTranslatedReferences(
|
|
1807
1659
|
references,
|
|
1808
1660
|
locale,
|
|
1809
|
-
|
|
1661
|
+
client
|
|
1810
1662
|
);
|
|
1811
1663
|
return updateDocumentReferences(doc, translatedRefs);
|
|
1812
1664
|
}
|
|
1813
|
-
async function commitResolvedRefs(translatedDoc, resolvedDoc,
|
|
1665
|
+
async function commitResolvedRefs(translatedDoc, resolvedDoc, client) {
|
|
1814
1666
|
const {
|
|
1815
1667
|
_id: _resolvedId,
|
|
1816
1668
|
_rev: _resolvedRev,
|
|
@@ -1819,11 +1671,11 @@ async function commitResolvedRefs(translatedDoc, resolvedDoc, client2) {
|
|
|
1819
1671
|
...changes
|
|
1820
1672
|
} = resolvedDoc;
|
|
1821
1673
|
if (translatedDoc._id.startsWith("drafts.")) {
|
|
1822
|
-
await
|
|
1674
|
+
await client.patch(translatedDoc._id).set(changes).commit();
|
|
1823
1675
|
return;
|
|
1824
1676
|
}
|
|
1825
1677
|
const draftId = `drafts.${getPublishedId(translatedDoc._id)}`;
|
|
1826
|
-
await
|
|
1678
|
+
await client.transaction().createIfNotExists({ ...translatedDoc, _id: draftId }).patch(draftId, (patch) => patch.set(changes)).commit();
|
|
1827
1679
|
}
|
|
1828
1680
|
function findReferences(obj, path = []) {
|
|
1829
1681
|
if (!obj || typeof obj != "object")
|
|
@@ -1835,14 +1687,14 @@ function findReferences(obj, path = []) {
|
|
|
1835
1687
|
references.push(...findReferences(obj[key], [...path, key]));
|
|
1836
1688
|
}), references;
|
|
1837
1689
|
}
|
|
1838
|
-
async function resolveTranslatedReferences(references, locale,
|
|
1690
|
+
async function resolveTranslatedReferences(references, locale, client) {
|
|
1839
1691
|
const refIds = references.map((r) => r.ref._ref), translatedRefs = /* @__PURE__ */ new Map();
|
|
1840
1692
|
if (refIds.length === 0)
|
|
1841
1693
|
return translatedRefs;
|
|
1842
1694
|
const sourceLocale = pluginConfig.getSourceLocale(), query = `*[_type == "${TRANSLATION_METADATA_TYPE}" && count(${metadataTranslations("== $sourceLocale", "value._ref in $refIds")}) > 0] {
|
|
1843
1695
|
"originalRef": ${metadataTranslationRef("$sourceLocale")},
|
|
1844
1696
|
"translatedRefs": ${metadataTranslations("== $locale")}.value._ref
|
|
1845
|
-
}[defined(originalRef) && count(translatedRefs) > 0]`, translationPairs = await
|
|
1697
|
+
}[defined(originalRef) && count(translatedRefs) > 0]`, translationPairs = await client.fetch(query, { refIds, sourceLocale, locale });
|
|
1846
1698
|
for (const { originalRef, translatedRefs: localeRefs } of translationPairs) {
|
|
1847
1699
|
const translatedRef = localeRefs[localeRefs.length - 1];
|
|
1848
1700
|
translatedRefs.set(originalRef, translatedRef);
|
|
@@ -1870,8 +1722,8 @@ function updateReferencesRecursive(obj, translatedRefs) {
|
|
|
1870
1722
|
);
|
|
1871
1723
|
}), updated;
|
|
1872
1724
|
}
|
|
1873
|
-
async function findTranslatedDocumentForLocale(sourceDocumentId, localeId,
|
|
1874
|
-
const cleanDocId = getPublishedId(sourceDocumentId), translatedDocIds = await
|
|
1725
|
+
async function findTranslatedDocumentForLocale(sourceDocumentId, localeId, client) {
|
|
1726
|
+
const cleanDocId = getPublishedId(sourceDocumentId), translatedDocIds = await client.fetch(`*[
|
|
1875
1727
|
_type == "translation.metadata" &&
|
|
1876
1728
|
(
|
|
1877
1729
|
translations[language == $sourceLocale][0].value._ref == $cleanDocId
|
|
@@ -1882,11 +1734,11 @@ async function findTranslatedDocumentForLocale(sourceDocumentId, localeId, clien
|
|
|
1882
1734
|
cleanDocId,
|
|
1883
1735
|
localeId
|
|
1884
1736
|
}), translatedDocId = translatedDocIds?.[translatedDocIds.length - 1];
|
|
1885
|
-
return translatedDocId && await findLatestDraft(translatedDocId,
|
|
1737
|
+
return translatedDocId && await findLatestDraft(translatedDocId, client) || null;
|
|
1886
1738
|
}
|
|
1887
|
-
async function findDocument(documentId,
|
|
1739
|
+
async function findDocument(documentId, client) {
|
|
1888
1740
|
const query = "*[_id == $id]", params = { id: documentId };
|
|
1889
|
-
return (await
|
|
1741
|
+
return (await client.fetch(query, params))[0] || null;
|
|
1890
1742
|
}
|
|
1891
1743
|
async function processBatch(items, processor, options = {}) {
|
|
1892
1744
|
const {
|
|
@@ -2023,9 +1875,9 @@ const TRANSLATION_DOCS_FOR_PUBLISH_QUERY = `*[
|
|
|
2023
1875
|
'docId': value._ref
|
|
2024
1876
|
}
|
|
2025
1877
|
}`;
|
|
2026
|
-
async function publishDocument(documentId,
|
|
1878
|
+
async function publishDocument(documentId, client) {
|
|
2027
1879
|
try {
|
|
2028
|
-
documentId.startsWith("drafts.") && await
|
|
1880
|
+
documentId.startsWith("drafts.") && await client.action(
|
|
2029
1881
|
{
|
|
2030
1882
|
actionType: "sanity.action.document.publish",
|
|
2031
1883
|
draftId: documentId,
|
|
@@ -2037,13 +1889,13 @@ async function publishDocument(documentId, client2) {
|
|
|
2037
1889
|
console.error("Error publishing document", error);
|
|
2038
1890
|
}
|
|
2039
1891
|
}
|
|
2040
|
-
async function publishTranslations(documentIds,
|
|
1892
|
+
async function publishTranslations(documentIds, client) {
|
|
2041
1893
|
const publishedDocumentIds = [];
|
|
2042
1894
|
return await processBatch(
|
|
2043
1895
|
documentIds,
|
|
2044
1896
|
async (documentId) => {
|
|
2045
|
-
const document2 = await findDocument(`drafts.${documentId}`,
|
|
2046
|
-
return document2 ? (await publishDocument(document2._id,
|
|
1897
|
+
const document2 = await findDocument(`drafts.${documentId}`, client);
|
|
1898
|
+
return document2 ? (await publishDocument(document2._id, client), publishedDocumentIds.push(documentId), { documentId, published: !0 }) : { documentId, published: !1 };
|
|
2047
1899
|
},
|
|
2048
1900
|
{
|
|
2049
1901
|
onItemFailure: (documentId, error) => {
|
|
@@ -2129,7 +1981,7 @@ const useTranslations = () => {
|
|
|
2129
1981
|
skipped: /* @__PURE__ */ new Set()
|
|
2130
1982
|
}), downloadStatusRef = useRef(downloadStatus), [translationStatuses, setTranslationStatuses] = useState(/* @__PURE__ */ new Map()), [pendingTranslations, setPendingTranslations] = useState(
|
|
2131
1983
|
/* @__PURE__ */ new Set()
|
|
2132
|
-
), [importingTranslations, setImportingTranslations] = useState(/* @__PURE__ */ new Set()), [isRefreshing, setIsRefreshing] = useState(!1),
|
|
1984
|
+
), [importingTranslations, setImportingTranslations] = useState(/* @__PURE__ */ new Set()), [isRefreshing, setIsRefreshing] = useState(!1), client = useClient(), { projectId, dataset } = client.config(), uploadedVersionsStorageKey = getUploadedVersionsStorageKey(
|
|
2133
1985
|
projectId,
|
|
2134
1986
|
dataset
|
|
2135
1987
|
), [uploadedVersions, setUploadedVersions] = useState(
|
|
@@ -2163,7 +2015,7 @@ const useTranslations = () => {
|
|
|
2163
2015
|
), setPreserveExistingTranslations = useCallback(
|
|
2164
2016
|
(value) => setPreference("preserveExistingTranslations", value),
|
|
2165
2017
|
[setPreference]
|
|
2166
|
-
), schema = useSchema(), translationContext = { client
|
|
2018
|
+
), schema = useSchema(), translationContext = { client, schema }, toast = useToast(), { loading: loadingSecrets, secrets } = useSecrets(
|
|
2167
2019
|
pluginConfig.getSecretsNamespace()
|
|
2168
2020
|
), [branchId, setBranchId] = useState(void 0);
|
|
2169
2021
|
useEffect(() => {
|
|
@@ -2182,7 +2034,7 @@ const useTranslations = () => {
|
|
|
2182
2034
|
const filterConditions = pluginConfig.getTranslateDocuments().map((filter) => filter.type && filter.documentId ? `(_type == "${filter.type}" && _id == "${filter.documentId}")` : filter.type ? `_type == "${filter.type}"` : filter.documentId ? `_id == "${filter.documentId}"` : null).filter(Boolean), languageField = pluginConfig.getLanguageField(), sourceLocale = pluginConfig.getSourceLocale(), languageFilter = `(!defined(${languageField}) || ${languageField} == "${sourceLocale}")`;
|
|
2183
2035
|
let query;
|
|
2184
2036
|
filterConditions.length === 0 ? query = `*[!(_type in ["system.group"]) && !(_id in path("_.**")) && ${languageFilter}]` : query = `*[!(_type in ["system.group"]) && !(_id in path("_.**")) && (${filterConditions.join(" || ")}) && ${languageFilter}]`;
|
|
2185
|
-
const docs = await
|
|
2037
|
+
const docs = await client.fetch(query);
|
|
2186
2038
|
setDocuments(dedupeDocumentsPreferDraft(docs));
|
|
2187
2039
|
} catch {
|
|
2188
2040
|
toast.push({
|
|
@@ -2193,7 +2045,7 @@ const useTranslations = () => {
|
|
|
2193
2045
|
} finally {
|
|
2194
2046
|
setLoadingDocuments(!1);
|
|
2195
2047
|
}
|
|
2196
|
-
}, [
|
|
2048
|
+
}, [client, singleDocument]), fetchLocales = useCallback(async () => {
|
|
2197
2049
|
if (secrets)
|
|
2198
2050
|
try {
|
|
2199
2051
|
const availableLocales = await getLocales(secrets);
|
|
@@ -2208,7 +2060,7 @@ const useTranslations = () => {
|
|
|
2208
2060
|
}, [secrets]), fetchExistingTranslations = useCallback(async () => {
|
|
2209
2061
|
if (!(!documents.length || !locales.length))
|
|
2210
2062
|
try {
|
|
2211
|
-
const sourceLocale = pluginConfig.getSourceLocale(), availableLocaleIds = locales.filter((locale) => locale.enabled !== !1).map((locale) => locale.localeId), documentIds = documents.map(getDocumentPublishedId), existingMetadata = await
|
|
2063
|
+
const sourceLocale = pluginConfig.getSourceLocale(), availableLocaleIds = locales.filter((locale) => locale.enabled !== !1).map((locale) => locale.localeId), documentIds = documents.map(getDocumentPublishedId), existingMetadata = await client.fetch(`*[
|
|
2212
2064
|
_type == 'translation.metadata' &&
|
|
2213
2065
|
translations[language == $sourceLocale][0].value._ref in $documentIds
|
|
2214
2066
|
] {
|
|
@@ -2237,7 +2089,7 @@ const useTranslations = () => {
|
|
|
2237
2089
|
closable: !0
|
|
2238
2090
|
});
|
|
2239
2091
|
}
|
|
2240
|
-
}, [documents, locales,
|
|
2092
|
+
}, [documents, locales, client]), serializeSourceDocuments = useCallback(
|
|
2241
2093
|
() => documents.map((doc) => {
|
|
2242
2094
|
const { [pluginConfig.getLanguageField()]: _, ...cleanDoc } = doc, baseLanguage = pluginConfig.getSourceLocale();
|
|
2243
2095
|
try {
|
|
@@ -2347,7 +2199,7 @@ const useTranslations = () => {
|
|
|
2347
2199
|
documents,
|
|
2348
2200
|
locales,
|
|
2349
2201
|
schema,
|
|
2350
|
-
|
|
2202
|
+
client,
|
|
2351
2203
|
branchId,
|
|
2352
2204
|
preserveExistingTranslations,
|
|
2353
2205
|
serializeSourceDocuments,
|
|
@@ -2383,7 +2235,7 @@ const useTranslations = () => {
|
|
|
2383
2235
|
setIsBusy(!1);
|
|
2384
2236
|
}
|
|
2385
2237
|
}
|
|
2386
|
-
}, [secrets, documents, locales,
|
|
2238
|
+
}, [secrets, documents, locales, client, schema, serializeSourceDocuments]), handleImportAll = useCallback(async () => {
|
|
2387
2239
|
if (!(!secrets || documents.length === 0 || !branchId)) {
|
|
2388
2240
|
setIsBusy(!0);
|
|
2389
2241
|
try {
|
|
@@ -2450,7 +2302,7 @@ const useTranslations = () => {
|
|
|
2450
2302
|
branchId
|
|
2451
2303
|
]), getExistingTranslations = useCallback(
|
|
2452
2304
|
async (documentIds, localeIds, branchId2) => {
|
|
2453
|
-
const sourceLocale = pluginConfig.getSourceLocale(), existingMetadata = await
|
|
2305
|
+
const sourceLocale = pluginConfig.getSourceLocale(), existingMetadata = await client.fetch(`*[
|
|
2454
2306
|
_type == 'translation.metadata' &&
|
|
2455
2307
|
translations[language == $sourceLocale][0].value._ref in $documentIds
|
|
2456
2308
|
] {
|
|
@@ -2474,7 +2326,7 @@ const useTranslations = () => {
|
|
|
2474
2326
|
});
|
|
2475
2327
|
}), existing;
|
|
2476
2328
|
},
|
|
2477
|
-
[
|
|
2329
|
+
[client]
|
|
2478
2330
|
), handleImportMissing = useCallback(async () => {
|
|
2479
2331
|
if (!(!secrets || documents.length === 0 || !branchId)) {
|
|
2480
2332
|
setIsBusy(!0);
|
|
@@ -2745,16 +2597,16 @@ const useTranslations = () => {
|
|
|
2745
2597
|
const translatedDoc = await findTranslatedDocumentForLocale(
|
|
2746
2598
|
doc._id,
|
|
2747
2599
|
localeId,
|
|
2748
|
-
|
|
2600
|
+
client
|
|
2749
2601
|
);
|
|
2750
2602
|
if (!translatedDoc)
|
|
2751
2603
|
return { patched: !1, doc, localeId, noTranslation: !0 };
|
|
2752
2604
|
const resolvedDoc = await resolveRefs(
|
|
2753
2605
|
translatedDoc,
|
|
2754
2606
|
localeId,
|
|
2755
|
-
|
|
2607
|
+
client
|
|
2756
2608
|
);
|
|
2757
|
-
return resolvedDoc !== translatedDoc ? (await commitResolvedRefs(translatedDoc, resolvedDoc,
|
|
2609
|
+
return resolvedDoc !== translatedDoc ? (await commitResolvedRefs(translatedDoc, resolvedDoc, client), { patched: !0, doc: translatedDoc, localeId }) : { patched: !1, doc: translatedDoc, localeId };
|
|
2758
2610
|
},
|
|
2759
2611
|
{
|
|
2760
2612
|
onProgress: (current, total) => {
|
|
@@ -2788,11 +2640,11 @@ const useTranslations = () => {
|
|
|
2788
2640
|
} finally {
|
|
2789
2641
|
setIsBusy(!1), setImportProgress({ current: 0, total: 0, isImporting: !1 });
|
|
2790
2642
|
}
|
|
2791
|
-
}, [secrets, documents, locales,
|
|
2643
|
+
}, [secrets, documents, locales, client, branchId]), handlePublishAllTranslations = useCallback(async () => {
|
|
2792
2644
|
if (!secrets || documents.length === 0) return 0;
|
|
2793
2645
|
setIsBusy(!0);
|
|
2794
2646
|
try {
|
|
2795
|
-
const sourceDocumentIds = documents.map(getDocumentPublishedId), publishedDocumentIds = await
|
|
2647
|
+
const sourceDocumentIds = documents.map(getDocumentPublishedId), publishedDocumentIds = await client.fetch(
|
|
2796
2648
|
"*[_id in $sourceDocumentIds]._id",
|
|
2797
2649
|
{ sourceDocumentIds }
|
|
2798
2650
|
);
|
|
@@ -2802,7 +2654,7 @@ const useTranslations = () => {
|
|
|
2802
2654
|
status: "warning",
|
|
2803
2655
|
closable: !0
|
|
2804
2656
|
}), 0;
|
|
2805
|
-
const translationMetadata = await
|
|
2657
|
+
const translationMetadata = await client.fetch(TRANSLATION_DOCS_FOR_PUBLISH_QUERY, {
|
|
2806
2658
|
publishedDocumentIds,
|
|
2807
2659
|
sourceDocumentIds
|
|
2808
2660
|
}), translationDocIds = /* @__PURE__ */ new Set();
|
|
@@ -2820,7 +2672,7 @@ const useTranslations = () => {
|
|
|
2820
2672
|
}), 0;
|
|
2821
2673
|
const translatedDocumentIds = await publishTranslations(
|
|
2822
2674
|
publishableIds,
|
|
2823
|
-
|
|
2675
|
+
client
|
|
2824
2676
|
);
|
|
2825
2677
|
return toast.push({
|
|
2826
2678
|
title: `Published ${translatedDocumentIds.length} translation documents`,
|
|
@@ -2836,7 +2688,7 @@ const useTranslations = () => {
|
|
|
2836
2688
|
} finally {
|
|
2837
2689
|
setIsBusy(!1);
|
|
2838
2690
|
}
|
|
2839
|
-
}, [secrets, documents,
|
|
2691
|
+
}, [secrets, documents, client, branchId]);
|
|
2840
2692
|
useEffect(() => {
|
|
2841
2693
|
fetchDocuments();
|
|
2842
2694
|
}, [fetchDocuments]), useEffect(() => {
|
|
@@ -2923,7 +2775,7 @@ const useTranslations = () => {
|
|
|
2923
2775
|
] }) })
|
|
2924
2776
|
}
|
|
2925
2777
|
) : null;
|
|
2926
|
-
var version = "4.0.
|
|
2778
|
+
var version = "4.0.15";
|
|
2927
2779
|
function buildDebugInfo({
|
|
2928
2780
|
secrets,
|
|
2929
2781
|
preferences,
|
|
@@ -3001,11 +2853,11 @@ function summarizeTranslations({
|
|
|
3001
2853
|
missing: Math.max(0, expected - unique.length)
|
|
3002
2854
|
};
|
|
3003
2855
|
}
|
|
3004
|
-
async function collectTranslationSummary(documents,
|
|
2856
|
+
async function collectTranslationSummary(documents, client) {
|
|
3005
2857
|
const documentIds = documents.map(getDocumentPublishedId), targetLocales = pluginConfig.getLocales();
|
|
3006
2858
|
if (documentIds.length === 0)
|
|
3007
2859
|
return EMPTY_TRANSLATION_SUMMARY;
|
|
3008
|
-
const translatedIds = (await
|
|
2860
|
+
const translatedIds = (await client.fetch(
|
|
3009
2861
|
`*[_type == 'translation.metadata' && references($documentIds)]{
|
|
3010
2862
|
'targets': translations[language in $targetLocales && defined(value._ref)]{
|
|
3011
2863
|
language,
|
|
@@ -3022,7 +2874,7 @@ async function collectTranslationSummary(documents, client2) {
|
|
|
3022
2874
|
publishedIds: [],
|
|
3023
2875
|
draftIds: []
|
|
3024
2876
|
});
|
|
3025
|
-
const unique = Array.from(new Set(translatedIds)), existence = await
|
|
2877
|
+
const unique = Array.from(new Set(translatedIds)), existence = await client.fetch(
|
|
3026
2878
|
`{
|
|
3027
2879
|
'publishedIds': *[_id in $ids]._id,
|
|
3028
2880
|
'draftIds': *[_id in $draftIds]._id
|
|
@@ -3050,9 +2902,9 @@ const DebugInfoDialog = ({
|
|
|
3050
2902
|
autoPatchReferences,
|
|
3051
2903
|
autoPublish,
|
|
3052
2904
|
preserveExistingTranslations
|
|
3053
|
-
} = useTranslations(),
|
|
2905
|
+
} = useTranslations(), client = useClient(), [copied, setCopied] = useState(!1), [translations, setTranslations] = useState(
|
|
3054
2906
|
EMPTY_TRANSLATION_SUMMARY
|
|
3055
|
-
), { projectId, dataset } =
|
|
2907
|
+
), { projectId, dataset } = client.config(), serialized = useMemo(
|
|
3056
2908
|
() => formatDebugInfo(
|
|
3057
2909
|
buildDebugInfo({
|
|
3058
2910
|
secrets,
|
|
@@ -3085,14 +2937,14 @@ const DebugInfoDialog = ({
|
|
|
3085
2937
|
return useEffect(() => {
|
|
3086
2938
|
if (!isOpen) return;
|
|
3087
2939
|
let cancelled = !1;
|
|
3088
|
-
return collectTranslationSummary(documents,
|
|
2940
|
+
return collectTranslationSummary(documents, client).then((summary) => {
|
|
3089
2941
|
cancelled || setTranslations(summary);
|
|
3090
2942
|
}).catch(() => {
|
|
3091
2943
|
cancelled || setTranslations(EMPTY_TRANSLATION_SUMMARY);
|
|
3092
2944
|
}), () => {
|
|
3093
2945
|
cancelled = !0;
|
|
3094
2946
|
};
|
|
3095
|
-
}, [isOpen, documents,
|
|
2947
|
+
}, [isOpen, documents, client]), isOpen ? /* @__PURE__ */ jsx(
|
|
3096
2948
|
Dialog,
|
|
3097
2949
|
{
|
|
3098
2950
|
header: "Debug info",
|
|
@@ -3668,11 +3520,11 @@ function toNativeFieldType(fieldType) {
|
|
|
3668
3520
|
of: [{ type: "block" }]
|
|
3669
3521
|
} : fieldType;
|
|
3670
3522
|
}
|
|
3671
|
-
function buildInternationalizedArrayPlugin(config, sourceLocale, locales,
|
|
3523
|
+
function buildInternationalizedArrayPlugin(config, sourceLocale, locales, customMapping) {
|
|
3672
3524
|
const languageTitle = (locale) => config.getLanguageTitle?.(locale) ?? config.languageTitles?.[locale] ?? formatLocalePropertiesLabel(
|
|
3673
3525
|
locale,
|
|
3674
|
-
getLocaleProperties(locale, sourceLocale,
|
|
3675
|
-
|
|
3526
|
+
getLocaleProperties(locale, sourceLocale, customMapping),
|
|
3527
|
+
customMapping?.[locale]
|
|
3676
3528
|
), allLocales = Array.from(/* @__PURE__ */ new Set([sourceLocale, ...locales]));
|
|
3677
3529
|
return internationalizedArray({
|
|
3678
3530
|
languages: allLocales.map((id) => ({ id, title: languageTitle(id) })),
|
|
@@ -4282,7 +4134,7 @@ const TranslationTab = (props) => {
|
|
|
4282
4134
|
sourceLocale,
|
|
4283
4135
|
defaultLocale,
|
|
4284
4136
|
locales,
|
|
4285
|
-
customMapping
|
|
4137
|
+
customMapping,
|
|
4286
4138
|
apiKey,
|
|
4287
4139
|
projectId,
|
|
4288
4140
|
singletons,
|
|
@@ -4337,7 +4189,7 @@ const TranslationTab = (props) => {
|
|
|
4337
4189
|
}
|
|
4338
4190
|
), gt.setConfig({
|
|
4339
4191
|
sourceLocale: resolvedSourceLocale,
|
|
4340
|
-
customMapping
|
|
4192
|
+
customMapping,
|
|
4341
4193
|
apiKey,
|
|
4342
4194
|
projectId
|
|
4343
4195
|
});
|
|
@@ -4351,14 +4203,14 @@ const TranslationTab = (props) => {
|
|
|
4351
4203
|
const props = getLocaleProperties(
|
|
4352
4204
|
locale,
|
|
4353
4205
|
resolvedSourceLocale,
|
|
4354
|
-
|
|
4206
|
+
customMapping
|
|
4355
4207
|
);
|
|
4356
4208
|
return {
|
|
4357
4209
|
id: locale,
|
|
4358
4210
|
title: formatLocalePropertiesLabel(
|
|
4359
4211
|
locale,
|
|
4360
4212
|
props,
|
|
4361
|
-
|
|
4213
|
+
customMapping?.[locale]
|
|
4362
4214
|
)
|
|
4363
4215
|
};
|
|
4364
4216
|
});
|
|
@@ -4376,7 +4228,7 @@ const TranslationTab = (props) => {
|
|
|
4376
4228
|
fieldLevelConfig,
|
|
4377
4229
|
resolvedSourceLocale,
|
|
4378
4230
|
targetLocales,
|
|
4379
|
-
|
|
4231
|
+
customMapping
|
|
4380
4232
|
)
|
|
4381
4233
|
), {
|
|
4382
4234
|
name: "gt-sanity",
|