feat: enhance terminal input handling and improve WebSocket connection safety
This commit is contained in:
@@ -175,21 +175,22 @@ const CodeChallenge = () => {
|
||||
const handleTerminalInput = (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (!terminalInput.trim()) {
|
||||
return;
|
||||
}
|
||||
// Allow empty input to be sent (user might press Enter on purpose)
|
||||
// But display it properly
|
||||
const inputValue = terminalInput;
|
||||
|
||||
// Display the input in terminal
|
||||
setTerminalOutput(prev => [
|
||||
...prev,
|
||||
{ type: 'input', content: terminalInput }
|
||||
{ type: 'input', content: inputValue || '(empty)' }
|
||||
]);
|
||||
|
||||
// If socket is available, send input to backend
|
||||
// Note: backend will add the newline, so we don't add it here
|
||||
if (activeSocket && activeSocket.readyState === WebSocket.OPEN) {
|
||||
activeSocket.send(JSON.stringify({
|
||||
type: 'input',
|
||||
content: terminalInput + '\n'
|
||||
content: inputValue
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -471,17 +472,13 @@ const CodeChallenge = () => {
|
||||
// Submit code to the backend
|
||||
const apiUrl = import.meta.env.VITE_API_URL || 'http://localhost:8080';
|
||||
|
||||
// Ensure code has actual newlines, not escaped \n strings
|
||||
// This handles cases where code might have literal "\n" instead of newlines
|
||||
const processedCode = code.replace(/\\n/g, '\n').replace(/\\t/g, '\t');
|
||||
|
||||
const response = await fetch(`${apiUrl}/api/submit`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
code: processedCode,
|
||||
code: code,
|
||||
language: getLanguageIdentifier(language),
|
||||
input: '',
|
||||
}),
|
||||
@@ -971,6 +968,13 @@ const CodeChallenge = () => {
|
||||
minimap: { enabled: false },
|
||||
scrollBeyondLastLine: false,
|
||||
automaticLayout: true,
|
||||
wordWrap: 'off',
|
||||
wrappingIndent: 'none',
|
||||
formatOnPaste: false,
|
||||
formatOnType: false,
|
||||
acceptSuggestionOnEnter: 'off',
|
||||
quickSuggestions: false,
|
||||
suggestOnTriggerCharacters: false,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user