-
-
Notifications
You must be signed in to change notification settings - Fork 102
Added switch based mapping for filters in searchbar #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
319c1d2
76834ca
018a81a
b1c17be
b3702de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { SearchResponse } from 'algoliasearch' | ||
import { AlgoliaRequestType, AlgoliaResponseType } from 'types/algolia' | ||
import { ENVIRONMENT } from 'utils/credentials' | ||
import { getParamsForFilters } from 'utils/filterMapping' | ||
import { client } from 'utils/helpers/algoliaClient' | ||
|
||
import { getParamsForIndexName } from 'utils/paramsMapping' | ||
|
@@ -19,17 +20,37 @@ export const fetchAlgoliaData = async <T>( | |
} | ||
try { | ||
const params = getParamsForIndexName(indexName) | ||
|
||
let filters = '' | ||
let searchQuery = query | ||
|
||
const filterRegex = /(\w+)([:><=!]+)([^ ]+)/g | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we put this logic in another function |
||
let match | ||
|
||
while ((match = filterRegex.exec(query)) !== null) { | ||
const [fullMatch, attribute, operator, value] = match | ||
filters += `${attribute}${operator}${value} AND ` | ||
searchQuery = searchQuery.replace(fullMatch, '').trim() | ||
} | ||
|
||
filters = filters.trim().replace(/AND$/, '') | ||
filters = getParamsForFilters(indexName, filters) | ||
|
||
if (filterKey) { | ||
filters = filters ? `(${filters}) AND idx_key:${filterKey}` : `idx_key:${filterKey}` | ||
} | ||
|
||
const request: AlgoliaRequestType = { | ||
attributesToHighlight: [], | ||
hitsPerPage: hitsPerPage, | ||
indexName: `${ENVIRONMENT}_${indexName}`, | ||
page: currentPage - 1, | ||
query: query, | ||
query: searchQuery, | ||
removeWordsIfNoResults: 'allOptional', | ||
...params, | ||
} | ||
if (filterKey) { | ||
request.filters = `idx_key: ${filterKey}` | ||
if (filters) { | ||
request.filters = filters | ||
} | ||
|
||
const { results } = await client.search({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const getParamsForFilters = (indexName: string, filter: string) => { | ||
filter = filter.trim() | ||
switch (indexName) { | ||
case 'projects': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about contribute page (issues), will it be addressed in a separate PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have gone through the issues index, its most of the attributes are seachable, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can also add languages and topics based filtering in contribute page |
||
switch (true) { | ||
case /^contributors([><]=?|=)(\d+)$/.test(filter): | ||
return filter.replace(/^contributors([><]=?|=)(\d+)$/, 'idx_contributors_count$1$2') | ||
case /^stars([><]=?|=)(\d+)$/.test(filter): | ||
return filter.replace(/^stars([><]=?|=)(\d+)$/, 'idx_stars_count$1$2') | ||
case /^forks([><]=?|=)(\d+)$/.test(filter): | ||
return filter.replace(/^forks([><]=?|=)(\d+)$/, 'idx_forks_count$1$2') | ||
case /^is:([a-zA-Z]+)$/.test(filter): | ||
return filter.replace(/^is:([a-zA-Z]+)$/, 'idx_type:$1') | ||
default: | ||
return filter | ||
Comment on lines
+6
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we extend it to datetime fields (created/updated) too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @arkid15r We can do that, how the user will input the dates? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ShashaankS we can always convert the dates into desirable format. |
||
} | ||
default: | ||
return filter | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please define the types for this? Otherwise, we may unintentionally mutate the variable from a string to another type, which could lead to unexpected behavior.