Skip to content

Commit

Permalink
added infinite scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJustChew committed Jun 1, 2024
1 parent a660a49 commit 9844546
Showing 1 changed file with 48 additions and 9 deletions.
57 changes: 48 additions & 9 deletions src/app/[lang]/(mods-pages)/courses/SearchContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { InfiniteHits, useClearRefinements, useStats, PoweredBy } from 'react-instantsearch';
import { useClearRefinements, useStats, PoweredBy, useInfiniteHits, useInstantSearch } from 'react-instantsearch';
import { createInfiniteHitsSessionStorageCache } from 'instantsearch.js/es/lib/infiniteHitsCache';
import algoliasearch from 'algoliasearch/lite';
import useDictionary from "@/dictionaries/useDictionary";
import CourseListItem from "@/components/Courses/CourseListItem";
import Filter from './Filters'
import { ScrollArea } from "@/components/ui/scroll-area";
import ClearAllButton from '@/app/[lang]/(mods-pages)/courses/ClearAllButton';
import { useEffect, useRef } from 'react';

type SearchClient = ReturnType<typeof algoliasearch>;
type InfiniteHitsCache = ReturnType<typeof createInfiniteHitsSessionStorageCache>;
Expand All @@ -14,6 +15,47 @@ const Hit = ({ hit }: { hit: any }) => {
return <CourseListItem course={hit} />;
}

export function InfiniteHits(props: Parameters<typeof useInfiniteHits>[0]) {
const { hits, isLastPage, showMore } = useInfiniteHits({
showPrevious: false,
...props
});
const { status } = useInstantSearch();

const sentinelRef = useRef(null);

useEffect(() => {
if (sentinelRef.current !== null) {
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting && !isLastPage) {
showMore();
}
});
});

observer.observe(sentinelRef.current);

return () => {
observer.disconnect();
};
}
}, [isLastPage, showMore]);

return (
<ScrollArea className="">
<div className="ais-InfiniteHits">
<ul className="ais-InfiniteHits-list flex flex-col w-full h-full space-y-5">
{hits.map((hit) => <Hit key={hit.objectID} hit={hit} />)}
<li ref={sentinelRef} aria-hidden={true} />
{status === 'loading' && <li>Loading...</li>}
</ul>
</div>
</ScrollArea>
);
}


const SearchContainer = ({
searchClient,
sessionStorageCache,
Expand All @@ -25,6 +67,10 @@ const SearchContainer = ({
const dict = useDictionary();
const { nbHits, processingTimeMS } = useStats();

const scrollRef = useRef<HTMLDivElement>(null);

// listen i

return <div className="flex w-full gap-4">
<div className="hidden md:flex flex-col gap-4 w-72">
<div className="flex justify-between items-end">
Expand Down Expand Up @@ -53,17 +99,10 @@ const SearchContainer = ({
</svg>
</a>
</div>
<ScrollArea className="">
<InfiniteHits
hitComponent={Hit}
<InfiniteHits
showPrevious={false}
cache={sessionStorageCache}
classNames={{
list: 'flex flex-col w-full h-full space-y-5',
loadMore: 'underline mt-5 pb-5',
}}
/>
</ScrollArea>
</div>
</div>
}
Expand Down

0 comments on commit 9844546

Please sign in to comment.