260202_01_ENH: route 구조 초창기 기획으로 다시 변경 및 Category 별 하위 페이지에 샘플 데이터 모두 적용
This commit is contained in:
@@ -1,21 +1,22 @@
|
||||
"use client";
|
||||
|
||||
import {ReactNode, useEffect, useMemo, useState} from "react";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import {ReactNode, useEffect, useMemo, useRef, useState} from "react";
|
||||
import {usePathname, useRouter} from "next/navigation";
|
||||
import {cn} from "@/lib/utils";
|
||||
import {SelectedMap} from "@/types/common";
|
||||
import {topMenus, totalSubMenus, categorySubMenus} from "@/config/menu.data";
|
||||
import {categoryTarget, TARGET_SLUG, totalFilters} from "@/config/filter.data";
|
||||
import {categoryTarget, totalFilters} from "@/config/filter.data";
|
||||
import {CategoryTargetData, CategoryTargetType} from "@/config/filter.types";
|
||||
import Header from "./Header";
|
||||
import SubMenu from "./SubMenu";
|
||||
import TotalFilter from "./TotalFilter";
|
||||
import CategoryFilter, {CategoryFilterValue} from "./CategoryFilter";
|
||||
import ExcelDownloadButton from "./ExcelDownloadButton";
|
||||
import {LayoutFilterProvider} from "./LayoutFilterContext";
|
||||
import {ExcelExportContext, LayoutFilterProvider} from "./LayoutFilterContext";
|
||||
import {PRODUCTS_FILTER_ITEM} from "@/const/lgConst";
|
||||
import {ProductsFilterItem} from "@/types/lg";
|
||||
import {useLoading} from "@/hooks/useLoading";
|
||||
import {ArrowUp} from "lucide-react";
|
||||
|
||||
type MainLayoutProps = {
|
||||
children: ReactNode;
|
||||
@@ -49,6 +50,9 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
const pathname = usePathname();
|
||||
const { withLoadingAsync } = useLoading();
|
||||
|
||||
const scrollRef = useRef<HTMLDivElement | null>(null);
|
||||
const [showScrollTop, setShowScrollTop] = useState(false);
|
||||
|
||||
const isTotal = pathname.startsWith("/total");
|
||||
const activeTop = topMenus.find((m) => m.match?.(pathname)) ?? topMenus[0];
|
||||
const subMenus = isTotal ? totalSubMenus : categorySubMenus;
|
||||
@@ -67,52 +71,45 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
router.push(path);
|
||||
};
|
||||
|
||||
const moveCategoryPage = (path: string) => {
|
||||
const slug = TARGET_SLUG[categoryValue.target] ?? "profile";
|
||||
const nextPath = `${path}/${slug}`;
|
||||
|
||||
if (pathname !== nextPath) {
|
||||
router.push(nextPath);
|
||||
}
|
||||
}
|
||||
|
||||
const toggleFilter = () => {
|
||||
if (isTotal) setIsTotalFilterOpen((v) => !v);
|
||||
else setIsCategoryFilterOpen((v) => !v);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
/*
|
||||
withLoadingAsync(async () => {
|
||||
setCategoryValue(prev => ({...prev, product: categoryFilterItems.items[0]}))
|
||||
});
|
||||
*/
|
||||
setCategoryValue(prev => ({...prev, product: categoryFilterItems.items[0]}))
|
||||
}, [categoryFilterItems]);
|
||||
const onScrollContent = () => {
|
||||
const el = scrollRef.current;
|
||||
if (!el) return;
|
||||
setShowScrollTop(el.scrollTop > 120);
|
||||
};
|
||||
|
||||
const scrollToTop = () => {
|
||||
scrollRef.current?.scrollTo({ top: 0, behavior: "smooth" });
|
||||
};
|
||||
|
||||
const ctx = useMemo<ExcelExportContext>(() => {
|
||||
return isTotal
|
||||
? { isTotal: true, filterValue: totalValue }
|
||||
: { isTotal: false, filterValue: categoryValue };
|
||||
}, [isTotal, totalValue, categoryValue]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isTotal) return;
|
||||
|
||||
const base = activeSub?.path; // "/category/ha-kitchen"
|
||||
if (!base) return;
|
||||
|
||||
const slug = TARGET_SLUG[categoryValue.target] ?? "profile";
|
||||
const nextPath = `${base}/${slug}`;
|
||||
|
||||
if (pathname !== nextPath) {
|
||||
router.replace(nextPath);
|
||||
if (isTotal){
|
||||
console.log("istotal category", buildDefaultCategoryValue(PRODUCTS_FILTER_ITEM[0]));
|
||||
setCategoryValue(buildDefaultCategoryValue(PRODUCTS_FILTER_ITEM[0]))
|
||||
return;
|
||||
}
|
||||
}, [isTotal, categoryValue.target, activeSub?.path, pathname, router]);
|
||||
|
||||
if(categoryValue.product != categoryFilterItems.items[0]){
|
||||
setCategoryValue((prev) => {
|
||||
return { ...prev, product: categoryFilterItems.items[0] };
|
||||
});
|
||||
}
|
||||
|
||||
}, [isTotal, categoryFilterItems]);
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex flex-col bg-transparent">
|
||||
<LayoutFilterProvider
|
||||
value={{
|
||||
isTotal,
|
||||
totalValue,
|
||||
categoryValue,
|
||||
}}
|
||||
>
|
||||
<div className="h-screen flex flex-col bg-transparent overflow-hidden">
|
||||
<LayoutFilterProvider value={ctx} >
|
||||
<Header
|
||||
activeTopId={activeTop.id}
|
||||
onNavigate={movePage}
|
||||
@@ -121,11 +118,11 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
<SubMenu
|
||||
subMenu={subMenus}
|
||||
activeSubId={activeSub.id}
|
||||
onNavigate={isTotal? movePage : moveCategoryPage}
|
||||
onNavigate={movePage}
|
||||
/>
|
||||
<div className="flex-1 min-h-0 flex flex-col pr-4">
|
||||
<div className="bg-card lg-tab-content flex-1 min-h-0">
|
||||
<div className="shrink-0 flex items-center justify-between px-2 py-1">
|
||||
<div className="bg-card lg-tab-content flex-1 min-h-0 flex flex-col overflow-hidden">
|
||||
<div className="shrink-0 flex items-center justify-between px-2 py-1">
|
||||
<button
|
||||
type="button"
|
||||
onClick={toggleFilter}
|
||||
@@ -150,10 +147,32 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex-1 min-h-0 overflow-auto">
|
||||
<div className="p-3">
|
||||
<div className="relative flex-1 min-h-0">
|
||||
<div
|
||||
ref={scrollRef}
|
||||
onScroll={onScrollContent}
|
||||
className="h-full overflow-auto"
|
||||
>
|
||||
<div className="p-3 pb-8">
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{showScrollTop && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={scrollToTop}
|
||||
aria-label="위로 가기"
|
||||
className={cn(
|
||||
"absolute -bottom-2 right-4 z-20",
|
||||
"h-8 w-8 rounded-full",
|
||||
"bg-gray-900/80 text-white hover:bg-primary",
|
||||
"flex items-center justify-center"
|
||||
)}
|
||||
>
|
||||
<ArrowUp className="h-5 w-5" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user