@sanity/embeddings-index-ui 1.1.0 → 1.1.1

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/embeddings-index-ui",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Various Sanity Studio plugins for integrating with the embeddings index API",
5
5
  "keywords": [
6
6
  "sanity",
@@ -1,6 +1,8 @@
1
1
  import {useCallback, useMemo} from 'react'
2
2
  import {SemanticSearchAutocomplete} from '../referenceInput/SemanticSearchAutocomplete'
3
3
  import {EmbeddingsIndexConfig} from '../schemas/typeDefExtensions'
4
+ import {useRouter} from 'sanity/router'
5
+ import {QueryResult} from '../api/embeddingsApi'
4
6
 
5
7
  export function QueryIndex(props: {indexName: string}) {
6
8
  const {indexName} = props
@@ -9,5 +11,21 @@ export function QueryIndex(props: {indexName: string}) {
9
11
  () => ({indexName, maxResults: 8}),
10
12
  [indexName],
11
13
  )
12
- return <SemanticSearchAutocomplete getEmptySearchValue={getEmpty} indexConfig={indexConfig} />
14
+
15
+ const {resolveIntentLink, navigateUrl} = useRouter()
16
+ const onSelect = useCallback(
17
+ (hit: QueryResult) => {
18
+ navigateUrl({
19
+ path: resolveIntentLink('edit', {id: hit.value.documentId, type: hit.value.type}),
20
+ })
21
+ },
22
+ [resolveIntentLink, navigateUrl],
23
+ )
24
+ return (
25
+ <SemanticSearchAutocomplete
26
+ getEmptySearchValue={getEmpty}
27
+ indexConfig={indexConfig}
28
+ onSelect={onSelect}
29
+ />
30
+ )
13
31
  }
@@ -21,7 +21,7 @@ export interface SemanticSearchAutocompleteProps {
21
21
  getEmptySearchValue: () => string
22
22
  typeFilter?: string[]
23
23
  filterResult?: (hit: QueryResult) => boolean
24
- onChange?: (value: string) => void
24
+ onSelect?: (value: QueryResult) => void
25
25
  onFocus?: FocusEventHandler<HTMLInputElement>
26
26
  onBlur?: FocusEventHandler<HTMLInputElement>
27
27
  readOnly?: boolean
@@ -52,7 +52,7 @@ export const SemanticSearchAutocomplete = forwardRef(function SemanticSearchAuto
52
52
  readOnly,
53
53
  onFocus,
54
54
  onBlur,
55
- onChange,
55
+ onSelect,
56
56
  typeFilter,
57
57
  } = props
58
58
  const id = useId()
@@ -138,9 +138,14 @@ export const SemanticSearchAutocomplete = forwardRef(function SemanticSearchAuto
138
138
  setOptions(NO_OPTIONS)
139
139
  return
140
140
  }
141
- onChange?.(value)
141
+ const option = (options as Option[])
142
+ .filter((r): r is Option => 'result' in r)
143
+ .find((r) => r.result.value.documentId === value)
144
+ if (option && onSelect) {
145
+ onSelect(option.result)
146
+ }
142
147
  },
143
- [onChange],
148
+ [onSelect, options],
144
149
  )
145
150
 
146
151
  return (
@@ -112,17 +112,16 @@ function SemanticSearchInput(props: ObjectInputProps & {indexConfig: EmbeddingsI
112
112
  const handleBlur = useCallback(() => onPathFocus([]), [onPathFocus])
113
113
 
114
114
  const handleChange = useCallback(
115
- (nextId: string) => {
116
- if (!nextId) {
115
+ (result: QueryResult) => {
116
+ if (!result) {
117
117
  onChange(unset())
118
118
  onPathFocus([])
119
119
  return
120
120
  }
121
-
122
121
  const patches = [
123
122
  setIfMissing({}),
124
123
  set(schemaType.name, ['_type']),
125
- set(publicId(nextId), ['_ref']),
124
+ set(publicId(result.value.documentId), ['_ref']),
126
125
  unset(['_weak']),
127
126
  unset(['_strengthenOnPublish']),
128
127
  ]
@@ -150,7 +149,7 @@ function SemanticSearchInput(props: ObjectInputProps & {indexConfig: EmbeddingsI
150
149
  ref={autocompleteRef}
151
150
  typeFilter={typeFilter}
152
151
  indexConfig={indexConfig}
153
- onChange={handleChange}
152
+ onSelect={handleChange}
154
153
  onFocus={handleFocus}
155
154
  onBlur={handleBlur}
156
155
  getEmptySearchValue={getEmptySearchValue}