@sanity/assist 1.2.9 → 1.2.11

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": "1.2.9",
3
+ "version": "1.2.11",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "sanity",
@@ -24,9 +24,14 @@ export function FieldRefPathInput(props: StringInputProps) {
24
24
  if (!field.key.includes('|') || !typePath) {
25
25
  return true
26
26
  }
27
- const dotSplit = typePath.split('.')
28
- const base = dotSplit.slice(0, dotSplit.length - 1).join('.')
29
- return field.key.includes(base)
27
+ if (field.key.includes('|') && !typePath.includes('|')) {
28
+ return false
29
+ }
30
+
31
+ const fieldSegments = field.key.split('.')
32
+ const lastArrayItemIndex = fieldSegments.findLastIndex((s) => s.includes('|'))
33
+ const mustStartWith = fieldSegments.slice(0, lastArrayItemIndex + 1).join('.')
34
+ return typePath.startsWith(mustStartWith)
30
35
  },
31
36
  [typePath]
32
37
  )
@@ -1,9 +1,10 @@
1
1
  import {createContext, useEffect, useMemo, useState} from 'react'
2
- import {InputProps, pathToString} from 'sanity'
2
+ import {InputProps, pathToString, useSyncState} from 'sanity'
3
3
  import {getCaptionFieldOption} from '../helpers/typeUtils'
4
4
  import {useAssistDocumentContext} from '../assistDocument/AssistDocumentContext'
5
5
  import {useApiClient, useGenerateCaption} from '../useApiClient'
6
6
  import {useAiAssistanceConfig} from '../assistLayout/AiAssistanceConfigContext'
7
+ import {publicId} from '../helpers/ids'
7
8
 
8
9
  export interface ImageContextValue {
9
10
  captionPath: string
@@ -17,18 +18,20 @@ export function ImageContextProvider(props: InputProps) {
17
18
  const assetRef = (value as any)?.asset?._ref
18
19
  const [assetRefState, setAssetRefState] = useState<string | undefined>(assetRef)
19
20
 
20
- const {documentId} = useAssistDocumentContext()
21
+ const {documentId, documentSchemaType} = useAssistDocumentContext()
21
22
  const {config} = useAiAssistanceConfig()
22
23
  const apiClient = useApiClient(config?.__customApiClient)
23
24
  const {generateCaption} = useGenerateCaption(apiClient)
24
25
 
26
+ const {isSyncing} = useSyncState(publicId(documentId), documentSchemaType.name)
27
+
25
28
  useEffect(() => {
26
29
  const captionField = getCaptionFieldOption(schemaType)
27
- if (assetRef && documentId && captionField && assetRef !== assetRefState) {
30
+ if (assetRef && documentId && captionField && assetRef !== assetRefState && !isSyncing) {
28
31
  setAssetRefState(assetRef)
29
32
  generateCaption({path: pathToString([...path, captionField]), documentId: documentId})
30
33
  }
31
- }, [schemaType, path, assetRef, assetRefState, documentId, generateCaption])
34
+ }, [schemaType, path, assetRef, assetRefState, documentId, generateCaption, isSyncing])
32
35
 
33
36
  const context: ImageContextValue | undefined = useMemo(() => {
34
37
  const captionField = getCaptionFieldOption(schemaType)