From 48936254439bac047b616e80f791965bd8f99249 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 13 Sep 2021 23:17:42 +0200 Subject: [PATCH] Issue-#545: Improve result render --- gatsby-node.js | 20 +++---- src/components/Search/SearchButton.jsx | 4 +- src/components/Search/SearchClient.jsx | 20 ++++--- src/components/Search/SearchComponent.jsx | 10 ++-- src/components/Search/SearchResultElement.jsx | 54 +++++++++++++++++++ 5 files changed, 79 insertions(+), 29 deletions(-) create mode 100644 src/components/Search/SearchResultElement.jsx diff --git a/gatsby-node.js b/gatsby-node.js index 0205bffd..d8ebf28c 100755 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -179,19 +179,15 @@ exports.createPages = ({ graphql, actions }) => { } const createSearchPage = async (createPage) => { - try { - const pageTemplate = path.resolve( - './src/components/Search/SearchComponent.jsx' - ) - const url = `/search/` + const pageTemplate = path.resolve( + './src/components/Search/SearchComponent.jsx' + ) + const url = `/search/` - createPage({ - path: pageTemplate, - component: url - }) - } catch (error) { - console.log(error.message) - } + createPage({ + path: url, + component: pageTemplate + }) } const createDeploymentsPage = async (createPage) => { diff --git a/src/components/Search/SearchButton.jsx b/src/components/Search/SearchButton.jsx index d9642a38..b7c41e6a 100644 --- a/src/components/Search/SearchButton.jsx +++ b/src/components/Search/SearchButton.jsx @@ -5,8 +5,8 @@ import { IconButton } from '@material-ui/core' import SearchIcon from '@material-ui/icons/Search' const SearchButton = () => { return ( - - navigate('/search')} /> + navigate('/search')}> + ) } diff --git a/src/components/Search/SearchClient.jsx b/src/components/Search/SearchClient.jsx index 1febcf5a..bba459af 100644 --- a/src/components/Search/SearchClient.jsx +++ b/src/components/Search/SearchClient.jsx @@ -1,14 +1,14 @@ import React, { useState, useEffect } from 'react' import * as JsSearch from 'js-search' -import { Link } from 'gatsby' import PropTypes from 'prop-types' import { makeStyles } from '@material-ui/core/styles' import List from '@material-ui/core/List' import ListItem from '@material-ui/core/ListItem' -import Pagination from '@material-ui/lab/Pagination' import TextField from '@material-ui/core/TextField' import InputAdornment from '@material-ui/core/InputAdornment' import SearchIcon from '@material-ui/icons/Search' +import SearchResultElement from './SearchResultElement' + const useStyles = makeStyles((theme) => ({ parent: { overflow: 'hidden', @@ -89,7 +89,6 @@ const SearchClient = ({ searchableData }) => { { > {searchState.touched ? (
-
Total results found: {searchState.searchResults.length}
- - + {/* {searchState.searchResults.map((element) => ( { {element.title} ))} - + + */} +
) : null} @@ -140,15 +139,14 @@ const ResultList = ({ searchResults }) => { const url = typeof window !== 'undefined' ? window.location.host : '' console.log('url', url) return ( -
+
Total results found: {searchResults.length}
-
- + {searchResults.map((element) => ( - {element.title} + ))} diff --git a/src/components/Search/SearchComponent.jsx b/src/components/Search/SearchComponent.jsx index a7795cde..28c69500 100644 --- a/src/components/Search/SearchComponent.jsx +++ b/src/components/Search/SearchComponent.jsx @@ -27,15 +27,17 @@ const SearchComponent = () => { } } `) - console.log('data', data) const searchableData = data.allMarkdownRemark.edges.map(({ node }) => { + var section = 'Concepts' + if (node.fields.slug.startsWith('/tutorials')) section = 'Tutorials' + return { title: node.frontmatter.title, description: node.frontmatter.description, - slug: - node.fields.slug[0] === '/' ? node.fields.slug : '/' + node.fields.slug, + slug: node.fields.slug, id: node.id, - text: node.plainText + text: node.plainText, + section: section } }) diff --git a/src/components/Search/SearchResultElement.jsx b/src/components/Search/SearchResultElement.jsx new file mode 100644 index 00000000..466b2ae6 --- /dev/null +++ b/src/components/Search/SearchResultElement.jsx @@ -0,0 +1,54 @@ +import React from 'react' +import { Link } from 'gatsby' +import PropTypes from 'prop-types' +import Card from '@material-ui/core/Card' +import CardContent from '@material-ui/core/CardContent' +import Typography from '@material-ui/core/Typography' +import { makeStyles } from '@material-ui/core/styles' + +const useStyles = makeStyles({ + root: { + minWidth: 275 + }, + bullet: { + display: 'inline-block', + margin: '0 2px', + transform: 'scale(0.8)' + }, + title: { + fontSize: 14 + }, + pos: { + marginBottom: 12 + } +}) + +const SearchResultElement = ({ element }) => { + const classes = useStyles() + const { slug, title, section, description } = element + return ( + + + + {section} + + + {title} + + + {description ? description.substring(0, 100) + '...' : null} + + + + ) +} + +SearchResultElement.propTypes = { + element: PropTypes.object.isRequired +} + +export default SearchResultElement