Skip to content

Commit 2f9ab14

Browse files
committed
feat: [v2] add translations doc publishing to cms client
1 parent b001bdb commit 2f9ab14

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

packages/root-cms/core/client.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export interface TranslationsDoc {
5050
linkedAt: Timestamp;
5151
linkedBy: string;
5252
};
53+
tags?: string[];
5354
};
5455
strings: TranslationsMap;
5556
}
@@ -482,6 +483,9 @@ export class RootCMSClient {
482483
});
483484
batchCount += 1;
484485

486+
this.publishTranslationsDoc(doc.id, {batch});
487+
batchCount += 2;
488+
485489
publishedDocs.push(doc);
486490

487491
if (batchCount >= 400) {
@@ -594,6 +598,9 @@ export class RootCMSClient {
594598
});
595599
batchCount += 1;
596600

601+
this.publishTranslationsDoc(doc.id, {batch});
602+
batchCount += 2;
603+
597604
publishedDocs.push(doc);
598605

599606
if (batchCount >= 400) {
@@ -635,6 +642,48 @@ export class RootCMSClient {
635642
}
636643
}
637644

645+
async publishTranslationsDoc(
646+
translationsId: string,
647+
options?: {batch?: WriteBatch; publishedBy?: string}
648+
) {
649+
const docSlug = translationsId.replaceAll('/', '--');
650+
const draftRef = this.db.doc(
651+
`Projects/${this.projectId}/TranslationsManager/draft/Translations/${docSlug}`
652+
);
653+
const snapshot = await draftRef.get();
654+
655+
if (!snapshot.exists) {
656+
// Ignore missing translations.
657+
console.warn(`translations ${translationsId} does not exist`);
658+
return;
659+
}
660+
661+
// If the translations publishing is tied to another batch request (e.g. doc
662+
// publishing), the batch request will be committed upstream.
663+
const commitBatch = !options?.batch;
664+
665+
const batch = options?.batch || this.db.batch();
666+
batch.update(draftRef, {
667+
'sys.publishedAt': Timestamp.now(),
668+
'sys.publishedBy': options?.publishedBy || 'root-cms-client',
669+
});
670+
671+
const publishedRef = this.db.doc(
672+
`Projects/${this.projectId}/TranslationsManager/published/Translations/${docSlug}`
673+
);
674+
const data = {...snapshot.data()};
675+
if (!data.sys) {
676+
data.sys = {};
677+
}
678+
data.sys.publishedAt = Timestamp.now();
679+
data.sys.publishedBy = options?.publishedBy || 'root-cms-client';
680+
batch.set(publishedRef, data);
681+
if (commitBatch) {
682+
await batch.commit();
683+
this.logAction('translations.publish', {metadata: {translationsId}});
684+
}
685+
}
686+
638687
/**
639688
* Checks if a doc is currently "locked" for publishing.
640689
*/
@@ -665,8 +714,6 @@ export class RootCMSClient {
665714
options?: LoadTranslationsOptions
666715
): Promise<TranslationsMap> {
667716
const dbPath = `Projects/${this.projectId}/TranslationsManager/published/Translations`;
668-
const query: Query = this.db.collection(dbPath);
669-
const snapshot = await query.get();
670717
const translationsMap: TranslationsMap = {};
671718

672719
const addTranslations = (hash: string, translations: Translation) => {
@@ -676,6 +723,11 @@ export class RootCMSClient {
676723
);
677724
};
678725

726+
let query: Query = this.db.collection(dbPath);
727+
if (options?.tags && options.tags.length > 0) {
728+
query = query.where('sys.tags', 'array-contains-any', options.tags);
729+
}
730+
const snapshot = await query.get();
679731
snapshot.forEach((doc) => {
680732
const data = doc.data();
681733
const strings = (data.strings || {}) as Record<string, Translation>;

packages/root-cms/core/compatibility.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async function migrateTranslationsToV2(
6161
querySnapshot.forEach((doc) => {
6262
const hash = doc.id;
6363
const translation = doc.data() as Translation;
64-
const tags = translation.tags || [];
64+
const tags = (translation.tags || []) as string[];
6565
delete translation.tags;
6666
for (const tag of tags) {
6767
if (tag.includes('/')) {
@@ -73,6 +73,7 @@ async function migrateTranslationsToV2(
7373
modifiedBy: 'root-cms-client',
7474
publishedAt: Timestamp.now(),
7575
publishedBy: 'root-cms-client',
76+
tags: tags,
7677
},
7778
strings: {},
7879
};

packages/root-cms/ui/db/translations.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
import {GoogleSheetId} from '@/utils/gsheets.js';
1919
import {normalizeString, sourceHash} from '@/utils/l10n.js';
2020
import {logAction} from './actions.js';
21-
import {getDraftDocRef} from './docs.js';
2221

2322
export interface Translations {
2423
[locale: string]: string;

0 commit comments

Comments
 (0)