Skip to content

Commit

Permalink
fix: aiWritingAssistance.exclude: true should disable document schema…
Browse files Browse the repository at this point in the history
… assist actions for all fields
  • Loading branch information
snorrees committed Oct 11, 2023
1 parent f33f35b commit 2571be6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion plugin/src/fieldActions/assistFieldActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {PrivateIcon} from './PrivateIcon'
import {generateCaptionsActions} from './generateCaptionActions'
import {useDocumentPane} from 'sanity/desk'
import {useSelectedField, useTypePath} from '../assistInspector/helpers'
import {isSchemaAssistEnabled} from '../helpers/assistSupported'

function node(node: DocumentFieldActionItem | DocumentFieldActionGroup) {
return node
Expand Down Expand Up @@ -69,7 +70,10 @@ export const assistFieldActions: DocumentFieldAction = {
})

const isSelectable = !!useSelectedField(documentSchemaType, typePath)
const assistSupported = useAssistSupported(props.path, schemaType) && isSelectable
const assistSupported =
useAssistSupported(props.path, schemaType) &&
isSelectable &&
isSchemaAssistEnabled(documentSchemaType)

const fieldAssist = useMemo(
() =>
Expand Down
5 changes: 4 additions & 1 deletion plugin/src/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export const assist = definePlugin<AssistPluginConfig | void>((config) => {
},
unstable_languageFilter: (prev, {documentId, schema, schemaType}) => {
const docSchema = schema.get(schemaType) as ObjectSchemaType
return [...prev, createAssistDocumentPresence(documentId, docSchema)]
if (isSchemaAssistEnabled(docSchema)) {
return [...prev, createAssistDocumentPresence(documentId, docSchema)]
}
return prev
},
},

Expand Down
22 changes: 22 additions & 0 deletions plugin/src/schemas/serialize/serializeSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ const mockStudioTypes = [
]

describe('serializeSchema', () => {
test('should not serialize excluded document schema', () => {
const schema = Schema.compile({
name: 'test',
types: [
defineType({
type: 'document',
name: 'article',
fields: [{type: 'string', name: 'title'}],
options: {
aiWritingAssistance: {
exclude: true,
},
},
}),
],
})

const serializedTypes = serializeSchema(schema, {leanFormat: true})

expect(serializedTypes).toEqual([])
})

test('should serialize simple schema', () => {
const schema = Schema.compile({
name: 'test',
Expand Down
1 change: 1 addition & 0 deletions plugin/src/schemas/serialize/serializeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function serializeSchema(schema: Schema, options?: Options): SerializedSc
.filter((t) => !(hiddenTypes.includes(t) || t.startsWith('sanity.')))
.map((t) => schema.get(t))
.filter((t): t is SchemaType => !!t)
.filter((t) => isAssistSupported(t))
.filter((t) => !t.hidden && !t.readOnly)
.map((t) => getSchemaStub(t, schema, options))
.filter((t) => {
Expand Down

0 comments on commit 2571be6

Please sign in to comment.