feat: add client close button
This commit is contained in:
parent
2f1745a0b5
commit
2a52095ce9
@ -1,4 +1,4 @@
|
|||||||
import { useState } from "react";
|
import { useRef, useState } from "react";
|
||||||
import ImageUpload from "./components/ImageUpload";
|
import ImageUpload from "./components/ImageUpload";
|
||||||
import CodePreview from "./components/CodePreview";
|
import CodePreview from "./components/CodePreview";
|
||||||
import Preview from "./components/Preview";
|
import Preview from "./components/Preview";
|
||||||
@ -17,7 +17,7 @@ import { Textarea } from "@/components/ui/textarea";
|
|||||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "./components/ui/tabs";
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from "./components/ui/tabs";
|
||||||
import CodeMirror from "./components/CodeMirror";
|
import CodeMirror from "./components/CodeMirror";
|
||||||
import SettingsDialog from "./components/SettingsDialog";
|
import SettingsDialog from "./components/SettingsDialog";
|
||||||
import { AppStatus, Settings } from "./types";
|
import { AppStatus, Settings, USER_CLOSE_WEB_SOCKET_CODE } from "./types";
|
||||||
import { IS_RUNNING_ON_CLOUD } from "./config";
|
import { IS_RUNNING_ON_CLOUD } from "./config";
|
||||||
import { PicoBadge } from "./components/PicoBadge";
|
import { PicoBadge } from "./components/PicoBadge";
|
||||||
import { OnboardingNote } from "./components/OnboardingNote";
|
import { OnboardingNote } from "./components/OnboardingNote";
|
||||||
@ -43,6 +43,7 @@ function App() {
|
|||||||
},
|
},
|
||||||
"setting"
|
"setting"
|
||||||
);
|
);
|
||||||
|
const wsRef = useRef<WebSocket>(null);
|
||||||
|
|
||||||
const downloadCode = () => {
|
const downloadCode = () => {
|
||||||
// Create a blob from the generated code
|
// Create a blob from the generated code
|
||||||
@ -69,6 +70,10 @@ function App() {
|
|||||||
setHistory([]);
|
setHistory([]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const stop = () => {
|
||||||
|
wsRef.current?.close?.(USER_CLOSE_WEB_SOCKET_CODE);
|
||||||
|
}
|
||||||
|
|
||||||
function doGenerateCode(params: CodeGenerationParams) {
|
function doGenerateCode(params: CodeGenerationParams) {
|
||||||
setExecutionConsole([]);
|
setExecutionConsole([]);
|
||||||
setAppState(AppStatus.CODING);
|
setAppState(AppStatus.CODING);
|
||||||
@ -77,6 +82,7 @@ function App() {
|
|||||||
const updatedParams = { ...params, ...settings };
|
const updatedParams = { ...params, ...settings };
|
||||||
|
|
||||||
generateCode(
|
generateCode(
|
||||||
|
wsRef,
|
||||||
updatedParams,
|
updatedParams,
|
||||||
(token) => setGeneratedCode((prev) => prev + token),
|
(token) => setGeneratedCode((prev) => prev + token),
|
||||||
(code) => setGeneratedCode(code),
|
(code) => setGeneratedCode(code),
|
||||||
@ -139,6 +145,14 @@ function App() {
|
|||||||
<Spinner />
|
<Spinner />
|
||||||
{executionConsole.slice(-1)[0]}
|
{executionConsole.slice(-1)[0]}
|
||||||
</div>
|
</div>
|
||||||
|
<div className="flex mt-4 w-full">
|
||||||
|
<Button
|
||||||
|
onClick={stop}
|
||||||
|
className="w-full"
|
||||||
|
>
|
||||||
|
Stop
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
<CodePreview code={generatedCode} />
|
<CodePreview code={generatedCode} />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@ -1,9 +1,12 @@
|
|||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { WS_BACKEND_URL } from "./config";
|
import { WS_BACKEND_URL } from "./config";
|
||||||
|
import { USER_CLOSE_WEB_SOCKET_CODE } from "./types";
|
||||||
|
|
||||||
const ERROR_MESSAGE =
|
const ERROR_MESSAGE =
|
||||||
"Error generating code. Check the Developer Console for details. Feel free to open a Github issue";
|
"Error generating code. Check the Developer Console for details. Feel free to open a Github issue";
|
||||||
|
|
||||||
|
const STOP_MESSAGE = "Code generation stopped";
|
||||||
|
|
||||||
export interface CodeGenerationParams {
|
export interface CodeGenerationParams {
|
||||||
generationType: "create" | "update";
|
generationType: "create" | "update";
|
||||||
image: string;
|
image: string;
|
||||||
@ -12,6 +15,7 @@ export interface CodeGenerationParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function generateCode(
|
export function generateCode(
|
||||||
|
wsRef: React.MutableRefObject<WebSocket | null>,
|
||||||
params: CodeGenerationParams,
|
params: CodeGenerationParams,
|
||||||
onChange: (chunk: string) => void,
|
onChange: (chunk: string) => void,
|
||||||
onSetCode: (code: string) => void,
|
onSetCode: (code: string) => void,
|
||||||
@ -22,6 +26,7 @@ export function generateCode(
|
|||||||
console.log("Connecting to backend @ ", wsUrl);
|
console.log("Connecting to backend @ ", wsUrl);
|
||||||
|
|
||||||
const ws = new WebSocket(wsUrl);
|
const ws = new WebSocket(wsUrl);
|
||||||
|
wsRef.current = ws;
|
||||||
|
|
||||||
ws.addEventListener("open", () => {
|
ws.addEventListener("open", () => {
|
||||||
ws.send(JSON.stringify(params));
|
ws.send(JSON.stringify(params));
|
||||||
@ -40,14 +45,16 @@ export function generateCode(
|
|||||||
toast.error(response.value);
|
toast.error(response.value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ws.addEventListener("close", (event) => {
|
ws.addEventListener("close", (event) => {
|
||||||
console.log("Connection closed", event.code, event.reason);
|
console.log("Connection closed", event.code, event.reason);
|
||||||
if (event.code != 1000) {
|
if (event.code === USER_CLOSE_WEB_SOCKET_CODE) {
|
||||||
|
toast.success(STOP_MESSAGE);
|
||||||
|
onComplete();
|
||||||
|
} else if (event.code === 1000) {
|
||||||
|
onComplete();
|
||||||
|
} else {
|
||||||
console.error("WebSocket error code", event);
|
console.error("WebSocket error code", event);
|
||||||
toast.error(ERROR_MESSAGE);
|
toast.error(ERROR_MESSAGE);
|
||||||
} else {
|
|
||||||
onComplete();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -10,3 +10,5 @@ export enum AppStatus {
|
|||||||
CODING = "CODING",
|
CODING = "CODING",
|
||||||
CODE_READY = "CODE_READY",
|
CODE_READY = "CODE_READY",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const USER_CLOSE_WEB_SOCKET_CODE = 4333;
|
||||||
Loading…
Reference in New Issue
Block a user