-
Notifications
You must be signed in to change notification settings - Fork 477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolve infinite scroll issue in Resource page #9267
base: develop
Are you sure you want to change the base?
Changes from 5 commits
83e9154
97119a0
4b3a557
c8452e5
669bdc9
d3b8102
f9249dc
a780d43
b9c0bc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { navigate } from "raviger"; | ||
import { Suspense, lazy, useState } from "react"; | ||
import React, { Suspense, lazy, useState } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
import CareIcon from "@/CAREUI/icons/CareIcon"; | ||
|
@@ -12,7 +12,7 @@ import PageTitle from "@/components/Common/PageTitle"; | |
import Tabs from "@/components/Common/Tabs"; | ||
import { ResourceModel } from "@/components/Facility/models"; | ||
import SearchInput from "@/components/Form/SearchInput"; | ||
import type { KanbanBoardType } from "@/components/Kanban/Board"; | ||
import type { KanbanBoardProps } from "@/components/Kanban/Board"; | ||
import BadgesList from "@/components/Resource/ResourceBadges"; | ||
import ResourceBlock from "@/components/Resource/ResourceBlock"; | ||
import { formatFilter } from "@/components/Resource/ResourceCommons"; | ||
|
@@ -25,9 +25,17 @@ import { RESOURCE_CHOICES } from "@/common/constants"; | |
import routes from "@/Utils/request/api"; | ||
import request from "@/Utils/request/request"; | ||
|
||
const KanbanBoard = lazy( | ||
// Helper function to type lazy-loaded components | ||
function lazyWithProps<T>( | ||
factory: () => Promise<{ default: React.ComponentType<T> }>, | ||
) { | ||
return lazy(factory) as React.LazyExoticComponent<React.ComponentType<T>>; | ||
} | ||
Comment on lines
+30
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider moving the The Create a new file import { lazy } from 'react';
export function lazyWithProps<T>(
factory: () => Promise<{ default: React.ComponentType<T> }>,
) {
return lazy(factory) as React.LazyExoticComponent<React.ComponentType<T>>;
} |
||
|
||
// Correctly lazy-load KanbanBoard with its props | ||
const KanbanBoard = lazyWithProps<KanbanBoardProps<ResourceModel>>( | ||
() => import("@/components/Kanban/Board"), | ||
) as KanbanBoardType; | ||
); | ||
|
||
const resourceStatusOptions = RESOURCE_CHOICES.map((obj) => obj.text); | ||
|
||
|
@@ -39,8 +47,7 @@ export default function BoardView() { | |
limit: -1, | ||
cacheBlacklist: ["title"], | ||
}); | ||
const [boardFilter, setBoardFilter] = useState(ACTIVE); | ||
// eslint-disable-next-line | ||
const [boardFilter, setBoardFilter] = useState(resourceStatusOptions); | ||
const appliedFilters = formatFilter(qParams); | ||
const { t } = useTranslation(); | ||
|
||
|
@@ -103,7 +110,7 @@ export default function BoardView() { | |
</div> | ||
</div> | ||
<Suspense fallback={<Loading />}> | ||
<KanbanBoard<ResourceModel> | ||
<KanbanBoard | ||
title={<BadgesList {...{ appliedFilters, FilterBadges }} />} | ||
sections={boardFilter.map((board) => ({ | ||
id: board, | ||
|
@@ -127,7 +134,7 @@ export default function BoardView() { | |
/> | ||
</h3> | ||
), | ||
fetchOptions: (id) => ({ | ||
fetchOptions: (id: string) => ({ | ||
NikhilA8606 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
route: routes.listResourceRequests, | ||
options: { | ||
query: formatFilter({ | ||
|
@@ -143,7 +150,9 @@ export default function BoardView() { | |
`/resource/${result.draggableId}/update?status=${result.destination?.droppableId}`, | ||
); | ||
}} | ||
itemRender={(resource) => <ResourceBlock resource={resource} />} | ||
itemRender={(resource: ResourceModel) => ( | ||
<ResourceBlock resource={resource} /> | ||
)} | ||
/> | ||
</Suspense> | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
export default function useDebounce<T extends any[]>( | ||
callback: (...args: T) => void, | ||
delay: number, | ||
) { | ||
const callbackRef = useRef(callback); | ||
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
||
useEffect(() => { | ||
callbackRef.current = callback; | ||
}, [callback]); | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (timeoutRef.current) clearTimeout(timeoutRef.current); | ||
}; | ||
}, []); | ||
|
||
const debouncedCallback = (...args: T) => { | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current); | ||
} | ||
timeoutRef.current = setTimeout(() => { | ||
callbackRef.current(...args); | ||
}, delay); | ||
}; | ||
return debouncedCallback; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance error handling and type safety
While the refresh logic is good, there are two areas for improvement:
Consider applying these changes:
📝 Committable suggestion