Skip to content

Commit 99e3c1f

Browse files
committed
Phase 27 Part A (1/3): 代码冗余清理 - 消除重复函数和废弃类型
清理了重复定义的工具函数和废弃的类型定义,统一使用utils/format.ts中的标准化函数。 ## 清理内容: ### 1. 消除重复的formatBytes函数 **问题**: Dashboard.tsx和AdminDashboard.tsx中重复定义了formatBytes函数 **解决方案**: - 删除本地formatBytes定义 - 统一使用utils/format.ts中的formatFileSize函数 - 更新所有使用点 **修改文件**: - frontend/src/pages/dashboard/Dashboard.tsx - 添加导入: formatFileSize from '@/utils/format' - 删除lines 48-56: formatBytes函数定义 - 替换line 160: formatBytes() → formatFileSize() - frontend/src/pages/admin/AdminDashboard.tsx - 添加导入: formatFileSize from '../../utils/format' - 删除lines 138-141: formatBytes函数定义 - 替换line 174: formatBytes() → formatFileSize() - 替换line 299: formatBytes() → formatFileSize() ### 2. 删除废弃的类型定义 **问题**: ProjectListResponse和TaskListResponse标记为@deprecated但未清理 **解决方案**: 删除废弃类型和未使用的导入 **修改文件**: - frontend/src/types/project.ts - 删除: ProjectListResponse type (lines 33-37) - 删除: PaginatedResponse导入(已无使用) - frontend/src/types/task.ts - 删除: TaskListResponse type (lines 46-49) - 删除: PaginatedResponse导入(已无使用) ## 收益: - ✅ 减少代码重复: 删除2个重复函数定义 - ✅ 代码更简洁: 删除2个废弃类型和2个未使用导入 - ✅ 统一格式化: 所有文件大小格式化使用相同函数 - ✅ 更好的维护性: 单一真实来源(Single Source of Truth) ## 验证: - ✅ TypeScript编译: 0错误 - ✅ 前端构建成功 - ✅ Bundle大小: 213.99 KB (main chunk)
1 parent dead6a1 commit 99e3c1f

File tree

4 files changed

+5
-33
lines changed

4 files changed

+5
-33
lines changed

frontend/src/pages/admin/AdminDashboard.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
EnhancedEmptyState,
4444
} from '../../components/common'
4545
import { toast } from '../../utils/notification'
46+
import { formatFileSize } from '../../utils/format'
4647
import type { StatisticItem } from '../../components/common'
4748
import type { User, UserAdminUpdate, SystemStats } from '../../types/admin'
4849
import dayjs from 'dayjs'
@@ -134,11 +135,6 @@ export const AdminDashboard: React.FC = () => {
134135
}
135136
}
136137

137-
const formatBytes = (bytes: number) => {
138-
const gb = bytes / (1024 * 1024 * 1024)
139-
return `${gb.toFixed(2)} GB`
140-
}
141-
142138
const columns: ColumnsType<User> = [
143139
{
144140
title: 'Username',
@@ -175,7 +171,7 @@ export const AdminDashboard: React.FC = () => {
175171
<div>
176172
<Progress percent={Math.round(percent)} size="small" status={percent > 90 ? 'exception' : 'normal'} />
177173
<div style={{ fontSize: 12, color: '#666' }}>
178-
{formatBytes(record?.storage_used ?? 0)} / {formatBytes(record?.storage_quota ?? 0)}
174+
{formatFileSize(record?.storage_used ?? 0)} / {formatFileSize(record?.storage_quota ?? 0)}
179175
</div>
180176
</div>
181177
)
@@ -300,7 +296,7 @@ export const AdminDashboard: React.FC = () => {
300296
status="active"
301297
/>
302298
<div style={{ marginTop: 8, fontSize: 14 }}>
303-
{formatBytes(stats?.total_storage_used || 0)} / {formatBytes(stats?.total_storage_quota || 0)}
299+
{formatFileSize(stats?.total_storage_used || 0)} / {formatFileSize(stats?.total_storage_quota || 0)}
304300
</div>
305301
</Card>
306302
</FadeIn>

frontend/src/pages/dashboard/Dashboard.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { authStore } from '@/store/authStore'
1414
import { PageSkeleton, FadeIn, StaggeredList, EnhancedEmptyState } from '@/components/common'
1515
import { useAsync } from '@/hooks'
1616
import { statsService } from '@/services/stats.service'
17+
import { formatFileSize } from '@/utils/format'
1718
import styles from './Dashboard.module.css'
1819

1920
const { Title, Text } = Typography
@@ -45,16 +46,6 @@ export const Dashboard: React.FC = () => {
4546

4647
const storagePercent = stats ? Math.round((stats.storageUsed / stats.storageQuota) * 100) : 0
4748

48-
const formatBytes = (bytes: number) => {
49-
if (bytes === 0) {
50-
return '0 Bytes'
51-
}
52-
const k = 1024
53-
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
54-
const i = Math.floor(Math.log(bytes) / Math.log(k))
55-
return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + ' ' + sizes[i]
56-
}
57-
5849
// Loading 状态 - Use PageSkeleton
5950
if (loading) {
6051
return <PageSkeleton hasHeader rows={4} />
@@ -166,7 +157,7 @@ export const Dashboard: React.FC = () => {
166157
size="small"
167158
/>
168159
<Text type="secondary" style={{ fontSize: 12, marginTop: 4, display: 'block' }}>
169-
{formatBytes(stats.storageUsed)} / {formatBytes(stats.storageQuota)}
160+
{formatFileSize(stats.storageUsed)} / {formatFileSize(stats.storageQuota)}
170161
</Text>
171162
</div>
172163
</Card>

frontend/src/types/project.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* Project types and interfaces
33
*/
44

5-
import type { PaginatedResponse } from './common'
6-
75
export interface Project {
86
id: string
97
name: string
@@ -30,12 +28,6 @@ export interface ProjectUpdate {
3028
config?: Record<string, any>
3129
}
3230

33-
/**
34-
* @deprecated Use PaginatedResponse<Project> instead
35-
* Kept for backward compatibility during migration
36-
*/
37-
export type ProjectListResponse = PaginatedResponse<Project>
38-
3931
export interface ProjectStats {
4032
total_projects: number
4133
active_projects: number

frontend/src/types/task.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* Task types and interfaces
33
*/
44

5-
import type { PaginatedResponse } from './common'
6-
75
export type TaskStatus = 'pending' | 'running' | 'completed' | 'failed' | 'cancelled'
86

97
export interface Task {
@@ -43,11 +41,6 @@ export interface TaskExecuteRequest {
4341
config?: Record<string, any>
4442
}
4543

46-
/**
47-
* @deprecated Use PaginatedResponse<Task> instead
48-
*/
49-
export type TaskListResponse = PaginatedResponse<Task>
50-
5144
export interface TaskStats {
5245
total_tasks: number
5346
pending_tasks: number

0 commit comments

Comments
 (0)