From 67bd80a477552a16f0ee3c382e997fe8d237dd1c Mon Sep 17 00:00:00 2001 From: Johan Isaksson Date: Fri, 20 Dec 2024 11:26:22 +0100 Subject: [PATCH] feat: adds callback function (#198) * 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 --- README.md | 24 +++++++++++++++++++----- src/components/LanguageOption.tsx | 27 ++++++++++++++++++++++++--- src/constants.ts | 1 + src/types.ts | 17 +++++++++++++++++ 4 files changed, 61 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f928718..5096eb4 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 + } }) ] }) diff --git a/src/components/LanguageOption.tsx b/src/components/LanguageOption.tsx index 53c2f02..5a74d36 100644 --- a/src/components/LanguageOption.tsx +++ b/src/components/LanguageOption.tsx @@ -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' @@ -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() @@ -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], @@ -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`, @@ -179,6 +199,7 @@ export default function LanguageOption(props: LanguageOptionProps) { sourceLanguageId, toast, weakReferences, + callback, ]) let message diff --git a/src/constants.ts b/src/constants.ts index b012d44..83703aa 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -12,4 +12,5 @@ export const DEFAULT_CONFIG: PluginConfigContext = { metadataFields: [], apiVersion: API_VERSION, allowCreateMetaDoc: false, + callback: null, } diff --git a/src/types.ts b/src/types.ts index 006362f..adda763 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,6 +6,8 @@ import type { ObjectSchemaType, Reference, SanityClient, + SanityDocument, + SanityDocumentLike, } from 'sanity' export type Language = { @@ -17,6 +19,15 @@ export type SupportedLanguages = | Language[] | ((client: SanityClient) => Promise) +export type PluginCallbackArgs = { + sourceDocument: SanityDocument + newDocument: SanityDocument + sourceLanguageId: string + destinationLanguageId: string + metaDocumentId: string + client: SanityClient +} + export type PluginConfig = { supportedLanguages: SupportedLanguages schemaTypes: string[] @@ -26,6 +37,7 @@ export type PluginConfig = { metadataFields?: FieldDefinition[] apiVersion?: string allowCreateMetaDoc?: boolean + callback?: ((args: PluginCallbackArgs) => Promise) | null } // Context version of config @@ -46,6 +58,11 @@ export type Metadata = { translations: TranslationReference[] } +export type MetadataDocument = SanityDocumentLike & { + schemaTypes: string[] + translations: TranslationReference[] +} + export type DocumentInternationalizationMenuProps = { schemaType: ObjectSchemaType documentId: string