@sanity/embeddings-index-ui 1.0.2 → 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.2",
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",
@@ -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
+ }
@@ -6,13 +6,23 @@ import {EditIndexDialog} from './IndexEditor'
6
6
  import {IndexList} from './IndexList'
7
7
  import {IndexInfo} from './IndexInfo'
8
8
  import {useApiClient} from '../api/embeddingsApiHooks'
9
+ import {FeatureDisabledNotice, useIsFeatureEnabled} from '../api/isEnabled'
9
10
 
10
11
  export function EmbeddingsIndexTool() {
12
+ const featureState = useIsFeatureEnabled()
11
13
  return (
12
14
  <Card>
13
15
  <Flex justify="center" flex={1}>
14
16
  <Card flex={1} style={{maxWidth: 1200}} padding={5}>
15
- <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}
16
26
  </Card>
17
27
  </Flex>
18
28
  </Card>
@@ -1,5 +1,13 @@
1
1
  import {ObjectInputProps, ReferenceSchemaType, set, setIfMissing, typed, unset} from 'sanity'
2
- import {Autocomplete, Box, Button, Flex, Text, AutocompleteOpenButtonProps} from '@sanity/ui'
2
+ import {
3
+ Autocomplete,
4
+ Box,
5
+ Button,
6
+ Flex,
7
+ Text,
8
+ AutocompleteOpenButtonProps,
9
+ Spinner,
10
+ } from '@sanity/ui'
3
11
  import {EarthGlobeIcon, LinkIcon} from '@sanity/icons'
4
12
  import {useCallback, useEffect, useId, useMemo, useRef, useState} from 'react'
5
13
  import {DocumentPreview} from '../preview/DocumentPreview'
@@ -7,6 +15,7 @@ import {useDocumentPane} from 'sanity/desk'
7
15
  import {queryIndex, QueryResult} from '../api/embeddingsApi'
8
16
  import {publicId} from '../utils/id'
9
17
  import {useApiClient} from '../api/embeddingsApiHooks'
18
+ import {FeatureDisabledNotice, useIsFeatureEnabledContext} from '../api/isEnabled'
10
19
 
11
20
  interface Option {
12
21
  result: QueryResult
@@ -20,13 +29,27 @@ export function SemanticSearchReferenceInput(props: ObjectInputProps) {
20
29
  const defaultEnabled =
21
30
  (props.schemaType as ReferenceSchemaType)?.options?.embeddingsIndex?.searchMode === 'embeddings'
22
31
 
32
+ const featureState = useIsFeatureEnabledContext()
33
+
23
34
  const [semantic, setSemantic] = useState<boolean>(defaultEnabled)
24
35
  const toggleSemantic = useCallback(() => setSemantic((current) => !current), [])
25
36
 
26
37
  return (
27
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
+
28
47
  <Box flex={1} style={{maxHeight: 36, overflow: 'hidden'}}>
29
- {semantic ? <SemanticSearchInput {...props} /> : props.renderDefault(props)}
48
+ {semantic && featureState == 'enabled' ? (
49
+ <SemanticSearchInput {...props} />
50
+ ) : (
51
+ props.renderDefault(props)
52
+ )}
30
53
  </Box>
31
54
  <Button
32
55
  icon={semantic ? EarthGlobeIcon : LinkIcon}
@@ -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) => {