@sanity/assist 1.2.2 → 1.2.4

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.2",
3
+ "version": "1.2.4",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "sanity",
@@ -28,7 +28,7 @@ import {
28
28
  import {BackToInstructionListLink} from './instruction/BackToInstructionsLink'
29
29
  import {useAiPaneRouter} from '../../assistInspector/helpers'
30
30
  import {SelectedFieldContextProvider, SelectedFieldContextValue} from './SelectedFieldContext'
31
- import {Stack} from '@sanity/ui'
31
+ import {Card, Stack, Text} from '@sanity/ui'
32
32
  import {documentTypeFromAiDocumentId} from '../../helpers/ids'
33
33
 
34
34
  const EMPTY_FIELDS: AssistField[] = []
@@ -36,6 +36,16 @@ const EMPTY_FIELDS: AssistField[] = []
36
36
  export const TypePathContext = createContext<string | undefined>(undefined)
37
37
 
38
38
  export function AssistDocumentForm(props: ObjectInputProps) {
39
+ if (props.readOnly) {
40
+ return (
41
+ <Card border tone="caution" padding={2}>
42
+ <Text size={1}> You do not have sufficient permissions to manage instructions.</Text>
43
+ </Card>
44
+ )
45
+ }
46
+ return <AssistDocumentFormEditable {...props} />
47
+ }
48
+ function AssistDocumentFormEditable(props: ObjectInputProps) {
39
49
  const {onChange} = props
40
50
  const value = props.value as AssistDocument | undefined
41
51
  const id = value?._id
@@ -26,11 +26,13 @@ import {maxHistoryVisibilityMs} from '../../constants'
26
26
  interface UseAssistDocumentProps {
27
27
  documentId: string
28
28
  schemaType: ObjectSchemaType
29
+ initDoc?: boolean
29
30
  }
30
31
 
31
32
  export function useStudioAssistDocument({
32
33
  documentId,
33
34
  schemaType,
35
+ initDoc,
34
36
  }: UseAssistDocumentProps): StudioAssistDocument | undefined {
35
37
  const documentTypeName = schemaType.name
36
38
  const currentUser = useCurrentUser()
@@ -48,13 +50,17 @@ export function useStudioAssistDocument({
48
50
  const client = useClient({apiVersion: '2023-01-01'})
49
51
 
50
52
  useEffect(() => {
51
- if (!assistDocument) {
52
- client.createIfNotExists({
53
- _id: assistDocumentId(documentTypeName),
54
- _type: assistDocumentTypeName,
55
- })
53
+ if (!assistDocument && initDoc) {
54
+ client
55
+ .createIfNotExists({
56
+ _id: assistDocumentId(documentTypeName),
57
+ _type: assistDocumentTypeName,
58
+ })
59
+ .catch((e) => {
60
+ // best effort
61
+ })
56
62
  }
57
- }, [client, assistDocument, documentTypeName])
63
+ }, [client, assistDocument, documentTypeName, initDoc])
58
64
 
59
65
  return useMemo(() => {
60
66
  if (!assistDocument) {
@@ -213,7 +213,7 @@ export function AssistInspector(props: DocumentInspectorProps) {
213
213
 
214
214
  const aiDocId = assistDocumentId(documentType)
215
215
 
216
- const assistDocument = useStudioAssistDocument({documentId, schemaType})
216
+ const assistDocument = useStudioAssistDocument({documentId, schemaType, initDoc: true})
217
217
  const assistField = assistDocument?.fields?.find((f) => f.path === typePath)
218
218
  const instruction = assistField?.instructions?.find((i) => i._key === instructionKey)
219
219
  const tasks = useMemo(
@@ -392,6 +392,36 @@ describe('serializeSchema', () => {
392
392
  ])
393
393
  })
394
394
 
395
+ test('should serialize list values in string array', () => {
396
+ const schema = Schema.compile({
397
+ name: 'test',
398
+ types: [
399
+ defineType({
400
+ type: 'array',
401
+ name: 'list',
402
+ of: [{type: 'string'}],
403
+ options: {
404
+ list: [
405
+ {title: 'A', value: 'a'},
406
+ {title: 'B', value: 'b'},
407
+ ],
408
+ },
409
+ }),
410
+ ],
411
+ })
412
+
413
+ const serializedTypes = serializeSchema(schema, {leanFormat: true})
414
+
415
+ expect(serializedTypes).toEqual([
416
+ {
417
+ name: 'list',
418
+ type: 'array',
419
+ of: [{type: 'string', name: 'string', title: 'String'}],
420
+ values: ['a', 'b'],
421
+ },
422
+ ])
423
+ })
424
+
395
425
  test('should exclude truthy hidden and readonly', () => {
396
426
  const schema = Schema.compile({
397
427
  name: 'test',
@@ -83,10 +83,11 @@ function getBaseFields(
83
83
  imagePromptField: imagePromptField,
84
84
  }
85
85
  : undefined,
86
- values:
87
- type.jsonType === 'string' && type?.options?.list
88
- ? type?.options?.list.map((v) => (typeof v === 'string' ? v : v.value ?? `${v.title}`))
89
- : undefined,
86
+ values: type?.options?.list
87
+ ? type?.options?.list.map((v: string | {value: string; title: string}) =>
88
+ typeof v === 'string' ? v : v.value ?? `${v.title}`
89
+ )
90
+ : undefined,
90
91
  of: 'of' in type && typeName === 'array' ? arrayOf(type, schema, options) : undefined,
91
92
  to:
92
93
  'to' in type && typeName === 'reference'
@@ -26,7 +26,6 @@ export interface InstructStatus {
26
26
  }
27
27
 
28
28
  const basePath = '/assist/tasks/instruction'
29
- const editorialAiFieldActionFeature = 'editorialAiFieldActions'
30
29
 
31
30
  export function useApiClient(customApiClient?: (defaultClient: SanityClient) => SanityClient) {
32
31
  const client = useClient({apiVersion: '2023-06-05'})
@@ -91,33 +90,22 @@ export function useGenerateCaption(apiClient: SanityClient) {
91
90
 
92
91
  export function useGetInstructStatus(apiClient: SanityClient) {
93
92
  const [loading, setLoading] = useState(true)
94
- const projectClient = useClient({apiVersion: '2023-06-05'})
95
93
 
96
94
  const getInstructStatus = useCallback(async () => {
97
95
  setLoading(true)
98
96
 
99
97
  const projectId = apiClient.config().projectId
100
98
  try {
101
- const features = await projectClient.request<string[]>({
102
- method: 'GET',
103
- url: `/projects/${projectId}/features`,
104
- })
105
-
106
- const enabled = features.some((f) => f === editorialAiFieldActionFeature)
107
-
108
- const status = await apiClient.request<Omit<InstructStatus, 'enabled'>>({
99
+ const status = await apiClient.request<InstructStatus>({
109
100
  method: 'GET',
110
101
  url: `${basePath}/${apiClient.config().dataset}/status?projectId=${projectId}`,
111
102
  })
112
103
 
113
- return {
114
- ...status,
115
- enabled,
116
- }
104
+ return status
117
105
  } finally {
118
106
  setLoading(false)
119
107
  }
120
- }, [setLoading, apiClient, projectClient])
108
+ }, [setLoading, apiClient])
121
109
 
122
110
  return {
123
111
  loading,