-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re introduce fulltext search
- Loading branch information
Showing
10 changed files
with
539 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { useEffect } from 'react'; | ||
import { Link } from 'react-router-dom'; | ||
import CommandPalette from 'react-cmdk'; | ||
import { Spin, Tag, Empty } from 'antd'; | ||
import { LoadingOutlined } from '@ant-design/icons'; | ||
|
||
import { getNormalizedTypes, getResourceLabel } from 'shared/utils'; | ||
import { useFullTextSearch } from './useFullTextSearch'; | ||
|
||
import 'react-cmdk/dist/cmdk.css'; | ||
import './styles.scss'; | ||
|
||
type Props = { | ||
openCmdk: boolean; | ||
onOpenCmdk(): void; | ||
}; | ||
|
||
const TagRenderer = ({ type }: { type?: string | string[] }) => { | ||
if (!type) return null; | ||
const types = getNormalizedTypes(type); | ||
return ( | ||
<div className="type-tags-list"> | ||
{types.map(t => ( | ||
<Tag color="blue" className="tag" title={t}> | ||
{t} | ||
</Tag> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
const FullTextSearch = ({ openCmdk, onOpenCmdk }: Props) => { | ||
const { | ||
search, | ||
onSearch, | ||
resetSearch, | ||
searchResults, | ||
isLoading, | ||
} = useFullTextSearch(); | ||
|
||
const onChangeOpen = () => { | ||
onOpenCmdk(); | ||
resetSearch(); | ||
}; | ||
|
||
let content = null; | ||
|
||
if (isLoading) { | ||
content = ( | ||
<div className="search-resources-loader"> | ||
<Spin size="large" indicator={<LoadingOutlined />} /> | ||
</div> | ||
); | ||
} else if (!Boolean(searchResults.length) && !Boolean(search)) { | ||
content = <div className="search-for-placeholder">Search for ""</div>; | ||
} else if (!Boolean(searchResults.length) && Boolean(search)) { | ||
content = <Empty description="No results found" />; | ||
} else { | ||
content = searchResults.map(({ id, title, items }) => ( | ||
<div className="cmdk-list-container" key={id}> | ||
<div className="cmdk-list-title"> | ||
<Tag color="geekblue">{title?.orgLabel}</Tag>| | ||
<Tag color="geekblue">{title?.projectLabel}</Tag> | ||
</div> | ||
<div className="cmdk-list-content"> | ||
{items.map(resource => { | ||
const label = getResourceLabel(resource); | ||
return ( | ||
<Link | ||
to={`/resolve/${encodeURIComponent(resource['@id'])}`} | ||
onClick={onOpenCmdk} | ||
className="cmdk-list-item" | ||
> | ||
<div className="item-title">{label}</div> | ||
<TagRenderer type={resource['@type']} /> | ||
</Link> | ||
); | ||
})} | ||
</div> | ||
</div> | ||
)); | ||
} | ||
|
||
useEffect(() => { | ||
const keyDown = (e: KeyboardEvent) => { | ||
if (e.ctrlKey && e.key === 'e') { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
onOpenCmdk(); | ||
} | ||
}; | ||
|
||
document.addEventListener('keydown', keyDown); | ||
return () => document.removeEventListener('keydown', keyDown); | ||
}, []); | ||
|
||
return ( | ||
<CommandPalette | ||
onChangeSearch={onSearch} | ||
onChangeOpen={onChangeOpen} | ||
search={search} | ||
isOpen={openCmdk} | ||
page="root" | ||
placeholder="Search for resources" | ||
> | ||
{content} | ||
</CommandPalette> | ||
); | ||
}; | ||
|
||
export default FullTextSearch; |
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,98 @@ | ||
@import '../../lib.scss'; | ||
|
||
.cmdk-list-container { | ||
padding: 10px; | ||
margin-top: 0 !important; | ||
} | ||
.cmdk-list-title { | ||
text-transform: uppercase !important; | ||
font-weight: bold !important; | ||
margin-bottom: 10px !important; | ||
color: $fusion-daybreak-7; | ||
display: flex; | ||
align-items: center; | ||
gap: 5px; | ||
|
||
.ant-tag { | ||
padding: 2px 4px; | ||
} | ||
} | ||
|
||
.cmdk-list-content { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 3px; | ||
} | ||
|
||
.cmdk-list-item { | ||
padding: 10px 8px; | ||
margin-left: 5px; | ||
border-radius: 4px; | ||
font-size: 16px; | ||
color: #333; | ||
border: 1px solid #efefef; | ||
|
||
&:hover { | ||
background: $fusion-blue-1 !important; | ||
box-shadow: 0 2px 12px 0px rgba($color: #333, $alpha: 0.14); | ||
} | ||
} | ||
|
||
.type-tags-list { | ||
display: flex; | ||
align-items: center; | ||
justify-content: flex-start; | ||
gap: 3px; | ||
margin: 4px 0px; | ||
.tag { | ||
padding: 2px 3px; | ||
} | ||
} | ||
|
||
.item-creation_date { | ||
font-size: 12px; | ||
} | ||
|
||
.search-resources-loader { | ||
width: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
padding-top: 10px; | ||
padding-bottom: 10px; | ||
} | ||
|
||
.search-for-placeholder { | ||
background-color: $fusion-neutral-2; | ||
padding: 10px; | ||
color: #333; | ||
user-select: none; | ||
border-radius: 4px; | ||
} | ||
|
||
@media (prefers-color-scheme: dark) { | ||
.cmdk-list-item { | ||
background: white; | ||
color: black; | ||
|
||
&:hover { | ||
background: #efefef !important; | ||
box-shadow: 0 2px 12px 0px rgba($color: white, $alpha: 0.14); | ||
color: black; | ||
.item-title { | ||
color: #333; | ||
} | ||
} | ||
} | ||
|
||
.search-for-placeholder { | ||
background-color: white; | ||
color: #101827; | ||
} | ||
|
||
.ant-empty { | ||
.ant-empty-description { | ||
color: white; | ||
} | ||
} | ||
} |
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,44 @@ | ||
import { useState } from 'react'; | ||
import { useNexusContext } from '@bbp/react-nexus'; | ||
import { useQuery } from 'react-query'; | ||
import { getOrgAndProjectFromProjectId } from 'shared/utils'; | ||
import { groupBy } from 'lodash'; | ||
|
||
export function useFullTextSearch() { | ||
const [search, setSearch] = useState(''); | ||
const nexus = useNexusContext(); | ||
|
||
const onSearch = (value: string) => setSearch(value); | ||
const resetSearch = () => setSearch(''); | ||
|
||
const { isLoading, data } = useQuery({ | ||
enabled: !!search, | ||
queryKey: ['cmdk-search', { search }], | ||
queryFn: () => | ||
nexus.Resource.list(undefined, undefined, { | ||
q: search, | ||
deprecated: false, | ||
}), | ||
select: data => data._results, | ||
staleTime: 2, | ||
}); | ||
|
||
const resources = groupBy(data, '_project'); | ||
|
||
const searchResults = Object.entries(resources).map(([key, value]) => { | ||
const orgProject = getOrgAndProjectFromProjectId(key); | ||
return { | ||
id: key, | ||
title: orgProject, | ||
items: value, | ||
}; | ||
}); | ||
|
||
return { | ||
search, | ||
onSearch, | ||
resetSearch, | ||
isLoading, | ||
searchResults, | ||
}; | ||
} |
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
Oops, something went wrong.