mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-10-18 11:56:01 +02:00
Refactors date and file formatting utilities
refactor(utils): consolidate date and file size formatting Extracts formatting logic into dedicated utility functions to improve code maintainability and consistency across components. Changes: - Consolidate date formatting functions (formatRelativeTime, etc.) - Consolidate file size formatting into single formatFileSize utility - Fix file size calculation errors - Update UI components to use new utilities - Change "ASAP" to "Waiting" for unscheduled queue items Tests: - Add comprehensive unit tests for date utilities - Add unit tests for file size formatting
This commit is contained in:
@@ -29,6 +29,7 @@ import { ModelViewer } from './ModelViewer';
|
||||
import { GcodeViewer } from './GcodeViewer';
|
||||
import type { PlateMetadata } from '../types/plates';
|
||||
import { useToast } from '../contexts/ToastContext';
|
||||
import { formatFileSize } from '../utils/file';
|
||||
|
||||
interface FileManagerModalProps {
|
||||
printerId: number;
|
||||
@@ -235,24 +236,6 @@ function PrinterFileViewerModal({ printerId, filePath, filename, onClose }: Prin
|
||||
);
|
||||
}
|
||||
|
||||
function formatFileSize(bytes: number): string {
|
||||
if (bytes === 0) return '0 B';
|
||||
const k = 1024;
|
||||
const sizes = ['B', 'KB', 'MB', 'GB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(1))} ${sizes[i]}`;
|
||||
}
|
||||
|
||||
function formatStorageSize(bytes: number): string {
|
||||
if (bytes === 0) return '0 GB';
|
||||
const gb = bytes / (1024 * 1024 * 1024);
|
||||
if (gb >= 1) {
|
||||
return `${gb.toFixed(1)} GB`;
|
||||
}
|
||||
const mb = bytes / (1024 * 1024);
|
||||
return `${mb.toFixed(0)} MB`;
|
||||
}
|
||||
|
||||
function getFileIcon(filename: string, isDirectory: boolean) {
|
||||
if (isDirectory) return Folder;
|
||||
|
||||
@@ -444,13 +427,13 @@ export function FileManagerModal({ printerId, printerName, onClose }: FileManage
|
||||
{storageData && (storageData.used_bytes != null || storageData.free_bytes != null) && (
|
||||
<div className="text-sm text-bambu-gray flex items-center gap-2">
|
||||
{storageData.used_bytes != null && (
|
||||
<span>{t('printerFiles.storageUsed')} {formatStorageSize(storageData.used_bytes)}</span>
|
||||
<span>{t('printerFiles.storageUsed')} {formatFileSize(storageData.used_bytes)}</span>
|
||||
)}
|
||||
{storageData.used_bytes != null && storageData.free_bytes != null && (
|
||||
<span className="text-bambu-dark-tertiary">|</span>
|
||||
)}
|
||||
{storageData.free_bytes != null && (
|
||||
<span>{t('printerFiles.storageFree')} {formatStorageSize(storageData.free_bytes)}</span>
|
||||
<span>{t('printerFiles.storageFree')} {formatFileSize(storageData.free_bytes)}</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -35,6 +35,7 @@ import { Button } from './Button';
|
||||
import { Toggle } from './Toggle';
|
||||
import { ConfirmModal } from './ConfirmModal';
|
||||
import { useToast } from '../contexts/ToastContext';
|
||||
import { formatRelativeTime } from '../utils/date';
|
||||
|
||||
interface StatusBadgeProps {
|
||||
status: string | null;
|
||||
@@ -71,29 +72,6 @@ function formatDateTime(dateStr: string | null): string {
|
||||
return date.toLocaleString();
|
||||
}
|
||||
|
||||
function formatRelativeTime(dateStr: string | null): string {
|
||||
if (!dateStr) return '-';
|
||||
const date = new Date(dateStr);
|
||||
const now = new Date();
|
||||
const diffMs = date.getTime() - now.getTime();
|
||||
const diffMins = Math.round(diffMs / 60000);
|
||||
|
||||
if (diffMins < 0) {
|
||||
const absMins = Math.abs(diffMins);
|
||||
if (absMins < 60) return `${absMins}m ago`;
|
||||
const hours = Math.floor(absMins / 60);
|
||||
if (hours < 24) return `${hours}h ago`;
|
||||
const days = Math.floor(hours / 24);
|
||||
return `${days}d ago`;
|
||||
} else {
|
||||
if (diffMins < 60) return `in ${diffMins}m`;
|
||||
const hours = Math.floor(diffMins / 60);
|
||||
if (hours < 24) return `in ${hours}h`;
|
||||
const days = Math.floor(hours / 24);
|
||||
return `in ${days}d`;
|
||||
}
|
||||
}
|
||||
|
||||
export function GitHubBackupSettings() {
|
||||
const queryClient = useQueryClient();
|
||||
const { showToast } = useToast();
|
||||
@@ -563,7 +541,7 @@ export function GitHubBackupSettings() {
|
||||
<div className="flex items-center gap-2 text-bambu-gray">
|
||||
{status.last_backup_at ? (
|
||||
<>
|
||||
<span>Last backup: {formatRelativeTime(status.last_backup_at)}</span>
|
||||
<span>Last backup: {formatRelativeTime(status.last_backup_at, 'system', t)}</span>
|
||||
<StatusBadge status={status.last_backup_status} />
|
||||
</>
|
||||
) : (
|
||||
@@ -573,7 +551,7 @@ export function GitHubBackupSettings() {
|
||||
{status.next_scheduled_run && (
|
||||
<span className="text-bambu-gray">
|
||||
<Clock className="w-3 h-3 inline mr-1" />
|
||||
Next: {formatRelativeTime(status.next_scheduled_run)}
|
||||
Next: {formatRelativeTime(status.next_scheduled_run, 'system', t)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -8,12 +8,7 @@ import { Card, CardContent, CardHeader } from './Card';
|
||||
import { Button } from './Button';
|
||||
import { useToast } from '../contexts/ToastContext';
|
||||
import { ConfirmModal } from './ConfirmModal';
|
||||
|
||||
function formatFileSize(bytes: number): string {
|
||||
if (bytes < 1024) return `${bytes} B`;
|
||||
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
||||
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
||||
}
|
||||
import { formatFileSize } from '../utils/file';
|
||||
|
||||
function formatTimeAgo(dateStr: string): string {
|
||||
const date = new Date(dateStr);
|
||||
|
||||
@@ -5,27 +5,13 @@ import { useTranslation } from 'react-i18next';
|
||||
import { api } from '../api/client';
|
||||
import { useAuth } from '../contexts/AuthContext';
|
||||
import { useToast } from '../contexts/ToastContext';
|
||||
import { parseUTCDate } from '../utils/date';
|
||||
import { formatRelativeTime } from '../utils/date';
|
||||
|
||||
interface PrinterQueueWidgetProps {
|
||||
printerId: number;
|
||||
printerState?: string | null;
|
||||
}
|
||||
|
||||
function formatRelativeTime(dateString: string | null): string {
|
||||
if (!dateString) return 'ASAP';
|
||||
const date = parseUTCDate(dateString);
|
||||
if (!date) return 'ASAP';
|
||||
const now = new Date();
|
||||
const diff = date.getTime() - now.getTime();
|
||||
|
||||
if (diff < 0) return 'Now';
|
||||
if (diff < 60000) return 'In <1 min';
|
||||
if (diff < 3600000) return `In ${Math.round(diff / 60000)} min`;
|
||||
if (diff < 86400000) return `In ${Math.round(diff / 3600000)}h`;
|
||||
return date.toLocaleDateString();
|
||||
}
|
||||
|
||||
export function PrinterQueueWidget({ printerId, printerState }: PrinterQueueWidgetProps) {
|
||||
const { t } = useTranslation();
|
||||
const queryClient = useQueryClient();
|
||||
@@ -116,7 +102,7 @@ export function PrinterQueueWidget({ printerId, printerState }: PrinterQueueWidg
|
||||
<div className="flex items-center gap-2 flex-shrink-0">
|
||||
<span className="text-xs text-bambu-gray flex items-center gap-1">
|
||||
<Clock className="w-3 h-3" />
|
||||
{formatRelativeTime(nextItem?.scheduled_time || null)}
|
||||
{nextItem?.scheduled_time ? formatRelativeTime(nextItem.scheduled_time, 'system', t) : t('time.waiting')}
|
||||
</span>
|
||||
{totalPending > 1 && (
|
||||
<span className="text-xs px-1.5 py-0.5 bg-yellow-400/20 text-yellow-400 rounded">
|
||||
|
||||
Reference in New Issue
Block a user