gt-sanity 4.0.11 → 4.0.13
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 +304 -114
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/adapter/api.test.ts +255 -0
- package/src/adapter/api.ts +292 -0
- package/src/adapter/core.ts +9 -4
- package/src/adapter/createTask.ts +3 -3
- package/src/adapter/getTranslation.ts +2 -2
- package/src/adapter/getTranslationTask.ts +2 -2
- package/src/components/TranslationsProvider.tsx +2 -2
- package/src/translation/__tests__/captureExistingTranslations.test.ts +2 -0
- package/src/translation/__tests__/initProject.test.ts +6 -6
- package/src/translation/captureExistingTranslations.ts +3 -3
- package/src/translation/checkTranslationStatus.ts +2 -2
- package/src/translation/createJobs.ts +4 -4
- package/src/translation/downloadTranslations.ts +2 -2
- package/src/translation/initProject.ts +4 -4
- package/src/translation/uploadFiles.ts +3 -3
- package/src/translation/uploadTranslations.ts +3 -3
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { GT, getLocaleProperties } from "generaltranslation";
|
|
2
|
-
import { libraryDefaultLocale, createDiagnosticMessage } from "generaltranslation/internal";
|
|
1
|
+
import { resolveCanonicalLocale, resolveAliasLocale, GT, getLocaleProperties } from "generaltranslation";
|
|
2
|
+
import { defaultBaseUrl, unwrapApiResult, decode as decode$1, libraryDefaultLocale, createDiagnosticMessage } from "generaltranslation/internal";
|
|
3
|
+
import { createApiClient, createBranch, getFileInfo, awaitJobs, generateProjectContext, processBatches, decodeFileContent, downloadFile, getTranslationStatus, enqueueFileTranslations, uploadTranslations as uploadTranslations$1, encodeFileContent, uploadSourceFiles, downloadFiles } from "generaltranslation/api";
|
|
3
4
|
import { useClient as useClient$1, useSchema, Preview, definePlugin } from "sanity";
|
|
4
5
|
import { useIntentLink, Link, route } from "sanity/router";
|
|
5
6
|
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
@@ -28,7 +29,195 @@ import { internationalizedArray } from "sanity-plugin-internationalized-array";
|
|
|
28
29
|
import { internationalizedArray as internationalizedArray2, internationalizedArrayLanguageFilter, isInternationalizedArrayItemType } from "sanity-plugin-internationalized-array";
|
|
29
30
|
import { documentInternationalization } from "@sanity/document-internationalization";
|
|
30
31
|
import { DocumentInternationalizationMenu, documentInternationalization as documentInternationalization2, useDeleteTranslationAction, useDocumentInternationalizationContext, useDuplicateWithTranslationsAction } from "@sanity/document-internationalization";
|
|
31
|
-
|
|
32
|
+
let client = createApiClient({ baseUrl: defaultBaseUrl }), customMapping;
|
|
33
|
+
function configureApiClient(config) {
|
|
34
|
+
const { customMapping: mapping, ...clientConfig } = config;
|
|
35
|
+
client = createApiClient({
|
|
36
|
+
baseUrl: defaultBaseUrl,
|
|
37
|
+
...clientConfig
|
|
38
|
+
}), customMapping = mapping;
|
|
39
|
+
}
|
|
40
|
+
const api = {
|
|
41
|
+
async uploadSourceFiles(files, options) {
|
|
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
|
+
},
|
|
113
|
+
async querySourceFile(query) {
|
|
114
|
+
const { fileId, ...queryParams } = query, result = unwrapApiResult(
|
|
115
|
+
await getTranslationStatus({
|
|
116
|
+
path: { fileId },
|
|
117
|
+
query: queryParams,
|
|
118
|
+
client
|
|
119
|
+
})
|
|
120
|
+
);
|
|
121
|
+
return {
|
|
122
|
+
...result,
|
|
123
|
+
translations: result.translations.map((translation) => ({
|
|
124
|
+
...translation,
|
|
125
|
+
locale: resolveAliasLocale(translation.locale, customMapping)
|
|
126
|
+
})),
|
|
127
|
+
sourceFile: {
|
|
128
|
+
...result.sourceFile,
|
|
129
|
+
sourceLocale: resolveAliasLocale(
|
|
130
|
+
result.sourceFile.sourceLocale,
|
|
131
|
+
customMapping
|
|
132
|
+
),
|
|
133
|
+
locales: result.sourceFile.locales.map(
|
|
134
|
+
(locale) => resolveAliasLocale(locale, customMapping)
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
async downloadFile(query) {
|
|
140
|
+
const { fileId, locale, ...queryParams } = query, response = unwrapApiResult(
|
|
141
|
+
await downloadFile({
|
|
142
|
+
path: { fileId },
|
|
143
|
+
query: {
|
|
144
|
+
...queryParams,
|
|
145
|
+
locale: locale ? resolveCanonicalLocale(locale, customMapping) : void 0
|
|
146
|
+
},
|
|
147
|
+
client
|
|
148
|
+
})
|
|
149
|
+
);
|
|
150
|
+
return decode$1(response.data);
|
|
151
|
+
},
|
|
152
|
+
async downloadFileBatch(files) {
|
|
153
|
+
if (files.length === 0) return { files: [], count: 0 };
|
|
154
|
+
const request = async (batch) => unwrapApiResult(
|
|
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 }));
|
|
219
|
+
}
|
|
220
|
+
}, SECRETS_NAMESPACE = "generaltranslation.secrets", SANITY_API_VERSION = "2025-09-15", gt = new GT(), DEFAULT_TRANSLATION_PREFERENCES = {
|
|
32
221
|
autoRefresh: !0,
|
|
33
222
|
autoImport: !0,
|
|
34
223
|
autoPatchReferences: !1,
|
|
@@ -39,10 +228,11 @@ const SECRETS_NAMESPACE = "generaltranslation.secrets", SANITY_API_VERSION = "20
|
|
|
39
228
|
preserveExistingTranslations: !1
|
|
40
229
|
};
|
|
41
230
|
function overrideConfig(secrets) {
|
|
42
|
-
|
|
43
|
-
...secrets?.project && { projectId: secrets
|
|
44
|
-
...secrets?.secret && { apiKey: secrets
|
|
45
|
-
}
|
|
231
|
+
const config = {
|
|
232
|
+
...secrets?.project && { projectId: secrets.project },
|
|
233
|
+
...secrets?.secret && { apiKey: secrets.secret }
|
|
234
|
+
};
|
|
235
|
+
gt.setConfig(config), configureApiClient({ ...config, customMapping: gt.customMapping });
|
|
46
236
|
}
|
|
47
237
|
class GTConfig {
|
|
48
238
|
secretsNamespace;
|
|
@@ -168,10 +358,10 @@ function formatLocalePropertiesLabel(code, properties, mapping) {
|
|
|
168
358
|
}
|
|
169
359
|
const useClient = () => useClient$1({ apiVersion: SANITY_API_VERSION });
|
|
170
360
|
function useSecrets(id) {
|
|
171
|
-
const [loading, setLoading] = useState(!0), [secrets, setSecrets] = useState(null),
|
|
361
|
+
const [loading, setLoading] = useState(!0), [secrets, setSecrets] = useState(null), client2 = useClient();
|
|
172
362
|
return useEffect(() => {
|
|
173
363
|
function fetchData() {
|
|
174
|
-
|
|
364
|
+
client2.fetch("* [_id == $id][0]", { id }).then((doc) => {
|
|
175
365
|
const result = {};
|
|
176
366
|
for (const key in doc)
|
|
177
367
|
key[0] !== "_" && (result[key] = doc[key]);
|
|
@@ -179,7 +369,7 @@ function useSecrets(id) {
|
|
|
179
369
|
});
|
|
180
370
|
}
|
|
181
371
|
fetchData();
|
|
182
|
-
}, [id,
|
|
372
|
+
}, [id, client2]), { loading, secrets };
|
|
183
373
|
}
|
|
184
374
|
const gtRadius = [0, 4, 6, 8, 12, 16, 24], baseTheme = buildTheme(), theme = {
|
|
185
375
|
...baseTheme,
|
|
@@ -288,18 +478,18 @@ function createStableTranslationKey(branchId, documentId, localeId) {
|
|
|
288
478
|
const publishedId = getPublishedId(documentId);
|
|
289
479
|
return branchId ? `${branchId}:${publishedId}:${localeId}` : `${publishedId}:${localeId}`;
|
|
290
480
|
}
|
|
291
|
-
const findLatestDraft = (documentId,
|
|
481
|
+
const findLatestDraft = (documentId, client2) => {
|
|
292
482
|
const publishedId = getPublishedId(documentId), query = "*[_id == $id || _id == $draftId]", params = { id: publishedId, draftId: `drafts.${publishedId}` };
|
|
293
|
-
return
|
|
483
|
+
return client2.fetch(query, params).then(
|
|
294
484
|
(docs) => docs.find((doc) => doc._id.startsWith("drafts.")) ?? docs[0]
|
|
295
485
|
);
|
|
296
|
-
}, findDocumentAtRevision = async (documentId, rev,
|
|
297
|
-
const baseUrl = `/data/history/${
|
|
486
|
+
}, findDocumentAtRevision = async (documentId, rev, client2) => {
|
|
487
|
+
const baseUrl = `/data/history/${client2.config().dataset}/documents/${documentId}?revision=${rev}`, url = client2.getUrl(baseUrl);
|
|
298
488
|
return await fetch(url, { credentials: "include" }).then((req) => req.json()).then((req) => req.documents && req.documents.length ? req.documents[0] : null);
|
|
299
489
|
};
|
|
300
|
-
async function requireSourceDocument(documentId, versionId,
|
|
490
|
+
async function requireSourceDocument(documentId, versionId, client2) {
|
|
301
491
|
let doc = null;
|
|
302
|
-
if (documentId && versionId && (doc = await findDocumentAtRevision(documentId, versionId,
|
|
492
|
+
if (documentId && versionId && (doc = await findDocumentAtRevision(documentId, versionId, client2)), doc || (doc = await findLatestDraft(documentId, client2)), !doc)
|
|
303
493
|
throw new Error(missingDocumentDiagnostic(documentId));
|
|
304
494
|
return doc;
|
|
305
495
|
}
|
|
@@ -399,7 +589,7 @@ function metadataTranslations(languageCondition, extraCondition) {
|
|
|
399
589
|
function metadataTranslationRef(languageExpression) {
|
|
400
590
|
return `${metadataTranslations(`== ${languageExpression}`)}[0].value._ref`;
|
|
401
591
|
}
|
|
402
|
-
async function createI18nDocAndPatchMetadata(sourceDocument, translatedDoc, localeId,
|
|
592
|
+
async function createI18nDocAndPatchMetadata(sourceDocument, translatedDoc, localeId, client2, translationMetadata, sourceDocumentId, languageField = "language") {
|
|
403
593
|
const publishedSourceDocumentId = getPublishedId(sourceDocumentId);
|
|
404
594
|
translatedDoc[languageField] = localeId;
|
|
405
595
|
const existingLocaleKey = translationMetadata.translations.find(
|
|
@@ -419,19 +609,19 @@ async function createI18nDocAndPatchMetadata(sourceDocument, translatedDoc, loca
|
|
|
419
609
|
publishedSourceDocumentId,
|
|
420
610
|
localeId
|
|
421
611
|
);
|
|
422
|
-
createDocumentPromise =
|
|
612
|
+
createDocumentPromise = client2.create({
|
|
423
613
|
...appliedDocument,
|
|
424
614
|
_type: rest._type,
|
|
425
615
|
_id: `drafts.${translatedDocId}`
|
|
426
616
|
});
|
|
427
617
|
} else
|
|
428
|
-
createDocumentPromise =
|
|
618
|
+
createDocumentPromise = client2.create({
|
|
429
619
|
...appliedDocument,
|
|
430
620
|
_type: rest._type,
|
|
431
621
|
_id: "drafts."
|
|
432
622
|
});
|
|
433
623
|
const doc = await createDocumentPromise, _ref = getPublishedId(doc._id);
|
|
434
|
-
await
|
|
624
|
+
await client2.transaction().patch(
|
|
435
625
|
translationMetadata._id,
|
|
436
626
|
(p) => p.insert(operation, location, [
|
|
437
627
|
{
|
|
@@ -450,8 +640,8 @@ async function createI18nDocAndPatchMetadata(sourceDocument, translatedDoc, loca
|
|
|
450
640
|
])
|
|
451
641
|
).commit();
|
|
452
642
|
}
|
|
453
|
-
const getOrCreateTranslationMetadata = async (documentId, baseDocument,
|
|
454
|
-
const publishedId = getPublishedId(documentId), existingMetadata = await
|
|
643
|
+
const getOrCreateTranslationMetadata = async (documentId, baseDocument, client2, baseLanguage) => {
|
|
644
|
+
const publishedId = getPublishedId(documentId), existingMetadata = await client2.fetch(
|
|
455
645
|
`*[
|
|
456
646
|
_type == '${TRANSLATION_METADATA_TYPE}' &&
|
|
457
647
|
${metadataTranslationRef("$baseLanguage")} == $id
|
|
@@ -479,13 +669,13 @@ const getOrCreateTranslationMetadata = async (documentId, baseDocument, client,
|
|
|
479
669
|
}
|
|
480
670
|
});
|
|
481
671
|
try {
|
|
482
|
-
return await
|
|
672
|
+
return await client2.createIfNotExists({
|
|
483
673
|
_id: translationMetadataId(publishedId),
|
|
484
674
|
_type: TRANSLATION_METADATA_TYPE,
|
|
485
675
|
translations: [baseLangEntry]
|
|
486
676
|
});
|
|
487
677
|
} catch (error) {
|
|
488
|
-
const metadata = await
|
|
678
|
+
const metadata = await client2.fetch(
|
|
489
679
|
`*[
|
|
490
680
|
_type == '${TRANSLATION_METADATA_TYPE}' &&
|
|
491
681
|
${metadataTranslationRef("$baseLanguage")} == $id
|
|
@@ -497,7 +687,7 @@ const getOrCreateTranslationMetadata = async (documentId, baseDocument, client,
|
|
|
497
687
|
throw error;
|
|
498
688
|
}
|
|
499
689
|
}, SYSTEM_FIELDS = ["_id", "_rev", "_updatedAt", "language"], isSystemField = (field) => SYSTEM_FIELDS.includes(field);
|
|
500
|
-
async function patchI18nDoc(sourceDocumentId, i18nDocId, sourceDocument, mergedDocument, translatedFields,
|
|
690
|
+
async function patchI18nDoc(sourceDocumentId, i18nDocId, sourceDocument, mergedDocument, translatedFields, client2, existingDocument) {
|
|
501
691
|
const cleanedMerge = {};
|
|
502
692
|
Object.entries(mergedDocument).forEach(([key, value]) => {
|
|
503
693
|
key in translatedFields && //don't overwrite any existing system values on the i18n doc
|
|
@@ -522,36 +712,36 @@ async function patchI18nDoc(sourceDocumentId, i18nDocId, sourceDocument, mergedD
|
|
|
522
712
|
JSONPointer.set(appliedDocument, result.pointer, result.value);
|
|
523
713
|
}
|
|
524
714
|
), i18nDocId.startsWith("drafts.")) {
|
|
525
|
-
await
|
|
715
|
+
await client2.patch(i18nDocId, { set: appliedDocument }).commit();
|
|
526
716
|
return;
|
|
527
717
|
}
|
|
528
|
-
const seed = existingDocument ?? await
|
|
718
|
+
const seed = existingDocument ?? await client2.getDocument(i18nDocId);
|
|
529
719
|
if (!seed) {
|
|
530
|
-
await
|
|
720
|
+
await client2.patch(i18nDocId, { set: appliedDocument }).commit();
|
|
531
721
|
return;
|
|
532
722
|
}
|
|
533
723
|
const draftId = `drafts.${getPublishedId(i18nDocId)}`;
|
|
534
|
-
await
|
|
724
|
+
await client2.transaction().createIfNotExists({ ...seed, _id: draftId }).patch(draftId, (patch) => patch.set(appliedDocument)).commit();
|
|
535
725
|
}
|
|
536
|
-
const documentLevelPatch = async (docInfo, translatedFields, localeId,
|
|
726
|
+
const documentLevelPatch = async (docInfo, translatedFields, localeId, client2, languageField = "language", mergeWithTargetLocale = !1) => {
|
|
537
727
|
const baseLanguage = pluginConfig.getSourceLocale();
|
|
538
728
|
let i18nDoc, baseDoc = await requireSourceDocument(
|
|
539
729
|
docInfo.documentId,
|
|
540
730
|
docInfo.versionId,
|
|
541
|
-
|
|
731
|
+
client2
|
|
542
732
|
);
|
|
543
733
|
const i18nDocId = (await getOrCreateTranslationMetadata(
|
|
544
734
|
docInfo.documentId,
|
|
545
735
|
baseDoc,
|
|
546
|
-
|
|
736
|
+
client2,
|
|
547
737
|
baseLanguage
|
|
548
738
|
)).translations.find(
|
|
549
739
|
(translation) => translation.language === localeId
|
|
550
740
|
)?.value?._ref;
|
|
551
|
-
i18nDocId && (i18nDoc = await findLatestDraft(i18nDocId,
|
|
741
|
+
i18nDocId && (i18nDoc = await findLatestDraft(i18nDocId, client2)), mergeWithTargetLocale && i18nDoc ? baseDoc = i18nDoc : docInfo.documentId && docInfo.versionId && (baseDoc = await requireSourceDocument(
|
|
552
742
|
docInfo.documentId,
|
|
553
743
|
docInfo.versionId,
|
|
554
|
-
|
|
744
|
+
client2
|
|
555
745
|
));
|
|
556
746
|
const merged = BaseDocumentMerger.documentLevelMerge(
|
|
557
747
|
translatedFields,
|
|
@@ -564,18 +754,18 @@ const documentLevelPatch = async (docInfo, translatedFields, localeId, client, l
|
|
|
564
754
|
baseDoc,
|
|
565
755
|
merged,
|
|
566
756
|
translatedFields,
|
|
567
|
-
|
|
757
|
+
client2,
|
|
568
758
|
i18nDoc
|
|
569
759
|
);
|
|
570
760
|
else {
|
|
571
761
|
const freshTranslationMetadata = await getOrCreateTranslationMetadata(
|
|
572
762
|
docInfo.documentId,
|
|
573
763
|
baseDoc,
|
|
574
|
-
|
|
764
|
+
client2,
|
|
575
765
|
baseLanguage
|
|
576
766
|
), freshI18nDocId = freshTranslationMetadata.translations.find(
|
|
577
767
|
(translation) => translation.language === localeId
|
|
578
|
-
)?.value?._ref, freshI18nDoc = freshI18nDocId ? await findLatestDraft(freshI18nDocId,
|
|
768
|
+
)?.value?._ref, freshI18nDoc = freshI18nDocId ? await findLatestDraft(freshI18nDocId, client2) : void 0;
|
|
579
769
|
if (freshI18nDoc) {
|
|
580
770
|
await patchI18nDoc(
|
|
581
771
|
docInfo.documentId,
|
|
@@ -583,7 +773,7 @@ const documentLevelPatch = async (docInfo, translatedFields, localeId, client, l
|
|
|
583
773
|
baseDoc,
|
|
584
774
|
merged,
|
|
585
775
|
translatedFields,
|
|
586
|
-
|
|
776
|
+
client2,
|
|
587
777
|
freshI18nDoc
|
|
588
778
|
);
|
|
589
779
|
return;
|
|
@@ -592,7 +782,7 @@ const documentLevelPatch = async (docInfo, translatedFields, localeId, client, l
|
|
|
592
782
|
baseDoc,
|
|
593
783
|
merged,
|
|
594
784
|
localeId,
|
|
595
|
-
|
|
785
|
+
client2,
|
|
596
786
|
freshTranslationMetadata,
|
|
597
787
|
docInfo.documentId,
|
|
598
788
|
languageField
|
|
@@ -682,8 +872,8 @@ function mergeInternationalizedArrays(baseDoc, translatedFields, targetLocale, s
|
|
|
682
872
|
}
|
|
683
873
|
return changes;
|
|
684
874
|
}
|
|
685
|
-
const internationalizedArrayPatch = async (docInfo, translatedFields, localeId,
|
|
686
|
-
const sourceLocale = pluginConfig.getSourceLocale(), baseDoc = await findLatestDraft(docInfo.documentId,
|
|
875
|
+
const internationalizedArrayPatch = async (docInfo, translatedFields, localeId, client2) => {
|
|
876
|
+
const sourceLocale = pluginConfig.getSourceLocale(), baseDoc = await findLatestDraft(docInfo.documentId, client2);
|
|
687
877
|
if (!baseDoc)
|
|
688
878
|
return;
|
|
689
879
|
const changes = mergeInternationalizedArrays(
|
|
@@ -695,11 +885,11 @@ const internationalizedArrayPatch = async (docInfo, translatedFields, localeId,
|
|
|
695
885
|
if (Object.keys(changes).length === 0)
|
|
696
886
|
return;
|
|
697
887
|
if (baseDoc._id.startsWith("drafts.")) {
|
|
698
|
-
await
|
|
888
|
+
await client2.patch(baseDoc._id).set(changes).commit();
|
|
699
889
|
return;
|
|
700
890
|
}
|
|
701
891
|
const draftId = `drafts.${getPublishedId(baseDoc._id)}`;
|
|
702
|
-
await
|
|
892
|
+
await client2.transaction().createIfNotExists({ ...baseDoc, _id: draftId }).patch(draftId, (patch) => patch.set(changes)).commit();
|
|
703
893
|
}, defaultSchema = Schema.compile({
|
|
704
894
|
name: "default",
|
|
705
895
|
types: [
|
|
@@ -1297,18 +1487,18 @@ function stripIgnoredFields(document2, ignoreFields, dedupeFields) {
|
|
|
1297
1487
|
const documentAdapter = {
|
|
1298
1488
|
level: "document",
|
|
1299
1489
|
serialize: (document2, schema, baseLanguage) => serializeDocument(document2, schema, baseLanguage, "document"),
|
|
1300
|
-
patch: (docInfo, deserialized, localeId,
|
|
1490
|
+
patch: (docInfo, deserialized, localeId, client2, mergeWithTargetLocale) => documentLevelPatch(
|
|
1301
1491
|
docInfo,
|
|
1302
1492
|
deserialized,
|
|
1303
1493
|
localeId,
|
|
1304
|
-
|
|
1494
|
+
client2,
|
|
1305
1495
|
pluginConfig.getLanguageField(),
|
|
1306
1496
|
mergeWithTargetLocale
|
|
1307
1497
|
)
|
|
1308
1498
|
}, internationalizedArrayAdapter = {
|
|
1309
1499
|
level: "internationalizedArray",
|
|
1310
1500
|
serialize: (document2, schema, baseLanguage) => serializeDocument(document2, schema, baseLanguage, "internationalizedArray"),
|
|
1311
|
-
patch: (docInfo, deserialized, localeId,
|
|
1501
|
+
patch: (docInfo, deserialized, localeId, client2) => internationalizedArrayPatch(docInfo, deserialized, localeId, client2)
|
|
1312
1502
|
};
|
|
1313
1503
|
function matchesFieldLevel(type) {
|
|
1314
1504
|
return type ? pluginConfig.getFieldLevelDocuments().some((filter) => filter.type === type) : !1;
|
|
@@ -1321,7 +1511,7 @@ function getTranslationStrategy(document2) {
|
|
|
1321
1511
|
return getTranslationStrategyForType(document2._type);
|
|
1322
1512
|
}
|
|
1323
1513
|
async function uploadFiles(documents, secrets) {
|
|
1324
|
-
return overrideConfig(secrets), await
|
|
1514
|
+
return overrideConfig(secrets), await api.uploadSourceFiles(
|
|
1325
1515
|
documents.map(({ info, serializedDocument }) => ({
|
|
1326
1516
|
source: {
|
|
1327
1517
|
content: serializedDocument.content,
|
|
@@ -1339,11 +1529,11 @@ async function uploadFiles(documents, secrets) {
|
|
|
1339
1529
|
}
|
|
1340
1530
|
async function initProject(uploadResult, options, secrets) {
|
|
1341
1531
|
overrideConfig(secrets);
|
|
1342
|
-
const timeoutVal = Number(options.timeout), setupTimeoutSeconds = Number.isFinite(timeoutVal) ? timeoutVal : 600, setupResult = await
|
|
1532
|
+
const timeoutVal = Number(options.timeout), setupTimeoutSeconds = Number.isFinite(timeoutVal) ? timeoutVal : 600, setupResult = await api.setupProject(uploadResult.uploadedFiles, {
|
|
1343
1533
|
locales: pluginConfig.getLocales()
|
|
1344
1534
|
});
|
|
1345
1535
|
if (setupResult.status === "queued") {
|
|
1346
|
-
const { complete, jobs } = await
|
|
1536
|
+
const { complete, jobs } = await api.awaitJobs([setupResult.setupJobId], {
|
|
1347
1537
|
pollingIntervalSeconds: 2,
|
|
1348
1538
|
timeoutSeconds: setupTimeoutSeconds
|
|
1349
1539
|
}), [job] = jobs;
|
|
@@ -1356,13 +1546,13 @@ async function initProject(uploadResult, options, secrets) {
|
|
|
1356
1546
|
return !0;
|
|
1357
1547
|
}
|
|
1358
1548
|
async function createJobs(uploadResult, localeIds, secrets, force = !1) {
|
|
1359
|
-
return overrideConfig(secrets), await
|
|
1549
|
+
return overrideConfig(secrets), await api.enqueueFiles(uploadResult.uploadedFiles, {
|
|
1360
1550
|
sourceLocale: gt.sourceLocale || libraryDefaultLocale,
|
|
1361
1551
|
targetLocales: localeIds,
|
|
1362
1552
|
force
|
|
1363
1553
|
});
|
|
1364
1554
|
}
|
|
1365
|
-
async function collectExistingTranslations(documents, targetLocaleIds, { client, schema }) {
|
|
1555
|
+
async function collectExistingTranslations(documents, targetLocaleIds, { client: client2, schema }) {
|
|
1366
1556
|
const existing = /* @__PURE__ */ new Map();
|
|
1367
1557
|
if (documents.length === 0 || targetLocaleIds.length === 0)
|
|
1368
1558
|
return existing;
|
|
@@ -1396,7 +1586,7 @@ async function collectExistingTranslations(documents, targetLocaleIds, { client,
|
|
|
1396
1586
|
localeIds,
|
|
1397
1587
|
sourceLocale,
|
|
1398
1588
|
languageField,
|
|
1399
|
-
|
|
1589
|
+
client2,
|
|
1400
1590
|
schema
|
|
1401
1591
|
);
|
|
1402
1592
|
for (const [sourceDocId, translations] of documentLevel)
|
|
@@ -1404,7 +1594,7 @@ async function collectExistingTranslations(documents, targetLocaleIds, { client,
|
|
|
1404
1594
|
}
|
|
1405
1595
|
return existing;
|
|
1406
1596
|
}
|
|
1407
|
-
async function collectDocumentLevelTranslations(documents, localeIds, sourceLocale, languageField,
|
|
1597
|
+
async function collectDocumentLevelTranslations(documents, localeIds, sourceLocale, languageField, client2, schema) {
|
|
1408
1598
|
const existing = /* @__PURE__ */ new Map(), sourceDocIds = documents.map(getDocumentPublishedId), query = `*[
|
|
1409
1599
|
_type == '${TRANSLATION_METADATA_TYPE}' &&
|
|
1410
1600
|
${metadataTranslationRef("$sourceLocale")} in $sourceDocIds
|
|
@@ -1414,7 +1604,7 @@ async function collectDocumentLevelTranslations(documents, localeIds, sourceLoca
|
|
|
1414
1604
|
language,
|
|
1415
1605
|
'docId': value._ref
|
|
1416
1606
|
}
|
|
1417
|
-
}`, metadataRows = await
|
|
1607
|
+
}`, metadataRows = await client2.fetch(query, {
|
|
1418
1608
|
sourceLocale,
|
|
1419
1609
|
sourceDocIds,
|
|
1420
1610
|
localeIds
|
|
@@ -1435,7 +1625,7 @@ async function collectDocumentLevelTranslations(documents, localeIds, sourceLoca
|
|
|
1435
1625
|
);
|
|
1436
1626
|
if (translatedDocIds.length === 0)
|
|
1437
1627
|
return existing;
|
|
1438
|
-
const translatedDocs = await
|
|
1628
|
+
const translatedDocs = await client2.fetch(
|
|
1439
1629
|
"*[_id in $ids || _id in $draftIds]",
|
|
1440
1630
|
{
|
|
1441
1631
|
ids: translatedDocIds,
|
|
@@ -1478,7 +1668,7 @@ async function captureExistingTranslations({
|
|
|
1478
1668
|
if (documents.length === 0 || localeIds.length === 0)
|
|
1479
1669
|
return EMPTY_RESULT;
|
|
1480
1670
|
overrideConfig(secrets);
|
|
1481
|
-
const sourceLocale = gt.sourceLocale || libraryDefaultLocale, seedFiles = await
|
|
1671
|
+
const sourceLocale = gt.sourceLocale || libraryDefaultLocale, seedFiles = await api.downloadFileBatch(
|
|
1482
1672
|
documents.map((document2) => ({
|
|
1483
1673
|
fileId: getDocumentPublishedId(document2),
|
|
1484
1674
|
branchId
|
|
@@ -1515,7 +1705,7 @@ async function captureExistingTranslations({
|
|
|
1515
1705
|
];
|
|
1516
1706
|
}
|
|
1517
1707
|
);
|
|
1518
|
-
return files.length === 0 ? EMPTY_RESULT : (await
|
|
1708
|
+
return files.length === 0 ? EMPTY_RESULT : (await api.uploadTranslations(files, { sourceLocale }), {
|
|
1519
1709
|
capturedCount: files.reduce(
|
|
1520
1710
|
(total, file) => total + file.translations.length,
|
|
1521
1711
|
0
|
|
@@ -1531,7 +1721,7 @@ async function uploadTranslations(documents, secrets) {
|
|
|
1531
1721
|
return null;
|
|
1532
1722
|
overrideConfig(secrets);
|
|
1533
1723
|
const sourceLocale = gt.sourceLocale || libraryDefaultLocale;
|
|
1534
|
-
return await
|
|
1724
|
+
return await api.uploadTranslations(
|
|
1535
1725
|
withTranslations.map(({ info, serializedDocument, translations }) => ({
|
|
1536
1726
|
source: {
|
|
1537
1727
|
content: serializedDocument.content,
|
|
@@ -1558,7 +1748,7 @@ async function downloadTranslations(files, secrets, maxRetries = 3, retryDelay =
|
|
|
1558
1748
|
let retries = 0;
|
|
1559
1749
|
for (; retries <= maxRetries; )
|
|
1560
1750
|
try {
|
|
1561
|
-
return (await
|
|
1751
|
+
return (await api.downloadFileBatch(
|
|
1562
1752
|
files.map((file) => ({
|
|
1563
1753
|
fileId: file.fileId,
|
|
1564
1754
|
branchId: file.branchId,
|
|
@@ -1587,7 +1777,7 @@ async function checkTranslationStatus(fileQueryData, downloadStatus, secrets) {
|
|
|
1587
1777
|
);
|
|
1588
1778
|
return !downloadStatus.downloaded.has(statusKey) && !downloadStatus.downloaded.has(stableKey) && !downloadStatus.failed.has(statusKey) && !downloadStatus.failed.has(stableKey) && !downloadStatus.skipped.has(statusKey) && !downloadStatus.skipped.has(stableKey);
|
|
1589
1779
|
});
|
|
1590
|
-
return currentQueryData.length === 0 ? !0 : ((await
|
|
1780
|
+
return currentQueryData.length === 0 ? !0 : ((await api.queryFileData({
|
|
1591
1781
|
translatedFiles: currentQueryData
|
|
1592
1782
|
})).translatedFiles || []).filter(
|
|
1593
1783
|
(translation) => translation.completedAt
|
|
@@ -1597,30 +1787,30 @@ async function checkTranslationStatus(fileQueryData, downloadStatus, secrets) {
|
|
|
1597
1787
|
}
|
|
1598
1788
|
}
|
|
1599
1789
|
async function importDocument(docInfo, localeId, document2, context, mergeWithTargetLocale = !1) {
|
|
1600
|
-
const { client } = context, deserialized = deserializeDocument(document2);
|
|
1790
|
+
const { client: client2 } = context, deserialized = deserializeDocument(document2);
|
|
1601
1791
|
return getTranslationStrategyForType(
|
|
1602
1792
|
deserialized._type
|
|
1603
1793
|
).patch(
|
|
1604
1794
|
docInfo,
|
|
1605
1795
|
deserialized,
|
|
1606
1796
|
localeId,
|
|
1607
|
-
|
|
1797
|
+
client2,
|
|
1608
1798
|
mergeWithTargetLocale
|
|
1609
1799
|
);
|
|
1610
1800
|
}
|
|
1611
1801
|
const isRecord = (value) => typeof value == "object" && value !== null && !Array.isArray(value), isReference = (value) => isRecord(value) && value._type === "reference" && typeof value._ref == "string";
|
|
1612
|
-
async function resolveRefs(doc, locale,
|
|
1802
|
+
async function resolveRefs(doc, locale, client2) {
|
|
1613
1803
|
const references = findReferences(doc);
|
|
1614
1804
|
if (references.length === 0)
|
|
1615
1805
|
return doc;
|
|
1616
1806
|
const translatedRefs = await resolveTranslatedReferences(
|
|
1617
1807
|
references,
|
|
1618
1808
|
locale,
|
|
1619
|
-
|
|
1809
|
+
client2
|
|
1620
1810
|
);
|
|
1621
1811
|
return updateDocumentReferences(doc, translatedRefs);
|
|
1622
1812
|
}
|
|
1623
|
-
async function commitResolvedRefs(translatedDoc, resolvedDoc,
|
|
1813
|
+
async function commitResolvedRefs(translatedDoc, resolvedDoc, client2) {
|
|
1624
1814
|
const {
|
|
1625
1815
|
_id: _resolvedId,
|
|
1626
1816
|
_rev: _resolvedRev,
|
|
@@ -1629,11 +1819,11 @@ async function commitResolvedRefs(translatedDoc, resolvedDoc, client) {
|
|
|
1629
1819
|
...changes
|
|
1630
1820
|
} = resolvedDoc;
|
|
1631
1821
|
if (translatedDoc._id.startsWith("drafts.")) {
|
|
1632
|
-
await
|
|
1822
|
+
await client2.patch(translatedDoc._id).set(changes).commit();
|
|
1633
1823
|
return;
|
|
1634
1824
|
}
|
|
1635
1825
|
const draftId = `drafts.${getPublishedId(translatedDoc._id)}`;
|
|
1636
|
-
await
|
|
1826
|
+
await client2.transaction().createIfNotExists({ ...translatedDoc, _id: draftId }).patch(draftId, (patch) => patch.set(changes)).commit();
|
|
1637
1827
|
}
|
|
1638
1828
|
function findReferences(obj, path = []) {
|
|
1639
1829
|
if (!obj || typeof obj != "object")
|
|
@@ -1645,14 +1835,14 @@ function findReferences(obj, path = []) {
|
|
|
1645
1835
|
references.push(...findReferences(obj[key], [...path, key]));
|
|
1646
1836
|
}), references;
|
|
1647
1837
|
}
|
|
1648
|
-
async function resolveTranslatedReferences(references, locale,
|
|
1838
|
+
async function resolveTranslatedReferences(references, locale, client2) {
|
|
1649
1839
|
const refIds = references.map((r) => r.ref._ref), translatedRefs = /* @__PURE__ */ new Map();
|
|
1650
1840
|
if (refIds.length === 0)
|
|
1651
1841
|
return translatedRefs;
|
|
1652
1842
|
const sourceLocale = pluginConfig.getSourceLocale(), query = `*[_type == "${TRANSLATION_METADATA_TYPE}" && count(${metadataTranslations("== $sourceLocale", "value._ref in $refIds")}) > 0] {
|
|
1653
1843
|
"originalRef": ${metadataTranslationRef("$sourceLocale")},
|
|
1654
1844
|
"translatedRefs": ${metadataTranslations("== $locale")}.value._ref
|
|
1655
|
-
}[defined(originalRef) && count(translatedRefs) > 0]`, translationPairs = await
|
|
1845
|
+
}[defined(originalRef) && count(translatedRefs) > 0]`, translationPairs = await client2.fetch(query, { refIds, sourceLocale, locale });
|
|
1656
1846
|
for (const { originalRef, translatedRefs: localeRefs } of translationPairs) {
|
|
1657
1847
|
const translatedRef = localeRefs[localeRefs.length - 1];
|
|
1658
1848
|
translatedRefs.set(originalRef, translatedRef);
|
|
@@ -1680,8 +1870,8 @@ function updateReferencesRecursive(obj, translatedRefs) {
|
|
|
1680
1870
|
);
|
|
1681
1871
|
}), updated;
|
|
1682
1872
|
}
|
|
1683
|
-
async function findTranslatedDocumentForLocale(sourceDocumentId, localeId,
|
|
1684
|
-
const cleanDocId = getPublishedId(sourceDocumentId), translatedDocIds = await
|
|
1873
|
+
async function findTranslatedDocumentForLocale(sourceDocumentId, localeId, client2) {
|
|
1874
|
+
const cleanDocId = getPublishedId(sourceDocumentId), translatedDocIds = await client2.fetch(`*[
|
|
1685
1875
|
_type == "translation.metadata" &&
|
|
1686
1876
|
(
|
|
1687
1877
|
translations[language == $sourceLocale][0].value._ref == $cleanDocId
|
|
@@ -1692,11 +1882,11 @@ async function findTranslatedDocumentForLocale(sourceDocumentId, localeId, clien
|
|
|
1692
1882
|
cleanDocId,
|
|
1693
1883
|
localeId
|
|
1694
1884
|
}), translatedDocId = translatedDocIds?.[translatedDocIds.length - 1];
|
|
1695
|
-
return translatedDocId && await findLatestDraft(translatedDocId,
|
|
1885
|
+
return translatedDocId && await findLatestDraft(translatedDocId, client2) || null;
|
|
1696
1886
|
}
|
|
1697
|
-
async function findDocument(documentId,
|
|
1887
|
+
async function findDocument(documentId, client2) {
|
|
1698
1888
|
const query = "*[_id == $id]", params = { id: documentId };
|
|
1699
|
-
return (await
|
|
1889
|
+
return (await client2.fetch(query, params))[0] || null;
|
|
1700
1890
|
}
|
|
1701
1891
|
async function processBatch(items, processor, options = {}) {
|
|
1702
1892
|
const {
|
|
@@ -1833,9 +2023,9 @@ const TRANSLATION_DOCS_FOR_PUBLISH_QUERY = `*[
|
|
|
1833
2023
|
'docId': value._ref
|
|
1834
2024
|
}
|
|
1835
2025
|
}`;
|
|
1836
|
-
async function publishDocument(documentId,
|
|
2026
|
+
async function publishDocument(documentId, client2) {
|
|
1837
2027
|
try {
|
|
1838
|
-
documentId.startsWith("drafts.") && await
|
|
2028
|
+
documentId.startsWith("drafts.") && await client2.action(
|
|
1839
2029
|
{
|
|
1840
2030
|
actionType: "sanity.action.document.publish",
|
|
1841
2031
|
draftId: documentId,
|
|
@@ -1847,13 +2037,13 @@ async function publishDocument(documentId, client) {
|
|
|
1847
2037
|
console.error("Error publishing document", error);
|
|
1848
2038
|
}
|
|
1849
2039
|
}
|
|
1850
|
-
async function publishTranslations(documentIds,
|
|
2040
|
+
async function publishTranslations(documentIds, client2) {
|
|
1851
2041
|
const publishedDocumentIds = [];
|
|
1852
2042
|
return await processBatch(
|
|
1853
2043
|
documentIds,
|
|
1854
2044
|
async (documentId) => {
|
|
1855
|
-
const document2 = await findDocument(`drafts.${documentId}`,
|
|
1856
|
-
return document2 ? (await publishDocument(document2._id,
|
|
2045
|
+
const document2 = await findDocument(`drafts.${documentId}`, client2);
|
|
2046
|
+
return document2 ? (await publishDocument(document2._id, client2), publishedDocumentIds.push(documentId), { documentId, published: !0 }) : { documentId, published: !1 };
|
|
1857
2047
|
},
|
|
1858
2048
|
{
|
|
1859
2049
|
onItemFailure: (documentId, error) => {
|
|
@@ -1939,7 +2129,7 @@ const useTranslations = () => {
|
|
|
1939
2129
|
skipped: /* @__PURE__ */ new Set()
|
|
1940
2130
|
}), downloadStatusRef = useRef(downloadStatus), [translationStatuses, setTranslationStatuses] = useState(/* @__PURE__ */ new Map()), [pendingTranslations, setPendingTranslations] = useState(
|
|
1941
2131
|
/* @__PURE__ */ new Set()
|
|
1942
|
-
), [importingTranslations, setImportingTranslations] = useState(/* @__PURE__ */ new Set()), [isRefreshing, setIsRefreshing] = useState(!1),
|
|
2132
|
+
), [importingTranslations, setImportingTranslations] = useState(/* @__PURE__ */ new Set()), [isRefreshing, setIsRefreshing] = useState(!1), client2 = useClient(), { projectId, dataset } = client2.config(), uploadedVersionsStorageKey = getUploadedVersionsStorageKey(
|
|
1943
2133
|
projectId,
|
|
1944
2134
|
dataset
|
|
1945
2135
|
), [uploadedVersions, setUploadedVersions] = useState(
|
|
@@ -1973,7 +2163,7 @@ const useTranslations = () => {
|
|
|
1973
2163
|
), setPreserveExistingTranslations = useCallback(
|
|
1974
2164
|
(value) => setPreference("preserveExistingTranslations", value),
|
|
1975
2165
|
[setPreference]
|
|
1976
|
-
), schema = useSchema(), translationContext = { client, schema }, toast = useToast(), { loading: loadingSecrets, secrets } = useSecrets(
|
|
2166
|
+
), schema = useSchema(), translationContext = { client: client2, schema }, toast = useToast(), { loading: loadingSecrets, secrets } = useSecrets(
|
|
1977
2167
|
pluginConfig.getSecretsNamespace()
|
|
1978
2168
|
), [branchId, setBranchId] = useState(void 0);
|
|
1979
2169
|
useEffect(() => {
|
|
@@ -1992,7 +2182,7 @@ const useTranslations = () => {
|
|
|
1992
2182
|
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}")`;
|
|
1993
2183
|
let query;
|
|
1994
2184
|
filterConditions.length === 0 ? query = `*[!(_type in ["system.group"]) && !(_id in path("_.**")) && ${languageFilter}]` : query = `*[!(_type in ["system.group"]) && !(_id in path("_.**")) && (${filterConditions.join(" || ")}) && ${languageFilter}]`;
|
|
1995
|
-
const docs = await
|
|
2185
|
+
const docs = await client2.fetch(query);
|
|
1996
2186
|
setDocuments(dedupeDocumentsPreferDraft(docs));
|
|
1997
2187
|
} catch {
|
|
1998
2188
|
toast.push({
|
|
@@ -2003,7 +2193,7 @@ const useTranslations = () => {
|
|
|
2003
2193
|
} finally {
|
|
2004
2194
|
setLoadingDocuments(!1);
|
|
2005
2195
|
}
|
|
2006
|
-
}, [
|
|
2196
|
+
}, [client2, singleDocument]), fetchLocales = useCallback(async () => {
|
|
2007
2197
|
if (secrets)
|
|
2008
2198
|
try {
|
|
2009
2199
|
const availableLocales = await getLocales(secrets);
|
|
@@ -2018,7 +2208,7 @@ const useTranslations = () => {
|
|
|
2018
2208
|
}, [secrets]), fetchExistingTranslations = useCallback(async () => {
|
|
2019
2209
|
if (!(!documents.length || !locales.length))
|
|
2020
2210
|
try {
|
|
2021
|
-
const sourceLocale = pluginConfig.getSourceLocale(), availableLocaleIds = locales.filter((locale) => locale.enabled !== !1).map((locale) => locale.localeId), documentIds = documents.map(getDocumentPublishedId), existingMetadata = await
|
|
2211
|
+
const sourceLocale = pluginConfig.getSourceLocale(), availableLocaleIds = locales.filter((locale) => locale.enabled !== !1).map((locale) => locale.localeId), documentIds = documents.map(getDocumentPublishedId), existingMetadata = await client2.fetch(`*[
|
|
2022
2212
|
_type == 'translation.metadata' &&
|
|
2023
2213
|
translations[language == $sourceLocale][0].value._ref in $documentIds
|
|
2024
2214
|
] {
|
|
@@ -2047,7 +2237,7 @@ const useTranslations = () => {
|
|
|
2047
2237
|
closable: !0
|
|
2048
2238
|
});
|
|
2049
2239
|
}
|
|
2050
|
-
}, [documents, locales,
|
|
2240
|
+
}, [documents, locales, client2]), serializeSourceDocuments = useCallback(
|
|
2051
2241
|
() => documents.map((doc) => {
|
|
2052
2242
|
const { [pluginConfig.getLanguageField()]: _, ...cleanDoc } = doc, baseLanguage = pluginConfig.getSourceLocale();
|
|
2053
2243
|
try {
|
|
@@ -2157,7 +2347,7 @@ const useTranslations = () => {
|
|
|
2157
2347
|
documents,
|
|
2158
2348
|
locales,
|
|
2159
2349
|
schema,
|
|
2160
|
-
|
|
2350
|
+
client2,
|
|
2161
2351
|
branchId,
|
|
2162
2352
|
preserveExistingTranslations,
|
|
2163
2353
|
serializeSourceDocuments,
|
|
@@ -2193,7 +2383,7 @@ const useTranslations = () => {
|
|
|
2193
2383
|
setIsBusy(!1);
|
|
2194
2384
|
}
|
|
2195
2385
|
}
|
|
2196
|
-
}, [secrets, documents, locales,
|
|
2386
|
+
}, [secrets, documents, locales, client2, schema, serializeSourceDocuments]), handleImportAll = useCallback(async () => {
|
|
2197
2387
|
if (!(!secrets || documents.length === 0 || !branchId)) {
|
|
2198
2388
|
setIsBusy(!0);
|
|
2199
2389
|
try {
|
|
@@ -2260,7 +2450,7 @@ const useTranslations = () => {
|
|
|
2260
2450
|
branchId
|
|
2261
2451
|
]), getExistingTranslations = useCallback(
|
|
2262
2452
|
async (documentIds, localeIds, branchId2) => {
|
|
2263
|
-
const sourceLocale = pluginConfig.getSourceLocale(), existingMetadata = await
|
|
2453
|
+
const sourceLocale = pluginConfig.getSourceLocale(), existingMetadata = await client2.fetch(`*[
|
|
2264
2454
|
_type == 'translation.metadata' &&
|
|
2265
2455
|
translations[language == $sourceLocale][0].value._ref in $documentIds
|
|
2266
2456
|
] {
|
|
@@ -2284,7 +2474,7 @@ const useTranslations = () => {
|
|
|
2284
2474
|
});
|
|
2285
2475
|
}), existing;
|
|
2286
2476
|
},
|
|
2287
|
-
[
|
|
2477
|
+
[client2]
|
|
2288
2478
|
), handleImportMissing = useCallback(async () => {
|
|
2289
2479
|
if (!(!secrets || documents.length === 0 || !branchId)) {
|
|
2290
2480
|
setIsBusy(!0);
|
|
@@ -2366,7 +2556,7 @@ const useTranslations = () => {
|
|
|
2366
2556
|
]), handleGetBranchId = useCallback(
|
|
2367
2557
|
async (secrets2) => {
|
|
2368
2558
|
overrideConfig(secrets2);
|
|
2369
|
-
const defaultBranch = await
|
|
2559
|
+
const defaultBranch = await api.createBranch({
|
|
2370
2560
|
branchName: "main",
|
|
2371
2561
|
defaultBranch: !0
|
|
2372
2562
|
});
|
|
@@ -2555,16 +2745,16 @@ const useTranslations = () => {
|
|
|
2555
2745
|
const translatedDoc = await findTranslatedDocumentForLocale(
|
|
2556
2746
|
doc._id,
|
|
2557
2747
|
localeId,
|
|
2558
|
-
|
|
2748
|
+
client2
|
|
2559
2749
|
);
|
|
2560
2750
|
if (!translatedDoc)
|
|
2561
2751
|
return { patched: !1, doc, localeId, noTranslation: !0 };
|
|
2562
2752
|
const resolvedDoc = await resolveRefs(
|
|
2563
2753
|
translatedDoc,
|
|
2564
2754
|
localeId,
|
|
2565
|
-
|
|
2755
|
+
client2
|
|
2566
2756
|
);
|
|
2567
|
-
return resolvedDoc !== translatedDoc ? (await commitResolvedRefs(translatedDoc, resolvedDoc,
|
|
2757
|
+
return resolvedDoc !== translatedDoc ? (await commitResolvedRefs(translatedDoc, resolvedDoc, client2), { patched: !0, doc: translatedDoc, localeId }) : { patched: !1, doc: translatedDoc, localeId };
|
|
2568
2758
|
},
|
|
2569
2759
|
{
|
|
2570
2760
|
onProgress: (current, total) => {
|
|
@@ -2598,11 +2788,11 @@ const useTranslations = () => {
|
|
|
2598
2788
|
} finally {
|
|
2599
2789
|
setIsBusy(!1), setImportProgress({ current: 0, total: 0, isImporting: !1 });
|
|
2600
2790
|
}
|
|
2601
|
-
}, [secrets, documents, locales,
|
|
2791
|
+
}, [secrets, documents, locales, client2, branchId]), handlePublishAllTranslations = useCallback(async () => {
|
|
2602
2792
|
if (!secrets || documents.length === 0) return 0;
|
|
2603
2793
|
setIsBusy(!0);
|
|
2604
2794
|
try {
|
|
2605
|
-
const sourceDocumentIds = documents.map(getDocumentPublishedId), publishedDocumentIds = await
|
|
2795
|
+
const sourceDocumentIds = documents.map(getDocumentPublishedId), publishedDocumentIds = await client2.fetch(
|
|
2606
2796
|
"*[_id in $sourceDocumentIds]._id",
|
|
2607
2797
|
{ sourceDocumentIds }
|
|
2608
2798
|
);
|
|
@@ -2612,7 +2802,7 @@ const useTranslations = () => {
|
|
|
2612
2802
|
status: "warning",
|
|
2613
2803
|
closable: !0
|
|
2614
2804
|
}), 0;
|
|
2615
|
-
const translationMetadata = await
|
|
2805
|
+
const translationMetadata = await client2.fetch(TRANSLATION_DOCS_FOR_PUBLISH_QUERY, {
|
|
2616
2806
|
publishedDocumentIds,
|
|
2617
2807
|
sourceDocumentIds
|
|
2618
2808
|
}), translationDocIds = /* @__PURE__ */ new Set();
|
|
@@ -2630,7 +2820,7 @@ const useTranslations = () => {
|
|
|
2630
2820
|
}), 0;
|
|
2631
2821
|
const translatedDocumentIds = await publishTranslations(
|
|
2632
2822
|
publishableIds,
|
|
2633
|
-
|
|
2823
|
+
client2
|
|
2634
2824
|
);
|
|
2635
2825
|
return toast.push({
|
|
2636
2826
|
title: `Published ${translatedDocumentIds.length} translation documents`,
|
|
@@ -2646,7 +2836,7 @@ const useTranslations = () => {
|
|
|
2646
2836
|
} finally {
|
|
2647
2837
|
setIsBusy(!1);
|
|
2648
2838
|
}
|
|
2649
|
-
}, [secrets, documents,
|
|
2839
|
+
}, [secrets, documents, client2, branchId]);
|
|
2650
2840
|
useEffect(() => {
|
|
2651
2841
|
fetchDocuments();
|
|
2652
2842
|
}, [fetchDocuments]), useEffect(() => {
|
|
@@ -2733,7 +2923,7 @@ const useTranslations = () => {
|
|
|
2733
2923
|
] }) })
|
|
2734
2924
|
}
|
|
2735
2925
|
) : null;
|
|
2736
|
-
var version = "4.0.
|
|
2926
|
+
var version = "4.0.13";
|
|
2737
2927
|
function buildDebugInfo({
|
|
2738
2928
|
secrets,
|
|
2739
2929
|
preferences,
|
|
@@ -2811,11 +3001,11 @@ function summarizeTranslations({
|
|
|
2811
3001
|
missing: Math.max(0, expected - unique.length)
|
|
2812
3002
|
};
|
|
2813
3003
|
}
|
|
2814
|
-
async function collectTranslationSummary(documents,
|
|
3004
|
+
async function collectTranslationSummary(documents, client2) {
|
|
2815
3005
|
const documentIds = documents.map(getDocumentPublishedId), targetLocales = pluginConfig.getLocales();
|
|
2816
3006
|
if (documentIds.length === 0)
|
|
2817
3007
|
return EMPTY_TRANSLATION_SUMMARY;
|
|
2818
|
-
const translatedIds = (await
|
|
3008
|
+
const translatedIds = (await client2.fetch(
|
|
2819
3009
|
`*[_type == 'translation.metadata' && references($documentIds)]{
|
|
2820
3010
|
'targets': translations[language in $targetLocales && defined(value._ref)]{
|
|
2821
3011
|
language,
|
|
@@ -2832,7 +3022,7 @@ async function collectTranslationSummary(documents, client) {
|
|
|
2832
3022
|
publishedIds: [],
|
|
2833
3023
|
draftIds: []
|
|
2834
3024
|
});
|
|
2835
|
-
const unique = Array.from(new Set(translatedIds)), existence = await
|
|
3025
|
+
const unique = Array.from(new Set(translatedIds)), existence = await client2.fetch(
|
|
2836
3026
|
`{
|
|
2837
3027
|
'publishedIds': *[_id in $ids]._id,
|
|
2838
3028
|
'draftIds': *[_id in $draftIds]._id
|
|
@@ -2860,9 +3050,9 @@ const DebugInfoDialog = ({
|
|
|
2860
3050
|
autoPatchReferences,
|
|
2861
3051
|
autoPublish,
|
|
2862
3052
|
preserveExistingTranslations
|
|
2863
|
-
} = useTranslations(),
|
|
3053
|
+
} = useTranslations(), client2 = useClient(), [copied, setCopied] = useState(!1), [translations, setTranslations] = useState(
|
|
2864
3054
|
EMPTY_TRANSLATION_SUMMARY
|
|
2865
|
-
), { projectId, dataset } =
|
|
3055
|
+
), { projectId, dataset } = client2.config(), serialized = useMemo(
|
|
2866
3056
|
() => formatDebugInfo(
|
|
2867
3057
|
buildDebugInfo({
|
|
2868
3058
|
secrets,
|
|
@@ -2895,14 +3085,14 @@ const DebugInfoDialog = ({
|
|
|
2895
3085
|
return useEffect(() => {
|
|
2896
3086
|
if (!isOpen) return;
|
|
2897
3087
|
let cancelled = !1;
|
|
2898
|
-
return collectTranslationSummary(documents,
|
|
3088
|
+
return collectTranslationSummary(documents, client2).then((summary) => {
|
|
2899
3089
|
cancelled || setTranslations(summary);
|
|
2900
3090
|
}).catch(() => {
|
|
2901
3091
|
cancelled || setTranslations(EMPTY_TRANSLATION_SUMMARY);
|
|
2902
3092
|
}), () => {
|
|
2903
3093
|
cancelled = !0;
|
|
2904
3094
|
};
|
|
2905
|
-
}, [isOpen, documents,
|
|
3095
|
+
}, [isOpen, documents, client2]), isOpen ? /* @__PURE__ */ jsx(
|
|
2906
3096
|
Dialog,
|
|
2907
3097
|
{
|
|
2908
3098
|
header: "Debug info",
|
|
@@ -3478,11 +3668,11 @@ function toNativeFieldType(fieldType) {
|
|
|
3478
3668
|
of: [{ type: "block" }]
|
|
3479
3669
|
} : fieldType;
|
|
3480
3670
|
}
|
|
3481
|
-
function buildInternationalizedArrayPlugin(config, sourceLocale, locales,
|
|
3671
|
+
function buildInternationalizedArrayPlugin(config, sourceLocale, locales, customMapping2) {
|
|
3482
3672
|
const languageTitle = (locale) => config.getLanguageTitle?.(locale) ?? config.languageTitles?.[locale] ?? formatLocalePropertiesLabel(
|
|
3483
3673
|
locale,
|
|
3484
|
-
getLocaleProperties(locale, sourceLocale,
|
|
3485
|
-
|
|
3674
|
+
getLocaleProperties(locale, sourceLocale, customMapping2),
|
|
3675
|
+
customMapping2?.[locale]
|
|
3486
3676
|
), allLocales = Array.from(/* @__PURE__ */ new Set([sourceLocale, ...locales]));
|
|
3487
3677
|
return internationalizedArray({
|
|
3488
3678
|
languages: allLocales.map((id) => ({ id, title: languageTitle(id) })),
|
|
@@ -4092,7 +4282,7 @@ const TranslationTab = (props) => {
|
|
|
4092
4282
|
sourceLocale,
|
|
4093
4283
|
defaultLocale,
|
|
4094
4284
|
locales,
|
|
4095
|
-
customMapping,
|
|
4285
|
+
customMapping: customMapping2,
|
|
4096
4286
|
apiKey,
|
|
4097
4287
|
projectId,
|
|
4098
4288
|
singletons,
|
|
@@ -4147,7 +4337,7 @@ const TranslationTab = (props) => {
|
|
|
4147
4337
|
}
|
|
4148
4338
|
), gt.setConfig({
|
|
4149
4339
|
sourceLocale: resolvedSourceLocale,
|
|
4150
|
-
customMapping,
|
|
4340
|
+
customMapping: customMapping2,
|
|
4151
4341
|
apiKey,
|
|
4152
4342
|
projectId
|
|
4153
4343
|
});
|
|
@@ -4161,14 +4351,14 @@ const TranslationTab = (props) => {
|
|
|
4161
4351
|
const props = getLocaleProperties(
|
|
4162
4352
|
locale,
|
|
4163
4353
|
resolvedSourceLocale,
|
|
4164
|
-
|
|
4354
|
+
customMapping2
|
|
4165
4355
|
);
|
|
4166
4356
|
return {
|
|
4167
4357
|
id: locale,
|
|
4168
4358
|
title: formatLocalePropertiesLabel(
|
|
4169
4359
|
locale,
|
|
4170
4360
|
props,
|
|
4171
|
-
|
|
4361
|
+
customMapping2?.[locale]
|
|
4172
4362
|
)
|
|
4173
4363
|
};
|
|
4174
4364
|
});
|
|
@@ -4186,7 +4376,7 @@ const TranslationTab = (props) => {
|
|
|
4186
4376
|
fieldLevelConfig,
|
|
4187
4377
|
resolvedSourceLocale,
|
|
4188
4378
|
targetLocales,
|
|
4189
|
-
|
|
4379
|
+
customMapping2
|
|
4190
4380
|
)
|
|
4191
4381
|
), {
|
|
4192
4382
|
name: "gt-sanity",
|