Skip to content

Commit 22db278

Browse files
catomeanclaude
andcommitted
refactor: consolidate formatFileSize duplicates to shared formatBytes
- Update app/documents/page.tsx to use formatBytes from lib/format - Update app/my-data/page.tsx to use formatBytes from lib/format - Update components/shared/demo/DemoFileUpload.tsx to use formatBytes - Update workspaceUtils.ts to re-export formatBytes as formatFileSize DRY: 4 duplicate formatFileSize implementations → 1 SSOT (lib/format.ts) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent f195154 commit 22db278

5 files changed

Lines changed: 10 additions & 32 deletions

File tree

app/bots/legal-expert/components/demo/workspace/workspaceUtils.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import { type UploadedFile } from '../types';
2+
import { formatBytes } from '@/lib/format';
23

3-
/**
4-
* Format file size in human-readable format
5-
*/
6-
export function formatFileSize(bytes: number): string {
7-
if (bytes === 0) return '0 Bytes';
8-
const k = 1024;
9-
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
10-
const i = Math.floor(Math.log(bytes) / Math.log(k));
11-
return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + ' ' + sizes[i];
12-
}
4+
// Re-export for backwards compatibility
5+
export const formatFileSize = formatBytes;
136

147
/**
158
* Calculate and format total size of files

app/documents/page.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { useState, useEffect, useRef, useCallback, type FormEvent, type ChangeEvent } from 'react';
44
import Link from 'next/link';
55
import { useAuth } from '@/lib/auth';
6+
import { formatBytes } from '@/lib/format';
67
import { documentToasts, conversationToasts } from '@/lib/toast';
78
import { PageLoading, InlineLoading, LoadingSpinner } from '@/components/shared/LoadingSpinner';
89
import { DocumentStatusBadge } from '@/components/shared/DocumentStatusBadge';
@@ -323,12 +324,6 @@ export default function DocumentsPage() {
323324
setAddToBotDocument(null);
324325
};
325326

326-
const formatFileSize = (bytes: number) => {
327-
if (bytes < 1024) return `${bytes} B`;
328-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
329-
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
330-
};
331-
332327
if (authLoading) {
333328
return <PageLoading />;
334329
}
@@ -479,7 +474,7 @@ export default function DocumentsPage() {
479474
<DocumentStatusBadge status={doc.status} />
480475
</div>
481476
<p className="text-sm text-gray-500 mt-1">
482-
{formatFileSize(doc.size_bytes || 0)}
477+
{formatBytes(doc.size_bytes || 0)}
483478
{doc.chunk_count ? ` • ${doc.chunk_count} chunks` : ''}
484479
</p>
485480
{doc.error_message && (

app/my-data/page.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { useState, useEffect, useRef, type FormEvent, type ChangeEvent } from 'react';
44
import Link from 'next/link';
55
import { useRequireAuth } from '@/lib/auth';
6+
import { formatBytes } from '@/lib/format';
67
import { PageLoading, InlineLoading, LoadingSpinner } from '@/components/shared/LoadingSpinner';
78
import { DocumentStatusBadge } from '@/components/shared/DocumentStatusBadge';
89
import { DOCUMENT_STATUS } from '@/lib/constants';
@@ -207,12 +208,6 @@ export default function MyDataPage() {
207208
window.location.href = '/';
208209
};
209210

210-
const formatFileSize = (bytes: number) => {
211-
if (bytes < 1024) return `${bytes} B`;
212-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
213-
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
214-
};
215-
216211
if (authLoading || !user) {
217212
return <PageLoading />;
218213
}
@@ -369,7 +364,7 @@ export default function MyDataPage() {
369364
<DocumentStatusBadge status={doc.status} />
370365
</div>
371366
<p className="text-sm text-gray-500 mt-1">
372-
{formatFileSize(doc.size_bytes || 0)}
367+
{formatBytes(doc.size_bytes || 0)}
373368
{doc.chunk_count ? ` • ${doc.chunk_count} chunks` : ''}
374369
</p>
375370
{doc.error_message && (

components/shared/demo/DemoFileUpload.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { useState, useRef, type FC, type DragEvent, type ChangeEvent } from 'react';
44
import type { FileCategory, UploadedFile } from '@/lib/demo/types';
55
import type { BotAccentColor } from '@/types/bot';
6+
import { formatBytes } from '@/lib/format';
67

78
interface DemoFileUploadProps {
89
files: UploadedFile[];
@@ -21,12 +22,6 @@ const accentColorClasses: Record<BotAccentColor, { border: string; bg: string; t
2122
amber: { border: 'border-amber-300', bg: 'bg-amber-50', text: 'text-amber-600' },
2223
};
2324

24-
function formatFileSize(bytes: number): string {
25-
if (bytes < 1024) return `${bytes} B`;
26-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
27-
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
28-
}
29-
3025
function getStatusIcon(status: UploadedFile['status']) {
3126
switch (status) {
3227
case 'uploading':
@@ -222,7 +217,7 @@ export const DemoFileUpload: FC<DemoFileUploadProps> = ({
222217
<div className="min-w-0">
223218
<p className="text-sm font-medium text-gray-900 truncate">{file.name}</p>
224219
<p className="text-xs text-gray-500">
225-
{formatFileSize(file.size)} &bull; {getStatusText(file.status)}
220+
{formatBytes(file.size)} &bull; {getStatusText(file.status)}
226221
{file.errorMessage && (
227222
<span className="text-red-500 ml-1">- {file.errorMessage}</span>
228223
)}

tsconfig.tsbuildinfo

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)