@sanity/assist 4.3.0 → 4.3.2

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/assist",
3
- "version": "4.3.0",
3
+ "version": "4.3.2",
4
4
  "description": "You create the instructions; Sanity AI Assist does the rest.",
5
5
  "keywords": [
6
6
  "sanity",
@@ -20,6 +20,7 @@ import {
20
20
  instructionTaskTypeName,
21
21
  } from '../types'
22
22
  import {randomKey} from '../_lib/randomKey'
23
+ import {isDefined} from '../helpers/misc'
23
24
 
24
25
  export interface AgentActionConditionalPath {
25
26
  path: AgentActionPath
@@ -159,7 +160,11 @@ export type AssistFieldActionGroup = Omit<
159
160
  DocumentFieldActionGroup,
160
161
  'renderAsButton' | 'expanded' | 'children'
161
162
  > & {
162
- children: AssistFieldActionNode[]
163
+ /**
164
+ * `children` can include undefined entries in the action array. These will be filtered out.
165
+ * If the group has no defined children, the group will also be filtered out.
166
+ */
167
+ children: (AssistFieldActionNode | undefined)[]
163
168
  }
164
169
 
165
170
  type PushToast = (params: ToastParams) => string
@@ -206,14 +211,17 @@ export function useCustomFieldActions(
206
211
 
207
212
  return useMemo(() => {
208
213
  const title = fieldActions?.title
209
- const customActions = configActions?.map((node) => {
210
- return createSafeNode({
211
- node,
212
- pushToast,
213
- addSyntheticTask,
214
- removeSyntheticTask,
214
+ const customActions = configActions
215
+ ?.filter(isDefined)
216
+ .map((node) => {
217
+ return createSafeNode({
218
+ node,
219
+ pushToast,
220
+ addSyntheticTask,
221
+ removeSyntheticTask,
222
+ })
215
223
  })
216
- })
224
+ .filter(isDefined)
217
225
  const onlyGroups =
218
226
  customActions?.length && customActions?.every((node) => node.type === 'group')
219
227
  const groups = customActions?.length
@@ -237,17 +245,25 @@ function createSafeNode(args: {
237
245
  pushToast: PushToast
238
246
  addSyntheticTask: (task: InstructionTask) => void
239
247
  removeSyntheticTask: (task: InstructionTask) => void
240
- }): DocumentFieldActionNode {
248
+ }): DocumentFieldActionNode | undefined {
241
249
  const {node} = args
242
250
  switch (node.type) {
243
251
  case 'action':
244
252
  return createSafeAction({...args, action: node})
245
253
  case 'group':
254
+ // eslint-disable-next-line no-case-declarations
255
+ const children = node.children
256
+ ?.filter(isDefined)
257
+ .map((child) => createSafeNode({...args, node: child}))
258
+ .filter(isDefined)
259
+ if (!children?.length) {
260
+ return undefined
261
+ }
246
262
  return {
247
263
  ...node,
248
264
  renderAsButton: false,
249
265
  expanded: true,
250
- children: node.children?.map((child) => createSafeNode({...args, node: child})),
266
+ children,
251
267
  }
252
268
  case 'divider':
253
269
  default:
@@ -19,3 +19,7 @@ export function getPathKey(path: Path | string) {
19
19
  export function getInstructionTitle(instruction?: StudioInstruction) {
20
20
  return instruction?.title ?? 'Untitled'
21
21
  }
22
+
23
+ export function isDefined<T>(t: T | undefined | null): t is T {
24
+ return t !== undefined && t !== null
25
+ }
@@ -4,6 +4,12 @@ export function isPortableTextArray(type: ArraySchemaType) {
4
4
  return type.of.find((t) => isType(t, 'block'))
5
5
  }
6
6
 
7
+ /**
8
+ * Returns true if the `schemaType` or any of its parent types (`schemaType.type`)` has `name` equal
9
+ * to `typeName`.
10
+ *
11
+ * Useful for checking if `schemaType` is a type alias of `ìmage`, `code` or similar.
12
+ */
7
13
  export function isType(schemaType: SchemaType, typeName: string): boolean {
8
14
  if (schemaType.name === typeName) {
9
15
  return true
package/src/index.ts CHANGED
@@ -22,3 +22,5 @@ export {
22
22
  type CustomInputResult,
23
23
  useUserInput,
24
24
  } from './fieldActions/useUserInput'
25
+
26
+ export {isType} from './helpers/typeUtils'
package/src/plugin.tsx CHANGED
@@ -33,7 +33,10 @@ export interface AssistPluginConfig {
33
33
 
34
34
  fieldActions?: {
35
35
  title?: string
36
- useFieldActions?: (props: AssistFieldActionProps) => AssistFieldActionNode[]
36
+ /**
37
+ * The returned array can include `undefined` entries in the action array. These will be filtered out.
38
+ */
39
+ useFieldActions?: (props: AssistFieldActionProps) => (AssistFieldActionNode | undefined)[]
37
40
  }
38
41
 
39
42
  /**