refactor: Web 服务器设置移动到单独小节和设置小调整 - #360
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
嘿——我发现了 2 个问题
AI 代理提示词
请处理此代码审查中的评论:
## 各条评论
### 评论 1
<location path="src/components/WebServerSection.tsx" line_range="28-31" />
<code_context>
+ const loadWebServerInfo = async () => {
+ if (isTauri()) {
+ try {
+ const { invoke } = await import('@tauri-apps/api/core');
+ const [port, localIp] = await Promise.all([
+ invoke<number>('get_web_server_port'),
+ invoke<string | null>('get_local_lan_ip'),
+ ]);
+ setWebServerPort(port);
+ setLanIp(localIp);
+ } catch {}
+ } else {
+ // 浏览器环境:从当前 URL 推导端口
</code_context>
<issue_to_address>
**问题 (bug_risk):** 当 `get_local_lan_ip` 失败时,`Promise.all` 会整体拒绝,因此成功获取的 Web 服务器端口结果也会被丢弃,`webServerPort` 保持为 0;随后该部分会隐藏服务器地址,尽管仍然可以通过 localhost 访问。
**触发条件:** Tauri 后端无法确定 LAN IP 时,例如机器没有活动的网络接口。
**建议修复:** 独立加载端口,并将失败的 LAN IP 查询视为 `null`,这样现有的 `lanIp || 'localhost'` 回退逻辑就能生成本地地址。
```suggestion
const port = await invoke<number>('get_web_server_port');
const localIp = await invoke<string | null>('get_local_lan_ip').catch(() => null);
```
</issue_to_address>
### 评论 2
<location path="src/components/WebServerSection.tsx" line_range="23-43" />
<code_context>
+ const [webServerPort, setWebServerPort] = useState<number>(0);
+ const [lanIp, setLanIp] = useState<string | null>(null);
+
+ useEffect(() => {
+ const loadWebServerInfo = async () => {
+ if (isTauri()) {
+ try {
+ const { invoke } = await import('@tauri-apps/api/core');
+ const [port, localIp] = await Promise.all([
+ invoke<number>('get_web_server_port'),
+ invoke<string | null>('get_local_lan_ip'),
+ ]);
+ setWebServerPort(port);
+ setLanIp(localIp);
+ } catch {}
+ } else {
+ // 浏览器环境:从当前 URL 推导端口
+ const port = parseInt(window.location.port, 10);
+ if (port) setWebServerPort(port);
+ }
+ setPortInput(String(configuredPort));
+ }
+ loadWebServerInfo();
+ }, [configuredPort]);
+
+ const webServerAddress = (() => {
</code_context>
<issue_to_address>
**问题 (bug_risk):** 异步加载会在 Tauri 调用完成后重置 `portInput`,因此延迟的请求可能会覆盖用户在端口字段中输入的文本,并导致后续失焦操作保存一个与用户意图不同的值。
**触发条件:** 用户在异步 Tauri Web 服务器查询完成之前编辑端口时。
**建议修复:** 避免从过时的 effect 重置输入,或者取消/为异步请求添加版本控制,仅在当前配置值仍未发生变化时同步 `portInput`。
</issue_to_address>Original comment in English
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/components/WebServerSection.tsx" line_range="28-31" />
<code_context>
+ const loadWebServerInfo = async () => {
+ if (isTauri()) {
+ try {
+ const { invoke } = await import('@tauri-apps/api/core');
+ const [port, localIp] = await Promise.all([
+ invoke<number>('get_web_server_port'),
+ invoke<string | null>('get_local_lan_ip'),
+ ]);
+ setWebServerPort(port);
+ setLanIp(localIp);
+ } catch {}
+ } else {
+ // 浏览器环境:从当前 URL 推导端口
</code_context>
<issue_to_address>
**issue (bug_risk):** The `Promise.all` rejects as a whole when `get_local_lan_ip` fails, so the successful web-server port result is discarded and `webServerPort` remains 0; the section then hides the server address even though localhost access is still available.
**Triggers:** When the Tauri backend cannot determine a LAN IP, such as on a machine without an active network interface.
**Suggested fix:** Load the port independently and treat a failed LAN-IP lookup as `null`, allowing the existing `lanIp || 'localhost'` fallback to produce the local address.
```suggestion
const port = await invoke<number>('get_web_server_port');
const localIp = await invoke<string | null>('get_local_lan_ip').catch(() => null);
```
</issue_to_address>
### Comment 2
<location path="src/components/WebServerSection.tsx" line_range="23-43" />
<code_context>
+ const [webServerPort, setWebServerPort] = useState<number>(0);
+ const [lanIp, setLanIp] = useState<string | null>(null);
+
+ useEffect(() => {
+ const loadWebServerInfo = async () => {
+ if (isTauri()) {
+ try {
+ const { invoke } = await import('@tauri-apps/api/core');
+ const [port, localIp] = await Promise.all([
+ invoke<number>('get_web_server_port'),
+ invoke<string | null>('get_local_lan_ip'),
+ ]);
+ setWebServerPort(port);
+ setLanIp(localIp);
+ } catch {}
+ } else {
+ // 浏览器环境:从当前 URL 推导端口
+ const port = parseInt(window.location.port, 10);
+ if (port) setWebServerPort(port);
+ }
+ setPortInput(String(configuredPort));
+ }
+ loadWebServerInfo();
+ }, [configuredPort]);
+
+ const webServerAddress = (() => {
</code_context>
<issue_to_address>
**issue (bug_risk):** The asynchronous load resets `portInput` after the Tauri invocations complete, so a delayed request overwrites text the user has entered in the port field and can cause the subsequent blur to save a different value than the user intended.
**Triggers:** When the user edits the port before the asynchronous Tauri web-server lookups finish.
**Suggested fix:** Avoid resetting the input from an outdated effect, or cancel/version the async request and only synchronize `portInput` when the current configured value is still unchanged.
</issue_to_address>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
顺便调整了其他样式
Sourcery 摘要
将 Web Server 配置移至独立的设置部分,并让调试设置专注于诊断信息。
新功能:
增强功能:
Original summary in English
Sourcery 总结
将 Web Server 配置与诊断设置分离,并优化相关设置的呈现方式和本地化。
新功能:
改进:
Original summary in English
Summary by Sourcery
Separate Web Server configuration from diagnostic settings and refine the related settings presentation and localization.
New Features:
Enhancements: