@@ -84,6 +84,10 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
8484 const [ lastStartAt , setLastStartAt ] = useState ( null )
8585 const heartbeatBusyRef = useRef ( false )
8686 const [ shouldRecheck , setShouldRecheck ] = useState ( false )
87+ // Step 2 action busy states (used to disable buttons until the async action fully exits)
88+ const [ checkingRemoteServerBusy , setCheckingRemoteServerBusy ] = useState ( false )
89+ const [ startingRemoteServerBusy , setStartingRemoteServerBusy ] = useState ( false )
90+ const [ checkingRemotePortBusy , setCheckingRemotePortBusy ] = useState ( false )
8791 const [ requirementsChecking , setRequirementsChecking ] = useState ( false )
8892 const [ requirementsMetRemote , setRequirementsMetRemote ] = useState ( false )
8993 const [ requirementsInstalling , setRequirementsInstalling ] = useState ( false )
@@ -110,6 +114,9 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
110114 const [ directoryContents , setDirectoryContents ] = useState ( [ ] )
111115 const [ remoteDirPath , setRemoteDirPath ] = useState ( "" )
112116
117+ // Prevent overlapping local port rebind operations while the user is editing
118+ const localPortRebindBusyRef = useRef ( { go : false , mongo : false } )
119+
113120 // const registerPublicKey = async (publicKeyToRegister, usernameToRegister) => {
114121 // setRegisterStatus("Registering...")
115122 // toast.info("Registering your SSH public key with the backend...")
@@ -346,6 +353,70 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
346353 }
347354 } , [ ] )
348355
356+ // Rebind local forwarded ports (GO/Mongo) while the tunnel is active.
357+ // This keeps the same remote port but moves the local listening port.
358+ useEffect ( ( ) => {
359+ if ( ! tunnelActive ) return
360+ const desiredLocalPort = Number ( localGoPort )
361+ if ( ! Number . isFinite ( desiredLocalPort ) || desiredLocalPort < 1 || desiredLocalPort > 65535 ) return
362+
363+ const timer = setTimeout ( async ( ) => {
364+ if ( localPortRebindBusyRef . current . go ) return
365+ localPortRebindBusyRef . current . go = true
366+ try {
367+ const state = await ipcRenderer . invoke ( 'getTunnelState' )
368+ const tunnels = Array . isArray ( state ?. tunnels ) ? state . tunnels : [ ]
369+ const entry = tunnels . find ( t => t && t . name === 'go' )
370+ const currentLocal = Number ( entry ?. localPort ?? state ?. localGoPort )
371+ const remotePort = Number ( entry ?. remotePort ?? state ?. remoteGoPort ?? remoteGoPort )
372+ if ( ! remotePort || Number . isNaN ( remotePort ) ) return
373+ if ( currentLocal && currentLocal === desiredLocalPort ) return
374+
375+ const res = await ipcRenderer . invoke ( 'rebindPortTunnel' , { name : 'go' , newLocalPort : desiredLocalPort , newRemotePort : remotePort } )
376+ if ( ! res ?. success ) {
377+ console . warn ( 'Failed to rebind GO forward local port:' , res ?. error || res )
378+ }
379+ } catch ( e ) {
380+ console . warn ( 'Failed to rebind GO forward local port:' , e && e . message ? e . message : String ( e ) )
381+ } finally {
382+ localPortRebindBusyRef . current . go = false
383+ }
384+ } , 500 )
385+
386+ return ( ) => clearTimeout ( timer )
387+ } , [ localGoPort , remoteGoPort , tunnelActive ] )
388+
389+ useEffect ( ( ) => {
390+ if ( ! tunnelActive ) return
391+ const desiredLocalPort = Number ( localDBPort )
392+ if ( ! Number . isFinite ( desiredLocalPort ) || desiredLocalPort < 1 || desiredLocalPort > 65535 ) return
393+
394+ const timer = setTimeout ( async ( ) => {
395+ if ( localPortRebindBusyRef . current . mongo ) return
396+ localPortRebindBusyRef . current . mongo = true
397+ try {
398+ const state = await ipcRenderer . invoke ( 'getTunnelState' )
399+ const tunnels = Array . isArray ( state ?. tunnels ) ? state . tunnels : [ ]
400+ const entry = tunnels . find ( t => t && t . name === 'mongo' )
401+ const currentLocal = Number ( entry ?. localPort ?? state ?. localDBPort )
402+ const remotePort = Number ( entry ?. remotePort ?? state ?. remoteDBPort ?? remoteDBPort )
403+ if ( ! remotePort || Number . isNaN ( remotePort ) ) return
404+ if ( currentLocal && currentLocal === desiredLocalPort ) return
405+
406+ const res = await ipcRenderer . invoke ( 'rebindPortTunnel' , { name : 'mongo' , newLocalPort : desiredLocalPort , newRemotePort : remotePort } )
407+ if ( ! res ?. success ) {
408+ console . warn ( 'Failed to rebind Mongo forward local port:' , res ?. error || res )
409+ }
410+ } catch ( e ) {
411+ console . warn ( 'Failed to rebind Mongo forward local port:' , e && e . message ? e . message : String ( e ) )
412+ } finally {
413+ localPortRebindBusyRef . current . mongo = false
414+ }
415+ } , 500 )
416+
417+ return ( ) => clearTimeout ( timer )
418+ } , [ localDBPort , remoteDBPort , tunnelActive ] )
419+
349420 // Step 1: Only establish SSH tunnel/auth to remote host
350421 const handleConnectSSH = async ( info , isReconnect = false ) => {
351422 setConnectionProcessing ( true )
@@ -497,6 +568,7 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
497568 return
498569 }
499570 try {
571+ setCheckingRemoteServerBusy ( true )
500572 setRemoteBackendStatus ( 'Checking remote server...' )
501573 // First, check installation presence on remote
502574 const presence = await ipcRenderer . invoke ( 'backendPresence' , { target : 'remote' } )
@@ -662,6 +734,8 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
662734 }
663735 } catch ( e ) {
664736 setRemoteBackendStatus ( 'Failed to check remote server: ' + ( e ?. message || String ( e ) ) )
737+ } finally {
738+ setCheckingRemoteServerBusy ( false )
665739 }
666740 }
667741
@@ -676,6 +750,9 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
676750 return
677751 }
678752 try {
753+ setInstallingRemote ( true )
754+ // Keep the UI disabled immediately on click (pre-check can take time)
755+ setRemoteInstallText ( 'Preparing install...' )
679756 // Pre-check: if latest release tag already exists on remote, skip install
680757 try {
681758 const latest = await ipcRenderer . invoke ( 'getLatestBackendReleaseInfo' )
@@ -708,7 +785,6 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
708785 }
709786 } catch { /* non-fatal; proceed with normal install */ }
710787
711- setInstallingRemote ( true )
712788 setRemoteInstallText ( 'Installing remote server...' )
713789 const payload = { }
714790 if ( DEFAULT_REMOTE_MANIFEST ) payload . manifestUrl = DEFAULT_REMOTE_MANIFEST
@@ -744,6 +820,7 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
744820 return
745821 }
746822 try {
823+ setStartingRemoteServerBusy ( true )
747824 setRemoteBackendStatus ( 'Starting remote server...' )
748825 // Prefer explicit start if path is known, else ensure
749826 let started
@@ -813,6 +890,8 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
813890 setRemoteBackendStatus ( 'Failed to start remote server: ' + msg )
814891 setLastStartDetails ( `Exception: ${ msg } ` )
815892 setRemoteServerRunning ( false )
893+ } finally {
894+ setStartingRemoteServerBusy ( false )
816895 }
817896 }
818897
@@ -925,6 +1004,26 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
9251004 }
9261005 }
9271006
1007+ const waitForRequirementsRemote = async ( { forwardedPort, maxWaitMs = 10 * 60 * 1000 , intervalMs = 5000 } = { } ) => {
1008+ const startedAt = Date . now ( )
1009+ while ( Date . now ( ) - startedAt < maxWaitMs ) {
1010+ try {
1011+ const resp = await window . backend . requestExpress ( { method : 'get' , path : '/check-requirements' , host : '127.0.0.1' , port : Number ( forwardedPort ) } )
1012+ const result = resp ?. data ?. result || { }
1013+ const pythonInstalled = ! ! ( result ?. pythonInstalled )
1014+ const mongoInstalled = ! ! ( result ?. mongoDBInstalled )
1015+ const ok = pythonInstalled && mongoInstalled
1016+ setRequirementsDetailsRemote ( { pythonInstalled, mongoInstalled } )
1017+ setRequirementsMetRemote ( ok )
1018+ if ( ok ) return true
1019+ } catch {
1020+ // Ignore transient errors while the remote is restarting/installing
1021+ }
1022+ await new Promise ( ( r ) => setTimeout ( r , intervalMs ) )
1023+ }
1024+ return false
1025+ }
1026+
9281027
9291028 const installRequirementsRemote = async ( ) => {
9301029 if ( ! tunnelActive || ! remoteServerRunning ) return
@@ -972,7 +1071,10 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
9721071 }
9731072 }
9741073 // Re-check
975- await checkRequirementsRemote ( )
1074+ const ok = await waitForRequirementsRemote ( { forwardedPort : Number ( forwardedPort ) } )
1075+ if ( ! ok ) {
1076+ toast . warn ( 'Remote requirements install is taking longer than expected. Please re-check in a moment.' )
1077+ }
9761078 } finally {
9771079 setRequirementsInstalling ( false )
9781080 }
@@ -1281,6 +1383,8 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
12811383 { label : 'Workspace' }
12821384 ]
12831385
1386+ const serverSetupActionsBusy = checkingRemoteServerBusy || installingRemote || startingRemoteServerBusy || checkingRemotePortBusy
1387+
12841388 // Pager visibility/disabled states
12851389 const prevDisabled = connectionProcessing || activeStep === 0
12861390 const nextDisabled = connectionProcessing ||
@@ -1456,8 +1560,8 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
14561560 </ div >
14571561 </ div >
14581562 < div style = { { display : 'flex' , gap : 8 , alignItems : 'center' , flexWrap : 'wrap' } } >
1459- < Button onClick = { checkRemoteServer } disabled = { ! tunnelActive || connectionProcessing } style = { { background : 'var(--button-bg)' , color : 'var(--button-text)' } } > { shouldRecheck ? 'Recheck status' : 'Check' } </ Button >
1460- < Button onClick = { installRemoteServer } disabled = { ! tunnelActive || installingRemote } style = { { background : 'var(--button-bg)' , color : 'var(--button-text)' } } > Install / Update</ Button >
1563+ < Button onClick = { checkRemoteServer } disabled = { ! tunnelActive || connectionProcessing || serverSetupActionsBusy } style = { { background : 'var(--button-bg)' , color : 'var(--button-text)' } } > { shouldRecheck ? 'Recheck status' : 'Check' } </ Button >
1564+ < Button onClick = { installRemoteServer } disabled = { ! tunnelActive || connectionProcessing || serverSetupActionsBusy } style = { { background : 'var(--button-bg)' , color : 'var(--button-text)' } } > Install / Update</ Button >
14611565 { ( installingRemote || remoteInstallPhase ) && (
14621566 < div style = { { display : 'flex' , alignItems : 'center' , gap : 12 , flexWrap : 'wrap' } } >
14631567 { typeof remoteDownloadPercent === 'number' ? (
@@ -1487,14 +1591,15 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
14871591 ) }
14881592 < div style = { { display : 'flex' , gap : 12 , alignItems : 'center' , marginTop : 8 } } >
14891593 < span > Start on port:</ span >
1490- < InputNumber disabled = { ! tunnelActive || connectionProcessing } value = { remoteStartPort } onChange = { e => setRemoteStartPort ( e . value ) } useGrouping = { false } min = { 1 } max = { 65535 } />
1594+ < InputNumber disabled = { ! tunnelActive || connectionProcessing || serverSetupActionsBusy } value = { remoteStartPort } onChange = { e => setRemoteStartPort ( e . value ) } useGrouping = { false } min = { 1 } max = { 65535 } />
14911595 { ! remoteServerRunning ? (
1492- < Button onClick = { startRemoteServer } disabled = { ! tunnelActive || connectionProcessing } style = { { background : 'var(--button-bg)' , color : 'var(--button-text)' } } > Start Server</ Button >
1596+ < Button onClick = { startRemoteServer } disabled = { ! tunnelActive || connectionProcessing || serverSetupActionsBusy } style = { { background : 'var(--button-bg)' , color : 'var(--button-text)' } } > Start Server</ Button >
14931597 ) : (
1494- < Button onClick = { stopRemoteServer } disabled = { ! tunnelActive || connectionProcessing } style = { { background : 'var(--danger)' , color : 'var(--button-text)' } } > Stop Server</ Button >
1598+ < Button onClick = { stopRemoteServer } disabled = { ! tunnelActive || connectionProcessing || serverSetupActionsBusy } style = { { background : 'var(--danger)' , color : 'var(--button-text)' } } > Stop Server</ Button >
14951599 ) }
14961600 < Button onClick = { async ( ) => {
14971601 if ( ! tunnelActive ) return toast . error ( 'SSH tunnel not active.' )
1602+ setCheckingRemotePortBusy ( true )
14981603 try {
14991604 const res = await ipcRenderer . invoke ( 'remoteCheckPort' , { port : Number ( remoteStartPort ) } )
15001605 if ( res && res . success ) {
@@ -1508,8 +1613,10 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
15081613 }
15091614 } catch ( e ) {
15101615 toast . error ( `Port check error: ${ e . message || e } ` )
1616+ } finally {
1617+ setCheckingRemotePortBusy ( false )
15111618 }
1512- } } disabled = { ! tunnelActive || connectionProcessing } style = { { background : 'var(--bg-secondary)' , color : 'var(--text-secondary)' } } > Check Port</ Button >
1619+ } } disabled = { ! tunnelActive || connectionProcessing || serverSetupActionsBusy } style = { { background : 'var(--bg-secondary)' , color : 'var(--text-secondary)' } } > Check Port</ Button >
15131620 </ div >
15141621 < div style = { { marginTop : 12 , paddingTop : 12 , borderTop : '1px solid var(--border-color)' } } >
15151622 < div style = { { display : 'flex' , alignItems : 'center' , justifyContent : 'space-between' } } >
@@ -1521,8 +1628,8 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
15211628 </ div >
15221629 < div style = { { display : 'flex' , gap : 8 , alignItems : 'center' , marginTop : 8 , flexWrap : 'wrap' } } >
15231630 < div style = { { display : 'flex' , gap : 8 , alignItems : 'center' } } >
1524- < Button onClick = { checkRequirementsRemote } disabled = { ! remoteServerRunning || requirementsChecking } style = { { background : 'var(--button-bg)' , color : 'var(--button-text)' } } > Check</ Button >
1525- < Button onClick = { installRequirementsRemote } disabled = { ! remoteServerRunning || requirementsInstalling || requirementsMetRemote } style = { { background : 'var(--button-bg)' , color : 'var(--button-text)' } } > Install Missing</ Button >
1631+ < Button onClick = { checkRequirementsRemote } disabled = { ! remoteServerRunning || requirementsChecking || requirementsInstalling } style = { { background : 'var(--button-bg)' , color : 'var(--button-text)' } } > Check</ Button >
1632+ < Button onClick = { installRequirementsRemote } disabled = { ! remoteServerRunning || requirementsInstalling || requirementsChecking || requirementsMetRemote } style = { { background : 'var(--button-bg)' , color : 'var(--button-text)' } } > Install Missing</ Button >
15261633 { ( requirementsChecking || requirementsInstalling ) && < ProgressBar mode = "indeterminate" style = { { width : 200 } } /> }
15271634 </ div >
15281635 < span style = { { fontSize : 11 , color : 'var(--text-secondary)' , maxWidth : 420 } } >
@@ -1570,43 +1677,13 @@ const ConnectionModal = ({ visible, closable, onClose, onConnect }) =>{
15701677 >
15711678 { showAdvanced && < >
15721679 < div style = { { width : '100%' } } >
1573- < label >
1574- Local Express Port:
1575- < InputNumber disabled = { tunnelActive || connectionProcessing } value = { localExpressPort } onChange = { e => setLocalExpressPort ( e . value ) } placeholder = "54280" useGrouping = { false } min = { 1 } max = { 65535 } />
1576- { inputErrors . localExpressPort && < div style = { { color : 'red' , fontSize : 13 } } > { inputErrors . localExpressPort } </ div > }
1577- { localPortWarning && < div style = { { color : 'var(--warning)' , fontSize : 13 , marginTop : 2 } } > { localPortWarning } </ div > }
1578- </ label >
15791680 < label >
15801681 Local GO Port:
1581- < InputNumber disabled = { tunnelActive || connectionProcessing } value = { localGoPort } onChange = { e => setLocalGoPort ( e . value ) } placeholder = "54380" useGrouping = { false } min = { 1 } max = { 65535 } />
1682+ < InputNumber disabled = { connectionProcessing } value = { localGoPort } onChange = { e => setLocalGoPort ( e . value == null ? '' : String ( e . value ) ) } placeholder = "54380" useGrouping = { false } min = { 1 } max = { 65535 } />
15821683 </ label >
15831684 < label >
15841685 Local MongoDB Port:
1585- < InputNumber disabled = { tunnelActive || connectionProcessing } value = { localDBPort } onChange = { e => setLocalDBPort ( e . value ) } placeholder = "54020" useGrouping = { false } min = { 1 } max = { 65535 } />
1586- </ label >
1587- </ div >
1588- < div style = { { width : '100%' } } >
1589- < label >
1590- Remote Express Port:
1591- < InputNumber disabled = { tunnelActive || connectionProcessing } value = { remoteExpressPort } onChange = { e => setRemoteExpressPort ( e . value ) } placeholder = "54288" useGrouping = { false } min = { 1 } max = { 65535 } />
1592- { inputErrors . remoteExpressPort && < div style = { { color : 'var(--danger)' , fontSize : 13 } } > { inputErrors . remoteExpressPort } </ div > }
1593- </ label >
1594- < label >
1595- Remote GO Port:
1596- < InputNumber disabled = { tunnelActive || connectionProcessing } value = { remoteGoPort } onChange = { e => setRemoteGoPort ( e . value ) } placeholder = "54388" useGrouping = { false } min = { 1 } max = { 65535 } />
1597- </ label >
1598- < label >
1599- Remote MongoDB Port:
1600- < InputNumber disabled = { tunnelActive || connectionProcessing } value = { remoteDBPort } onChange = { e => setRemoteDBPort ( e . value ) } placeholder = "54017" useGrouping = { false } min = { 1 } max = { 65535 } />
1601- </ label >
1602- { /* <label>
1603- SSH Key Comment:
1604- <InputText disabled={tunnelActive || connectionProcessing} className="ssh-key-command" value={keyComment} onChange={e => setKeyComment(e.target.value)} placeholder="medomicslab-app" />
1605- </label> */ }
1606- < label >
1607- Remote SSH Port:
1608- < InputNumber disabled = { tunnelActive || connectionProcessing } value = { remotePort } onChange = { e => setRemotePort ( e . value ) } placeholder = "22" useGrouping = { false } min = { 1 } max = { 65535 } />
1609- { inputErrors . remotePort && < div style = { { color : 'var(--danger)' , fontSize : 13 } } > { inputErrors . remotePort } </ div > }
1686+ < InputNumber disabled = { connectionProcessing } value = { localDBPort } onChange = { e => setLocalDBPort ( e . value == null ? '' : String ( e . value ) ) } placeholder = "54020" useGrouping = { false } min = { 1 } max = { 65535 } />
16101687 </ label >
16111688 </ div >
16121689 </ > }
0 commit comments