fix(desktop): guard cell detail previews for large values

This commit is contained in:
t8y2 committed 2026-06-01 19:49:53 +08:00
1 parent 41e97baa6e
commit b9fa36763e
9 files changed
+80 -11

No files matched your search

@@ -1,6 +1,8 @@
export type CellDetailTab = "details" | "valueEditor";
export type ValueEditorAction = "formatJson" | "setNull" | "restoreOriginal";
export const CELL_DETAIL_JSON_FORMAT_MAX_LENGTH = 100_000;
export interface CellDetailPresentationOptions {
isEditable: boolean;
}
@@ -66,6 +68,7 @@ export function isJsonColumnType(columnType: string | undefined): boolean {
export function canFormatCellDetailJson(value: unknown, columnType?: string): boolean {
if (value === null || value === undefined) return false;
const text = cellDetailRawEditorText(value);
if (text.length > CELL_DETAIL_JSON_FORMAT_MAX_LENGTH) return false;
if (isJsonColumnType(columnType)) return !!formatJsonText(text);
return typeof value === "string" && looksLikeJsonContainer(text) && !!formatJsonText(text);
}
@@ -73,6 +76,7 @@ export function canFormatCellDetailJson(value: unknown, columnType?: string): bo
export function formatJsonText(text: string): string | undefined {
const trimmed = text.trim();
if (!trimmed) return undefined;
if (trimmed.length > CELL_DETAIL_JSON_FORMAT_MAX_LENGTH) return undefined;
try {
return JSON.stringify(JSON.parse(trimmed), null, 2);
} catch {
+19 -2
View File
@@ -2,6 +2,8 @@ import { cellImagePreviewUrl } from "@/lib/cellImageUrl";
import { displayCellValue, type CellValue } from "@/lib/cellValue";
import { formatJsonText } from "@/lib/cellDetailPresentation";
export const CELL_DETAIL_VALUE_PREVIEW_MAX_LENGTH = 12_000;
export interface DataGridCellDetail {
rowNumber: number;
rowId: number;
@@ -11,7 +13,10 @@ export interface DataGridCellDetail {
comment: string;
value: CellValue;
rawValue: string;
rawValuePreview: string;
displayValue: string;
displayValuePreview: string;
isValuePreviewTruncated: boolean;
imagePreviewUrl: string | null;
length: number;
formattedJson: string;
@@ -78,7 +83,10 @@ export function buildDataGridCellDetail(options: BuildDataGridCellDetailOptions)
const value = options.row[options.columnIndex] ?? null;
const rawValue = displayCellValue(value);
const displayValue = options.displayValue(value, options.columnIndex);
const formattedJson = typeof value === "string" && looksLikeJsonContainer(value) ? (formatJsonText(value) ?? "") : "";
const rawValuePreview = previewText(rawValue);
const displayValuePreview = previewText(displayValue);
return {
rowNumber: options.rowIndex + 1,
@@ -89,8 +97,12 @@ export function buildDataGridCellDetail(options: BuildDataGridCellDetailOptions)
comment: options.commentByColumn?.get(column) ?? "",
value,
rawValue,
displayValue: options.displayValue(value, options.columnIndex),
imagePreviewUrl: cellImagePreviewUrl(value),
rawValuePreview,
displayValue,
displayValuePreview,
isValuePreviewTruncated:
rawValuePreview.length < rawValue.length || displayValuePreview.length < displayValue.length,
imagePreviewUrl: rawValue.length <= CELL_DETAIL_VALUE_PREVIEW_MAX_LENGTH ? cellImagePreviewUrl(value) : null,
length: value === null ? 0 : String(value).length,
formattedJson,
isEditable: options.isEditable,
@@ -181,3 +193,8 @@ function looksLikeJsonContainer(text: string): boolean {
const trimmed = text.trim();
return trimmed.startsWith("{") || trimmed.startsWith("[");
}
function previewText(text: string): string {
if (text.length <= CELL_DETAIL_VALUE_PREVIEW_MAX_LENGTH) return text;
return text.slice(0, CELL_DETAIL_VALUE_PREVIEW_MAX_LENGTH);
}