-
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.
Merge pull request #493 from docat-org/improvement/472-simplify-search
Improvement: Migrate Search Results from seperate page to home page
- Loading branch information
Showing
20 changed files
with
177 additions
and
525 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
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
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
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
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
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 |
---|---|---|
@@ -1,33 +1,41 @@ | ||
import _ from 'lodash' | ||
import { TextField } from '@mui/material' | ||
import { useNavigate } from 'react-router-dom' | ||
import React from 'react' | ||
import React, { useCallback } from 'react' | ||
import styles from '../style/components/SearchBar.module.css' | ||
import { Search } from '@mui/icons-material' | ||
import { useSearch } from '../data-providers/SearchProvider' | ||
|
||
export default function SearchBar(): JSX.Element { | ||
const navigate = useNavigate() | ||
const [searchQuery, setSearchQuery] = React.useState<string>('') | ||
export default function SearchBar (): JSX.Element { | ||
const { query, setQuery } = useSearch() | ||
const [searchQuery, setSearchQuery] = React.useState<string>(query) | ||
|
||
const updateSearchQueryInDataProvider = useCallback( | ||
_.debounce( | ||
(query: string): void => { | ||
setQuery(query) | ||
}, 500), | ||
[]) | ||
|
||
const onSearch = (e: React.ChangeEvent<HTMLInputElement>): void => { | ||
setSearchQuery(e.target.value) | ||
updateSearchQueryInDataProvider.cancel() | ||
updateSearchQueryInDataProvider(e.target.value) | ||
} | ||
|
||
return ( | ||
<> | ||
<Search | ||
className={styles['search-icon']} | ||
onClick={() => navigate('/search')} | ||
/> | ||
<div className={styles['search-bar']}> | ||
<TextField | ||
label="Search" | ||
type="search" | ||
value={searchQuery} | ||
onChange={(e) => setSearchQuery(e.target.value)} | ||
onKeyDown={(e) => { | ||
if (e.key === 'Enter') { | ||
navigate(`/search?query=${searchQuery}`) | ||
} | ||
}} | ||
variant="standard" | ||
></TextField> | ||
</div> | ||
</> | ||
<div className={styles['search-bar']}> | ||
<TextField | ||
label="Search" | ||
type="search" | ||
value={searchQuery} | ||
onChange={onSearch} | ||
onKeyDown={(e): void => { | ||
if (e.key === 'Enter') { | ||
e.preventDefault() | ||
setQuery(searchQuery) | ||
} | ||
}} | ||
variant="standard" | ||
></TextField> | ||
</div> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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
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,75 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-assignment */ | ||
/* | ||
We need any, because we don't know the type of the children, | ||
and we need the return those children again which is an "unsafe return" | ||
*/ | ||
|
||
import React, { createContext, useContext, useEffect, useState } from 'react' | ||
import { Project } from '../models/ProjectsResponse' | ||
import { useProjects } from './ProjectDataProvider' | ||
import Fuse from 'fuse.js' | ||
|
||
interface SearchState { | ||
filteredProjects: Project[] | null | ||
query: string | ||
setQuery: (query: string) => void | ||
} | ||
|
||
const Context = createContext<SearchState>({ | ||
filteredProjects: null, | ||
query: '', | ||
setQuery: (): void => { | ||
console.warn('SearchDataProvider not initialized') | ||
} | ||
}) | ||
|
||
export function SearchProvider ({ children }: any): JSX.Element { | ||
const { projects } = useProjects() | ||
|
||
const filterProjects = (query: string): Project[] | null => { | ||
if (projects == null) { | ||
return null | ||
} | ||
|
||
if (query.trim() === '') { | ||
return projects | ||
} | ||
|
||
const fuse = new Fuse(projects, { | ||
keys: ['name'], | ||
includeScore: true | ||
}) | ||
|
||
// sort by match score | ||
return fuse | ||
.search(query) | ||
.sort((x, y) => (x.score ?? 0) - (y.score ?? 0)) | ||
.map((result) => result.item) | ||
} | ||
|
||
const setQuery = (query: string): void => { | ||
setState({ | ||
query, | ||
filteredProjects: filterProjects(query), | ||
setQuery | ||
}) | ||
} | ||
|
||
const [state, setState] = useState<SearchState>({ | ||
filteredProjects: null, | ||
query: '', | ||
setQuery | ||
}) | ||
|
||
useEffect(() => { | ||
setState({ | ||
query: '', | ||
filteredProjects: filterProjects(''), | ||
setQuery | ||
}) | ||
}, [projects]) | ||
|
||
return <Context.Provider value={state}>{children}</Context.Provider> | ||
} | ||
|
||
export const useSearch = (): SearchState => useContext(Context) |
Oops, something went wrong.