refactor: 统一端口配置,通过 IPC 获取而非硬编码
This commit is contained in:
@@ -1,16 +1,20 @@
|
||||
import React, { useEffect, useState } from 'react'
|
||||
|
||||
const XCOPENCODEWEB_PORT = 3002
|
||||
|
||||
export const OpenCodePage: React.FC = () => {
|
||||
const [isRunning, setIsRunning] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [port, setPort] = useState<number>(3002)
|
||||
|
||||
useEffect(() => {
|
||||
let mounted = true
|
||||
|
||||
const startService = async () => {
|
||||
const init = async () => {
|
||||
try {
|
||||
const portResult = await window.electronAPI.xcOpenCodeWebGetPort()
|
||||
if (mounted) {
|
||||
setPort(portResult.port)
|
||||
}
|
||||
|
||||
const result = await window.electronAPI.xcOpenCodeWebStart()
|
||||
if (mounted) {
|
||||
if (result.success) {
|
||||
@@ -27,7 +31,7 @@ export const OpenCodePage: React.FC = () => {
|
||||
}
|
||||
}
|
||||
|
||||
startService()
|
||||
init()
|
||||
|
||||
return () => {
|
||||
mounted = false
|
||||
@@ -51,7 +55,7 @@ export const OpenCodePage: React.FC = () => {
|
||||
</div>
|
||||
{isRunning && (
|
||||
<iframe
|
||||
src={`http://localhost:${XCOPENCODEWEB_PORT}`}
|
||||
src={`http://localhost:${port}`}
|
||||
className="flex-1 w-full border-0"
|
||||
title="XCOpenCodeWeb"
|
||||
/>
|
||||
|
||||
1
src/types/electron.d.ts
vendored
1
src/types/electron.d.ts
vendored
@@ -43,6 +43,7 @@ export interface ElectronAPI {
|
||||
xcOpenCodeWebStart: () => Promise<{ success: boolean; error?: string }>
|
||||
xcOpenCodeWebStop: () => Promise<{ success: boolean; error?: string }>
|
||||
xcOpenCodeWebGetStatus: () => Promise<{ running: boolean; port: number }>
|
||||
xcOpenCodeWebGetPort: () => Promise<{ port: number }>
|
||||
}
|
||||
|
||||
declare global {
|
||||
|
||||
Reference in New Issue
Block a user