From 0f7799795348f3e3c1bdfa8120de5a02ae461a5f Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Tue, 18 May 2021 19:41:10 +0200 Subject: [PATCH] wip new search page components Signed-off-by: Emma Indal Signed-off-by: Eric Peterson --- .../SearchBarNext/SearchBarNext.tsx | 89 +++++++++++++++++++ .../src/components/SearchBarNext/index.tsx | 17 ++++ .../SearchResultNext/SearchResultNext.tsx | 67 ++++++++++++++ .../src/components/SearchResultNext/index.tsx | 17 ++++ 4 files changed, 190 insertions(+) create mode 100644 plugins/search/src/components/SearchBarNext/SearchBarNext.tsx create mode 100644 plugins/search/src/components/SearchBarNext/index.tsx create mode 100644 plugins/search/src/components/SearchResultNext/SearchResultNext.tsx create mode 100644 plugins/search/src/components/SearchResultNext/index.tsx diff --git a/plugins/search/src/components/SearchBarNext/SearchBarNext.tsx b/plugins/search/src/components/SearchBarNext/SearchBarNext.tsx new file mode 100644 index 0000000000..1e4a59ac8b --- /dev/null +++ b/plugins/search/src/components/SearchBarNext/SearchBarNext.tsx @@ -0,0 +1,89 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React, { useEffect } from 'react'; +import { useDebounce } from 'react-use'; +import { useQueryParamState } from '@backstage/core'; +import { Paper, InputBase, IconButton, makeStyles } from '@material-ui/core'; +import SearchIcon from '@material-ui/icons/Search'; +import ClearButton from '@material-ui/icons/Clear'; +import { useSearch } from '../SearchContext'; + +const useStyles = makeStyles(() => ({ + root: { + display: 'flex', + alignItems: 'center', + }, + input: { + flex: 1, + }, +})); + +export const SearchBarNext = () => { + const classes = useStyles(); + const { + queryState: { term }, + setQueryState, + } = useSearch(); + + const [queryString, setQueryString] = useQueryParamState('query'); + + useEffect(() => { + setQueryState({ term: queryString ?? '', pageCursor: '' }); + }, [queryString, setQueryState]); + + useDebounce( + () => { + setQueryString(term); + }, + 200, + [term], + ); + + const handleSearch = (event: React.ChangeEvent | React.FormEvent) => { + event.preventDefault(); + setQueryState({ + term: (event.target as HTMLInputElement).value, + pageCursor: '', + }); + }; + + const handleClearSearchBar = () => { + setQueryState({ term: '', pageCursor: '' }); + }; + + return ( + handleSearch(e)} + className={classes.root} + > + + + + handleSearch(e)} + inputProps={{ 'aria-label': 'search backstage' }} + /> + handleClearSearchBar()}> + + + + ); +}; diff --git a/plugins/search/src/components/SearchBarNext/index.tsx b/plugins/search/src/components/SearchBarNext/index.tsx new file mode 100644 index 0000000000..20ba7d9c55 --- /dev/null +++ b/plugins/search/src/components/SearchBarNext/index.tsx @@ -0,0 +1,17 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export { SearchBarNext } from './SearchBarNext'; diff --git a/plugins/search/src/components/SearchResultNext/SearchResultNext.tsx b/plugins/search/src/components/SearchResultNext/SearchResultNext.tsx new file mode 100644 index 0000000000..5b2133e573 --- /dev/null +++ b/plugins/search/src/components/SearchResultNext/SearchResultNext.tsx @@ -0,0 +1,67 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { EmptyState, Link, Progress } from '@backstage/core'; +import { Divider, List, ListItem, ListItemText } from '@material-ui/core'; +import { Alert } from '@material-ui/lab'; +import React from 'react'; + +import { useSearch } from '../SearchContext'; + +const DefaultResultListItem = ({ result }: any) => { + return ( + + + + + + + ); +}; + +export const SearchResultNext = () => { + const { + resultState: { loading, error, value }, + } = useSearch(); + + if (loading) { + return ; + } + if (error) { + return ( + + Error encountered while fetching search results. {error.toString()} + + ); + } + + if (!value) { + return ; + } + + return ( + + {value.results.map(result => ( + <> + + + ))} + + ); +}; diff --git a/plugins/search/src/components/SearchResultNext/index.tsx b/plugins/search/src/components/SearchResultNext/index.tsx new file mode 100644 index 0000000000..1a21491289 --- /dev/null +++ b/plugins/search/src/components/SearchResultNext/index.tsx @@ -0,0 +1,17 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export { SearchResultNext } from './SearchResultNext';