@sanity/document-internationalization 3.2.1 → 3.3.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/document-internationalization",
3
- "version": "3.2.1",
3
+ "version": "3.3.0",
4
4
  "description": "Create unique translations of a document based on its language, joined by a shared reference document.",
5
5
  "keywords": [
6
6
  "sanity",
@@ -15,7 +15,12 @@ import {type ObjectSchemaType, type SanityDocument, useClient} from 'sanity'
15
15
 
16
16
  import {METADATA_SCHEMA_NAME} from '../constants'
17
17
  import {useOpenInNewPane} from '../hooks/useOpenInNewPane'
18
- import type {Language, Metadata, TranslationReference} from '../types'
18
+ import type {
19
+ Language,
20
+ Metadata,
21
+ MetadataDocument,
22
+ TranslationReference,
23
+ } from '../types'
19
24
  import {createReference} from '../utils/createReference'
20
25
  import {removeExcludedPaths} from '../utils/excludePaths'
21
26
  import {useDocumentInternationalizationContext} from './DocumentInternationalizationContext'
@@ -58,7 +63,7 @@ export default function LanguageOption(props: LanguageOptionProps) {
58
63
  .length
59
64
  ? metadata.translations.find((t) => t._key === language.id)
60
65
  : undefined
61
- const {apiVersion, languageField, weakReferences} =
66
+ const {apiVersion, languageField, weakReferences, callback} =
62
67
  useDocumentInternationalizationContext()
63
68
  const client = useClient({apiVersion})
64
69
  const toast = useToast()
@@ -121,7 +126,7 @@ export default function LanguageOption(props: LanguageOptionProps) {
121
126
  schemaType.name,
122
127
  !weakReferences
123
128
  )
124
- const newMetadataDocument = {
129
+ const newMetadataDocument: MetadataDocument = {
125
130
  _id: metadataId,
126
131
  _type: METADATA_SCHEMA_NAME,
127
132
  schemaTypes: [schemaType.name],
@@ -146,6 +151,21 @@ export default function LanguageOption(props: LanguageOptionProps) {
146
151
  .then(() => {
147
152
  const metadataExisted = Boolean(metadata?._createdAt)
148
153
 
154
+ callback?.({
155
+ client,
156
+ sourceLanguageId,
157
+ sourceDocument: source,
158
+ newDocument: newTranslationDocument,
159
+ destinationLanguageId: language.id,
160
+ metaDocumentId: metadataId,
161
+ }).catch((err) => {
162
+ toast.push({
163
+ status: 'error',
164
+ title: `Callback`,
165
+ description: `Error while running callback - ${err}.`,
166
+ })
167
+ })
168
+
149
169
  return toast.push({
150
170
  status: 'success',
151
171
  title: `Created "${language.title}" translation`,
@@ -179,6 +199,7 @@ export default function LanguageOption(props: LanguageOptionProps) {
179
199
  sourceLanguageId,
180
200
  toast,
181
201
  weakReferences,
202
+ callback,
182
203
  ])
183
204
 
184
205
  let message
package/src/constants.ts CHANGED
@@ -12,4 +12,5 @@ export const DEFAULT_CONFIG: PluginConfigContext = {
12
12
  metadataFields: [],
13
13
  apiVersion: API_VERSION,
14
14
  allowCreateMetaDoc: false,
15
+ callback: null,
15
16
  }
package/src/types.ts CHANGED
@@ -6,6 +6,8 @@ import type {
6
6
  ObjectSchemaType,
7
7
  Reference,
8
8
  SanityClient,
9
+ SanityDocument,
10
+ SanityDocumentLike,
9
11
  } from 'sanity'
10
12
 
11
13
  export type Language = {
@@ -17,6 +19,15 @@ export type SupportedLanguages =
17
19
  | Language[]
18
20
  | ((client: SanityClient) => Promise<Language[]>)
19
21
 
22
+ export type PluginCallbackArgs = {
23
+ sourceDocument: SanityDocument
24
+ newDocument: SanityDocument
25
+ sourceLanguageId: string
26
+ destinationLanguageId: string
27
+ metaDocumentId: string
28
+ client: SanityClient
29
+ }
30
+
20
31
  export type PluginConfig = {
21
32
  supportedLanguages: SupportedLanguages
22
33
  schemaTypes: string[]
@@ -26,6 +37,7 @@ export type PluginConfig = {
26
37
  metadataFields?: FieldDefinition[]
27
38
  apiVersion?: string
28
39
  allowCreateMetaDoc?: boolean
40
+ callback?: ((args: PluginCallbackArgs) => Promise<void>) | null
29
41
  }
30
42
 
31
43
  // Context version of config
@@ -46,6 +58,11 @@ export type Metadata = {
46
58
  translations: TranslationReference[]
47
59
  }
48
60
 
61
+ export type MetadataDocument = SanityDocumentLike & {
62
+ schemaTypes: string[]
63
+ translations: TranslationReference[]
64
+ }
65
+
49
66
  export type DocumentInternationalizationMenuProps = {
50
67
  schemaType: ObjectSchemaType
51
68
  documentId: string