This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: extract search and category into hook
- Loading branch information
1 parent
5afc5b1
commit 89e122f
Showing
3 changed files
with
95 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useTessen } from '@newrelic/gatsby-theme-newrelic'; | ||
import { navigate, useLocation } from '@reach/router'; | ||
import CATEGORIES from '@data/instant-observability-categories'; | ||
|
||
const useSearchAndCategory = () => { | ||
const tessen = useTessen(); | ||
const location = useLocation(); | ||
|
||
const [search, setSearch] = useState(''); | ||
const [category, setCategory] = useState(''); | ||
|
||
// used to update search and category values | ||
useEffect(() => { | ||
const params = new URLSearchParams(location.search); | ||
const searchParam = params.get('search'); | ||
const categoryParam = params.get('category'); | ||
const validCategory = CATEGORIES.some((cat) => cat.value === categoryParam); | ||
|
||
setSearch(searchParam); | ||
setCategory(categoryParam && validCategory ? categoryParam : ''); | ||
if (searchParam || categoryParam) { | ||
tessen.track({ | ||
eventName: 'instantObservability', | ||
category: 'QuickstartCatalogSearch', | ||
search: searchParam, | ||
quickstartCategory: categoryParam, | ||
}); | ||
} | ||
}, [location.search, tessen]); | ||
|
||
/** | ||
* Updates search parameter from location | ||
* @param {String} value to update search term | ||
*/ | ||
const handleSearch = (value) => { | ||
if (value !== null && value !== undefined && value !== '') { | ||
const params = new URLSearchParams(location.search); | ||
params.set('search', value); | ||
|
||
navigate(`?${params.toString()}`); | ||
} else { | ||
const params = new URLSearchParams(location.search); | ||
params.delete('search'); | ||
|
||
navigate(location.pathname); | ||
} | ||
}; | ||
|
||
/** | ||
* Updates category parameter from location | ||
* @param {String} value to update category term | ||
*/ | ||
const handleCategory = (value) => { | ||
if (value !== null && value !== undefined && value !== '') { | ||
const params = new URLSearchParams(location.search); | ||
params.set('category', value); | ||
|
||
navigate(`?${params.toString()}`); | ||
} else { | ||
const params = new URLSearchParams(location.search); | ||
params.delete('category'); | ||
|
||
navigate(location.pathname); | ||
} | ||
}; | ||
|
||
return { search, category, handleSearch, handleCategory }; | ||
}; | ||
|
||
export default useSearchAndCategory; |
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