All files / src/components search-results.js

0% Statements 0/6
0% Branches 0/8
0% Functions 0/3
0% Lines 0/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                                                                               
import {Flex, Text} from '@primer/components'
import React from 'react'
import sentenceCase from 'sentence-case'
import useSiteMetadata from '../use-site-metadata'
 
function SearchResults({results, getItemProps, highlightedIndex}) {
  const siteMetadata = useSiteMetadata()
 
  if (results.length === 0) {
    return (
      <Text as="div" p={3} fontSize={1} color="gray.7" width="100%">
        No results
      </Text>
    )
  }
 
  return results.map((item, index) => (
    <Flex
      key={item.path}
      {...getItemProps({
        item,
        flexDirection: 'column',
        flex: '0 0 auto',
        px: 3,
        py: 2,
        color: highlightedIndex === index ? 'white' : 'gray.8',
        bg: highlightedIndex === index ? 'blue.5' : 'transparent',
        style: {cursor: 'pointer'},
      })}
    >
      <Text
        fontSize={0}
        color={highlightedIndex === index ? 'blue.2' : 'gray.7'}
      >
        {getBreadcrumbs(siteMetadata.shortName, item.path).join(' / ')}
      </Text>
      {item.title}
    </Flex>
  ))
}
 
function getBreadcrumbs(siteTitle, path) {
  return [
    siteTitle,
    ...path
      .split('/')
      .filter(Boolean)
      // The last title will be displayed separately, so we exclude it
      // from the breadcrumbs to avoid displaying it twice.
      .slice(0, -1)
      .map(sentenceCase),
  ]
}
 
export default SearchResults