260202_03_ENH: 페이지 깜빡임 최소화 및 카테고리에서 전체로 이동 시 페이지 렌더링 2회 되는 부분 수정

This commit is contained in:
2026-02-02 15:05:11 +09:00
parent 0950679874
commit 84560cef2d
9 changed files with 90 additions and 71 deletions

View File

@@ -67,6 +67,8 @@ const MainLayout = ({ children }: MainLayoutProps) => {
const [totalValue, setTotalValue] = useState<SelectedMap>(() => buildDefaultTotalValue());
const [categoryValue, setCategoryValue] = useState<CategoryFilterValue>(() => buildDefaultCategoryValue(categoryFilterItems));
const prevIsTotalRef = useRef<boolean>(isTotal);
const movePage = (path: string) => {
router.push(path);
};
@@ -86,30 +88,38 @@ const MainLayout = ({ children }: MainLayoutProps) => {
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){
console.log("istotal category", buildDefaultCategoryValue(PRODUCTS_FILTER_ITEM[0]));
setCategoryValue(buildDefaultCategoryValue(PRODUCTS_FILTER_ITEM[0]))
const prevIsTotal = prevIsTotalRef.current;
// 1) category -> total : 아무것도 안 함 (여기서 setCategoryValue 하면 1번 더 렌더됨)
if (isTotal) {
prevIsTotalRef.current = isTotal;
return;
}
if(categoryValue.product != categoryFilterItems.items[0]){
setCategoryValue((prev) => {
return { ...prev, product: categoryFilterItems.items[0] };
});
// 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 (
<div className="h-screen flex flex-col bg-transparent overflow-hidden">
<LayoutFilterProvider value={ctx} >
<LayoutFilterProvider
value={isTotal? { isTotal: true, filterValue: totalValue } : { isTotal: false, filterValue: categoryValue }}
>
<Header
activeTopId={activeTop.id}
onNavigate={movePage}

View File

@@ -1,9 +1,9 @@
import {ChartData, DataColumn, DataTable, HorizontalBar, LeftChartTable, SignStyle} from "@/components/lge-dashboard";
import {BannerKey, ServerBannerRow} from "@/types/lg";
import {formatRowLabel, isSampleRow} from "@/lib/lg-dashboard";
import {formatRowLabel, wrapSampleRow} from "@/lib/lg-dashboard";
import {useMemo} from "react";
import {BANNER_GROUP, BANNER_KEY_MAP, GRID_Y_NOT_PADDING} from "@/const/lgConst";
import {formatNumber, parseNumber} from "@/lib/utils";
import {parseNumber} from "@/lib/utils";
type SectionSpec = {
@@ -68,7 +68,6 @@ type BannerSummaryProps = {
};
const BannerSummary = ({ data, useChart }: BannerSummaryProps) => {
const buildChartData = (rows: ServerBannerRow[], bannerKeys: BannerKey[]): ChartData => {
const categories = rows
.filter((r) => r.rowLabel !== "사례수")
@@ -86,26 +85,7 @@ const BannerSummary = ({ data, useChart }: BannerSummaryProps) => {
return { categories, series };
};
const rows = useMemo<ServerBannerRow[]>(() => {
const allKeys = Array.from(
new Set(Object.values(BANNER_GROUP).flatMap((g) => g.keys))
) as string[];
return data.map((r) => {
const out: any = { ...r };
if (isSampleRow(r.rowLabel)) {
// total
out.total = `(${formatNumber(Number(r.total ?? 0))})`;
// banner values
allKeys.forEach((k) => {
const v = (r as any)[k];
if (typeof v === "number") out[k] = `(${formatNumber(v)})`;
});
}
return out as ServerBannerRow;
});
}, [data]);
const rows = useMemo<ServerBannerRow[]>(() => wrapSampleRow(data), [data]);
const signStyleGap: SignStyle<ServerBannerRow> = {
rowSelector: (row) => row.rowLabel === "gap",

View File

@@ -9,7 +9,12 @@ import BannerButtons from "@/components/lge-dashboard/BannerButtons";
import {totalFilters} from "@/config/filter.data";
import {useMemo, useState} from "react";
import {BannerGroupKey, CategoryChartMode, ServerBannerRow} from "@/types/lg";
import {buildRowLabelSeriesChartData, buildYearSeriesChartData, isBannerGroupKey} from "@/lib/lg-dashboard";
import {
buildRowLabelSeriesChartData,
buildYearSeriesChartData,
isBannerGroupKey,
wrapSampleRow
} from "@/lib/lg-dashboard";
import {BANNER_GROUP, BANNER_KEY_MAP, LAST_YEAR} from "@/const/lgConst";
import {createDefaultGrid, createDefaultLegend, reverseCategories} from "@/lib/chart-options";
@@ -84,7 +89,8 @@ const CategoryByData = ({
const v = selectedBanner.variable;
return isBannerGroupKey(v) ? buildColumns(v) : [];
}, [selectedBanner.variable]);
const tableData = useMemo(() => allTableData.filter((row) => row.year === LAST_YEAR), [allTableData, LAST_YEAR])
const tableData = useMemo(() => wrapSampleRow(allTableData).filter((row) => row.year === LAST_YEAR), [allTableData, LAST_YEAR])
return (
<div>

View File

@@ -3,13 +3,12 @@
import {useMemo} from "react";
import {useLayoutFilter} from "@/components/layout/LayoutFilterContext";
import {CategoryByData} from "@/components/lge-dashboard";
import {apiBrandShareByYear, apiPaymentMethodByYear} from "@/config/sample.data";
import {apiBrandShareByYear} from "@/config/sample.data";
import {PageData, ServerBannerRow} from "@/types/lg";
import {buildGapRow} from "@/lib/lg-dashboard";
const Brand = () => {
const { filterValue } = useLayoutFilter();
console.log(filterValue);
const tableDataArray = useMemo<PageData<ServerBannerRow>[]>(() => {
const cleaned = apiBrandShareByYear.filter(

View File

@@ -2,19 +2,15 @@
import {useLayoutFilter} from "@/components/layout/LayoutFilterContext";
import BannerSummary from "@/components/lge-dashboard/combo/BannerSummary";
import {useEffect, useState} from "react";
import {useEffect, useMemo, useState} from "react";
import {ServerBannerRow} from "@/types/lg";
import {apiYearIndex} from "@/config/sample.data";
import {apiOwnershipSummary, apiPurchaseRateKpis, apiYearIndex} from "@/config/sample.data";
const PenetrationChange = () => {
const { filterValue } = useLayoutFilter();
console.log(filterValue);
const [penetrationChangeData, setPenetrationChangeData] = useState<ServerBannerRow[]>([]);
useEffect(() => {
setPenetrationChangeData(apiYearIndex)
},[])
const penetrationChangeData = useMemo<ServerBannerRow[]>(() => apiYearIndex, []);
return(
<BannerSummary data={penetrationChangeData} useChart={true} />

View File

@@ -2,37 +2,29 @@
import {useLayoutFilter} from "@/components/layout/LayoutFilterContext";
import BannerSummary from "@/components/lge-dashboard/combo/BannerSummary";
import {useEffect, useMemo, useState} from "react";
import { useMemo } from "react";
import {ServerBannerRow} from "@/types/lg";
import {apiOwnershipSummary, apiPurchaseRateKpis} from "@/config/sample.data";
const Profile = () => {
const { isTotal, filterValue } = useLayoutFilter();
const [profileData, setProfileData] = useState<ServerBannerRow[]>([]);
const typeLabel = useMemo(() => {
if (isTotal) return null; // SelectedMap 경로
return filterValue.type.label; // CategoryFilterValue 경로로 TS가 좁혀짐
}, [isTotal, filterValue]);
const profileData = useMemo<ServerBannerRow[]>(() => {
if (isTotal) return [];
useEffect(() => {
if (!typeLabel) return;
const typeLabel = filterValue.type.label;
switch (typeLabel) {
case "보유":
setProfileData(apiOwnershipSummary);
break;
return apiOwnershipSummary;
case "구매":
setProfileData(apiPurchaseRateKpis);
break;
return apiPurchaseRateKpis;
case "향후 구매":
setProfileData(apiPurchaseRateKpis);
break;
return apiPurchaseRateKpis;
default:
setProfileData([]);
break;
return [];
}
}, [typeLabel]);
}, [isTotal, filterValue]);
return (
<BannerSummary data={profileData} useChart={false} />

View File

@@ -41,6 +41,12 @@ const signText = (
return zeroText;
};
const toCellContent = (raw: unknown): React.ReactNode => {
if (raw == null) return "";
if (typeof raw === "string" || typeof raw === "number") return raw;
return String(raw);
};
const DataTable = <T extends Record<string, unknown>>({
columns,
rows,
@@ -177,7 +183,7 @@ const DataTable = <T extends Record<string, unknown>>({
return (
<tr key={rIdx}>
{columns.map((c) => {
const raw = row[c.key];
const raw: unknown = c.key ? (row[c.key] as unknown) : undefined;
const align = c.align ?? (c.numeric ? "right" : "center");
// headRowSpan=2 컬럼은 thead 2행에서 제외되므로, tbody는 항상 렌더
@@ -205,13 +211,9 @@ const DataTable = <T extends Record<string, unknown>>({
}
// 일반 셀
const content = c.render
const content: React.ReactNode = c.render
? c.render(row)
: raw === null || raw === undefined
? ""
: typeof raw === "string" || typeof raw === "number"
? raw
: String(raw);
: toCellContent(raw);
if (c.rowHead) {
return (