feat: 添加@types/jest依赖并优化类型安全
refactor: 重构代码减少any类型使用,增加类型定义 fix: 修复TypeScript编译错误和类型不匹配问题 docs: 更新代码审查修复总结文档 style: 优化代码格式和注释 perf: 添加性能优化工具函数和虚拟滚动组件 test: 完善测试相关配置和类型定义 build: 更新package-lock.json文件
This commit is contained in:
124
dashboard/src/utils/PerformanceOptimization.tsx
Normal file
124
dashboard/src/utils/PerformanceOptimization.tsx
Normal file
@@ -0,0 +1,124 @@
|
||||
// FE-OP003: 前端性能优化
|
||||
import React from 'react';
|
||||
|
||||
// 懒加载组件
|
||||
export const lazyLoadComponent = (component: () => Promise<any>) => {
|
||||
return React.lazy(component);
|
||||
};
|
||||
|
||||
// 优化渲染性能
|
||||
export const useOptimizedRender = (deps: any[]) => {
|
||||
return React.useMemo(() => deps, deps);
|
||||
};
|
||||
|
||||
// 优化列表渲染
|
||||
export const optimizeListRender = <T>(
|
||||
items: T[],
|
||||
renderItem: (item: T, index: number) => React.ReactNode,
|
||||
keyExtractor: (item: T) => string | number
|
||||
) => {
|
||||
return items.map((item, index) => {
|
||||
const key = keyExtractor(item);
|
||||
return React.useMemo(() => renderItem(item, index), [item, index, renderItem]);
|
||||
});
|
||||
};
|
||||
|
||||
// 虚拟滚动组件
|
||||
export const VirtualList: React.FC<{
|
||||
items: any[];
|
||||
itemHeight: number;
|
||||
containerHeight: number;
|
||||
renderItem: (item: any, index: number) => React.ReactNode;
|
||||
keyExtractor: (item: any) => string | number;
|
||||
}> = ({ items, itemHeight, containerHeight, renderItem, keyExtractor }) => {
|
||||
const [scrollTop, setScrollTop] = React.useState(0);
|
||||
|
||||
const handleScroll = (e: React.UIEvent<HTMLDivElement>) => {
|
||||
setScrollTop(e.currentTarget.scrollTop);
|
||||
};
|
||||
|
||||
const startIndex = Math.floor(scrollTop / itemHeight);
|
||||
const endIndex = Math.min(
|
||||
startIndex + Math.ceil(containerHeight / itemHeight) + 1,
|
||||
items.length
|
||||
);
|
||||
|
||||
const visibleItems = items.slice(startIndex, endIndex);
|
||||
const offsetY = startIndex * itemHeight;
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
height: containerHeight,
|
||||
overflowY: 'auto',
|
||||
position: 'relative',
|
||||
}}
|
||||
onScroll={handleScroll}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
height: items.length * itemHeight,
|
||||
position: 'relative',
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
position: 'absolute',
|
||||
top: offsetY,
|
||||
left: 0,
|
||||
right: 0,
|
||||
}}
|
||||
>
|
||||
{visibleItems.map((item, index) => {
|
||||
const actualIndex = startIndex + index;
|
||||
return (
|
||||
<div
|
||||
key={keyExtractor(item)}
|
||||
style={{
|
||||
height: itemHeight,
|
||||
}}
|
||||
>
|
||||
{renderItem(item, actualIndex)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// 导入非React工具函数
|
||||
import {
|
||||
debounce,
|
||||
throttle,
|
||||
lazyLoadImage,
|
||||
memoize,
|
||||
batchProcess,
|
||||
preloadResource,
|
||||
measurePerformance
|
||||
} from './PerformanceOptimization';
|
||||
|
||||
export {
|
||||
debounce,
|
||||
throttle,
|
||||
lazyLoadImage,
|
||||
memoize,
|
||||
batchProcess,
|
||||
preloadResource,
|
||||
measurePerformance
|
||||
};
|
||||
|
||||
export default {
|
||||
debounce,
|
||||
throttle,
|
||||
lazyLoadComponent,
|
||||
lazyLoadImage,
|
||||
memoize,
|
||||
batchProcess,
|
||||
preloadResource,
|
||||
measurePerformance,
|
||||
useOptimizedRender,
|
||||
optimizeListRender,
|
||||
VirtualList,
|
||||
};
|
||||
Reference in New Issue
Block a user