@braze/sanity-plugin-ai-translation 0.2.75 → 0.2.76

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 CHANGED
@@ -870,14 +870,11 @@ const fetchTranslationPrompts = async ({
870
870
  languageFieldName
871
871
  }) => {
872
872
  try {
873
- const doc = await retryWithBackoff(
874
- () => client.fetch("*[_id == $documentId][0]", { documentId }),
875
- {
876
- maxAttempts: 3,
877
- initialDelayMs: 1e3,
878
- context: { operation: "localizeReferences.fetchDocument", documentId }
879
- }
880
- );
873
+ const doc = await retryWithBackoff(() => client.fetch("*[_id == $documentId][0]", { documentId }), {
874
+ maxAttempts: 3,
875
+ initialDelayMs: 1e3,
876
+ context: { operation: "localizeReferences.fetchDocument", documentId }
877
+ });
881
878
  if (!doc) {
882
879
  devLog.error("Document not found", new Error("Document not found"), {
883
880
  documentId,
@@ -904,20 +901,19 @@ const fetchTranslationPrompts = async ({
904
901
  component: "localizeReferences"
905
902
  });
906
903
  }
907
- }, updateReferences = async (documentId, client, refs, sourceLanguage, targetLanguage) => {
904
+ }, updateReferences = async (documentId, client, refs, _sourceLanguage, targetLanguage) => {
908
905
  if (!refs?.length)
909
906
  return [];
910
907
  const refIds = refs.map((r) => getPublishedId(r.value)), translations = await retryWithBackoff(
911
908
  () => client.fetch(
912
909
  `*[_type == $translationMetadataSchemaType && array::intersects(translations[].value._ref, $refIds)] {
913
- "sourceId": translations[_key == $sourceLanguage][0].value._ref,
910
+ "allSourceRefs": translations[_key != $targetLanguage][].value._ref,
914
911
  "targetId": translations[_key == $targetLanguage][0].value._ref,
915
- "targetType": translations[_key == $sourceLanguage][0].value -> _type
912
+ "targetType": translations[_key == $targetLanguage][0].value -> _type
916
913
  }`,
917
914
  {
918
915
  translationMetadataSchemaType: TRANSLATION_METADATA_TYPE,
919
916
  refIds,
920
- sourceLanguage,
921
917
  targetLanguage
922
918
  }
923
919
  ),
@@ -928,16 +924,21 @@ const fetchTranslationPrompts = async ({
928
924
  }
929
925
  ), translationMap = /* @__PURE__ */ new Map();
930
926
  for (const t of translations)
931
- t.sourceId && t.targetId && translationMap.set(t.sourceId, t.targetId);
927
+ if (t.targetId)
928
+ for (const sourceRef of t.allSourceRefs ?? [])
929
+ sourceRef && translationMap.set(sourceRef, { targetId: t.targetId, targetType: t.targetType });
932
930
  const patch = client.patch(documentId);
933
931
  let hasChanges = !1;
934
932
  for (const ref of refs) {
935
- const sourceId = getPublishedId(ref.value), newId = translationMap.get(sourceId);
936
- newId && (patch.set({ [pathToString(ref.path)]: getPublishedId(newId) }), patch.set({
933
+ const sourceId = getPublishedId(ref.value), match = translationMap.get(sourceId);
934
+ if (!match)
935
+ continue;
936
+ const newId = getPublishedId(match.targetId);
937
+ patch.set({ [pathToString(ref.path)]: newId }), patch.set({
937
938
  [`${pathToString(ref.path.slice(0, -1))}._strengthenOnPublish`]: {
938
- type: translations.find((t) => t.targetId === getPublishedId(newId))?.targetType || void 0
939
+ type: match.targetType || void 0
939
940
  }
940
- }), hasChanges = !0);
941
+ }), hasChanges = !0;
941
942
  }
942
943
  hasChanges && (await retryWithBackoff(
943
944
  async () => {