mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
Optimizes SFTP view rendering by isolating tab activation state, using context stores and memoization to prevent unnecessary re-renders on tab switch. Introduces cross-pane tab drag-and-drop, allowing users to move tabs between panes via the tab bar, and ensures stable callback references throughout. Refactors UI components for memoization and efficient prop updates, and wraps key dialogs with React.memo for better performance. Enhances overall responsiveness, especially when switching or managing tabs in complex SFTP sessions.
91 lines
2.6 KiB
TypeScript
91 lines
2.6 KiB
TypeScript
import { useRef } from "react";
|
|
import { logger } from "./logger";
|
|
|
|
/**
|
|
* 追踪组件渲染次数和原因
|
|
* 在开发环境下帮助识别不必要的重渲染
|
|
*
|
|
* @param componentName 组件名称
|
|
* @param props 当前 props(用于比较变化)
|
|
* @param enabled 是否启用追踪,默认 true
|
|
*/
|
|
export function useRenderTracker(
|
|
componentName: string,
|
|
props: Record<string, unknown>,
|
|
enabled: boolean = true
|
|
): void {
|
|
const renderCountRef = useRef(0);
|
|
const prevPropsRef = useRef<Record<string, unknown>>({});
|
|
|
|
renderCountRef.current += 1;
|
|
|
|
if (!enabled) return;
|
|
|
|
const renderCount = renderCountRef.current;
|
|
const prevProps = prevPropsRef.current;
|
|
|
|
// 找出变化的 props
|
|
const changedProps: string[] = [];
|
|
const allKeys = new Set([...Object.keys(props), ...Object.keys(prevProps)]);
|
|
|
|
for (const key of allKeys) {
|
|
if (prevProps[key] !== props[key]) {
|
|
changedProps.push(key);
|
|
}
|
|
}
|
|
|
|
// 只在有变化时打印(减少日志噪音)
|
|
if (renderCount === 1) {
|
|
logger.info(`[Render] ${componentName} - 首次渲染`);
|
|
} else if (changedProps.length > 0) {
|
|
logger.info(`[Render] ${componentName} - 第${renderCount}次渲染`, {
|
|
changedProps,
|
|
details: changedProps.reduce((acc, key) => {
|
|
acc[key] = {
|
|
prev: summarizeValue(prevProps[key]),
|
|
curr: summarizeValue(props[key]),
|
|
};
|
|
return acc;
|
|
}, {} as Record<string, { prev: string; curr: string }>),
|
|
});
|
|
}
|
|
// 不再打印 "props未变化" 的警告 - 这是正常的 React 行为
|
|
|
|
// 更新 prevProps
|
|
prevPropsRef.current = { ...props };
|
|
}
|
|
|
|
/**
|
|
* 简化值的显示,避免日志过长
|
|
*/
|
|
function summarizeValue(value: unknown): string {
|
|
if (value === undefined) return "undefined";
|
|
if (value === null) return "null";
|
|
if (typeof value === "function") return `fn:${value.name || "anonymous"}`;
|
|
if (typeof value === "object") {
|
|
if (Array.isArray(value)) return `Array(${value.length})`;
|
|
const keys = Object.keys(value);
|
|
if (keys.length <= 3) {
|
|
return `{${keys.join(", ")}}`;
|
|
}
|
|
return `Object(${keys.length} keys)`;
|
|
}
|
|
if (typeof value === "string" && value.length > 30) {
|
|
return `"${value.slice(0, 30)}..."`;
|
|
}
|
|
return String(value);
|
|
}
|
|
|
|
/**
|
|
* 简单的渲染计数器,只记录渲染次数不做详细分析
|
|
*/
|
|
export function useRenderCount(componentName: string): number {
|
|
const renderCountRef = useRef(0);
|
|
renderCountRef.current += 1;
|
|
|
|
// 每次渲染都打印
|
|
logger.info(`[Render] ${componentName} - 第${renderCountRef.current}次渲染`);
|
|
|
|
return renderCountRef.current;
|
|
}
|