diff --git a/client/src/components/SearchBar.tsx b/client/src/components/SearchBar.tsx new file mode 100644 index 0000000..c03405e --- /dev/null +++ b/client/src/components/SearchBar.tsx @@ -0,0 +1,38 @@ +import { useRef, useEffect, KeyboardEvent } from 'react'; +import { searchParser } from '../utils'; + +export const SearchBar = (): JSX.Element => { + const inputRef = useRef(document.createElement('input')); + + useEffect(() => { + inputRef.current.focus(); + }, [inputRef]); + + const inputHandler = (e: KeyboardEvent) => { + const rawQuery = searchParser(inputRef.current.value); + }; + + return ( +
+ inputHandler(e)} + /> +
+ Submit search query by pressing `Enter`. Read more about available + filters{' '} + + here + +
+
+ ); +}; diff --git a/client/src/containers/Home.tsx b/client/src/containers/Home.tsx index 5a62cab..054c35d 100644 --- a/client/src/containers/Home.tsx +++ b/client/src/containers/Home.tsx @@ -2,6 +2,7 @@ import { useEffect, useContext, Fragment } from 'react'; import { SnippetsContext } from '../store'; import { Layout, PageHeader, EmptyState } from '../components/UI'; import { SnippetGrid } from '../components/Snippets/SnippetGrid'; +import { SearchBar } from '../components/SearchBar'; export const Home = (): JSX.Element => { const { snippets, getSnippets } = useContext(SnippetsContext); @@ -16,6 +17,12 @@ export const Home = (): JSX.Element => { ) : ( + + + {/*
+ s.isPinned)} /> +
*/} + {snippets.some(s => s.isPinned) && ( diff --git a/client/src/utils/index.ts b/client/src/utils/index.ts index e76dd3b..c2d8f34 100644 --- a/client/src/utils/index.ts +++ b/client/src/utils/index.ts @@ -1,3 +1,4 @@ export * from './dateParser'; export * from './badgeColor'; export * from './findLanguage'; +export * from './searchParser'; diff --git a/client/src/utils/searchParser.ts b/client/src/utils/searchParser.ts new file mode 100644 index 0000000..a43c483 --- /dev/null +++ b/client/src/utils/searchParser.ts @@ -0,0 +1,25 @@ +export const searchParser = (rawQuery: string): void => { + // const rawQuery = 'my search tags:ui,react lang:typescript'; + + // Extract filters from query + const tags = extractFilters(rawQuery, 'tags'); + const languages = extractFilters(rawQuery, 'lang'); + const query = rawQuery.replaceAll(/(tags|lang):[a-zA-Z]+(,[a-zA-Z]+)*/g, ''); + + console.log(tags); + console.log(languages); + console.log(query); +}; + +const extractFilters = (query: string, filter: string): string[] => { + let filters: string[] = []; + + const regex = new RegExp(filter + ':[a-zA-Z]+(,[a-zA-Z]+)*'); + const matcher = query.match(regex); + + if (matcher) { + filters = matcher[0].split(':')[1].split(','); + } + + return filters; +};