-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
129 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { uniqueId } from 'lodash' | ||
import React, { useEffect, useState } from 'react' | ||
import { Link } from 'react-router-dom' | ||
import { ApiSearchResponse } from '../models/SearchResult' | ||
|
||
import styles from '../style/components/SearchResults.module.css' | ||
|
||
interface Props { | ||
searchQuery: string | ||
loading: boolean | ||
results: ApiSearchResponse | ||
} | ||
|
||
export default function SearchResults (props: Props): JSX.Element { | ||
const [loading, setLoading] = useState<boolean>(props.loading) | ||
const [resultElements, setResultElements] = useState<JSX.Element[]>([]) | ||
|
||
useEffect(() => { | ||
setLoading(props.loading) | ||
}, [props.loading]) | ||
|
||
useEffect(() => { | ||
setLoading(true) | ||
setResultElements(createSearchResultElements(props.results)) | ||
setLoading(false) | ||
}, [props.results]) | ||
|
||
/** | ||
* Used to create the result elements before updating the UI, | ||
* as this can lag if there are many results. | ||
* @param res ApiSearchResponse | ||
* @returns Array of JSX.Element | ||
*/ | ||
const createSearchResultElements = ( | ||
res: ApiSearchResponse | ||
): JSX.Element[] => { | ||
const projects = res.projects.map((p) => ( | ||
<Link | ||
className={styles['search-result']} | ||
key={`project-${p.name}`} | ||
to={`/${p.name}`} | ||
dangerouslySetInnerHTML={{ | ||
__html: highlighedText(p.name) | ||
}} | ||
></Link> | ||
)) | ||
|
||
const versions = res.versions.map((v) => ( | ||
<Link | ||
className={styles['search-result']} | ||
key={`version-${v.project}-${v.version}`} | ||
to={`/${v.project}/${v.version}`} | ||
dangerouslySetInnerHTML={{ | ||
__html: highlighedText(`${v.project} v. ${v.version}`) | ||
}} | ||
></Link> | ||
)) | ||
|
||
const files = res.files.map((f) => ( | ||
<Link | ||
className={styles['search-result']} | ||
key={`file-${f.project}-${f.version}-${f.path}`} | ||
to={`/${f.project}/${f.version}/${f.path}`} | ||
dangerouslySetInnerHTML={{ | ||
__html: highlighedText(`${f.project} v. ${f.version} - ${f.path}`) | ||
}} | ||
></Link> | ||
)) | ||
|
||
return [...projects, ...versions, ...files] | ||
} | ||
|
||
/** | ||
* Used to replace any unsafe characters in the displayed name | ||
* with their html counterparts because we need to set the text as | ||
* html to be able to highlight the search query. | ||
* @param text content to be escaped | ||
* @returns excaped text | ||
*/ | ||
const escapeHtml = (text: string): string => { | ||
return text | ||
.replaceAll('&', '&') | ||
.replaceAll('<', '<') | ||
.replaceAll('>', '>') | ||
.replaceAll('"', '"') | ||
.replaceAll("'", ''') | ||
} | ||
|
||
/** | ||
* Used to highlight the query in the displayed link text. | ||
* @param text link text | ||
* @returns html string with highlighted query | ||
*/ | ||
const highlighedText = (text: string): string => { | ||
text = escapeHtml(text) | ||
return text.replaceAll( | ||
props.searchQuery, | ||
`<span class="${styles.highlighted}" key="highlighted-${uniqueId()}">${ | ||
props.searchQuery | ||
}</span>` | ||
) | ||
} | ||
|
||
if (loading) { | ||
return <div className="loading-spinner"></div> | ||
} | ||
|
||
if (resultElements.length === 0) { | ||
if (props.searchQuery.trim().length === 0) { | ||
return <div className={styles['no-results']}>No results</div> | ||
} | ||
return ( | ||
<div className={styles['no-results']}> | ||
No results for "{props.searchQuery}" | ||
</div> | ||
) | ||
} | ||
|
||
return <div className={styles['search-results']}>{resultElements}</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.