Skip to content

Commit

Permalink
client: Fix race condition in InfiniteScroll
Browse files Browse the repository at this point in the history
  • Loading branch information
XxRoloxX committed Dec 15, 2024
1 parent da2f790 commit 2e37164
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
9 changes: 3 additions & 6 deletions client/src/api/managment-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ class ManagmentServiceApi {
// eslint-disable-next-line
`/api/v1/reports/application-incidents/${incidentId}/sources?page=${page}&size=${size}&sort=timestamp`,
);

return report.data;
}

Expand Down Expand Up @@ -512,13 +513,9 @@ class ManagmentServiceApi {
return response.data;
}

public async getLatestReport(): Promise<
ReportDetails
> {
public async getLatestReport(): Promise<ReportDetails> {
await this.refreshTokenIfExpired();
const response = await this.axiosInstance.get(
'/api/v1/reports/latest',
);
const response = await this.axiosInstance.get('/api/v1/reports/latest');
return response.data;
}

Expand Down
15 changes: 11 additions & 4 deletions client/src/hooks/useInfiniteScroll.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { debounce } from 'lib/debounce';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';

interface UseInfiniteScrollParams {
scrollTargetRef: React.RefObject<HTMLDivElement>;
debounceTreshhold?: number;
scrollTreshhold?: number;
handleScroll: () => void;
handleScroll: () => Promise<void> | void;
}

const useInfiniteScroll = ({
Expand All @@ -14,18 +14,25 @@ const useInfiniteScroll = ({
scrollTreshhold = 2,
handleScroll,
}: UseInfiniteScrollParams) => {
const [isHandling, setIsHandling] = useState(false);
useEffect(() => {
const onScroll = () => {
const onScroll = async () => {
const element = scrollTargetRef.current;
if (!element) {
return;
}

if (isHandling === true) {
return;
}

if (
element.scrollTop + element.clientHeight >=
element.scrollHeight / scrollTreshhold
) {
handleScroll();
setIsHandling(true);
await handleScroll();
setIsHandling(false);
}
};

Expand Down

0 comments on commit 2e37164

Please sign in to comment.