@backstage/plugin-search-react 0.2.0-next.1 → 0.2.1-next.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,52 @@
1
1
  # @backstage/plugin-search-react
2
2
 
3
+ ## 0.2.1-next.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies
8
+ - @backstage/core-plugin-api@1.0.3-next.0
9
+ - @backstage/plugin-search-common@0.3.5-next.0
10
+
11
+ ## 0.2.0
12
+
13
+ ### Minor Changes
14
+
15
+ - bdbe620797: **BREAKING**: `SearchContextProviderForStorybook` and `SearchApiProviderForStorybook` has been deleted. New mock implementation of the `SearchApi` introduced. If you need to mock the api we recommend you to do the following:
16
+
17
+ ```tsx
18
+ import {
19
+ searchApiRef,
20
+ MockSearchApi,
21
+ SearchContextProvider,
22
+ } from '@backstage/plugin-search-react';
23
+ import { TestApiProvider } from '@backstage/test-utils';
24
+
25
+ <TestApiProvider apis={[[searchApiRef, new MockSearchApi()]]}>
26
+ <SearchContextProvider>
27
+ <Component />
28
+ </SearchContextProvider>
29
+ </TestApiProvider>;
30
+ ```
31
+
32
+ ### Patch Changes
33
+
34
+ - 11a46863de: Export `useSearchContextCheck` hook to check if the search context is available
35
+ - a307a14be0: Removed dependency on `@backstage/core-app-api`.
36
+ - 3a74e203a8: Updated search result components to support rendering content with highlighted matched terms
37
+ - Updated dependencies
38
+ - @backstage/core-plugin-api@1.0.2
39
+ - @backstage/plugin-search-common@0.3.4
40
+
41
+ ## 0.2.0-next.2
42
+
43
+ ### Patch Changes
44
+
45
+ - 3a74e203a8: Updated search result components to support rendering content with highlighted matched terms
46
+ - Updated dependencies
47
+ - @backstage/plugin-search-common@0.3.4-next.0
48
+ - @backstage/core-plugin-api@1.0.2-next.1
49
+
3
50
  ## 0.2.0-next.1
4
51
 
5
52
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import * as _backstage_core_plugin_api from '@backstage/core-plugin-api';
2
3
  import { SearchQuery, SearchResultSet } from '@backstage/plugin-search-common';
3
4
  import { JsonObject } from '@backstage/types';
@@ -25,6 +26,19 @@ declare class MockSearchApi implements SearchApi {
25
26
  query(): Promise<SearchResultSet>;
26
27
  }
27
28
 
29
+ /**
30
+ * @public
31
+ */
32
+ declare type HighlightedSearchResultTextProps = {
33
+ text: string;
34
+ preTag: string;
35
+ postTag: string;
36
+ };
37
+ /**
38
+ * @public
39
+ */
40
+ declare const HighlightedSearchResultText: ({ text, preTag, postTag, }: HighlightedSearchResultTextProps) => JSX.Element;
41
+
28
42
  /**
29
43
  *
30
44
  * @public
@@ -75,4 +89,4 @@ declare type SearchContextProviderProps = PropsWithChildren<{
75
89
  */
76
90
  declare const SearchContextProvider: (props: SearchContextProviderProps) => JSX.Element;
77
91
 
78
- export { MockSearchApi, SearchApi, SearchContextProvider, SearchContextProviderProps, SearchContextState, SearchContextValue, searchApiRef, useSearch, useSearchContextCheck };
92
+ export { HighlightedSearchResultText, HighlightedSearchResultTextProps, MockSearchApi, SearchApi, SearchContextProvider, SearchContextProviderProps, SearchContextState, SearchContextValue, searchApiRef, useSearch, useSearchContextCheck };
package/dist/index.esm.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createApiRef, useApi, AnalyticsContext } from '@backstage/core-plugin-api';
2
+ import React, { useMemo, useContext, useState, useCallback, useEffect } from 'react';
3
+ import { makeStyles } from '@material-ui/core';
2
4
  import { createVersionedContext, createVersionedValueMap } from '@backstage/version-bridge';
3
- import React, { useContext, useState, useCallback, useEffect } from 'react';
4
5
  import useAsync from 'react-use/lib/useAsync';
5
6
  import usePrevious from 'react-use/lib/usePrevious';
6
7
 
@@ -16,6 +17,22 @@ class MockSearchApi {
16
17
  }
17
18
  }
18
19
 
20
+ const useStyles = makeStyles(() => ({
21
+ highlight: {}
22
+ }), { name: "BackstageHighlightedSearchResultText" });
23
+ const HighlightedSearchResultText = ({
24
+ text,
25
+ preTag,
26
+ postTag
27
+ }) => {
28
+ const classes = useStyles();
29
+ const terms = useMemo(() => text.split(new RegExp(`(${preTag}.+?${postTag})`)), [postTag, preTag, text]);
30
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, terms.map((t, idx) => t.includes(preTag) ? /* @__PURE__ */ React.createElement("mark", {
31
+ className: classes.highlight,
32
+ key: idx
33
+ }, t.replace(new RegExp(`${preTag}|${postTag}`, "g"), "")) : t));
34
+ };
35
+
19
36
  const SearchContext = createVersionedContext("search-context");
20
37
  const useSearch = () => {
21
38
  const context = useContext(SearchContext);
@@ -90,5 +107,5 @@ const SearchContextProvider = (props) => {
90
107
  }));
91
108
  };
92
109
 
93
- export { MockSearchApi, SearchContextProvider, searchApiRef, useSearch, useSearchContextCheck };
110
+ export { HighlightedSearchResultText, MockSearchApi, SearchContextProvider, searchApiRef, useSearch, useSearchContextCheck };
94
111
  //# sourceMappingURL=index.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":["../src/api.ts","../src/context/SearchContext.tsx"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { SearchQuery, SearchResultSet } from '@backstage/plugin-search-common';\nimport { createApiRef } from '@backstage/core-plugin-api';\n\n/**\n * @public\n */\nexport const searchApiRef = createApiRef<SearchApi>({\n id: 'plugin.search.queryservice',\n});\n\n/**\n * @public\n */\nexport interface SearchApi {\n query(query: SearchQuery): Promise<SearchResultSet>;\n}\n\n/**\n * @public\n *\n * Search Api Mock that can be used in tests and storybooks\n */\nexport class MockSearchApi implements SearchApi {\n constructor(public mockedResults?: SearchResultSet) {}\n\n query(): Promise<SearchResultSet> {\n return Promise.resolve(this.mockedResults || { results: [] });\n }\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject } from '@backstage/types';\nimport { useApi, AnalyticsContext } from '@backstage/core-plugin-api';\nimport { SearchResultSet } from '@backstage/plugin-search-common';\nimport {\n createVersionedContext,\n createVersionedValueMap,\n} from '@backstage/version-bridge';\nimport React, {\n PropsWithChildren,\n useCallback,\n useContext,\n useEffect,\n useState,\n} from 'react';\nimport useAsync, { AsyncState } from 'react-use/lib/useAsync';\nimport usePrevious from 'react-use/lib/usePrevious';\nimport { searchApiRef } from '../api';\n\n/**\n *\n * @public\n */\nexport type SearchContextValue = {\n result: AsyncState<SearchResultSet>;\n setTerm: React.Dispatch<React.SetStateAction<string>>;\n setTypes: React.Dispatch<React.SetStateAction<string[]>>;\n setFilters: React.Dispatch<React.SetStateAction<JsonObject>>;\n setPageCursor: React.Dispatch<React.SetStateAction<string | undefined>>;\n fetchNextPage?: React.DispatchWithoutAction;\n fetchPreviousPage?: React.DispatchWithoutAction;\n} & SearchContextState;\n\n/**\n *\n * @public\n */\nexport type SearchContextState = {\n term: string;\n types: string[];\n filters: JsonObject;\n pageCursor?: string;\n};\n\nconst SearchContext = createVersionedContext<{\n 1: SearchContextValue;\n}>('search-context');\n\n/**\n * @public\n *\n * React hook which provides the search context\n */\nexport const useSearch = () => {\n const context = useContext(SearchContext);\n if (!context) {\n throw new Error('useSearch must be used within a SearchContextProvider');\n }\n\n const value = context.atVersion(1);\n if (!value) {\n throw new Error('No SearchContext v1 found');\n }\n return value;\n};\n\n/**\n * @public\n *\n * React hook which checks for an existing search context\n */\nexport const useSearchContextCheck = () => {\n const context = useContext(SearchContext);\n return context !== undefined;\n};\n\n/**\n * The initial state of `SearchContextProvider`.\n *\n */\nconst searchInitialState: SearchContextState = {\n term: '',\n pageCursor: undefined,\n filters: {},\n types: [],\n};\n\n/**\n * Props for {@link SearchContextProvider}\n *\n * @public\n */\nexport type SearchContextProviderProps = PropsWithChildren<{\n initialState?: SearchContextState;\n}>;\n\n/**\n * @public\n *\n * Search context provider which gives you access to shared state between search components\n */\nexport const SearchContextProvider = (props: SearchContextProviderProps) => {\n const { initialState = searchInitialState, children } = props;\n const searchApi = useApi(searchApiRef);\n const [pageCursor, setPageCursor] = useState<string | undefined>(\n initialState.pageCursor,\n );\n const [filters, setFilters] = useState<JsonObject>(initialState.filters);\n const [term, setTerm] = useState<string>(initialState.term);\n const [types, setTypes] = useState<string[]>(initialState.types);\n\n const prevTerm = usePrevious(term);\n\n const result = useAsync(\n () =>\n searchApi.query({\n term,\n filters,\n pageCursor,\n types,\n }),\n [term, filters, types, pageCursor],\n );\n\n const hasNextPage =\n !result.loading && !result.error && result.value?.nextPageCursor;\n const hasPreviousPage =\n !result.loading && !result.error && result.value?.previousPageCursor;\n const fetchNextPage = useCallback(() => {\n setPageCursor(result.value?.nextPageCursor);\n }, [result.value?.nextPageCursor]);\n const fetchPreviousPage = useCallback(() => {\n setPageCursor(result.value?.previousPageCursor);\n }, [result.value?.previousPageCursor]);\n\n useEffect(() => {\n // Any time a term is reset, we want to start from page 0.\n if (term && prevTerm && term !== prevTerm) {\n setPageCursor(undefined);\n }\n }, [term, prevTerm, initialState.pageCursor]);\n\n const value: SearchContextValue = {\n result,\n filters,\n setFilters,\n term,\n setTerm,\n types,\n setTypes,\n pageCursor,\n setPageCursor,\n fetchNextPage: hasNextPage ? fetchNextPage : undefined,\n fetchPreviousPage: hasPreviousPage ? fetchPreviousPage : undefined,\n };\n\n const versionedValue = createVersionedValueMap({ 1: value });\n\n return (\n <AnalyticsContext attributes={{ searchTypes: types.sort().join(',') }}>\n <SearchContext.Provider value={versionedValue} children={children} />\n </AnalyticsContext>\n );\n};\n"],"names":[],"mappings":";;;;;;AACY,MAAC,YAAY,GAAG,YAAY,CAAC;AACzC,EAAE,EAAE,EAAE,4BAA4B;AAClC,CAAC,EAAE;AACI,MAAM,aAAa,CAAC;AAC3B,EAAE,WAAW,CAAC,aAAa,EAAE;AAC7B,IAAI,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;AACvC,GAAG;AACH,EAAE,KAAK,GAAG;AACV,IAAI,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC;AAClE,GAAG;AACH;;ACGA,MAAM,aAAa,GAAG,sBAAsB,CAAC,gBAAgB,CAAC,CAAC;AACnD,MAAC,SAAS,GAAG,MAAM;AAC/B,EAAE,MAAM,OAAO,GAAG,UAAU,CAAC,aAAa,CAAC,CAAC;AAC5C,EAAE,IAAI,CAAC,OAAO,EAAE;AAChB,IAAI,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;AAC7E,GAAG;AACH,EAAE,MAAM,KAAK,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;AACrC,EAAE,IAAI,CAAC,KAAK,EAAE;AACd,IAAI,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;AACjD,GAAG;AACH,EAAE,OAAO,KAAK,CAAC;AACf,EAAE;AACU,MAAC,qBAAqB,GAAG,MAAM;AAC3C,EAAE,MAAM,OAAO,GAAG,UAAU,CAAC,aAAa,CAAC,CAAC;AAC5C,EAAE,OAAO,OAAO,KAAK,KAAK,CAAC,CAAC;AAC5B,EAAE;AACF,MAAM,kBAAkB,GAAG;AAC3B,EAAE,IAAI,EAAE,EAAE;AACV,EAA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AI,OAAO;AACX,IAAI,KAAK;AACT,IAAI,QAAQ;AACZ,IAAI,UAAU;AACd,IAAI,aAAa;AACjB,IAAI,aAAa,EAAE,WAAW,GAAG,aAAa,GAAG,KAAK,CAAC;AACvD,IAAI,iBAAiB,EAAE,eAAe,GAAG,iBAAiB,GAAG,KAAK,CAAC;AACnE,GAAG,CAAC;AACJ,EAAE,MAAM,cAAc,GAAG,uBAAuB,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;AAC/D,EAAE,uBAAuB,KAAK,CAAC,aAAa,CAAC,gBAAgB,EAAE;AAC/D,IAAI,UAAU,EAAE,EAAE,WAAW,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;AACvD,GAAG,kBAAkB,KAAK,CAAC,aAAa,CAAC,aAAa,CAAC,QAAQ,EAAE;AACjE,IAAI,KAAK,EAAE,cAAc;AACzB,IAAI,QAAQ;AACZ,GAAG,CAAC,CAAC,CAAC;AACN;;;;"}
1
+ {"version":3,"file":"index.esm.js","sources":["../src/api.ts","../src/components/HighlightedSearchResultText/HighlightedSearchResultText.tsx","../src/context/SearchContext.tsx"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { SearchQuery, SearchResultSet } from '@backstage/plugin-search-common';\nimport { createApiRef } from '@backstage/core-plugin-api';\n\n/**\n * @public\n */\nexport const searchApiRef = createApiRef<SearchApi>({\n id: 'plugin.search.queryservice',\n});\n\n/**\n * @public\n */\nexport interface SearchApi {\n query(query: SearchQuery): Promise<SearchResultSet>;\n}\n\n/**\n * @public\n *\n * Search Api Mock that can be used in tests and storybooks\n */\nexport class MockSearchApi implements SearchApi {\n constructor(public mockedResults?: SearchResultSet) {}\n\n query(): Promise<SearchResultSet> {\n return Promise.resolve(this.mockedResults || { results: [] });\n }\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { useMemo } from 'react';\nimport { makeStyles } from '@material-ui/core';\n\nconst useStyles = makeStyles(\n () => ({\n highlight: {},\n }),\n { name: 'BackstageHighlightedSearchResultText' },\n);\n\n/**\n * @public\n */\nexport type HighlightedSearchResultTextProps = {\n text: string;\n preTag: string;\n postTag: string;\n};\n\n/**\n * @public\n */\nexport const HighlightedSearchResultText = ({\n text,\n preTag,\n postTag,\n}: HighlightedSearchResultTextProps) => {\n const classes = useStyles();\n const terms = useMemo(\n () => text.split(new RegExp(`(${preTag}.+?${postTag})`)),\n [postTag, preTag, text],\n );\n return (\n <>\n {terms.map((t, idx) =>\n t.includes(preTag) ? (\n <mark className={classes.highlight} key={idx}>\n {t.replace(new RegExp(`${preTag}|${postTag}`, 'g'), '')}\n </mark>\n ) : (\n t\n ),\n )}\n </>\n );\n};\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject } from '@backstage/types';\nimport { useApi, AnalyticsContext } from '@backstage/core-plugin-api';\nimport { SearchResultSet } from '@backstage/plugin-search-common';\nimport {\n createVersionedContext,\n createVersionedValueMap,\n} from '@backstage/version-bridge';\nimport React, {\n PropsWithChildren,\n useCallback,\n useContext,\n useEffect,\n useState,\n} from 'react';\nimport useAsync, { AsyncState } from 'react-use/lib/useAsync';\nimport usePrevious from 'react-use/lib/usePrevious';\nimport { searchApiRef } from '../api';\n\n/**\n *\n * @public\n */\nexport type SearchContextValue = {\n result: AsyncState<SearchResultSet>;\n setTerm: React.Dispatch<React.SetStateAction<string>>;\n setTypes: React.Dispatch<React.SetStateAction<string[]>>;\n setFilters: React.Dispatch<React.SetStateAction<JsonObject>>;\n setPageCursor: React.Dispatch<React.SetStateAction<string | undefined>>;\n fetchNextPage?: React.DispatchWithoutAction;\n fetchPreviousPage?: React.DispatchWithoutAction;\n} & SearchContextState;\n\n/**\n *\n * @public\n */\nexport type SearchContextState = {\n term: string;\n types: string[];\n filters: JsonObject;\n pageCursor?: string;\n};\n\nconst SearchContext = createVersionedContext<{\n 1: SearchContextValue;\n}>('search-context');\n\n/**\n * @public\n *\n * React hook which provides the search context\n */\nexport const useSearch = () => {\n const context = useContext(SearchContext);\n if (!context) {\n throw new Error('useSearch must be used within a SearchContextProvider');\n }\n\n const value = context.atVersion(1);\n if (!value) {\n throw new Error('No SearchContext v1 found');\n }\n return value;\n};\n\n/**\n * @public\n *\n * React hook which checks for an existing search context\n */\nexport const useSearchContextCheck = () => {\n const context = useContext(SearchContext);\n return context !== undefined;\n};\n\n/**\n * The initial state of `SearchContextProvider`.\n *\n */\nconst searchInitialState: SearchContextState = {\n term: '',\n pageCursor: undefined,\n filters: {},\n types: [],\n};\n\n/**\n * Props for {@link SearchContextProvider}\n *\n * @public\n */\nexport type SearchContextProviderProps = PropsWithChildren<{\n initialState?: SearchContextState;\n}>;\n\n/**\n * @public\n *\n * Search context provider which gives you access to shared state between search components\n */\nexport const SearchContextProvider = (props: SearchContextProviderProps) => {\n const { initialState = searchInitialState, children } = props;\n const searchApi = useApi(searchApiRef);\n const [pageCursor, setPageCursor] = useState<string | undefined>(\n initialState.pageCursor,\n );\n const [filters, setFilters] = useState<JsonObject>(initialState.filters);\n const [term, setTerm] = useState<string>(initialState.term);\n const [types, setTypes] = useState<string[]>(initialState.types);\n\n const prevTerm = usePrevious(term);\n\n const result = useAsync(\n () =>\n searchApi.query({\n term,\n filters,\n pageCursor,\n types,\n }),\n [term, filters, types, pageCursor],\n );\n\n const hasNextPage =\n !result.loading && !result.error && result.value?.nextPageCursor;\n const hasPreviousPage =\n !result.loading && !result.error && result.value?.previousPageCursor;\n const fetchNextPage = useCallback(() => {\n setPageCursor(result.value?.nextPageCursor);\n }, [result.value?.nextPageCursor]);\n const fetchPreviousPage = useCallback(() => {\n setPageCursor(result.value?.previousPageCursor);\n }, [result.value?.previousPageCursor]);\n\n useEffect(() => {\n // Any time a term is reset, we want to start from page 0.\n if (term && prevTerm && term !== prevTerm) {\n setPageCursor(undefined);\n }\n }, [term, prevTerm, initialState.pageCursor]);\n\n const value: SearchContextValue = {\n result,\n filters,\n setFilters,\n term,\n setTerm,\n types,\n setTypes,\n pageCursor,\n setPageCursor,\n fetchNextPage: hasNextPage ? fetchNextPage : undefined,\n fetchPreviousPage: hasPreviousPage ? fetchPreviousPage : undefined,\n };\n\n const versionedValue = createVersionedValueMap({ 1: value });\n\n return (\n <AnalyticsContext attributes={{ searchTypes: types.sort().join(',') }}>\n <SearchContext.Provider value={versionedValue} children={children} />\n </AnalyticsContext>\n );\n};\n"],"names":[],"mappings":";;;;;;;AACY,MAAC,YAAY,GAAG,YAAY,CAAC;AACzC,EAAE,EAAE,EAAE,4BAA4B;AAClC,CAAC,EAAE;AACI,MAAM,aAAa,CAAC;AAC3B,EAAE,WAAW,CAAC,aAAa,EAAE;AAC7B,IAAI,IAAI,CAAC,aAAa,GAAG,aAAa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AAG,CAAC,CAAC,CAAC;AACN;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/plugin-search-react",
3
- "version": "0.2.0-next.1",
3
+ "version": "0.2.1-next.0",
4
4
  "main": "dist/index.esm.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "Apache-2.0",
@@ -31,10 +31,11 @@
31
31
  "start": "backstage-cli package start"
32
32
  },
33
33
  "dependencies": {
34
- "@backstage/core-plugin-api": "^1.0.2-next.0",
35
- "@backstage/plugin-search-common": "^0.3.3",
34
+ "@backstage/core-plugin-api": "^1.0.3-next.0",
35
+ "@backstage/plugin-search-common": "^0.3.5-next.0",
36
36
  "@backstage/types": "^1.0.0",
37
37
  "@backstage/version-bridge": "^1.0.1",
38
+ "@material-ui/core": "^4.12.2",
38
39
  "react-use": "^17.3.2"
39
40
  },
40
41
  "peerDependencies": {
@@ -42,8 +43,8 @@
42
43
  "react": "^16.13.1 || ^17.0.0"
43
44
  },
44
45
  "devDependencies": {
45
- "@backstage/core-app-api": "^1.0.2-next.0",
46
- "@backstage/test-utils": "^1.1.0-next.1",
46
+ "@backstage/core-app-api": "^1.0.3-next.0",
47
+ "@backstage/test-utils": "^1.1.1-next.0",
47
48
  "@testing-library/jest-dom": "^5.10.1",
48
49
  "@testing-library/react": "^12.1.3",
49
50
  "@testing-library/react-hooks": "^8.0.0"
@@ -51,5 +52,5 @@
51
52
  "files": [
52
53
  "dist"
53
54
  ],
54
- "gitHead": "0b3df66a238c66a5498dab85b1ed85a8607289f1"
55
+ "gitHead": "e15c24ddb5d14034629ced8a5a5d8f12b8f1a7dd"
55
56
  }