@sanity/embeddings-index-ui 1.0.1 → 1.0.3

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.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Various Sanity Studio plugins for integrating with the embeddings index API",
5
5
  "keywords": [
6
6
  "sanity",
@@ -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
- () => (customApiClient ? customApiClient(client) : client),
10
- [client, customApiClient],
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
  }
@@ -0,0 +1,53 @@
1
+ import {useClient} from 'sanity'
2
+ import {createContext, PropsWithChildren, useContext, useEffect, useState} from 'react'
3
+ import {Card, Text} from '@sanity/ui'
4
+
5
+ const featureName = 'embeddingsIndexApi'
6
+
7
+ export type FeatureStatus = 'enabled' | 'disabled' | 'loading'
8
+ export const FeatureEnabledContext = createContext<FeatureStatus>('loading')
9
+
10
+ export function useIsFeatureEnabled() {
11
+ const client = useClient({apiVersion: '2023-09-01'})
12
+ const [status, setStatus] = useState<FeatureStatus>('loading')
13
+
14
+ useEffect(() => {
15
+ client
16
+ .request<string | boolean>({
17
+ method: 'GET',
18
+ url: `/projects/${client.config().projectId}/features/${featureName}`,
19
+ })
20
+ .then((isEnabled) => {
21
+ setStatus(isEnabled === 'true' || isEnabled === true ? 'enabled' : 'disabled')
22
+ })
23
+ .catch((err) => {
24
+ console.error(err)
25
+ setStatus('disabled')
26
+ })
27
+ }, [client])
28
+
29
+ return status
30
+ }
31
+
32
+ export function FeatureEnabledProvider(props: PropsWithChildren<{}>) {
33
+ const status = useIsFeatureEnabled()
34
+ return (
35
+ <FeatureEnabledContext.Provider value={status}>{props.children}</FeatureEnabledContext.Provider>
36
+ )
37
+ }
38
+
39
+ export function useIsFeatureEnabledContext(): FeatureStatus {
40
+ return useContext(FeatureEnabledContext)
41
+ }
42
+
43
+ export function FeatureDisabledNotice() {
44
+ return (
45
+ <Card tone="primary" border padding={2}>
46
+ <Text size={1}>
47
+ Embeddings index APIs are only available on the{' '}
48
+ <a href="https://sanity.io/pricing">Team tier and above</a>. Please upgrade to enable
49
+ access.
50
+ </Text>
51
+ </Card>
52
+ )
53
+ }
@@ -1,23 +1,28 @@
1
1
  import {Box, Button, Card, Flex, Heading, Spinner, Stack} from '@sanity/ui'
2
- import {useClient} from 'sanity'
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'
9
+ import {FeatureDisabledNotice, useIsFeatureEnabled} from '../api/isEnabled'
14
10
 
15
11
  export function EmbeddingsIndexTool() {
12
+ const featureState = useIsFeatureEnabled()
16
13
  return (
17
14
  <Card>
18
15
  <Flex justify="center" flex={1}>
19
16
  <Card flex={1} style={{maxWidth: 1200}} padding={5}>
20
- <Indexes />
17
+ {featureState == 'loading' ? (
18
+ <Box padding={2}>
19
+ <Spinner />
20
+ </Box>
21
+ ) : null}
22
+
23
+ {featureState == 'disabled' ? <FeatureDisabledNotice /> : null}
24
+
25
+ {featureState == 'enabled' ? <Indexes /> : null}
21
26
  </Card>
22
27
  </Flex>
23
28
  </Card>
@@ -1,19 +1,21 @@
1
+ import {ObjectInputProps, ReferenceSchemaType, set, setIfMissing, typed, unset} from 'sanity'
1
2
  import {
2
- ObjectInputProps,
3
- ReferenceSchemaType,
4
- set,
5
- setIfMissing,
6
- typed,
7
- unset,
8
- useClient,
9
- } from 'sanity'
10
- import {Autocomplete, Box, Button, Flex, Text, AutocompleteOpenButtonProps} from '@sanity/ui'
3
+ Autocomplete,
4
+ Box,
5
+ Button,
6
+ Flex,
7
+ Text,
8
+ AutocompleteOpenButtonProps,
9
+ Spinner,
10
+ } from '@sanity/ui'
11
11
  import {EarthGlobeIcon, LinkIcon} from '@sanity/icons'
12
12
  import {useCallback, useEffect, useId, useMemo, useRef, useState} from 'react'
13
13
  import {DocumentPreview} from '../preview/DocumentPreview'
14
14
  import {useDocumentPane} from 'sanity/desk'
15
15
  import {queryIndex, QueryResult} from '../api/embeddingsApi'
16
16
  import {publicId} from '../utils/id'
17
+ import {useApiClient} from '../api/embeddingsApiHooks'
18
+ import {FeatureDisabledNotice, useIsFeatureEnabledContext} from '../api/isEnabled'
17
19
 
18
20
  interface Option {
19
21
  result: QueryResult
@@ -27,13 +29,27 @@ export function SemanticSearchReferenceInput(props: ObjectInputProps) {
27
29
  const defaultEnabled =
28
30
  (props.schemaType as ReferenceSchemaType)?.options?.embeddingsIndex?.searchMode === 'embeddings'
29
31
 
32
+ const featureState = useIsFeatureEnabledContext()
33
+
30
34
  const [semantic, setSemantic] = useState<boolean>(defaultEnabled)
31
35
  const toggleSemantic = useCallback(() => setSemantic((current) => !current), [])
32
36
 
33
37
  return (
34
38
  <Flex gap={2} flex={1} style={{width: '100%'}}>
39
+ {semantic && featureState == 'loading' ? (
40
+ <Box padding={2}>
41
+ <Spinner />
42
+ </Box>
43
+ ) : null}
44
+
45
+ {semantic && featureState == 'disabled' ? <FeatureDisabledNotice /> : null}
46
+
35
47
  <Box flex={1} style={{maxHeight: 36, overflow: 'hidden'}}>
36
- {semantic ? <SemanticSearchInput {...props} /> : props.renderDefault(props)}
48
+ {semantic && featureState == 'enabled' ? (
49
+ <SemanticSearchInput {...props} />
50
+ ) : (
51
+ props.renderDefault(props)
52
+ )}
37
53
  </Box>
38
54
  <Button
39
55
  icon={semantic ? EarthGlobeIcon : LinkIcon}
@@ -60,11 +76,6 @@ function useDebouncedValue<T>(value: T, ms: number) {
60
76
  return debouncedValue
61
77
  }
62
78
 
63
- function useApiClient() {
64
- const client = useClient({apiVersion: 'vX'})
65
- return useMemo(() => client, [client])
66
- }
67
-
68
79
  function SemanticSearchInput(props: ObjectInputProps) {
69
80
  const {onPathFocus, onChange, readOnly, schemaType, value} = props
70
81
 
@@ -1,9 +1,17 @@
1
1
  import {definePlugin, isObjectInputProps, ObjectInputProps, ReferenceSchemaType} from 'sanity'
2
2
  import {SemanticSearchReferenceInput} from './SemanticSearchReferenceInput'
3
3
  import {isType} from '../utils/types'
4
+ import {FeatureEnabledProvider} from '../api/isEnabled'
4
5
 
5
6
  export const embeddingsIndexReferenceInput = definePlugin({
6
7
  name: '@sanity/embeddings-index-reference-input',
8
+ studio: {
9
+ components: {
10
+ layout: (props) => {
11
+ return <FeatureEnabledProvider>{props.renderDefault(props)}</FeatureEnabledProvider>
12
+ },
13
+ },
14
+ },
7
15
  form: {
8
16
  components: {
9
17
  input: (props) => {