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:
@@ -130,25 +130,36 @@ export default function App() {
|
||||
|
||||
{/* ── Terminal Tab ── */}
|
||||
{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.termOutput}>
|
||||
<ScrollView>
|
||||
<Text style={styles.termText} selectable>Execute remote commands via Gateway MCP.{'\n'}Type a shell command and press Execute.{'\n'}</Text>
|
||||
<ScrollView style={styles.termOutput}>
|
||||
<Text style={styles.termText} selectable>{out}</Text>
|
||||
</ScrollView>
|
||||
</View>
|
||||
<View style={styles.inputBar}>
|
||||
<TextInput
|
||||
style={styles.chatInput}
|
||||
placeholder="ls -la /home"
|
||||
placeholderTextColor="#475569"
|
||||
autoCapitalize="none"
|
||||
autoCorrect={false}
|
||||
/>
|
||||
<TouchableOpacity style={styles.sendBtn}>
|
||||
<Text style={styles.sendBtnText}>Run</Text>
|
||||
<TextInput style={styles.chatInput} value={cmd} onChangeText={setCmd} placeholder="ls -la /home" placeholderTextColor="#475569" autoCapitalize="none" autoCorrect={false} onSubmitEditing={runCmd} returnKeyType="send" />
|
||||
<TouchableOpacity style={[styles.sendBtn, running && styles.sendBtnDisabled]} onPress={runCmd} disabled={running}>
|
||||
<Text style={styles.sendBtnText}>{running ? '...' : 'Run'}</Text>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
</View>
|
||||
);
|
||||
})()
|
||||
)}
|
||||
|
||||
{/* ── Settings Tab ── */}
|
||||
|
||||
Reference in New Issue
Block a user