"use client"; import { useEffect, useMemo, useState } from "react"; import TopChartTable from "@/components/lge-dashboard/combo/TopChartTable"; import VerticalBar from "@/components/lge-dashboard/chart/VerticalBar"; import { type ChartData, GroupedStackBar, signStyleGapRow, type TopChartTableRow, type GroupedStackBarData, BottomDescription, } from "@/components/lge-dashboard"; import { useLayoutFilter } from "@/components/layout/LayoutFilterContext"; import { Button } from "@/components/ui/button"; import { BRAND_LIST, PRODUCT_KEYS, PRODUCTS, YEARS, AXIS_HIDE_STYLE, GRID_X_NOT_PADDING, PRODUCT_KEY_MAP, SITE_TITLE, USE_CACHE } from "@/const/lgConst"; import { calcCagrRow, calcGapProductRow, formatRowLabel, normalizeFilterCondition, withCagrRow, withGapRow } from "@/lib/lg-dashboard"; import {buildHeader_ProductKeyMap, exportDashboardExcel, Section} from "@/lib/lg-export-excel"; import {StatisticsRequest} from "@/types/lg"; import {useProductStatisticsQuery} from "@/query/statistics"; import {useDebouncedValue} from "@/hooks/useDebouncedValue"; import {toNumOr0} from "@/lib/utils"; import {SelectedMap} from "@/types/common"; const PenetrationPage = () => { const { filterValue, registerExcelExporter } = useLayoutFilter(); const debouncedFilterValue = useDebouncedValue(filterValue as SelectedMap, 500); const [isBrandShare, setIsBrandShare] = useState(false); const chartColorByYear = Object.fromEntries( YEARS.map((year, i) => [ year, [`--chart-cbrand-${i + 1}`,`--chart-samsung-${i + 1}`,`--chart-lg-${i + 1}`], ]) ) as Record<(typeof YEARS)[number], string[]>; const req = useMemo(() => { return { variableIdMap: { penetration: "KQA1", penetration_brand: "KQA3_3_NET" }, bannerIdList: ["product"], filterCondition: normalizeFilterCondition(debouncedFilterValue), option: { useCache: USE_CACHE, lastYear: false, decimalPoint: 0 }, }; }, [debouncedFilterValue]); const { data, isError, error } = useProductStatisticsQuery(req); if(isError){ console.error(error); } const penetrationRows = data?.statisticsDataMap?.penetration .filter((r) => r.rowLabel == `보유`) .map((row) => { return { ...row, rowLabel: row.year, } }) ?? []; const penetrationBrandRows = data?.statisticsDataMap?.penetration_brand .filter((row) => BRAND_LIST.includes(row.rowLabel)) ?? []; const penetrationRowsWithGapCagr = useMemo(() => { const withGap = withGapRow(penetrationRows, calcGapProductRow); return withCagrRow(withGap, calcCagrRow); }, [penetrationRows]); const productTableRows = useMemo(() => { return penetrationRowsWithGapCagr.map((row) => ({ label: formatRowLabel(row.rowLabel), total: row.total, values: PRODUCT_KEYS.map((k) => (row as any)[k] ?? null), })) }, [penetrationRowsWithGapCagr]); const productChartData = useMemo(() => { return { categories: [...PRODUCTS], series: penetrationRows .map((row) => ({ name: row.year, data: PRODUCT_KEYS.map((k) => toNumOr0((row as any)[k])), })), }; }, [penetrationRows]); const brandShareChartData = useMemo(() => { const rowMap = new Map(); for (const r of penetrationBrandRows) { rowMap.set(`${r.year}__${r.rowLabel}`, r); } const values: number[][][] = YEARS.map(() => BRAND_LIST.map(() => new Array(PRODUCTS.length).fill(0)) ); YEARS.forEach((year, yi) => { BRAND_LIST.forEach((brand, bi) => { PRODUCT_KEYS.forEach((k, ci) => { const row = rowMap.get(`${year}__${brand}`); values[yi][bi][ci] = (row as any)?.[k]; }); }); }); return { categories: PRODUCTS, years: YEARS, brands: BRAND_LIST, values }; }, [penetrationBrandRows]); const description = useMemo(() => { if(isBrandShare){ return "Q. 보유하고 계신 (제품)의 브랜드는 어떻게 되십니까?"; } return "Q. 다음 중 귀댁에서 보유하고 계신 가전/전자 제품은 무엇입니까? 모두 선택해주세요.
" + "* 구독/렌탈/선물/경품/아파트 내 빌트인 제품을 모두 포함하여 선택해주세요."; }, [isBrandShare]); useEffect(() => { registerExcelExporter(async () => { const excelHeader = buildHeader_ProductKeyMap(PRODUCT_KEY_MAP); type AnyRow = Record; const sections: Section[] = [ { title: "전체 보급률", header: excelHeader, rows: penetrationRowsWithGapCagr, rowGap: 1 }, ...YEARS.map((year) => ( { title: `브랜드 별 점유율_${year}년`, header: excelHeader, rows: penetrationBrandRows.filter((row) => row.year === year), rowGap: 1, } )) ]; await exportDashboardExcel({ fileName: `${SITE_TITLE}(보급률).xlsx`, sheetName: "전체 보급률", sections: sections, }); }); return () => registerExcelExporter(null); }, [registerExcelExporter, penetrationRows, penetrationBrandRows]); return (
) : ( ) } signStyle={signStyleGapRow} fillCAGR={true} />
); }; export default PenetrationPage;