Skip to content

Commit

Permalink
style: addresses feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Jan 22, 2024
1 parent 3be81f7 commit b1a012a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
21 changes: 9 additions & 12 deletions components/Common/Search/States/WithSearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const WithSearchBox: FC<SearchBoxProps> = ({ onClose }) => {
<div className={styles.fulltextResultsContainer}>
{searchError ? <WithError /> : null}

{(searchTerm ? (
{searchTerm ? (
searchResults?.count ? (
searchResults?.hits.map(hit => (
<WithSearchResult
Expand All @@ -168,18 +168,15 @@ export const WithSearchBox: FC<SearchBoxProps> = ({ onClose }) => {
)
) : (
<WithEmptyState />
)) ?? null}
)}

{searchResults?.count
? searchResults?.count > 8 &&
searchTerm && (
<WithAllResults
searchResults={searchResults}
searchTerm={searchTerm}
selectedFacetName={selectedFacetName}
/>
)
: null}
{searchResults?.count && searchResults?.count > 8 && searchTerm ? (
<WithAllResults
searchResults={searchResults}
searchTerm={searchTerm}
selectedFacetName={selectedFacetName}
/>
) : null}
</div>
<WithPoweredBy />
</div>
Expand Down
20 changes: 6 additions & 14 deletions hooks/react-client/useBottomScrollListener.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { useState, useEffect } from 'react';

import { debounce } from '@/util/debounce';

type CallbackFunction = () => void;

const useBottomScrollListener = (
callback: CallbackFunction,
debounceTime = 300
) => {
const [bottomReached, setBottomReached] = useState(false);
let timeoutId: NodeJS.Timeout | null = null;

const debouncedCallback = debounce(callback, debounceTime);

const handleScroll = () => {
const scrollTop = document.documentElement.scrollTop;
Expand All @@ -18,26 +21,15 @@ const useBottomScrollListener = (

if (bottomOfWindow) {
setBottomReached(true);
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
callback();
}, debounceTime);
debouncedCallback();
} else {
setBottomReached(false);
}
};

useEffect(() => {
// Add the event listener with the passive option set to true
window.addEventListener('scroll', handleScroll, { passive: true });
return () => {
window.removeEventListener('scroll', handleScroll);
if (timeoutId) {
clearTimeout(timeoutId);
}
};
return () => window.removeEventListener('scroll', handleScroll);
}, []);

return bottomReached;
Expand Down
16 changes: 16 additions & 0 deletions util/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type DebounceFunction<T = unknown> = (...args: Array<T>) => void;

export const debounce = <T extends DebounceFunction>(
func: T,
delay: number
): ((...args: Parameters<T>) => void) => {
let timeoutId: NodeJS.Timeout;

return (...args: Parameters<T>) => {
clearTimeout(timeoutId);

timeoutId = setTimeout(() => {
func(...args);
}, delay);
};
};

0 comments on commit b1a012a

Please sign in to comment.