From 88eeb620aec52d6e4172cc908843339412ade90b Mon Sep 17 00:00:00 2001 From: Sriram Hariharan Date: Fri, 4 Oct 2024 22:20:28 -0500 Subject: [PATCH] fix: doesn't autoload on pages that don't have pages to load (#270) * fix: no autoload on singular pages without next page * fix: formatted --- src/views/components/injected/AutoLoad/AutoLoad.tsx | 11 +++++++++-- src/views/lib/loadNextCourseCatalogPage.ts | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/views/components/injected/AutoLoad/AutoLoad.tsx b/src/views/components/injected/AutoLoad/AutoLoad.tsx index 2aa4552b..259ba748 100644 --- a/src/views/components/injected/AutoLoad/AutoLoad.tsx +++ b/src/views/components/injected/AutoLoad/AutoLoad.tsx @@ -7,6 +7,7 @@ import { SiteSupport } from '@views/lib/getSiteSupport'; import type { AutoLoadStatusType } from '@views/lib/loadNextCourseCatalogPage'; import { AutoLoadStatus, + getNextButton, loadNextCourseCatalogPage, removePaginationButtons, } from '@views/lib/loadNextCourseCatalogPage'; @@ -27,6 +28,7 @@ type Props = { export default function AutoLoad({ addRows }: Props): JSX.Element | null { const [container, setContainer] = useState(null); const [status, setStatus] = useState(AutoLoadStatus.IDLE); + const [isSinglePage, setIsSinglePage] = useState(false); useEffect(() => { const portalContainer = document.createElement('div'); @@ -35,6 +37,10 @@ export default function AutoLoad({ addRows }: Props): JSX.Element | null { setContainer(portalContainer); }, []); + useEffect(() => { + setIsSinglePage(!getNextButton(document)); + }, []); + useEffect(() => { removePaginationButtons(document); console.log(`AutoLoad is now ${status}`); @@ -43,6 +49,7 @@ export default function AutoLoad({ addRows }: Props): JSX.Element | null { // This hook will call the callback when the user scrolls to the bottom of the page. useInfiniteScroll(async () => { + if (isSinglePage) return; // fetch the next page of courses const [status, nextRows] = await loadNextCourseCatalogPage(); setStatus(status); @@ -55,9 +62,9 @@ export default function AutoLoad({ addRows }: Props): JSX.Element | null { // add the scraped courses to the current page addRows(scrapedRows); - }, [addRows]); + }, [addRows, isSinglePage]); - if (!container || status === AutoLoadStatus.DONE) { + if (!container || status === AutoLoadStatus.DONE || isSinglePage) { return null; } diff --git a/src/views/lib/loadNextCourseCatalogPage.ts b/src/views/lib/loadNextCourseCatalogPage.ts index 239748b6..8b154d5b 100644 --- a/src/views/lib/loadNextCourseCatalogPage.ts +++ b/src/views/lib/loadNextCourseCatalogPage.ts @@ -70,7 +70,7 @@ export async function loadNextCourseCatalogPage(): Promise<[AutoLoadStatusType, * @param doc the document to get the next button from * @returns the next button from the document */ -function getNextButton(doc: Document) { +export function getNextButton(doc: Document) { return doc.querySelector(NEXT_PAGE_BUTTON_SELECTOR); }