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

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,6 +1,6 @@
1
1
  import { getPublishedId, pathToString, useClient, useWorkspace, validateDocument, defineType, defineField, definePlugin } from "sanity";
2
2
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
3
- import { WarningOutlineIcon, ErrorOutlineIcon, CheckmarkIcon, CloseIcon, SyncIcon, PanelRightIcon, SearchIcon, CircleIcon, ArrowRightIcon, ResetIcon, TranslateIcon } from "@sanity/icons";
3
+ import { WarningOutlineIcon, ErrorOutlineIcon, CheckmarkIcon, SyncIcon, PanelRightIcon, SearchIcon, CircleIcon, ArrowRightIcon, ResetIcon, TranslateIcon } from "@sanity/icons";
4
4
  import { createContext, useReducer, useRef, useEffect, useContext, useMemo, useState, useCallback } from "react";
5
5
  import defaults from "lodash.defaults";
6
6
  import { Card, Stack, Flex, Text, Box, Heading, Button, Spinner, Badge, Inline, Radio, Checkbox, Grid, TextInput, Select, Label, Code, Tooltip } from "@sanity/ui";
@@ -3409,18 +3409,19 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3409
3409
  padding: 2
3410
3410
  }
3411
3411
  );
3412
- }, DocumentValidator = ({ documentId, documentType, onResult }) => {
3412
+ }, DocumentValidator = ({ documentId, documentType, perspective, onResult }) => {
3413
3413
  const client = useClient({ apiVersion: "vX" }), workspace = useWorkspace(), hasRun = useRef(!1);
3414
3414
  return useEffect(() => {
3415
3415
  hasRun.current || (hasRun.current = !0, (async () => {
3416
3416
  onResult(documentId, { isValidating: !0, errors: [] });
3417
3417
  try {
3418
- const publishedId = getPublishedId(documentId), draftId = `drafts.${publishedId}`, doc = await client.fetch(
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(
3419
3421
  "*[_id in [$draftId, $publishedId]] | order(_id desc) [0]",
3420
3422
  { draftId, publishedId }
3421
- );
3422
- if (!doc) {
3423
- devLog.warn("Document not found for validation", { documentId }), onResult(documentId, { isValidating: !1, errors: [] });
3423
+ ), !doc) {
3424
+ devLog.warn("Document not found for validation", { documentId, perspective }), onResult(documentId, { isValidating: !1, errors: [] });
3424
3425
  return;
3425
3426
  }
3426
3427
  const errors = (await validateDocument({
@@ -3430,6 +3431,7 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3430
3431
  })).filter((m) => m.level === "error");
3431
3432
  errors.length > 0 && devLog.debug("Validation errors found", {
3432
3433
  documentId,
3434
+ perspective,
3433
3435
  errorCount: errors.length,
3434
3436
  component: "DocumentValidator"
3435
3437
  }), onResult(documentId, { isValidating: !1, errors });
@@ -3441,7 +3443,7 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3441
3443
  ), onResult(documentId, { isValidating: !1, errors: [] });
3442
3444
  }
3443
3445
  })());
3444
- }, [documentId, documentType, onResult, client, workspace]), null;
3446
+ }, [documentId, documentType, perspective, onResult, client, workspace]), null;
3445
3447
  }, PreflightView = () => {
3446
3448
  const {
3447
3449
  document,
@@ -3455,7 +3457,7 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3455
3457
  translationMode,
3456
3458
  releaseConfig,
3457
3459
  documentSource
3458
- } = useTranslationContext(), client = useClient({ apiVersion: "vX" }), [scanKey, setScanKey] = useState(0), [validationResults, setValidationResults] = useState(/* @__PURE__ */ new Map()), [sourceRefsWithDrafts, setSourceRefsWithDrafts] = useState([]), [isSourceDraftCheckLoading, setIsSourceDraftCheckLoading] = useState(!0), [staleDrafts, setStaleDrafts] = useState([]), [isDraftCheckLoading, setIsDraftCheckLoading] = useState(!0), [draftAction, setDraftAction] = useState("none"), [isDraftActionInProgress, setIsDraftActionInProgress] = useState(!1), [conflictContext, setConflictContext] = useState(null), [isCheckingConflicts, setIsCheckingConflicts] = useState(!0), filteredReferences = useMemo(
3460
+ } = useTranslationContext(), client = useClient({ apiVersion: "vX" }), [scanKey, setScanKey] = useState(0), [validationResults, setValidationResults] = useState(/* @__PURE__ */ new Map()), [sourceRefsWithDrafts, setSourceRefsWithDrafts] = useState([]), [isSourceDraftCheckLoading, setIsSourceDraftCheckLoading] = useState(!0), [staleDrafts, setStaleDrafts] = useState([]), [isDraftCheckLoading, setIsDraftCheckLoading] = useState(!0), [draftAction, setDraftAction] = useState("none"), [conflictContext, setConflictContext] = useState(null), [isCheckingConflicts, setIsCheckingConflicts] = useState(!0), filteredReferences = useMemo(
3459
3461
  () => references.filter((ref) => selectedReferenceIds.has(ref._id)),
3460
3462
  [references, selectedReferenceIds]
3461
3463
  ), documentsToValidate = useMemo(() => {
@@ -3557,58 +3559,7 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3557
3559
  useEffect(() => {
3558
3560
  runSourceDraftCheck(), runDraftCheck(), runConflictCheck();
3559
3561
  }, [scanKey]);
3560
- const handleDiscardDrafts = useCallback(async () => {
3561
- setIsDraftActionInProgress(!0);
3562
- try {
3563
- const draftIds = staleDrafts.map((d) => d.draftId);
3564
- devLog.info("Discarding stale drafts", { draftIds, component: "PreflightView" });
3565
- const transaction = client.transaction();
3566
- for (const id of draftIds)
3567
- transaction.delete(id);
3568
- await transaction.commit(), devLog.info("Successfully discarded stale drafts", {
3569
- count: draftIds.length,
3570
- component: "PreflightView"
3571
- }), await runDraftCheck();
3572
- } catch (error) {
3573
- devLog.error("Failed to discard drafts", error instanceof Error ? error : new Error(String(error)), {
3574
- component: "PreflightView"
3575
- });
3576
- } finally {
3577
- setIsDraftActionInProgress(!1);
3578
- }
3579
- }, [staleDrafts, client, runDraftCheck]), handlePublishDrafts = useCallback(async () => {
3580
- setIsDraftActionInProgress(!0);
3581
- try {
3582
- devLog.info("Publishing stale drafts", {
3583
- count: staleDrafts.length,
3584
- component: "PreflightView"
3585
- });
3586
- for (const draft of staleDrafts)
3587
- await client.request({
3588
- method: "POST",
3589
- uri: "/data/actions/mutate",
3590
- body: {
3591
- actions: [
3592
- {
3593
- actionType: "sanity.action.document.publish",
3594
- draftId: draft.draftId,
3595
- publishedId: draft.targetId
3596
- }
3597
- ]
3598
- }
3599
- });
3600
- devLog.info("Successfully published stale drafts", {
3601
- count: staleDrafts.length,
3602
- component: "PreflightView"
3603
- }), await runDraftCheck();
3604
- } catch (error) {
3605
- devLog.error("Failed to publish drafts", error instanceof Error ? error : new Error(String(error)), {
3606
- component: "PreflightView"
3607
- });
3608
- } finally {
3609
- setIsDraftActionInProgress(!1);
3610
- }
3611
- }, [staleDrafts, client, runDraftCheck]), startTranslation = useCallback(
3562
+ const startTranslation = useCallback(
3612
3563
  (replaceExisting, overrideReleaseId, skipConflicts) => {
3613
3564
  translateDocumentSet({
3614
3565
  sourceDocument: document,
@@ -3676,6 +3627,7 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3676
3627
  {
3677
3628
  documentId: doc._id,
3678
3629
  documentType: doc._type,
3630
+ perspective: documentSource === "published" ? "published" : "drafts",
3679
3631
  onResult: handleValidationResult
3680
3632
  },
3681
3633
  `${doc._id}-${scanKey}`
@@ -3747,10 +3699,10 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3747
3699
  (isDraftCheckLoading || hasStaleDrafts) && /* @__PURE__ */ jsx(Card, { padding: 4, border: !0, radius: 2, tone: hasStaleDrafts && !draftsDismissed ? "caution" : void 0, children: /* @__PURE__ */ jsxs(Stack, { space: 3, children: [
3748
3700
  /* @__PURE__ */ jsxs(Flex, { align: "center", gap: 2, children: [
3749
3701
  isDraftCheckLoading ? /* @__PURE__ */ jsx(Spinner, { muted: !0 }) : draftsDismissed ? /* @__PURE__ */ jsx(Text, { size: 2, children: /* @__PURE__ */ jsx(CheckmarkIcon, {}) }) : /* @__PURE__ */ jsx(Text, { size: 2, children: /* @__PURE__ */ jsx(WarningOutlineIcon, {}) }),
3750
- /* @__PURE__ */ jsx(Text, { weight: "semibold", children: isDraftCheckLoading ? "Checking for stale drafts on target documents\u2026" : draftsDismissed ? "Stale drafts acknowledged" : `${staleDrafts.length} stale draft${staleDrafts.length !== 1 ? "s" : ""} found on target documents` })
3702
+ /* @__PURE__ */ jsx(Text, { weight: "semibold", children: isDraftCheckLoading ? "Checking for stale drafts on target documents\u2026" : draftsDismissed ? "Stale drafts acknowledged" : `${staleDrafts.length} draft${staleDrafts.length !== 1 ? "s" : ""} found on existing translated document${staleDrafts.length !== 1 ? "s" : ""}` })
3751
3703
  ] }),
3752
3704
  hasStaleDrafts && !draftsDismissed && /* @__PURE__ */ jsxs(Fragment, { children: [
3753
- /* @__PURE__ */ jsx(Text, { size: 1, children: "These target documents have unpublished drafts that may contain outdated content. Close and reopen the dialog after resolving, or use the actions below." }),
3705
+ /* @__PURE__ */ jsx(Text, { size: 1, children: "These existing translations have unpublished changes that will be out-of-date after this translation is completed. Use the links below to either publish each draft or discard the changes." }),
3754
3706
  /* @__PURE__ */ jsx(Card, { padding: 3, border: !0, radius: 2, children: /* @__PURE__ */ jsx(Box, { style: { maxHeight: "300px", overflowY: "auto" }, children: /* @__PURE__ */ jsx(Stack, { space: 3, children: staleDrafts.map((draft) => /* @__PURE__ */ jsxs(Flex, { align: "center", justify: "space-between", gap: 2, children: [
3755
3707
  /* @__PURE__ */ jsxs(Flex, { align: "center", gap: 2, flex: 1, style: { minWidth: 0 }, children: [
3756
3708
  /* @__PURE__ */ jsx(Text, { size: 1, children: draft.title || draft.targetId }),
@@ -3759,42 +3711,14 @@ ${protectedPhrases.join(", ")}`, buildStyleGuideSection = (globalPrompt, localeP
3759
3711
  ] }),
3760
3712
  /* @__PURE__ */ jsx(DocumentOpenerButton, { documentId: draft.targetId, documentType: draft._type })
3761
3713
  ] }, draft.draftId)) }) }) }),
3762
- /* @__PURE__ */ jsxs(Flex, { gap: 2, wrap: "wrap", children: [
3763
- /* @__PURE__ */ jsx(
3764
- Button,
3765
- {
3766
- mode: "ghost",
3767
- tone: "caution",
3768
- text: "Discard All Drafts",
3769
- icon: CloseIcon,
3770
- onClick: handleDiscardDrafts,
3771
- disabled: isDraftActionInProgress
3772
- }
3773
- ),
3774
- /* @__PURE__ */ jsx(
3775
- Button,
3776
- {
3777
- mode: "ghost",
3778
- tone: "caution",
3779
- text: "Publish All Drafts",
3780
- onClick: handlePublishDrafts,
3781
- disabled: isDraftActionInProgress
3782
- }
3783
- ),
3784
- /* @__PURE__ */ jsx(
3785
- Button,
3786
- {
3787
- mode: "ghost",
3788
- text: "Continue Anyway",
3789
- onClick: () => setDraftAction("continue"),
3790
- disabled: isDraftActionInProgress
3791
- }
3792
- )
3793
- ] }),
3794
- isDraftActionInProgress && /* @__PURE__ */ jsxs(Flex, { align: "center", gap: 2, children: [
3795
- /* @__PURE__ */ jsx(Spinner, { muted: !0 }),
3796
- /* @__PURE__ */ jsx(Text, { size: 1, muted: !0, children: "Processing\u2026" })
3797
- ] })
3714
+ /* @__PURE__ */ jsx(
3715
+ Button,
3716
+ {
3717
+ mode: "ghost",
3718
+ text: "Continue Anyway",
3719
+ onClick: () => setDraftAction("continue")
3720
+ }
3721
+ )
3798
3722
  ] })
3799
3723
  ] }) }),
3800
3724
  /* @__PURE__ */ jsxs(Flex, { justify: "space-between", gap: 2, children: [