Update MobileCallPage and useVoicePreview to handle call end state

- Introduce a new state variable `callEnded` in `useVoicePreview` to manage call termination more effectively.
- Modify `MobileCallPage` to adjust the `inCall` condition based on `callEnded`, ensuring accurate UI updates during and after calls.
- Update rendering logic to display appropriate messages based on call status, enhancing user feedback during call transitions.
- Refactor button visibility to only show when in an active call, improving user interface clarity.
This commit is contained in:
Xin Wang
2026-07-10 22:48:40 +08:00
parent bb71d28721
commit 80d59fddc6
2 changed files with 52 additions and 27 deletions

View File

@@ -214,6 +214,9 @@ export function useVoicePreview(
// 远端(助手 TTS)媒体流:除挂到 <audio> 播放外,也暴露给波形可视化
const [remoteStream, setRemoteStream] = useState<MediaStream | null>(null);
const [messages, setMessages] = useState<ChatMessage[]>([]);
// 后端结束节点/EndFrame 触发的正常结束。与 failed 区分,供独立通话页
// 立即收起媒体画面并展示重新开始入口。
const [callEnded, setCallEnded] = useState(false);
const [networkQuality, setNetworkQuality] =
useState<NetworkQuality>("unknown");
// 可选麦克风/扬声器列表与当前选择(空串表示交给浏览器选默认设备)
@@ -398,6 +401,7 @@ export function useVoicePreview(
setError(null);
setMicWarning(null);
setMessages([]); // 新会话清空上一轮聊天记录
setCallEnded(false);
endedByServerRef.current = false;
setStatus("connecting");
@@ -630,9 +634,12 @@ export function useVoicePreview(
// 工作流:后端报告当前激活节点,交给画布高亮
onNodeActiveRef.current?.(msg.nodeId);
} else if (msg?.type === "call-ended") {
// 后端走到结束节点、正常收尾:随后的断开按正常结束处理,不报错
// 后端已播完结束节点的收尾语,即将排入 EndFrame。此时主动释放
// 浏览器侧资源,不依赖后续 WebRTC 状态事件是否及时到达。
endedByServerRef.current = true;
setCallEnded(true);
onNodeActiveRef.current?.(null);
disconnect();
}
} catch {
/* 非 JSON / 未知消息,忽略 */
@@ -689,7 +696,14 @@ export function useVoicePreview(
} finally {
startingRef.current = false;
}
}, [assistantId, applyOutputDevice, fail, closeOnRemoteEnd, refreshDevices]);
}, [
assistantId,
applyOutputDevice,
fail,
closeOnRemoteEnd,
disconnect,
refreshDevices,
]);
useEffect(() => {
if (status !== "connected") return;
@@ -826,6 +840,7 @@ export function useVoicePreview(
localStream,
remoteStream,
messages,
callEnded,
networkQuality,
audioInputs,
audioOutputs,