import { useState, useEffect } from 'react'; import { invoke } from '@tauri-apps/api/core'; import { X, Server, WifiOff, Wifi } from 'lucide-react'; import { SettingsService } from '../services/SettingsService'; import { getProfilerService } from '../services/getService'; import '../styles/PortManager.css'; interface PortManagerProps { onClose: () => void; } export function PortManager({ onClose }: PortManagerProps) { const [isServerRunning, setIsServerRunning] = useState(false); const [serverPort, setServerPort] = useState('8080'); const [isChecking, setIsChecking] = useState(false); const [isStopping, setIsStopping] = useState(false); const [isStarting, setIsStarting] = useState(false); useEffect(() => { const settings = SettingsService.getInstance(); const savedPort = settings.get('profiler.port', 8080); console.log('[PortManager] Initial port from settings:', savedPort); setServerPort(String(savedPort)); const handleSettingsChange = ((event: CustomEvent) => { console.log('[PortManager] settings:changed event received:', event.detail); const newPort = event.detail['profiler.port']; if (newPort !== undefined) { console.log('[PortManager] Updating port to:', newPort); setServerPort(String(newPort)); } }) as EventListener; window.addEventListener('settings:changed', handleSettingsChange); return () => { window.removeEventListener('settings:changed', handleSettingsChange); }; }, []); useEffect(() => { checkServerStatus(); }, []); const checkServerStatus = async () => { setIsChecking(true); try { const status = await invoke('get_profiler_status'); setIsServerRunning(status); } catch (error) { console.error('[PortManager] Failed to check server status:', error); setIsServerRunning(false); } finally { setIsChecking(false); } }; const handleStopServer = async () => { setIsStopping(true); try { const profilerService = getProfilerService(); if (profilerService) { await profilerService.manualStopServer(); setIsServerRunning(false); } } catch (error) { console.error('[PortManager] Failed to stop server:', error); } finally { setIsStopping(false); } }; const handleStartServer = async () => { setIsStarting(true); try { const profilerService = getProfilerService(); if (profilerService) { await profilerService.manualStartServer(); await new Promise((resolve) => setTimeout(resolve, 500)); await checkServerStatus(); } } catch (error) { console.error('[PortManager] Failed to start server:', error); } finally { setIsStarting(false); } }; return (
e.stopPropagation()}>

Port Manager

Profiler Server

Status: {isChecking ? 'Checking...' : isServerRunning ? 'Running' : 'Stopped'}
{isServerRunning && (
Port: {serverPort}
)}
{isServerRunning && (
)} {!isServerRunning && ( <>

No server is currently running.

Click "Start Server" to start the profiler server.

)}

Tips

  • Use this when the Profiler server port is stuck and cannot be restarted
  • The server will automatically stop when the Profiler window is closed
  • Current configured port: {serverPort}
); }