260202_04_ENH: Total 하위 페이지들에 useMemo 적용 및 LifeStyle 샘플 데이터 전체 값 조정
This commit is contained in:
@@ -1,57 +1,66 @@
|
||||
'use client'
|
||||
"use client";
|
||||
|
||||
|
||||
import {PRODUCT_KEYS, PRODUCTS, YEARS} from "@/const/lgConst";
|
||||
import { useMemo } from "react";
|
||||
import { PRODUCT_KEYS, PRODUCTS, YEARS } from "@/const/lgConst";
|
||||
import {
|
||||
GroupedOverlayBar,
|
||||
GroupedOverlayBarData,
|
||||
type GroupedOverlayBarData,
|
||||
signStyleGapRow,
|
||||
TopChartTable,
|
||||
TopChartTableRow
|
||||
type TopChartTableRow,
|
||||
} from "@/components/lge-dashboard";
|
||||
import { apiOwnShareOfTotalPurchase, apiTrendBuy} from "@/config/sample.data";
|
||||
import {useLayoutFilter} from "@/components/layout/LayoutFilterContext";
|
||||
import {formatRowLabel} from "@/lib/lg-dashboard";
|
||||
import { apiOwnShareOfTotalPurchase, apiTrendBuy } from "@/config/sample.data";
|
||||
import { useLayoutFilter } from "@/components/layout/LayoutFilterContext";
|
||||
import { formatRowLabel } from "@/lib/lg-dashboard";
|
||||
|
||||
const LGPurchasePage = () => {
|
||||
const { isTotal, filterValue } = useLayoutFilter();
|
||||
console.log(filterValue);
|
||||
|
||||
const purchaseTableRows: TopChartTableRow[] = apiOwnShareOfTotalPurchase.map((row, i) => ({
|
||||
label: formatRowLabel(row.rowLabel),
|
||||
values: PRODUCT_KEYS.map((k) => (row as any)[k] ?? null),
|
||||
}));
|
||||
const purchaseTableRows = useMemo<TopChartTableRow[]>(() => {
|
||||
return apiOwnShareOfTotalPurchase.map((row) => ({
|
||||
label: formatRowLabel(row.rowLabel),
|
||||
values: PRODUCT_KEYS.map((k) => (row as any)[k] ?? null),
|
||||
}));
|
||||
}, []);
|
||||
|
||||
const total: Array<Array<number>> = YEARS.map(() => Array(PRODUCT_KEYS.length).fill(0));
|
||||
const focus: Array<Array<number>> = YEARS.map(() => Array(PRODUCT_KEYS.length).fill(0));
|
||||
const purchaseChartData = useMemo<GroupedOverlayBarData>(() => {
|
||||
const total: Array<Array<number>> = YEARS.map(() => Array(PRODUCT_KEYS.length).fill(0));
|
||||
const focus: Array<Array<number>> = YEARS.map(() => Array(PRODUCT_KEYS.length).fill(0));
|
||||
|
||||
const yearIndexMap = new Map<number, number>(YEARS.map((y, i) => [y, i]));
|
||||
const yearIndexMap = new Map<number, number>(YEARS.map((y, i) => [y, i]));
|
||||
|
||||
const totalRows = apiTrendBuy.filter((r) => YEARS.includes(r.year as any) && r.rowLabel.endsWith("년"));
|
||||
const lgRows = apiOwnShareOfTotalPurchase.filter((r) => YEARS.includes(r.year as any) && r.rowLabel.endsWith("년"));
|
||||
const totalRows = apiTrendBuy.filter(
|
||||
(r) => YEARS.includes(r.year as any) && r.rowLabel.endsWith("년")
|
||||
);
|
||||
const lgRows = apiOwnShareOfTotalPurchase.filter(
|
||||
(r) => YEARS.includes(r.year as any) && r.rowLabel.endsWith("년")
|
||||
);
|
||||
|
||||
lgRows.forEach((r, ri) => {
|
||||
const yi = yearIndexMap.get(r.year);
|
||||
if (yi == null) return;
|
||||
lgRows.forEach((r, ri) => {
|
||||
const yi = yearIndexMap.get(r.year);
|
||||
if (yi == null) return;
|
||||
|
||||
for (let ci = 0; ci < PRODUCT_KEYS.length; ci++) {
|
||||
const k = PRODUCT_KEYS[ci];
|
||||
const t = (totalRows[ri] as any)[k];
|
||||
const v = (r as any)[k];
|
||||
// total/focus 모두 같은 값(“전체 보급률 중 자사제품 비율”이므로)
|
||||
total[yi][ci] = typeof v === "number" ? t : 0;
|
||||
focus[yi][ci] = typeof v === "number" ? v : 0;
|
||||
}
|
||||
})
|
||||
for (let ci = 0; ci < PRODUCT_KEYS.length; ci++) {
|
||||
const k = PRODUCT_KEYS[ci];
|
||||
const t = (totalRows[ri] as any)[k];
|
||||
const v = (r as any)[k];
|
||||
|
||||
const purchaseChartData: GroupedOverlayBarData = {
|
||||
categories: PRODUCTS,
|
||||
years: YEARS,
|
||||
totalName: "전체",
|
||||
focusName: "LG",
|
||||
total: total,
|
||||
focus: focus,
|
||||
}
|
||||
// total은 t 기준으로 체크해야 안전
|
||||
total[yi][ci] = typeof t === "number" ? t : 0;
|
||||
focus[yi][ci] = typeof v === "number" ? v : 0;
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
categories: PRODUCTS,
|
||||
years: YEARS,
|
||||
totalName: "전체",
|
||||
focusName: "LG",
|
||||
total,
|
||||
focus,
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div>
|
||||
@@ -71,6 +80,6 @@ const LGPurchasePage = () => {
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default LGPurchasePage;
|
||||
Reference in New Issue
Block a user