Skip to content

Commit fb081fa

Browse files
Copilothuangyiirene
andcommitted
Address code review feedback - improve error handling and string formatting
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent fcb4a74 commit fb081fa

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

packages/plugin-object/src/ObjectForm.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ export const ObjectForm: React.FC<ObjectFormProps> = ({
149149
formField.accept = field.accept?.join(',');
150150
// Add validation hints for file size and dimensions
151151
if (field.max_size) {
152-
formField.description = (formField.description || '') + ` (Max size: ${formatFileSize(field.max_size)})`;
152+
const sizeHint = `Max size: ${formatFileSize(field.max_size)}`;
153+
formField.description = formField.description
154+
? `${formField.description} (${sizeHint})`
155+
: sizeHint;
153156
}
154157
}
155158

@@ -347,10 +350,18 @@ function mapFieldTypeToFormType(fieldType: string): string {
347350

348351
/**
349352
* Formats file size in bytes to human-readable string
350-
* @param bytes - File size in bytes
353+
* @param bytes - File size in bytes (must be non-negative)
351354
* @returns Formatted string (e.g., "5 MB", "1.5 GB")
352355
*/
353356
function formatFileSize(bytes: number): string {
357+
if (bytes < 0 || !Number.isFinite(bytes)) {
358+
return '0 B';
359+
}
360+
361+
if (bytes === 0) {
362+
return '0 B';
363+
}
364+
354365
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
355366
let size = bytes;
356367
let unitIndex = 0;

packages/plugin-object/src/ObjectTable.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,18 @@ export const ObjectTable: React.FC<ObjectTableProps> = ({
108108
column.cell = (value: any) => {
109109
if (!value) return '-';
110110
if (Array.isArray(value)) {
111-
return `${value.length} ${field.type}(s)`;
111+
const count = value.length;
112+
const fileType = field.type === 'image' ? 'image' : 'file';
113+
return count === 1 ? `1 ${fileType}` : `${count} ${fileType}s`;
112114
}
113115
return value.name || value.original_name || 'File';
114116
};
115117
} else if (field.type === 'lookup' || field.type === 'master_detail') {
116118
// For relationship fields, display the name property if available
117119
column.cell = (value: any) => {
118120
if (!value) return '-';
119-
if (typeof value === 'object') {
120-
return value.name || value.label || value._id || String(value);
121+
if (typeof value === 'object' && value !== null) {
122+
return value.name || value.label || value._id || JSON.stringify(value);
121123
}
122124
return String(value);
123125
};

0 commit comments

Comments
 (0)