Skip to content

Commit 7d74ffa

Browse files
committed
refactor: General cleanup
1 parent a7afdce commit 7d74ffa

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/components/FeedOutputBrowser/useFeedBrowser.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { keepPreviousData, useQuery } from "@tanstack/react-query";
2-
import { useEffect, useRef, useState } from "react";
2+
import { useCallback, useEffect, useRef, useState } from "react";
33
import { useAppSelector } from "../../store/hooks";
44
import { fetchFolders } from "../NewLibrary";
55

@@ -56,9 +56,9 @@ export const useFeedBrowser = () => {
5656
});
5757

5858
// Handle pagination by incrementing the page number
59-
const handlePagination = () => {
59+
const handlePagination = useCallback(() => {
6060
setPageNumber((prevState) => prevState + 1);
61-
};
61+
}, []);
6262

6363
const observerTarget = useRef(null);
6464

@@ -87,7 +87,7 @@ export const useFeedBrowser = () => {
8787
observer.unobserve(observerTarget.current);
8888
}
8989
};
90-
}, [fetchMore]);
90+
}, [fetchMore, handlePagination]);
9191

9292
useEffect(() => {
9393
if ((statusTitle && status.includes(statusTitle)) || finished) {
@@ -103,7 +103,7 @@ export const useFeedBrowser = () => {
103103
}, 3000);
104104
}
105105
}
106-
}, [finished, pluginFilesPayload, statusTitle, download.error]);
106+
}, [finished, statusTitle, download.error]);
107107

108108
useEffect(() => {
109109
setCurrentPath(selected?.data.output_path);

src/components/NewLibrary/index.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
import { Button, Grid, PageSection } from "@patternfly/react-core";
77
import { keepPreviousData, useQuery } from "@tanstack/react-query";
88
import { debounce } from "lodash";
9-
import { useEffect, useRef, useState } from "react";
9+
import { useCallback, useEffect, useRef, useState } from "react";
1010
import { useLocation, useNavigate } from "react-router";
1111
import ChrisAPIClient from "../../api/chrisapiclient";
1212
import { useAppSelector } from "../../store/hooks";
@@ -146,7 +146,7 @@ const NewLibrary = () => {
146146
});
147147
});
148148
}
149-
}, [data?.errorMessages]);
149+
}, [api.error, data?.errorMessages]);
150150

151151
const fetchMore =
152152
data?.foldersPagination?.hasNextPage ||
@@ -158,12 +158,34 @@ const NewLibrary = () => {
158158
navigate(url);
159159
}, 500);
160160

161-
const handlePagination = () => {
161+
const handlePagination = useCallback(() => {
162162
setPageNumber((prevState) => prevState + 1);
163-
};
163+
}, []);
164164

165165
const observerTarget = useRef(null);
166166

167+
// Set up an intersection observer to load more data when the user scrolls to the bottom of the page
168+
useEffect(() => {
169+
const observer = new IntersectionObserver(
170+
(entries) => {
171+
if (entries[0].isIntersecting && fetchMore) {
172+
handlePagination();
173+
}
174+
},
175+
{ threshold: 0.5 },
176+
);
177+
178+
if (observerTarget.current) {
179+
observer.observe(observerTarget.current);
180+
}
181+
182+
return () => {
183+
if (observerTarget.current) {
184+
observer.unobserve(observerTarget.current);
185+
}
186+
};
187+
}, [fetchMore, handlePagination]);
188+
167189
if (isFirstLoad && pathname === "/library") {
168190
return null;
169191
}

0 commit comments

Comments
 (0)