diff --git a/frontend/src/components/Product/ProductList/ProductList.tsx b/frontend/src/components/Product/ProductList/ProductList.tsx index 2afa5f995..40b492f73 100644 --- a/frontend/src/components/Product/ProductList/ProductList.tsx +++ b/frontend/src/components/Product/ProductList/ProductList.tsx @@ -20,6 +20,7 @@ interface ProductListProps { const ProductList = ({ category, isHomePage, selectedOption }: ProductListProps) => { const scrollRef = useRef(null); + const productListRef = useRef(null); const { categoryIds } = useCategoryValueContext(); @@ -27,13 +28,17 @@ const ProductList = ({ category, isHomePage, selectedOption }: ProductListProps) categoryIds[category], selectedOption?.value ?? 'reviewCount,desc' ); + + useIntersectionObserver(fetchNextPage, scrollRef, hasNextPage); + + useScrollRestoration(categoryIds[category], productListRef); + const productList = data.pages.flatMap((page) => page.products); const productsToDisplay = displaySlice(isHomePage, productList); - useIntersectionObserver(fetchNextPage, scrollRef, hasNextPage); return ( - <> - + + {productsToDisplay.map((product) => (
  • @@ -41,14 +46,19 @@ const ProductList = ({ category, isHomePage, selectedOption }: ProductListProps)
  • ))} -
    +
    - + ); }; export default ProductList; -const ProductListContainer = styled.ul` +const ProductListContainer = styled.div` + height: calc(100% - 150px); + overflow-y: auto; +`; + +const ProductListWrapper = styled.ul` display: flex; flex-direction: column; diff --git a/frontend/src/hooks/common/useScrollRestoration.ts b/frontend/src/hooks/common/useScrollRestoration.ts index c03001048..00e53598a 100644 --- a/frontend/src/hooks/common/useScrollRestoration.ts +++ b/frontend/src/hooks/common/useScrollRestoration.ts @@ -2,10 +2,11 @@ import type { RefObject } from 'react'; import { useEffect } from 'react'; import useTimeout from './useTimeout'; -import { useCategoryActionContext } from '../context'; +import { useCategoryActionContext, useCategoryValueContext } from '../context'; const useScrollRestoration = (currentCategoryId: number, ref: RefObject) => { const { saveCurrentTabScroll } = useCategoryActionContext(); + const { currentTabScroll } = useCategoryValueContext(); const handleScroll = () => { if (!ref.current) return; @@ -19,6 +20,9 @@ const useScrollRestoration = (currentCategoryId: number, ref: RefObject { ref.current?.removeEventListener('scroll', timeoutFn); }; diff --git a/frontend/src/pages/ProductListPage.tsx b/frontend/src/pages/ProductListPage.tsx index 075a42459..ae32a6b29 100644 --- a/frontend/src/pages/ProductListPage.tsx +++ b/frontend/src/pages/ProductListPage.tsx @@ -1,6 +1,6 @@ import { BottomSheet, Spacing, useBottomSheet } from '@fun-eat/design-system'; import { useQueryErrorResetBoundary } from '@tanstack/react-query'; -import { Suspense, useEffect, useRef } from 'react'; +import { Suspense } from 'react'; import { useParams } from 'react-router-dom'; import styled from 'styled-components'; @@ -17,8 +17,7 @@ import { import { ProductList } from '@/components/Product'; import { PRODUCT_SORT_OPTIONS } from '@/constants'; import { PATH } from '@/constants/path'; -import { useScrollRestoration, useSortOption } from '@/hooks/common'; -import { useCategoryValueContext } from '@/hooks/context'; +import { useSortOption } from '@/hooks/common'; import { isCategoryVariant } from '@/types/common'; const PAGE_TITLE = { food: '공통 상품', store: 'PB 상품' }; @@ -26,24 +25,13 @@ const PAGE_TITLE = { food: '공통 상품', store: 'PB 상품' }; const ProductListPage = () => { const { category } = useParams(); - if (!category || !isCategoryVariant(category)) { - return null; - } - const { ref, isClosing, handleOpenBottomSheet, handleCloseBottomSheet } = useBottomSheet(); const { selectedOption, selectSortOption } = useSortOption(PRODUCT_SORT_OPTIONS[0]); const { reset } = useQueryErrorResetBoundary(); - const { categoryIds, currentTabScroll } = useCategoryValueContext(); - const currentCategoryId = categoryIds[category]; - - const productListRef = useRef(null); - useScrollRestoration(currentCategoryId, productListRef); - - useEffect(() => { - const scrollY = currentTabScroll[currentCategoryId]; - productListRef.current?.scrollTo(0, scrollY); - }, [currentCategoryId]); + if (!category || !isCategoryVariant(category)) { + return null; + } return ( <> @@ -61,9 +49,7 @@ const ProductListPage = () => { - - - + @@ -90,8 +76,3 @@ const SortButtonWrapper = styled.div` justify-content: flex-end; margin-top: 20px; `; - -const ProductListWrapper = styled.div` - height: calc(100% - 150px); - overflow-y: auto; -`;