Skip to content

Commit

Permalink
feat: adds callback function (#198)
Browse files Browse the repository at this point in the history
* feat: adds callback function
* feat: adds metadata document to callback
* feat: additional callback arguments, callback error handling, type fixes
* chore: updates readme
* chore: error handling, types
* chore: adds commas, list callback props
  • Loading branch information
johisak authored Dec 20, 2024
1 parent d1b5c9d commit 67bd80a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,21 @@ export const defineConfig({

// Optional
// Customizes the name of the language field
languageField: `language` // defauts to "language"
languageField: `language`, // defauts to "language"

// Optional
// Keep translation.metadata references weak
weakReferences: true // defaults to false
weakReferences: true, // defaults to false

// Optional
// Adds UI for publishing all translations at once. Requires access to the Scheduling API
// https://www.sanity.io/docs/scheduling-api
bulkPublish: true // defaults to false
bulkPublish: true, // defaults to false

// Optional
// Adds additional fields to the metadata document
metadataFields: [
defineField({ name: 'slug', type: 'slug' })
defineField({ name: 'slug', type: 'slug' }),
],

// Optional
Expand All @@ -153,7 +153,21 @@ export const defineConfig({
// Optional
// Enable "manage translations" button without creating a translated version. Helpful if you have
// pre-existing documents that you need to tie together through the metadata document
allowCreateMetaDoc: true // defaults to false
allowCreateMetaDoc: true, // defaults to false

// Optional
// Callback function that runs after a translation document has been created
// Note: Defaults to null
callback: ({
sourceDocument, // The document in the original language
newDocument, // The newly created translation of the source document
sourceLanguageId, // The id of the original language
destinationLanguageId, // The id of the destination language
metaDocumentId, // The id of the meta document referencing the document translations
client // Sanity client
}) {
// Your function implementation
}
})
]
})
Expand Down
27 changes: 24 additions & 3 deletions src/components/LanguageOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import {type ObjectSchemaType, type SanityDocument, useClient} from 'sanity'

import {METADATA_SCHEMA_NAME} from '../constants'
import {useOpenInNewPane} from '../hooks/useOpenInNewPane'
import type {Language, Metadata, TranslationReference} from '../types'
import type {
Language,
Metadata,
MetadataDocument,
TranslationReference,
} from '../types'
import {createReference} from '../utils/createReference'
import {removeExcludedPaths} from '../utils/excludePaths'
import {useDocumentInternationalizationContext} from './DocumentInternationalizationContext'
Expand Down Expand Up @@ -58,7 +63,7 @@ export default function LanguageOption(props: LanguageOptionProps) {
.length
? metadata.translations.find((t) => t._key === language.id)
: undefined
const {apiVersion, languageField, weakReferences} =
const {apiVersion, languageField, weakReferences, callback} =
useDocumentInternationalizationContext()
const client = useClient({apiVersion})
const toast = useToast()
Expand Down Expand Up @@ -121,7 +126,7 @@ export default function LanguageOption(props: LanguageOptionProps) {
schemaType.name,
!weakReferences
)
const newMetadataDocument = {
const newMetadataDocument: MetadataDocument = {
_id: metadataId,
_type: METADATA_SCHEMA_NAME,
schemaTypes: [schemaType.name],
Expand All @@ -146,6 +151,21 @@ export default function LanguageOption(props: LanguageOptionProps) {
.then(() => {
const metadataExisted = Boolean(metadata?._createdAt)

callback?.({
client,
sourceLanguageId,
sourceDocument: source,
newDocument: newTranslationDocument,
destinationLanguageId: language.id,
metaDocumentId: metadataId,
}).catch((err) => {
toast.push({
status: 'error',
title: `Callback`,
description: `Error while running callback - ${err}.`,
})
})

return toast.push({
status: 'success',
title: `Created "${language.title}" translation`,
Expand Down Expand Up @@ -179,6 +199,7 @@ export default function LanguageOption(props: LanguageOptionProps) {
sourceLanguageId,
toast,
weakReferences,
callback,
])

let message
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export const DEFAULT_CONFIG: PluginConfigContext = {
metadataFields: [],
apiVersion: API_VERSION,
allowCreateMetaDoc: false,
callback: null,
}
17 changes: 17 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type {
ObjectSchemaType,
Reference,
SanityClient,
SanityDocument,
SanityDocumentLike,
} from 'sanity'

export type Language = {
Expand All @@ -17,6 +19,15 @@ export type SupportedLanguages =
| Language[]
| ((client: SanityClient) => Promise<Language[]>)

export type PluginCallbackArgs = {
sourceDocument: SanityDocument
newDocument: SanityDocument
sourceLanguageId: string
destinationLanguageId: string
metaDocumentId: string
client: SanityClient
}

export type PluginConfig = {
supportedLanguages: SupportedLanguages
schemaTypes: string[]
Expand All @@ -26,6 +37,7 @@ export type PluginConfig = {
metadataFields?: FieldDefinition[]
apiVersion?: string
allowCreateMetaDoc?: boolean
callback?: ((args: PluginCallbackArgs) => Promise<void>) | null
}

// Context version of config
Expand All @@ -46,6 +58,11 @@ export type Metadata = {
translations: TranslationReference[]
}

export type MetadataDocument = SanityDocumentLike & {
schemaTypes: string[]
translations: TranslationReference[]
}

export type DocumentInternationalizationMenuProps = {
schemaType: ObjectSchemaType
documentId: string
Expand Down

0 comments on commit 67bd80a

Please sign in to comment.