@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/dist/index.esm.js CHANGED
@@ -1,8 +1,8 @@
1
- import { useSchema, DefaultPreview, useDocumentPreviewStore, SanityDefaultPreview, getPreviewValueWithFallback, getPreviewStateObservable, typed, unset, setIfMissing, set, useClient, definePlugin, isObjectInputProps } from 'sanity';
1
+ import { useSchema, DefaultPreview, useDocumentPreviewStore, SanityDefaultPreview, getPreviewValueWithFallback, getPreviewStateObservable, useClient, typed, unset, setIfMissing, set, definePlugin, isObjectInputProps } from 'sanity';
2
2
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
- import { Card, Button, Box, Flex, Autocomplete, Text, Stack, Label, TextInput, TextArea, Dialog, Spinner, Heading, MenuButton, Menu, MenuItem } from '@sanity/ui';
3
+ import { Card, Button, Box, Text, Flex, Spinner, Autocomplete, Stack, Label, TextInput, TextArea, Dialog, Heading, MenuButton, Menu, MenuItem } from '@sanity/ui';
4
4
  import { ErrorOutlineIcon, EarthGlobeIcon, LinkIcon, AddIcon, SearchIcon, EllipsisVerticalIcon, TrashIcon, UndoIcon } from '@sanity/icons';
5
- import require$$0, { useMemo, useRef, useEffect, useState, useCallback, useId } from 'react';
5
+ import require$$0, { useMemo, useRef, useEffect, createContext, useState, useContext, useCallback, useId } from 'react';
6
6
  import { useIntentLink } from 'sanity/router';
7
7
  import { useDocumentPane } from 'sanity/desk';
8
8
 
@@ -1879,11 +1879,72 @@ function deleteIndex(indexName, client) {
1879
1879
  function publicId(id) {
1880
1880
  return id.replace("drafts.", "");
1881
1881
  }
1882
+ function useApiClient() {
1883
+ const client = useClient({
1884
+ apiVersion: "vX"
1885
+ });
1886
+ return useMemo(() => {
1887
+ const customHost = localStorage.getItem("embeddings-index-host");
1888
+ if (customHost) {
1889
+ return client.withConfig({
1890
+ apiHost: customHost,
1891
+ useProjectHostname: false,
1892
+ withCredentials: false
1893
+ });
1894
+ }
1895
+ return client;
1896
+ }, [client]);
1897
+ }
1898
+ const featureName = "embeddingsIndexApi";
1899
+ const FeatureEnabledContext = createContext("loading");
1900
+ function useIsFeatureEnabled() {
1901
+ const client = useClient({
1902
+ apiVersion: "2023-09-01"
1903
+ });
1904
+ const [status, setStatus] = useState("loading");
1905
+ useEffect(() => {
1906
+ client.request({
1907
+ method: "GET",
1908
+ url: "/projects/".concat(client.config().projectId, "/features/").concat(featureName)
1909
+ }).then(isEnabled => {
1910
+ setStatus(isEnabled === "true" || isEnabled === true ? "enabled" : "disabled");
1911
+ }).catch(err => {
1912
+ console.error(err);
1913
+ setStatus("disabled");
1914
+ });
1915
+ }, [client]);
1916
+ return status;
1917
+ }
1918
+ function FeatureEnabledProvider(props) {
1919
+ const status = useIsFeatureEnabled();
1920
+ return /* @__PURE__ */jsx(FeatureEnabledContext.Provider, {
1921
+ value: status,
1922
+ children: props.children
1923
+ });
1924
+ }
1925
+ function useIsFeatureEnabledContext() {
1926
+ return useContext(FeatureEnabledContext);
1927
+ }
1928
+ function FeatureDisabledNotice() {
1929
+ return /* @__PURE__ */jsx(Card, {
1930
+ tone: "primary",
1931
+ border: true,
1932
+ padding: 2,
1933
+ children: /* @__PURE__ */jsxs(Text, {
1934
+ size: 1,
1935
+ children: ["Embeddings index APIs are only available on the", " ", /* @__PURE__ */jsx("a", {
1936
+ href: "https://sanity.io/pricing",
1937
+ children: "Team tier and above"
1938
+ }), ". Please upgrade to enable access."]
1939
+ })
1940
+ });
1941
+ }
1882
1942
  const NO_OPTIONS = [];
1883
1943
  const NO_FILTER = () => true;
1884
1944
  function SemanticSearchReferenceInput(props) {
1885
1945
  var _a, _b, _c;
1886
1946
  const defaultEnabled = ((_c = (_b = (_a = props.schemaType) == null ? void 0 : _a.options) == null ? void 0 : _b.embeddingsIndex) == null ? void 0 : _c.searchMode) === "embeddings";
1947
+ const featureState = useIsFeatureEnabledContext();
1887
1948
  const [semantic, setSemantic] = useState(defaultEnabled);
1888
1949
  const toggleSemantic = useCallback(() => setSemantic(current => !current), []);
1889
1950
  return /* @__PURE__ */jsxs(Flex, {
@@ -1892,13 +1953,16 @@ function SemanticSearchReferenceInput(props) {
1892
1953
  style: {
1893
1954
  width: "100%"
1894
1955
  },
1895
- children: [/* @__PURE__ */jsx(Box, {
1956
+ children: [semantic && featureState == "loading" ? /* @__PURE__ */jsx(Box, {
1957
+ padding: 2,
1958
+ children: /* @__PURE__ */jsx(Spinner, {})
1959
+ }) : null, semantic && featureState == "disabled" ? /* @__PURE__ */jsx(FeatureDisabledNotice, {}) : null, /* @__PURE__ */jsx(Box, {
1896
1960
  flex: 1,
1897
1961
  style: {
1898
1962
  maxHeight: 36,
1899
1963
  overflow: "hidden"
1900
1964
  },
1901
- children: semantic ? /* @__PURE__ */jsx(SemanticSearchInput, {
1965
+ children: semantic && featureState == "enabled" ? /* @__PURE__ */jsx(SemanticSearchInput, {
1902
1966
  ...props
1903
1967
  }) : props.renderDefault(props)
1904
1968
  }), /* @__PURE__ */jsx(Button, {
@@ -1919,12 +1983,6 @@ function useDebouncedValue(value, ms) {
1919
1983
  }, [value, ms]);
1920
1984
  return debouncedValue;
1921
1985
  }
1922
- function useApiClient$2() {
1923
- const client = useClient({
1924
- apiVersion: "vX"
1925
- });
1926
- return useMemo(() => client, [client]);
1927
- }
1928
1986
  function SemanticSearchInput(props) {
1929
1987
  const {
1930
1988
  onPathFocus,
@@ -1945,7 +2003,7 @@ function SemanticSearchInput(props) {
1945
2003
  const prevDebouncedQuery = useRef(debouncedQuery);
1946
2004
  const [searching, setSearching] = useState(false);
1947
2005
  const [options, setOptions] = useState(NO_OPTIONS);
1948
- const client = useApiClient$2();
2006
+ const client = useApiClient();
1949
2007
  useEffect(() => {
1950
2008
  docRef.current = currentDocument;
1951
2009
  }, [currentDocument]);
@@ -2070,6 +2128,15 @@ function isType(schemaType, typeName) {
2070
2128
  }
2071
2129
  const embeddingsIndexReferenceInput = definePlugin({
2072
2130
  name: "@sanity/embeddings-index-reference-input",
2131
+ studio: {
2132
+ components: {
2133
+ layout: props => {
2134
+ return /* @__PURE__ */jsx(FeatureEnabledProvider, {
2135
+ children: props.renderDefault(props)
2136
+ });
2137
+ }
2138
+ }
2139
+ },
2073
2140
  form: {
2074
2141
  components: {
2075
2142
  input: props => {
@@ -2084,12 +2151,6 @@ const embeddingsIndexReferenceInput = definePlugin({
2084
2151
  }
2085
2152
  }
2086
2153
  });
2087
- function useApiClient$1(customApiClient) {
2088
- const client = useClient({
2089
- apiVersion: "vX"
2090
- });
2091
- return useMemo(() => customApiClient ? customApiClient(client) : client, [client, customApiClient]);
2092
- }
2093
2154
  const defaultProjection = "{...}";
2094
2155
  function useDefaultIndex(schema, dataset) {
2095
2156
  const defaultFilter = useMemo(() => "_type in [".concat(schema.getTypeNames().map(n => schema.get(n)).filter(schemaType => Boolean(schemaType && isType(schemaType, "document"))).filter(documentType => !documentType.name.startsWith("sanity.") && !documentType.name.startsWith("assist.") && documentType.name !== "document").map(documentType => '"'.concat(documentType.name, '"')).join(",\n"), "]"), [schema]);
@@ -2206,7 +2267,7 @@ function IndexEditor(props) {
2206
2267
  index: selectedIndex,
2207
2268
  onSubmit
2208
2269
  } = props;
2209
- const client = useApiClient$1();
2270
+ const client = useApiClient();
2210
2271
  const schema = useSchema();
2211
2272
  const defaultIndex = useDefaultIndex(schema, (_a = client.config().dataset) != null ? _a : "");
2212
2273
  const [errors, setErrors] = useState();
@@ -2426,7 +2487,7 @@ function QueryIndex(props) {
2426
2487
  const [query, setQuery] = useState("");
2427
2488
  const [searching, setSearching] = useState(false);
2428
2489
  const [results, setResults] = useState(NO_RESULTS);
2429
- const client = useApiClient$1();
2490
+ const client = useApiClient();
2430
2491
  const search = useCallback(queryString => {
2431
2492
  setSearching(true);
2432
2493
  return queryIndex({
@@ -2644,24 +2705,22 @@ function IndexStatus(_ref4) {
2644
2705
  })]
2645
2706
  });
2646
2707
  }
2647
- function useApiClient() {
2648
- const client = useClient({
2649
- apiVersion: "vX"
2650
- });
2651
- return useMemo(() => client, [client]);
2652
- }
2653
2708
  function EmbeddingsIndexTool() {
2709
+ const featureState = useIsFeatureEnabled();
2654
2710
  return /* @__PURE__ */jsx(Card, {
2655
2711
  children: /* @__PURE__ */jsx(Flex, {
2656
2712
  justify: "center",
2657
2713
  flex: 1,
2658
- children: /* @__PURE__ */jsx(Card, {
2714
+ children: /* @__PURE__ */jsxs(Card, {
2659
2715
  flex: 1,
2660
2716
  style: {
2661
2717
  maxWidth: 1200
2662
2718
  },
2663
2719
  padding: 5,
2664
- children: /* @__PURE__ */jsx(Indexes, {})
2720
+ children: [featureState == "loading" ? /* @__PURE__ */jsx(Box, {
2721
+ padding: 2,
2722
+ children: /* @__PURE__ */jsx(Spinner, {})
2723
+ }) : null, featureState == "disabled" ? /* @__PURE__ */jsx(FeatureDisabledNotice, {}) : null, featureState == "enabled" ? /* @__PURE__ */jsx(Indexes, {}) : null]
2665
2724
  })
2666
2725
  })
2667
2726
  });