⚡ perf(playground): optimize config loading and saving to reduce console spam
- Cache initial config using useRef to prevent repeated loadConfig() calls - Fix useEffect dependencies to only trigger on actual config changes - Modify debouncedSaveConfig dependency from function reference to actual config values - Update handleConfigReset to use DEFAULT_CONFIG directly instead of reloading - Prevent excessive console logging during chat interactions and frequent re-renders This resolves the issue where console was flooded with: "配置已从本地存储加载" and "配置已保存到本地存储" messages, especially during active chat sessions where logs appeared every second. Fixes: Frequent config load/save operations causing performance issues
This commit is contained in:
@@ -22,7 +22,7 @@ import { Layout } from '@douyinfe/semi-ui';
|
|||||||
import Midjourney from './pages/Midjourney';
|
import Midjourney from './pages/Midjourney';
|
||||||
import Pricing from './pages/Pricing/index.js';
|
import Pricing from './pages/Pricing/index.js';
|
||||||
import Task from './pages/Task/index.js';
|
import Task from './pages/Task/index.js';
|
||||||
import Playground from './pages/Playground/Playground.js';
|
import Playground from './pages/Playground/index.js';
|
||||||
import OAuth2Callback from './components/OAuth2Callback.js';
|
import OAuth2Callback from './components/OAuth2Callback.js';
|
||||||
import PersonalSetting from './components/PersonalSetting.js';
|
import PersonalSetting from './components/PersonalSetting.js';
|
||||||
import Setup from './pages/Setup/index.js';
|
import Setup from './pages/Setup/index.js';
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
import { useState, useCallback, useRef } from 'react';
|
import { useState, useCallback, useRef, useEffect } from 'react';
|
||||||
import { DEFAULT_MESSAGES, DEFAULT_CONFIG, DEBUG_TABS } from '../utils/constants';
|
import { DEFAULT_MESSAGES, DEFAULT_CONFIG, DEBUG_TABS } from '../utils/constants';
|
||||||
import { loadConfig, saveConfig } from '../components/playground/configStorage';
|
import { loadConfig, saveConfig } from '../components/playground/configStorage';
|
||||||
|
|
||||||
export const usePlaygroundState = () => {
|
export const usePlaygroundState = () => {
|
||||||
const savedConfig = loadConfig();
|
// 使用 ref 缓存初始配置,只加载一次
|
||||||
|
const initialConfigRef = useRef(null);
|
||||||
|
if (!initialConfigRef.current) {
|
||||||
|
initialConfigRef.current = loadConfig();
|
||||||
|
}
|
||||||
|
const savedConfig = initialConfigRef.current;
|
||||||
|
|
||||||
// 基础配置状态
|
// 基础配置状态
|
||||||
const [inputs, setInputs] = useState(savedConfig.inputs || DEFAULT_CONFIG.inputs);
|
const [inputs, setInputs] = useState(savedConfig.inputs || DEFAULT_CONFIG.inputs);
|
||||||
@@ -92,11 +97,10 @@ export const usePlaygroundState = () => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleConfigReset = useCallback(() => {
|
const handleConfigReset = useCallback(() => {
|
||||||
const defaultConfig = loadConfig();
|
setInputs(DEFAULT_CONFIG.inputs);
|
||||||
setInputs(defaultConfig.inputs);
|
setParameterEnabled(DEFAULT_CONFIG.parameterEnabled);
|
||||||
setParameterEnabled(defaultConfig.parameterEnabled);
|
setSystemPrompt(DEFAULT_CONFIG.systemPrompt);
|
||||||
setSystemPrompt(defaultConfig.systemPrompt);
|
setShowDebugPanel(DEFAULT_CONFIG.showDebugPanel);
|
||||||
setShowDebugPanel(defaultConfig.showDebugPanel);
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -10,35 +10,35 @@ import { StyleContext } from '../../context/Style/index.js';
|
|||||||
// Utils and hooks
|
// Utils and hooks
|
||||||
import { API, showError, getLogo, isMobile } from '../../helpers/index.js';
|
import { API, showError, getLogo, isMobile } from '../../helpers/index.js';
|
||||||
import { stringToColor } from '../../helpers/render.js';
|
import { stringToColor } from '../../helpers/render.js';
|
||||||
import { usePlaygroundState } from '../../hooks/usePlaygroundState';
|
import { usePlaygroundState } from '../../hooks/usePlaygroundState.js';
|
||||||
import { useMessageActions } from '../../hooks/useMessageActions';
|
import { useMessageActions } from '../../hooks/useMessageActions.js';
|
||||||
import { useApiRequest } from '../../hooks/useApiRequest';
|
import { useApiRequest } from '../../hooks/useApiRequest.js';
|
||||||
|
|
||||||
// Constants and utils
|
// Constants and utils
|
||||||
import {
|
import {
|
||||||
DEFAULT_MESSAGES,
|
DEFAULT_MESSAGES,
|
||||||
MESSAGE_ROLES,
|
MESSAGE_ROLES,
|
||||||
API_ENDPOINTS
|
API_ENDPOINTS
|
||||||
} from '../../utils/constants';
|
} from '../../utils/constants.js';
|
||||||
import {
|
import {
|
||||||
buildMessageContent,
|
buildMessageContent,
|
||||||
createMessage,
|
createMessage,
|
||||||
createLoadingAssistantMessage,
|
createLoadingAssistantMessage,
|
||||||
getTextContent
|
getTextContent
|
||||||
} from '../../utils/messageUtils';
|
} from '../../utils/messageUtils.js';
|
||||||
import {
|
import {
|
||||||
buildApiPayload,
|
buildApiPayload,
|
||||||
processModelsData,
|
processModelsData,
|
||||||
processGroupsData
|
processGroupsData
|
||||||
} from '../../utils/apiUtils';
|
} from '../../utils/apiUtils.js';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import SettingsPanel from '../../components/playground/SettingsPanel';
|
import SettingsPanel from '../../components/playground/SettingsPanel.js';
|
||||||
import ChatArea from '../../components/playground/ChatArea';
|
import ChatArea from '../../components/playground/ChatArea.js';
|
||||||
import DebugPanel from '../../components/playground/DebugPanel';
|
import DebugPanel from '../../components/playground/DebugPanel.js';
|
||||||
import MessageContent from '../../components/playground/MessageContent';
|
import MessageContent from '../../components/playground/MessageContent.js';
|
||||||
import MessageActions from '../../components/playground/MessageActions';
|
import MessageActions from '../../components/playground/MessageActions.js';
|
||||||
import FloatingButtons from '../../components/playground/FloatingButtons';
|
import FloatingButtons from '../../components/playground/FloatingButtons.js';
|
||||||
|
|
||||||
// 生成头像
|
// 生成头像
|
||||||
const generateAvatarDataUrl = (username) => {
|
const generateAvatarDataUrl = (username) => {
|
||||||
@@ -413,9 +413,10 @@ const Playground = () => {
|
|||||||
}));
|
}));
|
||||||
}, [constructPreviewPayload, setPreviewPayload, setDebugData]);
|
}, [constructPreviewPayload, setPreviewPayload, setDebugData]);
|
||||||
|
|
||||||
|
// 监听配置变化并自动保存
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
debouncedSaveConfig();
|
debouncedSaveConfig();
|
||||||
}, [debouncedSaveConfig]);
|
}, [inputs, parameterEnabled, systemPrompt, showDebugPanel]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-full bg-gray-50">
|
<div className="h-full bg-gray-50">
|
||||||
Reference in New Issue
Block a user