fix: Terminal阻塞修复—cmd state+onPress+fetch /tools/exec

- IIFE内useState: cmd/out/running
- Run按钮→POST /tools/exec→显示stdout/stderr
- onSubmitEditing键盘发送
- loading disabled反馈
This commit is contained in:
LukeMackin
2026-07-22 17:53:44 +08:00
parent c407f6c483
commit 02967ae5c3

View File

@@ -130,25 +130,36 @@ export default function App() {
{/* ── Terminal Tab ── */} {/* ── Terminal Tab ── */}
{tab === 'terminal' && ( {tab === 'terminal' && (
(() => {
const [cmd, setCmd] = useState('');
const [out, setOut] = useState('Execute remote commands via Gateway MCP.\nType a shell command and press Execute.\n');
const [running, setRunning] = useState(false);
const runCmd = async () => {
if (!cmd.trim() || running) return;
const c = cmd.trim(); setCmd(''); setRunning(true);
setOut(o => o + `$ ${c}\n`);
try {
const r = await fetch(`${gatewayUrl}/tools/exec`, { method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({command:c}), signal:AbortSignal.timeout(30000) });
if (!r.ok) throw new Error(`HTTP ${r.status}`);
const d = await r.json() as any;
setOut(o => o + (d.stdout||d.stderr||JSON.stringify(d)) + '\n');
} catch(e:any) { setOut(o => o + `Error: ${e.message}\n`); }
finally { setRunning(false); }
};
return (
<View style={styles.chatContainer}> <View style={styles.chatContainer}>
<View style={styles.termOutput}> <ScrollView style={styles.termOutput}>
<ScrollView> <Text style={styles.termText} selectable>{out}</Text>
<Text style={styles.termText} selectable>Execute remote commands via Gateway MCP.{'\n'}Type a shell command and press Execute.{'\n'}</Text>
</ScrollView> </ScrollView>
</View>
<View style={styles.inputBar}> <View style={styles.inputBar}>
<TextInput <TextInput style={styles.chatInput} value={cmd} onChangeText={setCmd} placeholder="ls -la /home" placeholderTextColor="#475569" autoCapitalize="none" autoCorrect={false} onSubmitEditing={runCmd} returnKeyType="send" />
style={styles.chatInput} <TouchableOpacity style={[styles.sendBtn, running && styles.sendBtnDisabled]} onPress={runCmd} disabled={running}>
placeholder="ls -la /home" <Text style={styles.sendBtnText}>{running ? '...' : 'Run'}</Text>
placeholderTextColor="#475569"
autoCapitalize="none"
autoCorrect={false}
/>
<TouchableOpacity style={styles.sendBtn}>
<Text style={styles.sendBtnText}>Run</Text>
</TouchableOpacity> </TouchableOpacity>
</View> </View>
</View> </View>
);
})()
)} )}
{/* ── Settings Tab ── */} {/* ── Settings Tab ── */}