From 7503707ef10bd6461658592e3dc19149777e9230 Mon Sep 17 00:00:00 2001 From: Mathis HERRIOT <197931332+0x485254@users.noreply.github.com> Date: Wed, 21 Jan 2026 13:42:17 +0100 Subject: [PATCH] refactor(contents): extract and memoize `fetchInitial` logic - Refactored `fetchInitial` function to make it reusable using `useCallback`. - Updated `ContentCard` to call `fetchInitial` via `onUpdate` prop for better reusability. - Removed duplicate logic from `useEffect` for improved code readability and maintainability. --- frontend/src/components/content-list.tsx | 43 ++++++++++++------------ 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/frontend/src/components/content-list.tsx b/frontend/src/components/content-list.tsx index 235e057..7959f90 100644 --- a/frontend/src/components/content-list.tsx +++ b/frontend/src/components/content-list.tsx @@ -20,6 +20,27 @@ export function ContentList({ fetchFn, title }: ContentListProps) { const [offset, setOffset] = React.useState(0); const [hasMore, setHasMore] = React.useState(true); + const fetchInitial = React.useCallback(async () => { + setLoading(true); + try { + const response = await fetchFn({ + limit: 10, + offset: 0, + }); + setContents(response.data); + setOffset(0); + setHasMore(response.data.length === 10); + } catch (error) { + console.error("Failed to fetch contents:", error); + } finally { + setLoading(false); + } + }, [fetchFn]); + + React.useEffect(() => { + fetchInitial(); + }, [fetchInitial]); + const loadMore = React.useCallback(async () => { if (!hasMore || loading) return; @@ -46,32 +67,12 @@ export function ContentList({ fetchFn, title }: ContentListProps) { onLoadMore: loadMore, }); - React.useEffect(() => { - const fetchInitial = async () => { - setLoading(true); - try { - const response = await fetchFn({ - limit: 10, - offset: 0, - }); - setContents(response.data); - setHasMore(response.data.length === 10); - } catch (error) { - console.error("Failed to fetch contents:", error); - } finally { - setLoading(false); - } - }; - - fetchInitial(); - }, [fetchFn]); - return (