// frontend-react/src/components/ToolBar/ToolBar.jsx import React, { useState, useRef, useEffect } from 'react'; import RoleSelector from '../RoleSelector/RoleSelector'; import useRoleSelectorStore from '../../Store/Slices/RoleSelectorSlice'; import './ToolBar.css'; const Toolbar = () => { const [activePanel, setActivePanel] = useState(null); const panelRef = useRef(null); const selectedRole = useRoleSelectorStore((state) => state.selectedRole); const selectedChat = useRoleSelectorStore((state) => state.selectedChat); // 点击外部关闭面板 React.useEffect(() => { const handleClickOutside = (event) => { if (panelRef.current && !panelRef.current.contains(event.target)) { setActivePanel(null); } }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, []); // 监听selectedRole和selectedChat的变化 useEffect(() => { console.log('当前选中的角色:', selectedRole); console.log('当前选中的聊天:', selectedChat); // 这里可以添加其他需要响应角色变化的逻辑 }, [selectedRole, selectedChat]); // 处理面板切换 const handlePanelToggle = (panelName) => { if (activePanel === panelName) { setActivePanel(null); } else { setActivePanel(panelName); } }; // 关闭面板 const handleClosePanel = () => { setActivePanel(null); }; // 截断文本 const truncateText = (text, maxLength = 20) => { if (!text) return '未选择'; return text.length > maxLength ? text.substring(0, maxLength) + '...' : text; }; // 构建显示文本 const getDisplayText = () => { if (!selectedRole) return '未选择'; return selectedChat ? `${selectedRole} / ${selectedChat}` : selectedRole; }; return ( <>
{/* 左侧:当前角色 */}
handlePanelToggle('currentRole')} > 👤 当前角色
{truncateText(getDisplayText())}
{/* 中间:角色管理 */}
handlePanelToggle('role')} > 🎭 角色管理
{truncateText(getDisplayText())}
{/* 全局世界书 */}
handlePanelToggle('worldBook')} > 📚 全局世界书
全局世界书
{/* 右侧:设置和拓展 */}
handlePanelToggle('settings')} > ⚙️
handlePanelToggle('extensions')} > ➕
{/* 角色管理面板 */} {activePanel === 'role' && (

用户角色管理

)} {/* 当前角色面板(暂时留空) */} {activePanel === 'currentRole' && (

当前ai角色

当前角色详情...

)} {/* 全局世界书面板 */} {activePanel === 'worldBook' && (

全局世界书

全局世界书内容...

)} {/* 设置面板 */} {activePanel === 'settings' && (

系统设置

系统设置内容...

)} {/* 拓展面板 */} {activePanel === 'extensions' && (

功能拓展

功能拓展内容...

)} ); }; export default Toolbar;