@sanity/embeddings-index-ui 1.0.0 → 1.0.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/README.md +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.esm.js +34 -39
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +33 -38
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/api/embeddingsApi.ts +1 -1
- package/src/api/embeddingsApiHooks.ts +12 -7
- package/src/embeddingsIndexDashboard/EmbeddingsIndexTool.tsx +2 -7
- package/src/embeddingsIndexDashboard/QueryIndex.tsx +2 -1
- package/src/preview/DocumentPreview.tsx +7 -11
- package/src/referenceInput/SemanticSearchReferenceInput.tsx +6 -17
package/package.json
CHANGED
package/src/api/embeddingsApi.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import {SanityClient, useClient} from 'sanity'
|
|
2
2
|
import {useMemo} from 'react'
|
|
3
3
|
|
|
4
|
-
export function useApiClient(
|
|
5
|
-
customApiClient?: (defaultClient: SanityClient) => SanityClient,
|
|
6
|
-
): SanityClient {
|
|
4
|
+
export function useApiClient(): SanityClient {
|
|
7
5
|
const client = useClient({apiVersion: 'vX'})
|
|
8
|
-
return useMemo(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
return useMemo(() => {
|
|
7
|
+
const customHost = localStorage.getItem('embeddings-index-host')
|
|
8
|
+
if (customHost) {
|
|
9
|
+
return client.withConfig({
|
|
10
|
+
apiHost: customHost,
|
|
11
|
+
useProjectHostname: false,
|
|
12
|
+
withCredentials: false,
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
return client
|
|
16
|
+
}, [client])
|
|
12
17
|
}
|
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
import {Box, Button, Card, Flex, Heading, Spinner, Stack} from '@sanity/ui'
|
|
2
|
-
import {
|
|
3
|
-
import {useCallback, useEffect, useMemo, useState} from 'react'
|
|
2
|
+
import {useCallback, useEffect, useState} from 'react'
|
|
4
3
|
import {AddIcon, UndoIcon} from '@sanity/icons'
|
|
5
4
|
import {deleteIndex, getIndexes, IndexState, NamedIndex} from '../api/embeddingsApi'
|
|
6
5
|
import {EditIndexDialog} from './IndexEditor'
|
|
7
6
|
import {IndexList} from './IndexList'
|
|
8
7
|
import {IndexInfo} from './IndexInfo'
|
|
9
|
-
|
|
10
|
-
function useApiClient() {
|
|
11
|
-
const client = useClient({apiVersion: 'vX'})
|
|
12
|
-
return useMemo(() => client, [client])
|
|
13
|
-
}
|
|
8
|
+
import {useApiClient} from '../api/embeddingsApiHooks'
|
|
14
9
|
|
|
15
10
|
export function EmbeddingsIndexTool() {
|
|
16
11
|
return (
|
|
@@ -92,10 +92,11 @@ export function ResultList(props: {results: QueryResult[]; query: string}) {
|
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
function ResultEntry(props: {result: QueryResult}) {
|
|
95
|
+
const value = props.result.value
|
|
95
96
|
return (
|
|
96
97
|
<Flex gap={4} align="center">
|
|
97
98
|
<Box flex={1}>
|
|
98
|
-
<DocumentPreview documentId={
|
|
99
|
+
<DocumentPreview documentId={value.documentId} schemaTypeName={value.type} button />
|
|
99
100
|
</Box>
|
|
100
101
|
<Box>
|
|
101
102
|
<Text muted size={1}>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {CSSProperties,
|
|
1
|
+
import {CSSProperties, useMemo} from 'react'
|
|
2
2
|
import {useMemoObservable} from 'react-rx'
|
|
3
3
|
import {
|
|
4
4
|
DefaultPreview,
|
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
SanityDefaultPreview,
|
|
8
8
|
SanityDocument,
|
|
9
9
|
SchemaType,
|
|
10
|
-
useClient,
|
|
11
10
|
useDocumentPreviewStore,
|
|
12
11
|
useSchema,
|
|
13
12
|
} from 'sanity'
|
|
@@ -17,6 +16,7 @@ import {ErrorOutlineIcon} from '@sanity/icons'
|
|
|
17
16
|
|
|
18
17
|
interface ResultPreviewProps {
|
|
19
18
|
documentId: string
|
|
19
|
+
schemaTypeName: string
|
|
20
20
|
button?: boolean
|
|
21
21
|
style?: CSSProperties
|
|
22
22
|
}
|
|
@@ -24,18 +24,13 @@ interface ResultPreviewProps {
|
|
|
24
24
|
export function DocumentPreview({
|
|
25
25
|
documentId,
|
|
26
26
|
style,
|
|
27
|
+
schemaTypeName,
|
|
27
28
|
...buttonProps
|
|
28
29
|
}: ResultPreviewProps & ButtonProps) {
|
|
29
|
-
const client = useClient({apiVersion: '2023-06-06'})
|
|
30
|
-
const [type, setType] = useState<string | undefined>(undefined)
|
|
31
30
|
const schema = useSchema()
|
|
32
|
-
const schemaType =
|
|
31
|
+
const schemaType = schemaTypeName ? schema.get(schemaTypeName) : undefined
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
client.fetch<string>(`* [_id==$id]._type`, {id: documentId}).then(setType)
|
|
36
|
-
}, [documentId, client])
|
|
37
|
-
|
|
38
|
-
if (!type) {
|
|
33
|
+
if (!schemaTypeName) {
|
|
39
34
|
return (
|
|
40
35
|
<Card style={{minHeight: '36px'}}>
|
|
41
36
|
<DefaultPreview
|
|
@@ -58,7 +53,7 @@ export function DocumentPreview({
|
|
|
58
53
|
media={() => <ErrorOutlineIcon />}
|
|
59
54
|
title={
|
|
60
55
|
<>
|
|
61
|
-
Unknown type <code>{
|
|
56
|
+
Unknown type <code>{schemaTypeName ?? 'N/A'}</code> for {documentId}
|
|
62
57
|
</>
|
|
63
58
|
}
|
|
64
59
|
/>
|
|
@@ -69,6 +64,7 @@ export function DocumentPreview({
|
|
|
69
64
|
return (
|
|
70
65
|
<DocumentPreviewInner
|
|
71
66
|
documentId={documentId}
|
|
67
|
+
schemaTypeName={schemaTypeName}
|
|
72
68
|
schemaType={schemaType}
|
|
73
69
|
style={style}
|
|
74
70
|
{...buttonProps}
|
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ObjectInputProps,
|
|
3
|
-
ReferenceSchemaType,
|
|
4
|
-
set,
|
|
5
|
-
setIfMissing,
|
|
6
|
-
typed,
|
|
7
|
-
unset,
|
|
8
|
-
useClient,
|
|
9
|
-
} from 'sanity'
|
|
1
|
+
import {ObjectInputProps, ReferenceSchemaType, set, setIfMissing, typed, unset} from 'sanity'
|
|
10
2
|
import {Autocomplete, Box, Button, Flex, Text, AutocompleteOpenButtonProps} from '@sanity/ui'
|
|
11
3
|
import {EarthGlobeIcon, LinkIcon} from '@sanity/icons'
|
|
12
4
|
import {useCallback, useEffect, useId, useMemo, useRef, useState} from 'react'
|
|
@@ -14,6 +6,7 @@ import {DocumentPreview} from '../preview/DocumentPreview'
|
|
|
14
6
|
import {useDocumentPane} from 'sanity/desk'
|
|
15
7
|
import {queryIndex, QueryResult} from '../api/embeddingsApi'
|
|
16
8
|
import {publicId} from '../utils/id'
|
|
9
|
+
import {useApiClient} from '../api/embeddingsApiHooks'
|
|
17
10
|
|
|
18
11
|
interface Option {
|
|
19
12
|
result: QueryResult
|
|
@@ -60,11 +53,6 @@ function useDebouncedValue<T>(value: T, ms: number) {
|
|
|
60
53
|
return debouncedValue
|
|
61
54
|
}
|
|
62
55
|
|
|
63
|
-
function useApiClient() {
|
|
64
|
-
const client = useClient({apiVersion: 'vX'})
|
|
65
|
-
return useMemo(() => client, [client])
|
|
66
|
-
}
|
|
67
|
-
|
|
68
56
|
function SemanticSearchInput(props: ObjectInputProps) {
|
|
69
57
|
const {onPathFocus, onChange, readOnly, schemaType, value} = props
|
|
70
58
|
|
|
@@ -116,7 +104,7 @@ function SemanticSearchInput(props: ObjectInputProps) {
|
|
|
116
104
|
|
|
117
105
|
queryIndex(
|
|
118
106
|
{
|
|
119
|
-
query: queryString,
|
|
107
|
+
query: queryString.trim().length ? queryString : JSON.stringify(docRef.current) ?? '',
|
|
120
108
|
indexName,
|
|
121
109
|
maxResults,
|
|
122
110
|
filter: {
|
|
@@ -194,7 +182,7 @@ function SemanticSearchInput(props: ObjectInputProps) {
|
|
|
194
182
|
id={id}
|
|
195
183
|
ref={autocompleteRef}
|
|
196
184
|
data-testid="semantic-autocomplete"
|
|
197
|
-
placeholder="Type to search"
|
|
185
|
+
placeholder="Type to search..."
|
|
198
186
|
openButton={openButtonConfig}
|
|
199
187
|
onFocus={handleFocus}
|
|
200
188
|
onChange={handleChange}
|
|
@@ -210,11 +198,12 @@ function SemanticSearchInput(props: ObjectInputProps) {
|
|
|
210
198
|
}
|
|
211
199
|
|
|
212
200
|
function AutocompleteOption(props: Option) {
|
|
201
|
+
const value = props.result.value
|
|
213
202
|
return (
|
|
214
203
|
<Button mode="bleed" padding={1} style={{width: '100%'}}>
|
|
215
204
|
<Flex gap={2} align="center">
|
|
216
205
|
<Box flex={1}>
|
|
217
|
-
<DocumentPreview documentId={
|
|
206
|
+
<DocumentPreview documentId={value.documentId} schemaTypeName={value.type} />
|
|
218
207
|
</Box>
|
|
219
208
|
<Box padding={2}>
|
|
220
209
|
<Text size={1} muted title={'Relevance'}>
|