@braze/sanity-plugin-ai-translation 0.2.83 → 0.2.85

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
@@ -1,4 +1,4 @@
1
- import { getPublishedId, pathToString, useClient, useValidationStatus, defineType, defineField, definePlugin } from "sanity";
1
+ import { getPublishedId, pathToString, useClient, useWorkspace, validateDocument, defineType, defineField, definePlugin } from "sanity";
2
2
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
3
3
  import { WarningOutlineIcon, ErrorOutlineIcon, CheckmarkIcon, CloseIcon, SyncIcon, PanelRightIcon, SearchIcon, CircleIcon, ArrowRightIcon, ResetIcon, TranslateIcon } from "@sanity/icons";
4
4
  import { createContext, useReducer, useRef, useEffect, useContext, useMemo, useState, useCallback } from "react";
@@ -416,9 +416,7 @@ const isLoggingEnabled = () => process.env.NEXT_PUBLIC_SANITY_STUDIO_AI_TRANSLAT
416
416
  const refIdSet = new Set(refIds);
417
417
  for (const metadataDoc of refMetadataDocs) {
418
418
  if (!metadataDoc.translations?.length) continue;
419
- const matchingRefId = metadataDoc.translations.find(
420
- (t) => t.value?._ref && refIdSet.has(t.value._ref)
421
- )?.value._ref;
419
+ const matchingRefId = metadataDoc.translations.find((t) => t.value?._ref && refIdSet.has(t.value._ref))?.value._ref;
422
420
  if (matchingRefId)
423
421
  for (const lang of selectedTargetLanguages) {
424
422
  const targetId = getExistingTranslationId(metadataDoc, lang);
@@ -3411,20 +3409,39 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3411
3409
  padding: 2
3412
3410
  }
3413
3411
  );
3414
- }, VALIDATION_TIMEOUT_MS = 2e3, DocumentValidator = ({ documentId, documentType, onResult }) => {
3415
- const publishedId = getPublishedId(documentId), { isValidating, validation } = useValidationStatus(publishedId, documentType), hasReceivedRealResult = useRef(!1), hasSettled = useRef(!1), lastReported = useRef(null), [timedOut, setTimedOut] = useState(!1);
3412
+ }, DocumentValidator = ({ documentId, documentType, onResult }) => {
3413
+ const client = useClient({ apiVersion: "vX" }), workspace = useWorkspace(), hasRun = useRef(!1);
3416
3414
  return useEffect(() => {
3417
- const timer = setTimeout(() => setTimedOut(!0), VALIDATION_TIMEOUT_MS);
3418
- return () => clearTimeout(timer);
3419
- }, []), useEffect(() => {
3420
- if (hasSettled.current) return;
3421
- (isValidating || validation.length > 0) && (hasReceivedRealResult.current = !0);
3422
- const stillWaitingForFirstResult = !hasReceivedRealResult.current && !timedOut, errors = validation.filter((v) => v.level === "error"), computedIsValidating = isValidating || stillWaitingForFirstResult;
3423
- !computedIsValidating && hasReceivedRealResult.current && (hasSettled.current = !0), !(lastReported.current && lastReported.current.isValidating === computedIsValidating && lastReported.current.errorCount === errors.length) && (lastReported.current = { isValidating: computedIsValidating, errorCount: errors.length }, onResult(documentId, {
3424
- isValidating: computedIsValidating,
3425
- errors
3426
- }));
3427
- }, [isValidating, validation, documentId, onResult, timedOut]), null;
3415
+ hasRun.current || (hasRun.current = !0, (async () => {
3416
+ onResult(documentId, { isValidating: !0, errors: [] });
3417
+ try {
3418
+ const publishedId = getPublishedId(documentId), draftId = `drafts.${publishedId}`, doc = await client.fetch(
3419
+ "*[_id in [$draftId, $publishedId]] | order(_id desc) [0]",
3420
+ { draftId, publishedId }
3421
+ );
3422
+ if (!doc) {
3423
+ devLog.warn("Document not found for validation", { documentId }), onResult(documentId, { isValidating: !1, errors: [] });
3424
+ return;
3425
+ }
3426
+ const errors = (await validateDocument({
3427
+ document: doc,
3428
+ workspace,
3429
+ environment: "studio"
3430
+ })).filter((m) => m.level === "error");
3431
+ errors.length > 0 && devLog.debug("Validation errors found", {
3432
+ documentId,
3433
+ errorCount: errors.length,
3434
+ component: "DocumentValidator"
3435
+ }), onResult(documentId, { isValidating: !1, errors });
3436
+ } catch (error) {
3437
+ devLog.error(
3438
+ "Failed to validate document",
3439
+ error instanceof Error ? error : new Error(String(error)),
3440
+ { documentId, component: "DocumentValidator" }
3441
+ ), onResult(documentId, { isValidating: !1, errors: [] });
3442
+ }
3443
+ })());
3444
+ }, [documentId, documentType, onResult, client, workspace]), null;
3428
3445
  }, PreflightView = () => {
3429
3446
  const {
3430
3447
  document,
@@ -3512,7 +3529,14 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3512
3529
  } finally {
3513
3530
  setIsDraftCheckLoading(!1);
3514
3531
  }
3515
- }, [client, translationMetadata, selectedTargetLanguages, document._id, shouldTranslateReferences, filteredReferences]), runConflictCheck = useCallback(async () => {
3532
+ }, [
3533
+ client,
3534
+ translationMetadata,
3535
+ selectedTargetLanguages,
3536
+ document._id,
3537
+ shouldTranslateReferences,
3538
+ filteredReferences
3539
+ ]), runConflictCheck = useCallback(async () => {
3516
3540
  if (releaseConfig.mode === "new") {
3517
3541
  setIsCheckingConflicts(!1), setConflictContext(null);
3518
3542
  return;
@@ -3776,16 +3800,7 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3776
3800
  /* @__PURE__ */ jsxs(Flex, { justify: "space-between", gap: 2, children: [
3777
3801
  /* @__PURE__ */ jsx(Button, { mode: "ghost", text: "Back", onClick: handleBack }),
3778
3802
  /* @__PURE__ */ jsxs(Flex, { gap: 2, children: [
3779
- /* @__PURE__ */ jsx(
3780
- Button,
3781
- {
3782
- mode: "ghost",
3783
- text: "Rescan",
3784
- icon: SyncIcon,
3785
- onClick: handleRescan,
3786
- disabled: isLoading
3787
- }
3788
- ),
3803
+ /* @__PURE__ */ jsx(Button, { mode: "ghost", text: "Rescan", icon: SyncIcon, onClick: handleRescan, disabled: isLoading }),
3789
3804
  /* @__PURE__ */ jsx(
3790
3805
  Button,
3791
3806
  {