"use client"; 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 "@/const/menu.const"; import {categoryTarget, totalFilters} from "@/const/filter.const"; import {CategoryTargetData, CategoryTargetType} from "@/types/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 {LAST_YEAR} from "@/const/lg-common.const"; import {PRODUCTS_FILTER_ITEM} from "@/const/lg-product.const"; import {ProductsFilterItem} from "@/types/lg.types"; import {ArrowUp, Filter} from "lucide-react"; import {Button} from "@/components/ui/button"; type MainLayoutProps = { children: ReactNode; }; const buildDefaultTotalValue = (): SelectedMap => { const init: SelectedMap = {}; totalFilters.forEach((f) => { // "전체"가 있으면 그 value, 없으면 첫 번째 값 const all = f.children.find((c) => c.label === "전체"); init[f.variable] = all?.value ?? f.children[0]?.value ?? ""; }); return init; }; const buildDefaultCategoryValue = (categoryFilterItems: ProductsFilterItem): CategoryFilterValue => { const initProduct = categoryFilterItems.items[0]; const initType = {label:"보유", value:CategoryTargetType.PENETRATION} const firstTarget = categoryTarget.find((x) => x.type.includes(initType.value))?.children[0]; const initTarget = firstTarget ?? { value: CategoryTargetData.PROFILE, label: "Profile"}; return { year: LAST_YEAR, product: initProduct, type: initType, target: initTarget, }; }; const buildTotalPartDescription = (pathname:string) => { switch(pathname) { case "/total/penetration": return "■ 보급률: 전체 소비자 중, 조사 년도에 해당 제품을 보유하고 있는 비율"; case "/total/lg-penetration": return "■ 보급률: 전체 소비자 중, 조사 년도에 LG 제품을 보유하고 있는 비율"; case "/total/purchase": return "■ 구매율: 전체 소비자 중, 조사 년도에 해당 제품을 구매한 비율"; case "/total/lg-purchase": return "■ 구매율: 전체 소비자 중, 조사 년도에 LG 제품을 구매한 비율"; default: return ""; } } const MainLayout = ({ children }: MainLayoutProps) => { const router = useRouter(); const pathname = usePathname(); const scrollRef = useRef(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; const activeSub = subMenus.find((m) => pathname.startsWith(m.path)) ?? subMenus[0]; const [isTotalFilterOpen, setIsTotalFilterOpen] = useState(true); const [isCategoryFilterOpen, setIsCategoryFilterOpen] = useState(true); const isFilterOpen = isTotal ? isTotalFilterOpen : isCategoryFilterOpen; const categoryFilterItems = PRODUCTS_FILTER_ITEM.find((m) => pathname.match?.(m.path)) ?? PRODUCTS_FILTER_ITEM[0]; const totalPartDescription = buildTotalPartDescription(pathname); const [totalValue, setTotalValue] = useState(() => buildDefaultTotalValue()); const [categoryValue, setCategoryValue] = useState(() => buildDefaultCategoryValue(categoryFilterItems)); const prevIsTotalRef = useRef(isTotal); const movePage = (path: string) => { router.push(path); }; const toggleFilter = () => { if (isTotal) setIsTotalFilterOpen((v) => !v); else setIsCategoryFilterOpen((v) => !v); }; const onScrollContent = () => { const el = scrollRef.current; if (!el) return; setShowScrollTop(el.scrollTop > 120); }; const scrollToTop = () => { scrollRef.current?.scrollTo({ top: 0, behavior: "smooth" }); }; const isDefaultTotalFilter = useMemo(() => { if(!isTotal) return false; return Object.values(totalValue).every(value => value === "0"); },[isTotal, totalValue]) const setDefaletTotalFilter = () => { if(isTotal && !isDefaultTotalFilter){ setTotalValue(prev => Object.fromEntries( Object.keys(prev).map(key => [key, "0"]) ) ); } } useEffect(() => { const prevIsTotal = prevIsTotalRef.current; // 1) category -> total : 아무것도 안 함 (여기서 setCategoryValue 하면 1번 더 렌더됨) if (isTotal) { prevIsTotalRef.current = isTotal; return; } // 2) total -> category : 들어오는 순간에만 초기화 if (prevIsTotal) { setCategoryValue(buildDefaultCategoryValue(categoryFilterItems)); prevIsTotalRef.current = isTotal; return; } // 3) category 내부 이동(카테고리 path 바뀜 등) : product만 동기화 const nextProduct = categoryFilterItems.items[0]; setCategoryValue((prev) => { if (prev.product === nextProduct) return prev; return { ...prev, product: nextProduct }; }); prevIsTotalRef.current = isTotal; }, [isTotal, categoryFilterItems]); return (
{isTotal && ( <>
)}
{isFilterOpen && (
{isTotal ? ( ) : ( )}
)}
{children}
{showScrollTop && ( )}
); } export default MainLayout;