@sanity/context 0.0.3 → 0.0.5

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.
@@ -0,0 +1,48 @@
1
+ import {parse} from 'groq-js'
2
+
3
+ /**
4
+ * Convert a list of type names to a GROQ filter query
5
+ */
6
+ export const listToQuery = (types: string[]): string => {
7
+ const quoted = types.map((t) => `"${t}"`).join(', ')
8
+ return `_type in [${quoted}]`
9
+ }
10
+
11
+ /**
12
+ * Parse type names from a GROQ `_type in [...]` query
13
+ */
14
+ export const queryToList = (query: string): string[] => {
15
+ const match = query.match(/_type\s+in\s+\[([^\]]*)\]/)
16
+ if (!match?.[1]) return []
17
+
18
+ return match[1]
19
+ .split(',')
20
+ .map((s) => s.trim().replace(/"/g, ''))
21
+ .filter(Boolean)
22
+ }
23
+
24
+ /**
25
+ * Check if query is a simple `_type in [...]` filter that can be edited via the Types UI.
26
+ * Returns false for complex queries like `_type in ["a"] && published == true`
27
+ */
28
+ export const isSimpleTypeQuery = (query: string | undefined): boolean => {
29
+ if (!query) return true // Empty is simple (can start fresh)
30
+ return /^_type\s+in\s+\[[^\]]*\]$/.test(query.trim())
31
+ }
32
+
33
+ /**
34
+ * Validate a GROQ query string using groq-js parser
35
+ */
36
+ export const validateGroq = (query: string | undefined): {valid: boolean; error?: string} => {
37
+ if (!query) return {valid: true}
38
+
39
+ try {
40
+ parse(query)
41
+ return {valid: true}
42
+ } catch (e) {
43
+ return {
44
+ valid: false,
45
+ error: e instanceof Error ? e.message : 'Invalid GROQ syntax',
46
+ }
47
+ }
48
+ }
@@ -0,0 +1,17 @@
1
+ import {type Ref, useCallback} from 'react'
2
+
3
+ export function useComposedRefs<T>(...refs: (Ref<T> | undefined)[]): (node: T | null) => void {
4
+ return useCallback(
5
+ (node: T | null) => {
6
+ for (const ref of refs) {
7
+ if (typeof ref === 'function') {
8
+ ref(node)
9
+ } else if (ref) {
10
+ ;(ref as {current: T | null}).current = node
11
+ }
12
+ }
13
+ },
14
+ // eslint-disable-next-line react-hooks/exhaustive-deps
15
+ refs,
16
+ )
17
+ }
@@ -1,12 +1,10 @@
1
1
  import {definePlugin} from 'sanity'
2
- import {type StructureBuilder, structureTool} from 'sanity/structure'
3
2
 
4
3
  import {
5
4
  AGENT_CONTEXT_SCHEMA_TITLE,
6
5
  AGENT_CONTEXT_SCHEMA_TYPE_NAME,
7
6
  agentContextSchema,
8
7
  } from './agentContextSchema'
9
- import {AgentDocumentInput} from './AgentDocumentInput'
10
8
 
11
9
  /**
12
10
  * The plugin for the agent context.
@@ -30,36 +28,4 @@ export const contextPlugin = definePlugin({
30
28
  },
31
29
  ],
32
30
  },
33
-
34
- form: {
35
- components: {
36
- input: (props) => {
37
- if (props.schemaType.name === AGENT_CONTEXT_SCHEMA_TYPE_NAME) {
38
- return <AgentDocumentInput {...props} />
39
- }
40
-
41
- return props.renderDefault(props)
42
- },
43
- },
44
- },
45
-
46
- plugins: [
47
- structureTool({
48
- title: AGENT_CONTEXT_SCHEMA_TITLE,
49
- name: 'agent-context-structure',
50
- structure: (S: StructureBuilder) => {
51
- return S.list()
52
- .title('Content')
53
- .items([
54
- S.listItem()
55
- .title(AGENT_CONTEXT_SCHEMA_TITLE)
56
- .child(
57
- S.documentTypeList(AGENT_CONTEXT_SCHEMA_TYPE_NAME).title(
58
- AGENT_CONTEXT_SCHEMA_TITLE,
59
- ),
60
- ),
61
- ])
62
- },
63
- }),
64
- ],
65
31
  })
@@ -1 +1,6 @@
1
+ // Add exports here for the `@sanity/context/studio` package
2
+ export {
3
+ AGENT_CONTEXT_SCHEMA_TYPE_NAME,
4
+ agentContextSchema,
5
+ } from './context-plugin/agentContextSchema'
1
6
  export {contextPlugin} from './context-plugin/plugin'