@braze/sanity-plugin-ai-translation 0.2.84 → 0.2.86

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";
@@ -3409,20 +3409,41 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3409
3409
  padding: 2
3410
3410
  }
3411
3411
  );
3412
- }, VALIDATION_TIMEOUT_MS = 1e4, DocumentValidator = ({ documentId, documentType, onResult }) => {
3413
- const publishedId = getPublishedId(documentId), { isValidating, validation } = useValidationStatus(publishedId, documentType), hasReceivedRealResult = useRef(!1), hasSettled = useRef(!1), lastReported = useRef(null), [timedOut, setTimedOut] = useState(!1), initialValidationRef = useRef(validation);
3412
+ }, DocumentValidator = ({ documentId, documentType, perspective, onResult }) => {
3413
+ const client = useClient({ apiVersion: "vX" }), workspace = useWorkspace(), hasRun = useRef(!1);
3414
3414
  return useEffect(() => {
3415
- const timer = setTimeout(() => setTimedOut(!0), VALIDATION_TIMEOUT_MS);
3416
- return () => clearTimeout(timer);
3417
- }, []), useEffect(() => {
3418
- if (hasSettled.current) return;
3419
- (isValidating || validation.length > 0 || validation !== initialValidationRef.current) && (hasReceivedRealResult.current = !0);
3420
- const stillWaitingForFirstResult = !hasReceivedRealResult.current && !timedOut, errors = validation.filter((v) => v.level === "error"), computedIsValidating = isValidating || stillWaitingForFirstResult;
3421
- !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, {
3422
- isValidating: computedIsValidating,
3423
- errors
3424
- }));
3425
- }, [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}`;
3419
+ let doc;
3420
+ if (perspective === "published" ? doc = await client.fetch("*[_id == $publishedId][0]", { publishedId }) : doc = await client.fetch(
3421
+ "*[_id in [$draftId, $publishedId]] | order(_id desc) [0]",
3422
+ { draftId, publishedId }
3423
+ ), !doc) {
3424
+ devLog.warn("Document not found for validation", { documentId, perspective }), onResult(documentId, { isValidating: !1, errors: [] });
3425
+ return;
3426
+ }
3427
+ const errors = (await validateDocument({
3428
+ document: doc,
3429
+ workspace,
3430
+ environment: "studio"
3431
+ })).filter((m) => m.level === "error");
3432
+ errors.length > 0 && devLog.debug("Validation errors found", {
3433
+ documentId,
3434
+ perspective,
3435
+ errorCount: errors.length,
3436
+ component: "DocumentValidator"
3437
+ }), onResult(documentId, { isValidating: !1, errors });
3438
+ } catch (error) {
3439
+ devLog.error(
3440
+ "Failed to validate document",
3441
+ error instanceof Error ? error : new Error(String(error)),
3442
+ { documentId, component: "DocumentValidator" }
3443
+ ), onResult(documentId, { isValidating: !1, errors: [] });
3444
+ }
3445
+ })());
3446
+ }, [documentId, documentType, perspective, onResult, client, workspace]), null;
3426
3447
  }, PreflightView = () => {
3427
3448
  const {
3428
3449
  document,
@@ -3657,6 +3678,7 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3657
3678
  {
3658
3679
  documentId: doc._id,
3659
3680
  documentType: doc._type,
3681
+ perspective: documentSource === "published" ? "published" : "drafts",
3660
3682
  onResult: handleValidationResult
3661
3683
  },
3662
3684
  `${doc._id}-${scanKey}`