@braze/sanity-plugin-ai-translation 0.2.84 → 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";
@@ -3409,20 +3409,39 @@ ${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, 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}`, 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;
3426
3445
  }, PreflightView = () => {
3427
3446
  const {
3428
3447
  document,