Skip to content

Commit

Permalink
feat: add communities to the global search
Browse files Browse the repository at this point in the history
  • Loading branch information
ewan-escience committed Jul 15, 2024
1 parent 3c7e701 commit ed4caf7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
26 changes: 23 additions & 3 deletions database/100-create-api-views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ $$
software.short_statement ILIKE CONCAT('%', query, '%')
OR
BOOL_OR(keyword.value ILIKE CONCAT('%', query, '%'))
UNION
UNION ALL
-- PROJECT search item
SELECT
project.slug,
Expand Down Expand Up @@ -904,7 +904,7 @@ $$
project.subtitle ILIKE CONCAT('%', query, '%')
OR
BOOL_OR(keyword.value ILIKE CONCAT('%', query, '%'))
UNION
UNION ALL
-- ORGANISATION search item
SELECT
organisation.slug,
Expand All @@ -928,7 +928,27 @@ $$
organisation.parent IS NULL
AND
(organisation.slug ILIKE CONCAT('%', query, '%') OR organisation."name" ILIKE CONCAT('%', query, '%'))
;
UNION ALL
-- COMMUNITY search item
SELECT
community.slug,
community."name",
'communities' AS "source",
TRUE AS is_published,
(CASE
WHEN community.slug ILIKE query OR community."name" ILIKE query THEN 0
WHEN community.slug ILIKE CONCAT(query, '%') OR community."name" ILIKE CONCAT(query, '%') THEN 2
ELSE 3
END) AS rank,
(CASE
WHEN community.slug ILIKE query OR community."name" ILIKE query THEN 0
WHEN community.slug ILIKE CONCAT(query, '%') OR community."name" ILIKE CONCAT(query, '%') THEN 0
ELSE LEAST(POSITION(query IN community.slug), POSITION(query IN community."name"))
END) AS index_found
FROM
community
WHERE
community.slug ILIKE CONCAT('%', query, '%') OR community."name" ILIKE CONCAT('%', query, '%');
$$;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

import logger from '~/utils/logger'
import {createJsonHeaders} from '~/utils/fetchHelpers'
import {sortBySearchFor} from '~/utils/sortFn'

export type GlobalSearchResults = {
slug: string,
name: string,
source: string,
source: 'software' | 'projects' | 'organisations' | 'communities',
is_published?: boolean,
search_text?: string
} | undefined
Expand All @@ -22,7 +21,7 @@ export type GlobalSearchResults = {
* @param searchText
* @param token
*/
export async function getGlobalSearch(searchText: string, token: string,) {
export async function getGlobalSearch(searchText: string, token: string,): Promise<GlobalSearchResults[]> {
try {
// call the function query
const query = `rpc/global_search?query=${searchText}&limit=30&order=rank.asc,index_found.asc`
Expand All @@ -35,13 +34,13 @@ export async function getGlobalSearch(searchText: string, token: string,) {
}
})
if (resp.status === 200) {
const rawData: GlobalSearchResults[] = await resp.json()
// sort by search value based on name property
const sorted = rawData.sort((a, b) => sortBySearchFor(a,b,'name',searchText))
return sorted
// already sorted by the backend, see the query above
return await resp.json()
} else {
throw new Error(`We received an error message when doing a global search, status code ${resp.status}, body ${await resp.text()}`)
}
} catch (e: any) {
logger(`getGlobalSearch: ${e?.message}`, 'error')
return []
throw e
}
}
17 changes: 15 additions & 2 deletions frontend/components/GlobalSearchAutocomplete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import {useDebounce} from '~/utils/useDebounce'
import TerminalIcon from '@mui/icons-material/Terminal'
import ListAltIcon from '@mui/icons-material/ListAlt'
import BusinessIcon from '@mui/icons-material/Business'
import Diversity3Icon from '@mui/icons-material/Diversity3'
import logger from '~/utils/logger'
import useSnackbar from '~/components/snackbar/useSnackbar'

type Props = {
className?: string
Expand All @@ -37,6 +40,7 @@ export default function GlobalSearchAutocomplete(props: Props) {

const lastValue = useDebounce(inputValue, 150)
const inputRef = useRef<HTMLInputElement>(null)
const {showErrorMessage} = useSnackbar()

// console.group('GlobalSearchAutocomplete')
// console.log('inputValue...', inputValue)
Expand All @@ -60,15 +64,23 @@ export default function GlobalSearchAutocomplete(props: Props) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [lastValue])

const defaultValues = [
const defaultValues: GlobalSearchResults[] = [
{name: 'Go to Software page', slug: '', source: 'software'},
{name: 'Go to Projects page', slug: '', source: 'projects'},
{name: 'Go to Organisations page', slug: '', source: 'organisations'},
{name: 'Go to Communities page', slug: '', source: 'communities'},
]

async function fetchData(search: string) {
// Fetch api
const data = await getGlobalSearch(search, session.token) || []
let data: GlobalSearchResults[]
try {
data = await getGlobalSearch(search, session.token) || []
} catch (e: any) {
logger(e?.message, 'error')
showErrorMessage('Something went wrong getting the search results')
data = []
}

if (data?.length === 0) {
setHasResults(false)
Expand Down Expand Up @@ -217,6 +229,7 @@ export default function GlobalSearchAutocomplete(props: Props) {
{item?.source === 'software' && <TerminalIcon/>}
{item?.source === 'projects' && <ListAltIcon/>}
{item?.source === 'organisations' && <BusinessIcon/>}
{item?.source === 'communities' && <Diversity3Icon/>}
</div>

<div className="flex-grow ">
Expand Down

0 comments on commit ed4caf7

Please sign in to comment.