Skip to content

Commit

Permalink
Main search vs search on page (#2212)
Browse files Browse the repository at this point in the history
Fixes #2131
  • Loading branch information
tom2drum authored Sep 6, 2024
1 parent 9e3e337 commit e9e0b8c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
16 changes: 11 additions & 5 deletions ui/snippets/searchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const SearchBar = ({ isHomepage }: Props) => {

const recentSearchKeywords = getRecentSearchKeywords();

const { searchTerm, debouncedSearchTerm, handleSearchTermChange, query, pathname } = useQuickSearchQuery();
const { searchTerm, debouncedSearchTerm, handleSearchTermChange, query } = useQuickSearchQuery();

const handleSubmit = React.useCallback((event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
Expand All @@ -55,13 +55,13 @@ const SearchBar = ({ isHomepage }: Props) => {
const url = route(resultRoute);
mixpanel.logEvent(mixpanel.EventTypes.SEARCH_QUERY, {
'Search query': searchTerm,
'Source page type': mixpanel.getPageType(pathname),
'Source page type': mixpanel.getPageType(router.pathname),
'Result URL': url,
});
saveToRecentKeywords(searchTerm);
router.push(resultRoute, undefined, { shallow: true });
}
}, [ searchTerm, pathname, router ]);
}, [ searchTerm, router ]);

const handleFocus = React.useCallback(() => {
onOpen();
Expand Down Expand Up @@ -90,18 +90,24 @@ const SearchBar = ({ isHomepage }: Props) => {
const handleItemClick = React.useCallback((event: React.MouseEvent<HTMLAnchorElement>) => {
mixpanel.logEvent(mixpanel.EventTypes.SEARCH_QUERY, {
'Search query': searchTerm,
'Source page type': mixpanel.getPageType(pathname),
'Source page type': mixpanel.getPageType(router.pathname),
'Result URL': event.currentTarget.href,
});
saveToRecentKeywords(searchTerm);
onClose();
}, [ pathname, searchTerm, onClose ]);
}, [ router.pathname, searchTerm, onClose ]);

const menuPaddingX = isMobile && !isHomepage ? 24 : 0;
const calculateMenuWidth = React.useCallback(() => {
menuWidth.current = (inputRef.current?.getBoundingClientRect().width || 0) - menuPaddingX;
}, [ menuPaddingX ]);

// clear input on page change
React.useEffect(() => {
handleSearchTermChange('');
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ router.pathname ]);

React.useEffect(() => {
const inputEl = inputRef.current;
if (!inputEl) {
Expand Down
7 changes: 1 addition & 6 deletions ui/snippets/searchBar/useQuickSearchQuery.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { useRouter } from 'next/router';
import React from 'react';

import useApiQuery from 'lib/api/useApiQuery';
import useDebounce from 'lib/hooks/useDebounce';

export default function useQuickSearchQuery() {
const router = useRouter();

const [ searchTerm, setSearchTerm ] = React.useState('');

const debouncedSearchTerm = useDebounce(searchTerm, 300);
const pathname = router.pathname;

const query = useApiQuery('quick_search', {
queryParams: { q: debouncedSearchTerm },
Expand All @@ -30,6 +26,5 @@ export default function useQuickSearchQuery() {
handleSearchTermChange: setSearchTerm,
query,
redirectCheckQuery,
pathname,
}), [ debouncedSearchTerm, pathname, query, redirectCheckQuery, searchTerm ]);
}), [ debouncedSearchTerm, query, redirectCheckQuery, searchTerm ]);
}

0 comments on commit e9e0b8c

Please sign in to comment.