/** * Sidebar - 侧边栏组件 * 支持宽度拖拽调整 */ import { useState, useEffect, useRef, useCallback } from 'react' import { Plus, Zap, Server, Settings, ListChecks, Terminal, FolderSync, X, TerminalSquare } from 'lucide-react' import { useSessionStore, Session } from '../../stores' import { useSettingsStore } from '../../stores/settingsStore' import { useTabStore } from '../../stores/tabStore' import { SessionTree } from './sidebar/SessionTree' import { SearchBox } from './sidebar/SearchBox' import { useConfirmDialog } from '../common/ConfirmDialog' import BatchCommandDialog from '../sessions/BatchCommandDialog' const MIN_SIDEBAR_WIDTH = 180 const MAX_SIDEBAR_WIDTH = 400 interface SidebarProps { onNewSession: () => void onQuickConnect: () => void onEditSession: (session: Session) => void } export default function Sidebar({ onNewSession, onQuickConnect, onEditSession }: SidebarProps) { const { sessions, setSessions, addSession, removeSession } = useSessionStore() const { settings, setSetting } = useSettingsStore() const { addTab } = useTabStore() const { confirm, ConfirmDialogComponent } = useConfirmDialog() const [searchTerm, setSearchTerm] = useState('') const [expandedGroups, setExpandedGroups] = useState>(new Set(['default'])) const [selectedSessionId, setSelectedSessionId] = useState(null) const [contextMenu, setContextMenu] = useState<{ x: number; y: number; session: Session } | null>(null) const [isResizing, setIsResizing] = useState(false) const [sidebarWidth, setSidebarWidth] = useState(settings.sidebarWidth || 256) const [sessionsLoading, setSessionsLoading] = useState(true) const [batchMode, setBatchMode] = useState(false) const [batchSelection, setBatchSelection] = useState>(new Set()) const [batchCommandSessions, setBatchCommandSessions] = useState([]) const [batchCommandOpen, setBatchCommandOpen] = useState(false) const contextMenuRef = useRef(null) const sidebarRef = useRef(null) // Sync width from settings on load useEffect(() => { if (settings.sidebarWidth) { setSidebarWidth(settings.sidebarWidth) } }, [settings.sidebarWidth]) // Load sessions useEffect(() => { window.opsxApi?.sessionGetAll() .then((result) => { if (result.success && result.sessions) setSessions(result.sessions) }) .catch(() => undefined) .finally(() => setSessionsLoading(false)) }, [setSessions]) // Click outside to close context menu useEffect(() => { const handleClickOutside = (e: MouseEvent) => { if (contextMenuRef.current && !contextMenuRef.current.contains(e.target as Node)) { setContextMenu(null) } } document.addEventListener('mousedown', handleClickOutside) return () => document.removeEventListener('mousedown', handleClickOutside) }, []) // Resize handler const handleResizeStart = useCallback((e: React.MouseEvent) => { e.preventDefault() setIsResizing(true) const startX = e.clientX const startWidth = sidebarWidth const handleMouseMove = (moveEvent: MouseEvent) => { const delta = moveEvent.clientX - startX const newWidth = Math.min(MAX_SIDEBAR_WIDTH, Math.max(MIN_SIDEBAR_WIDTH, startWidth + delta)) setSidebarWidth(newWidth) } const handleMouseUp = () => { setIsResizing(false) document.body.style.userSelect = '' document.body.style.cursor = '' // Save to settings on drag end setSetting('sidebarWidth', sidebarWidth) document.removeEventListener('mousemove', handleMouseMove) document.removeEventListener('mouseup', handleMouseUp) } // Prevent text selection during drag document.body.style.userSelect = 'none' document.body.style.cursor = 'col-resize' document.addEventListener('mousemove', handleMouseMove) document.addEventListener('mouseup', handleMouseUp) }, [sidebarWidth, setSetting]) // Save final width on unmount or when resizing ends (we save in handleMouseUp) useEffect(() => { if (!isResizing && sidebarWidth !== settings.sidebarWidth) { setSetting('sidebarWidth', sidebarWidth) } }, [isResizing, sidebarWidth, settings.sidebarWidth, setSetting]) const toggleGroup = (group: string) => { const newExpanded = new Set(expandedGroups) if (newExpanded.has(group)) newExpanded.delete(group) else newExpanded.add(group) setExpandedGroups(newExpanded) } const handleSelect = (session: Session) => { setSelectedSessionId(session.id) } const handleConnect = (session: Session) => { setSelectedSessionId(session.id) addTab({ title: session.name, type: 'terminal', sessionId: session.id, }) } const handleOpenSftp = (session: Session) => { setSelectedSessionId(session.id) addTab({ title: `SFTP: ${session.name}`, type: 'sftp', sessionId: session.id, }) } const toggleBatchSelection = useCallback((session: Session) => { setBatchSelection((current) => { const next = new Set(current) if (next.has(session.id)) next.delete(session.id) else next.add(session.id) return next }) }, []) const visibleSessions = sessions.filter((session) => { const query = searchTerm.trim().toLowerCase() return !query || session.name.toLowerCase().includes(query) || session.host.toLowerCase().includes(query) || session.username?.toLowerCase().includes(query) }) const toggleSelectVisible = useCallback(() => { setBatchSelection((current) => { const visibleIds = visibleSessions.map((session) => session.id) const allSelected = visibleIds.length > 0 && visibleIds.every((id) => current.has(id)) const next = new Set(current) for (const id of visibleIds) { if (allSelected) next.delete(id) else next.add(id) } return next }) }, [visibleSessions]) const selectedSessions = sessions.filter((session) => batchSelection.has(session.id)) const finishBatchMode = useCallback(() => { setBatchMode(false) setBatchSelection(new Set()) }, []) const handleBatchTerminal = useCallback(() => { for (const session of selectedSessions) { addTab({ title: session.name, type: 'terminal', sessionId: session.id, connectOnMount: true, }) } finishBatchMode() }, [addTab, finishBatchMode, selectedSessions]) const handleBatchSftp = useCallback(() => { for (const session of selectedSessions) { addTab({ title: `SFTP: ${session.name}`, type: 'sftp', sessionId: session.id }) } finishBatchMode() }, [addTab, finishBatchMode, selectedSessions]) const handleBatchCommand = useCallback(() => { setBatchCommandSessions(selectedSessions) setBatchCommandOpen(true) finishBatchMode() }, [finishBatchMode, selectedSessions]) const handleContextMenu = (e: React.MouseEvent, session: Session) => { e.preventDefault() setSelectedSessionId(session.id) setContextMenu({ x: e.clientX, y: e.clientY, session }) } const handleContextMenuAction = async (action: string) => { if (!contextMenu) return const session = contextMenu.session switch (action) { case 'connect': handleConnect(session) break case 'sftp': handleOpenSftp(session) break case 'edit': onEditSession(session) break case 'duplicate': { const { id: _id, ...sessionWithoutId } = session window.opsxApi?.sessionCreate({ ...sessionWithoutId, name: `${session.name} (副本)`, }).then((result) => { if (result?.success && result.session) { addSession(result.session as Session) } }) break } case 'delete': { const confirmed = await confirm({ title: '确认删除', message: `确定删除会话 "${session.name}"?`, danger: true, confirmText: '删除', }) if (confirmed) { removeSession(session.id) window.opsxApi?.sessionDelete(session.id) } break } } setContextMenu(null) } return ( <>
{/* Header */}
OpsXShell
远程运维工作台
<>
{batchMode && (
已选 {batchSelection.size} 个会话
)}
{batchMode && (
)} {/* Footer */}
{/* Context Menu */} {contextMenu && (
{[ { label: '打开终端', action: 'connect' }, { label: 'SFTP 文件', action: 'sftp' }, { label: '编辑', action: 'edit' }, { label: '克隆', action: 'duplicate' }, { label: '删除', action: 'delete', danger: true }, ].map((item) => ( ))}
)}
{/* Resize Handle */}
{ConfirmDialogComponent} setBatchCommandOpen(false)} /> ) }