260902_01_ENH: 테이블에서 학당명잘리지 않게 처리 (영문명 줄바꿈, 가장 긴 학당 명 기준 처리)

This commit is contained in:
2026-09-02 09:09:19 +09:00
parent 63fff1ff25
commit c12fd7133e
5 changed files with 78 additions and 9 deletions

View File

@@ -6,6 +6,8 @@ import { MultiSelect } from "@/components/common/multi-select";
import { ExcelDownloadButton } from "@/components/report/ExcelDownloadButton";
import { exportReportToExcel } from "@/components/report/exportReportToExcel";
import { ReportTable, type ReportLeadingColumn } from "@/components/report/ReportTable";
import { formatCenterName } from "@/components/report/formatCenterName";
import { useAutoFitCenterNameWidth } from "@/components/report/useAutoFitCenterNameWidth";
import { useAuth } from "@/hooks/useAuth";
import { useCentersQuery } from "@/query/center";
import { useProgressReportQuery } from "@/query/report";
@@ -13,7 +15,7 @@ import { isAdminRole } from "@/types/auth";
const LEADING_COLUMNS: ReportLeadingColumn[] = [
{ key: "centerCd", label: "id", width: 64, sticky: true, render: (row) => row.centerCd },
{ key: "centerName", label: "학당명", width: 200, sticky: true, render: (row) => row.centerName },
{ key: "centerName", label: "학당명", width: 200, sticky: true, render: (row) => formatCenterName(row.centerName) },
{ key: "targetCount", label: "목표 표본", width: 100, render: (row) => row.targetCount },
{ key: "completeCount", label: "완료 수", width: 100, render: (row) => row.completeCount ?? "-" },
];
@@ -39,6 +41,12 @@ export default function ProgressPage() {
return rows.filter((row) => row.centerCd === 0 || selected.has(row.centerCd));
}, [reportQuery.data, admin, selectedCenterCds]);
const centerNameWidth = useAutoFitCenterNameWidth(content);
const leadingColumns = useMemo(
() => LEADING_COLUMNS.map((column) => column.key === "centerName" ? { ...column, width: centerNameWidth } : column),
[centerNameWidth],
);
const handleDownload = () => {
if (!reportQuery.data) return;
exportReportToExcel({
@@ -47,7 +55,7 @@ export default function ProgressPage() {
headers: reportQuery.data.headers,
content,
scale: "current",
leadingColumns: LEADING_COLUMNS,
leadingColumns,
});
};
@@ -71,7 +79,7 @@ export default function ProgressPage() {
headers={reportQuery.data?.headers ?? []}
content={content}
scale="current"
leadingColumns={LEADING_COLUMNS}
leadingColumns={leadingColumns}
mergeLeadingColumnsWhen={(row) => row.centerCd === 0}
/>
)}

View File

@@ -6,13 +6,15 @@ import { MultiSelect } from "@/components/common/multi-select";
import { ExcelDownloadButton } from "@/components/report/ExcelDownloadButton";
import { exportReportToExcel } from "@/components/report/exportReportToExcel";
import { ReportTable, type ReportLeadingColumn } from "@/components/report/ReportTable";
import { formatCenterName } from "@/components/report/formatCenterName";
import { useAutoFitCenterNameWidth } from "@/components/report/useAutoFitCenterNameWidth";
import { useAuth } from "@/hooks/useAuth";
import { useCentersQuery } from "@/query/center";
import { useFinalReportQuery } from "@/query/report";
import { isAdminRole } from "@/types/auth";
const LEADING_COLUMNS: ReportLeadingColumn[] = [
{ key: "centerName", label: "학당명", width: 200, sticky: true, render: (row) => row.centerName },
{ key: "centerName", label: "학당명", width: 200, sticky: true, render: (row) => formatCenterName(row.centerName) },
{ key: "completeCount", label: "완료 수", width: 100, render: (row) => row.completeCount ?? 0 },
];
@@ -37,6 +39,12 @@ export default function ReportPage() {
return rows.filter((row) => row.centerCd === 0 || selected.has(row.centerCd));
}, [reportQuery.data, admin, selectedCenterCds]);
const centerNameWidth = useAutoFitCenterNameWidth(content);
const leadingColumns = useMemo(
() => LEADING_COLUMNS.map((column) => column.key === "centerName" ? { ...column, width: centerNameWidth } : column),
[centerNameWidth],
);
const handleDownload = () => {
if (!reportQuery.data) return;
exportReportToExcel({
@@ -45,7 +53,7 @@ export default function ReportPage() {
headers: reportQuery.data.headers,
content,
scale: "final",
leadingColumns: LEADING_COLUMNS,
leadingColumns,
});
};
@@ -65,7 +73,7 @@ export default function ReportPage() {
{reportQuery.isLoading ? (
<p className="text-sm text-muted-foreground">불러오는 중...</p>
) : (
<ReportTable headers={reportQuery.data?.headers ?? []} content={content} scale="final" leadingColumns={LEADING_COLUMNS} />
<ReportTable headers={reportQuery.data?.headers ?? []} content={content} scale="final" leadingColumns={leadingColumns} />
)}
</div>
);

View File

@@ -28,6 +28,7 @@ export interface ReportTableProps {
}
const BODY_CELL_TEXT_CLASS = "truncate";
const CENTER_NAME_CELL_CLASS = "whitespace-pre-line break-keep";
// value7은 소수점 2자리(예: "7.00"), value100은 소수점 1자리(예: "100.0")로 반올림 표기한다.
// null은 항상 "-"로 표시하고, 0은 zeroAsDash 옵션에 따라 "-" 또는 반올림된 값("0.00"/"0.0")으로 표시한다.
@@ -166,18 +167,18 @@ export function ReportTable({
return (
<TableRow key={row.centerCd}>
{merged ? (
<TableCell colSpan={stickyCount} style={{ position: "sticky", left: 0, zIndex: 10 }} className={cn("bg-background text-center font-medium", BODY_CELL_TEXT_CLASS)}>
<TableCell colSpan={stickyCount} style={{ position: "sticky", left: 0, zIndex: 10 }} className={cn("bg-background text-center font-medium", leadingColumns[stickyCount - 1].key === "centerName" ? CENTER_NAME_CELL_CLASS : BODY_CELL_TEXT_CLASS)}>
{leadingColumns[stickyCount - 1].render(row)}
</TableCell>
) : (
leadingColumns.slice(0, stickyCount).map((column, columnIndex) => (
<TableCell key={column.key} style={{ position: "sticky", left: leftOffsets[columnIndex], zIndex: 10 }} className={cn("bg-background text-center", BODY_CELL_TEXT_CLASS)}>
<TableCell key={column.key} style={{ position: "sticky", left: leftOffsets[columnIndex], zIndex: 10 }} className={cn("bg-background text-center", column.key === "centerName" ? CENTER_NAME_CELL_CLASS : BODY_CELL_TEXT_CLASS)}>
{column.render(row)}
</TableCell>
))
)}
{leadingColumns.slice(stickyCount).map((column) => (
<TableCell key={column.key} className={cn("text-center", BODY_CELL_TEXT_CLASS)}>
<TableCell key={column.key} className={cn("text-center", column.key === "centerName" ? CENTER_NAME_CELL_CLASS : BODY_CELL_TEXT_CLASS)}>
{column.render(row)}
</TableCell>
))}

View File

@@ -0,0 +1,3 @@
export function formatCenterName(centerName: string): string {
return centerName.replace(/^(.+?[가-힣][가-힣0-9]*)\s+(?=[A-Za-z])/, "$1\n");
}

View File

@@ -0,0 +1,49 @@
import { useLayoutEffect, useState } from "react";
import type { ReportContent } from "@/types/report";
import { formatCenterName } from "./formatCenterName";
const DEFAULT_WIDTH = 200;
const MIN_WIDTH = 200;
const MAX_WIDTH = 400;
const CELL_HORIZONTAL_PADDING = 10;
const measureCenterNameWidth = (names: string[]): number => {
if (typeof document === "undefined") return DEFAULT_WIDTH;
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
if (!context) return DEFAULT_WIDTH;
const style = window.getComputedStyle(document.body);
context.font = [style.fontStyle, style.fontVariant, style.fontWeight, style.fontSize, style.fontFamily]
.filter(Boolean)
.join(" ");
const maxTextWidth = Math.max(
context.measureText("학당명").width,
...names.flatMap((name) => formatCenterName(name).split("\n")).map((line) => context.measureText(line).width),
);
return Math.min(MAX_WIDTH, Math.max(MIN_WIDTH, Math.ceil(maxTextWidth + CELL_HORIZONTAL_PADDING)));
};
export function useAutoFitCenterNameWidth(content: ReportContent[]): number {
const [width, setWidth] = useState(DEFAULT_WIDTH);
useLayoutEffect(() => {
let cancelled = false;
const measure = () => {
if (cancelled) return;
setWidth(measureCenterNameWidth(content.map((row) => row.centerName)));
};
measure();
document.fonts?.ready.then(measure);
return () => {
cancelled = true;
};
}, [content]);
return width;
}