Skip to content
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

feat: fuzzy matching for marketplace search bar #674

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"algoliasearch": "4.14.2",
"clsx": "1.2.1",
"focus-trap-react": "10.0.2",
"fuzzy": "^0.1.3",
"mermaid": "9.2.2",
"next-videos": "1.5.0",
"nextra": "2.0.0-beta.43",
Expand Down
22 changes: 11 additions & 11 deletions packages/components/src/components/marketplace-search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IMarketplaceSearchProps } from '../types/components';
import { MarketplaceList } from './marketplace-list';
import { Tag, TagsContainer } from './tag';
import { SearchIcon } from './icons';
import fuzzy from 'fuzzy';

const renderQueryPlaceholder = (placeholder: string | ReactElement, query: string) => {
if (!query || isValidElement(placeholder)) {
Expand Down Expand Up @@ -35,18 +36,17 @@ export const MarketplaceSearch = ({
const items = useMemo(() => {
let results = null;
if (query && queryList) {
const tagsFilter = query.split(' ').filter(t => t.trim().length > 1 && t.startsWith('#'));
Copy link

@andykenward andykenward Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YassinEldeeb & @B2o5T The tag filter search feature no longer works on the envelop plugins page.

See
https://the-guild.dev/graphql/envelop/plugins

CleanShot.2023-01-05.at.22.20.59.mp4

const queryWithoutTags =
tagsFilter.length > 0 ? query.replace(/#\w\w+\s?/g, '').toLowerCase() : query.toLowerCase();
const matchedResults = fuzzy
.filter(
// Removes all special characters from the query string for better fuzzy matching
query.replace(/[^\w\s]/gi, ''),
// Mapping the queryList items into a list of strings including the titles
queryList.items.map(e => e.title),
)
.map(e => e.original);

results = queryList.items.filter(item => {
const matchesContent = item.title.toLowerCase().includes(queryWithoutTags);

if (tagsFilter.length === 0) {
return matchesContent;
}
return item.tags?.some(tag => tagsFilter.includes(`#${tag}`)) && matchesContent;
});
const filteredItems = queryList.items.filter(e => matchedResults.includes(e.title));
results = filteredItems;
}
return results;
}, [query, queryList]);
Expand Down
21 changes: 13 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.