Skip to content

Commit

Permalink
after code generation cancellation, leave the app in a good state (re…
Browse files Browse the repository at this point in the history
…vert to last version or reset app if no last version)
  • Loading branch information
abi committed Dec 11, 2023
1 parent 89c716f commit 9f064c5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
24 changes: 20 additions & 4 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,21 @@ function App() {
setIsImportedFromCode(false);
};

const stop = () => {
const cancelCodeGeneration = () => {
wsRef.current?.close?.(USER_CLOSE_WEB_SOCKET_CODE);
// make sure stop can correct the state even if the websocket is already closed
setAppState(AppState.CODE_READY);
cancelCodeGenerationAndReset();
};

const cancelCodeGenerationAndReset = () => {
// When this is the first version, reset the entire app state
if (currentVersion === null) {
reset();
} else {
// Otherwise, revert to the last version
setGeneratedCode(appHistory[currentVersion].code);
setAppState(AppState.CODE_READY);
}
};

function doGenerateCode(
Expand Down Expand Up @@ -189,6 +200,11 @@ function App() {
}
},
(line) => setExecutionConsole((prev) => [...prev, line]),
// On cancel
() => {
cancelCodeGenerationAndReset();
},
// On complete
() => {
setAppState(AppState.CODE_READY);
}
Expand Down Expand Up @@ -343,10 +359,10 @@ function App() {
</div>
<div className="flex mt-4 w-full">
<Button
onClick={stop}
onClick={cancelCodeGeneration}
className="w-full dark:text-white dark:bg-gray-700"
>
Stop
Cancel
</Button>
</div>
<CodePreview code={generatedCode} />
Expand Down
10 changes: 7 additions & 3 deletions frontend/src/generateCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { FullGenerationSettings } from "./types";
const ERROR_MESSAGE =
"Error generating code. Check the Developer Console AND the backend logs for details. Feel free to open a Github issue.";

const STOP_MESSAGE = "Code generation stopped";
const CANCEL_MESSAGE = "Code generation cancelled";

export function generateCode(
wsRef: React.MutableRefObject<WebSocket | null>,
params: FullGenerationSettings,
onChange: (chunk: string) => void,
onSetCode: (code: string) => void,
onStatusUpdate: (status: string) => void,
onCancel: () => void,
onComplete: () => void
) {
const wsUrl = `${WS_BACKEND_URL}/generate-code`;
Expand All @@ -39,15 +40,18 @@ export function generateCode(
toast.error(response.value);
}
});

ws.addEventListener("close", (event) => {
console.log("Connection closed", event.code, event.reason);
if (event.code === USER_CLOSE_WEB_SOCKET_CODE) {
toast.success(STOP_MESSAGE);
toast.success(CANCEL_MESSAGE);
onCancel();
} else if (event.code !== 1000) {
console.error("WebSocket error code", event);
toast.error(ERROR_MESSAGE);
} else {
onComplete();
}
onComplete();
});

ws.addEventListener("error", (error) => {
Expand Down

0 comments on commit 9f064c5

Please sign in to comment.