Compare commits
4 Commits
fix-file-l
...
run-button
Author | SHA1 | Date | |
---|---|---|---|
af45b6196c | |||
a15c1f15f5 | |||
f1a65106b0 | |||
5726cecb22 |
@ -143,8 +143,6 @@ io.on("connection", async (socket) => {
|
|||||||
isOwner: boolean;
|
isOwner: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log("user:",data)
|
|
||||||
|
|
||||||
if (data.isOwner) {
|
if (data.isOwner) {
|
||||||
isOwnerConnected = true;
|
isOwnerConnected = true;
|
||||||
connections[data.sandboxId] = (connections[data.sandboxId] ?? 0) + 1;
|
connections[data.sandboxId] = (connections[data.sandboxId] ?? 0) + 1;
|
||||||
@ -185,7 +183,6 @@ io.on("connection", async (socket) => {
|
|||||||
fixPermissions();
|
fixPermissions();
|
||||||
|
|
||||||
socket.emit("loaded", sandboxFiles.files);
|
socket.emit("loaded", sandboxFiles.files);
|
||||||
console.log("files got", sandboxFiles.files)
|
|
||||||
|
|
||||||
socket.on("getFile", (fileId: string, callback) => {
|
socket.on("getFile", (fileId: string, callback) => {
|
||||||
console.log(fileId);
|
console.log(fileId);
|
||||||
|
@ -7,8 +7,7 @@ import { ClerkProvider } from "@clerk/nextjs"
|
|||||||
import { Toaster } from "@/components/ui/sonner"
|
import { Toaster } from "@/components/ui/sonner"
|
||||||
import { Analytics } from "@vercel/analytics/react"
|
import { Analytics } from "@vercel/analytics/react"
|
||||||
import { TerminalProvider } from '@/context/TerminalContext';
|
import { TerminalProvider } from '@/context/TerminalContext';
|
||||||
import { PreviewProvider } from "@/context/PreviewContext";
|
import { PreviewProvider } from "@/context/PreviewContext"
|
||||||
import { SocketProvider } from '@/context/SocketContext'
|
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
title: "Sandbox",
|
title: "Sandbox",
|
||||||
@ -16,7 +15,7 @@ export const metadata: Metadata = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function RootLayout({
|
export default function RootLayout({
|
||||||
children
|
children,
|
||||||
}: Readonly<{
|
}: Readonly<{
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
}>) {
|
}>) {
|
||||||
@ -30,13 +29,11 @@ export default function RootLayout({
|
|||||||
forcedTheme="dark"
|
forcedTheme="dark"
|
||||||
disableTransitionOnChange
|
disableTransitionOnChange
|
||||||
>
|
>
|
||||||
<SocketProvider>
|
|
||||||
<PreviewProvider>
|
<PreviewProvider>
|
||||||
<TerminalProvider>
|
<TerminalProvider>
|
||||||
{children}
|
{children}
|
||||||
</TerminalProvider>
|
</TerminalProvider>
|
||||||
</PreviewProvider>
|
</PreviewProvider>
|
||||||
</SocketProvider>
|
|
||||||
<Analytics />
|
<Analytics />
|
||||||
<Toaster position="bottom-left" richColors />
|
<Toaster position="bottom-left" richColors />
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
|
@ -50,9 +50,9 @@ export default function Dashboard({
|
|||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// if (!sandboxes) {
|
if (!sandboxes) {
|
||||||
router.refresh() // fix: update the dashboard to show the new project
|
router.refresh()
|
||||||
// }
|
}
|
||||||
}, [sandboxes])
|
}, [sandboxes])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
import { SetStateAction, useCallback, useEffect, useRef, useState } from "react"
|
import { SetStateAction, useCallback, useEffect, useRef, useState } from "react"
|
||||||
import monaco from "monaco-editor"
|
import monaco from "monaco-editor"
|
||||||
import Editor, { BeforeMount, OnMount } from "@monaco-editor/react"
|
import Editor, { BeforeMount, OnMount } from "@monaco-editor/react"
|
||||||
|
import { Socket, io } from "socket.io-client"
|
||||||
import { toast } from "sonner"
|
import { toast } from "sonner"
|
||||||
import { useClerk } from "@clerk/nextjs"
|
import { useClerk } from "@clerk/nextjs"
|
||||||
|
|
||||||
@ -31,7 +32,7 @@ import PreviewWindow from "./preview"
|
|||||||
import Terminals from "./terminals"
|
import Terminals from "./terminals"
|
||||||
import { ImperativePanelHandle } from "react-resizable-panels"
|
import { ImperativePanelHandle } from "react-resizable-panels"
|
||||||
import { PreviewProvider, usePreview } from '@/context/PreviewContext';
|
import { PreviewProvider, usePreview } from '@/context/PreviewContext';
|
||||||
import { useSocket } from "@/context/SocketContext"
|
import { useTerminal } from '@/context/TerminalContext';
|
||||||
|
|
||||||
export default function CodeEditor({
|
export default function CodeEditor({
|
||||||
userData,
|
userData,
|
||||||
@ -40,22 +41,24 @@ export default function CodeEditor({
|
|||||||
userData: User
|
userData: User
|
||||||
sandboxData: Sandbox
|
sandboxData: Sandbox
|
||||||
}) {
|
}) {
|
||||||
|
const socketRef = useRef<Socket | null>(null);
|
||||||
|
|
||||||
//SocketContext functions and effects
|
// Initialize socket connection if it doesn't exist
|
||||||
const { socket, setUserAndSandboxId } = useSocket();
|
if (!socketRef.current) {
|
||||||
|
socketRef.current = io(
|
||||||
|
`${process.env.NEXT_PUBLIC_SERVER_URL}?userId=${userData.id}&sandboxId=${sandboxData.id}`,
|
||||||
|
{
|
||||||
|
timeout: 2000,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Terminalcontext functionsand effects
|
||||||
|
const { setUserAndSandboxId } = useTerminal();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log('Effect triggered:', { socket, userData, sandboxData });
|
setUserAndSandboxId(userData.id, sandboxData.id);
|
||||||
// Ensure userData.id and sandboxData.id are available before attempting to connect
|
}, [userData.id, sandboxData.id, setUserAndSandboxId]);
|
||||||
if (userData.id && sandboxData.id) {
|
|
||||||
// Check if the socket is not initialized or not connected
|
|
||||||
if (!socket || (socket && !socket.connected)) {
|
|
||||||
// Initialize socket connection
|
|
||||||
console.log('Initializing socket...');
|
|
||||||
setUserAndSandboxId(userData.id, sandboxData.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [socket, userData.id, sandboxData.id, setUserAndSandboxId]);
|
|
||||||
|
|
||||||
//Preview Button state
|
//Preview Button state
|
||||||
const [isPreviewCollapsed, setIsPreviewCollapsed] = useState(true)
|
const [isPreviewCollapsed, setIsPreviewCollapsed] = useState(true)
|
||||||
@ -117,7 +120,7 @@ export default function CodeEditor({
|
|||||||
const room = useRoom()
|
const room = useRoom()
|
||||||
const [provider, setProvider] = useState<TypedLiveblocksProvider>()
|
const [provider, setProvider] = useState<TypedLiveblocksProvider>()
|
||||||
const userInfo = useSelf((me) => me.info)
|
const userInfo = useSelf((me) => me.info)
|
||||||
|
|
||||||
// Liveblocks providers map to prevent reinitializing providers
|
// Liveblocks providers map to prevent reinitializing providers
|
||||||
type ProviderData = {
|
type ProviderData = {
|
||||||
provider: LiveblocksProvider<never, never, never, never>;
|
provider: LiveblocksProvider<never, never, never, never>;
|
||||||
@ -330,9 +333,9 @@ export default function CodeEditor({
|
|||||||
);
|
);
|
||||||
console.log(`Saving file...${activeFileId}`);
|
console.log(`Saving file...${activeFileId}`);
|
||||||
console.log(`Saving file...${value}`);
|
console.log(`Saving file...${value}`);
|
||||||
socket?.emit("saveFile", activeFileId, value);
|
socketRef.current?.emit("saveFile", activeFileId, value);
|
||||||
}, Number(process.env.FILE_SAVE_DEBOUNCE_DELAY) || 1000),
|
}, Number(process.env.FILE_SAVE_DEBOUNCE_DELAY) || 1000),
|
||||||
[socket]
|
[socketRef]
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -429,9 +432,9 @@ export default function CodeEditor({
|
|||||||
|
|
||||||
// Connection/disconnection effect
|
// Connection/disconnection effect
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
socket?.connect()
|
socketRef.current?.connect()
|
||||||
return () => {
|
return () => {
|
||||||
socket?.disconnect()
|
socketRef.current?.disconnect()
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
@ -445,7 +448,6 @@ export default function CodeEditor({
|
|||||||
|
|
||||||
const onLoadedEvent = (files: (TFolder | TFile)[]) => {
|
const onLoadedEvent = (files: (TFolder | TFile)[]) => {
|
||||||
setFiles(files)
|
setFiles(files)
|
||||||
console.log("loaded", files)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const onError = (message: string) => {
|
const onError = (message: string) => {
|
||||||
@ -467,25 +469,25 @@ export default function CodeEditor({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
socket?.on("connect", onConnect)
|
socketRef.current?.on("connect", onConnect)
|
||||||
socket?.on("disconnect", onDisconnect)
|
socketRef.current?.on("disconnect", onDisconnect)
|
||||||
socket?.on("loaded", onLoadedEvent)
|
socketRef.current?.on("loaded", onLoadedEvent)
|
||||||
socket?.on("error", onError)
|
socketRef.current?.on("error", onError)
|
||||||
socket?.on("terminalResponse", onTerminalResponse)
|
socketRef.current?.on("terminalResponse", onTerminalResponse)
|
||||||
socket?.on("disableAccess", onDisableAccess)
|
socketRef.current?.on("disableAccess", onDisableAccess)
|
||||||
socket?.on("previewURL", loadPreviewURL)
|
socketRef.current?.on("previewURL", loadPreviewURL)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
socket?.off("connect", onConnect)
|
socketRef.current?.off("connect", onConnect)
|
||||||
socket?.off("disconnect", onDisconnect)
|
socketRef.current?.off("disconnect", onDisconnect)
|
||||||
socket?.off("loaded", onLoadedEvent)
|
socketRef.current?.off("loaded", onLoadedEvent)
|
||||||
socket?.off("error", onError)
|
socketRef.current?.off("error", onError)
|
||||||
socket?.off("terminalResponse", onTerminalResponse)
|
socketRef.current?.off("terminalResponse", onTerminalResponse)
|
||||||
socket?.off("disableAccess", onDisableAccess)
|
socketRef.current?.off("disableAccess", onDisableAccess)
|
||||||
socket?.off("previewURL", loadPreviewURL)
|
socketRef.current?.off("previewURL", loadPreviewURL)
|
||||||
}
|
}
|
||||||
// }, []);
|
// }, []);
|
||||||
}, [socket, terminals, setTerminals, setFiles, toast, setDisableAccess, isOwner, loadPreviewURL]);
|
}, [terminals])
|
||||||
|
|
||||||
// Helper functions for tabs:
|
// Helper functions for tabs:
|
||||||
|
|
||||||
@ -497,7 +499,7 @@ export default function CodeEditor({
|
|||||||
// Debounced function to get file content
|
// Debounced function to get file content
|
||||||
const debouncedGetFile = useCallback(
|
const debouncedGetFile = useCallback(
|
||||||
debounce((tabId, callback) => {
|
debounce((tabId, callback) => {
|
||||||
socket?.emit('getFile', tabId, callback);
|
socketRef.current?.emit('getFile', tabId, callback);
|
||||||
}, 300), // 300ms debounce delay, adjust as needed
|
}, 300), // 300ms debounce delay, adjust as needed
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
@ -601,7 +603,7 @@ export default function CodeEditor({
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
socket?.emit("renameFile", id, newName)
|
socketRef.current?.emit("renameFile", id, newName)
|
||||||
setTabs((prev) =>
|
setTabs((prev) =>
|
||||||
prev.map((tab) => (tab.id === id ? { ...tab, name: newName } : tab))
|
prev.map((tab) => (tab.id === id ? { ...tab, name: newName } : tab))
|
||||||
)
|
)
|
||||||
@ -610,7 +612,7 @@ export default function CodeEditor({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const handleDeleteFile = (file: TFile) => {
|
const handleDeleteFile = (file: TFile) => {
|
||||||
socket?.emit("deleteFile", file.id, (response: (TFolder | TFile)[]) => {
|
socketRef.current?.emit("deleteFile", file.id, (response: (TFolder | TFile)[]) => {
|
||||||
setFiles(response)
|
setFiles(response)
|
||||||
})
|
})
|
||||||
closeTab(file.id)
|
closeTab(file.id)
|
||||||
@ -620,11 +622,11 @@ export default function CodeEditor({
|
|||||||
setDeletingFolderId(folder.id)
|
setDeletingFolderId(folder.id)
|
||||||
console.log("deleting folder", folder.id)
|
console.log("deleting folder", folder.id)
|
||||||
|
|
||||||
socket?.emit("getFolder", folder.id, (response: string[]) =>
|
socketRef.current?.emit("getFolder", folder.id, (response: string[]) =>
|
||||||
closeTabs(response)
|
closeTabs(response)
|
||||||
)
|
)
|
||||||
|
|
||||||
socket?.emit("deleteFolder", folder.id, (response: (TFolder | TFile)[]) => {
|
socketRef.current?.emit("deleteFolder", folder.id, (response: (TFolder | TFile)[]) => {
|
||||||
setFiles(response)
|
setFiles(response)
|
||||||
setDeletingFolderId("")
|
setDeletingFolderId("")
|
||||||
})
|
})
|
||||||
@ -652,7 +654,7 @@ export default function CodeEditor({
|
|||||||
{generate.show && ai ? (
|
{generate.show && ai ? (
|
||||||
<GenerateInput
|
<GenerateInput
|
||||||
user={userData}
|
user={userData}
|
||||||
socket={socket!}
|
socket={socketRef.current}
|
||||||
width={generate.width - 90}
|
width={generate.width - 90}
|
||||||
data={{
|
data={{
|
||||||
fileName: tabs.find((t) => t.id === activeFileId)?.name ?? "",
|
fileName: tabs.find((t) => t.id === activeFileId)?.name ?? "",
|
||||||
@ -712,7 +714,7 @@ export default function CodeEditor({
|
|||||||
handleRename={handleRename}
|
handleRename={handleRename}
|
||||||
handleDeleteFile={handleDeleteFile}
|
handleDeleteFile={handleDeleteFile}
|
||||||
handleDeleteFolder={handleDeleteFolder}
|
handleDeleteFolder={handleDeleteFolder}
|
||||||
socket={socket!}
|
socket={socketRef.current}
|
||||||
setFiles={setFiles}
|
setFiles={setFiles}
|
||||||
addNew={(name, type) => addNew(name, type, setFiles, sandboxData)}
|
addNew={(name, type) => addNew(name, type, setFiles, sandboxData)}
|
||||||
deletingFolderId={deletingFolderId}
|
deletingFolderId={deletingFolderId}
|
||||||
@ -830,7 +832,7 @@ export default function CodeEditor({
|
|||||||
open={() => {
|
open={() => {
|
||||||
usePreview().previewPanelRef.current?.expand()
|
usePreview().previewPanelRef.current?.expand()
|
||||||
setIsPreviewCollapsed(false)
|
setIsPreviewCollapsed(false)
|
||||||
}} collapsed={isPreviewCollapsed} src={previewURL} ref={previewWindowRef} />
|
} } collapsed={isPreviewCollapsed} src={previewURL} ref={previewWindowRef} />
|
||||||
</ResizablePanel>
|
</ResizablePanel>
|
||||||
<ResizableHandle />
|
<ResizableHandle />
|
||||||
<ResizablePanel
|
<ResizablePanel
|
||||||
|
@ -44,7 +44,7 @@ export default function RunButtonModal({
|
|||||||
"pip install -r requirements.txt && streamlit run main.py --server.runOnSave true"
|
"pip install -r requirements.txt && streamlit run main.py --server.runOnSave true"
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
createNewTerminal("yarn install && yarn dev");
|
createNewTerminal("yarn install && yarn dev");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
toast.error("You reached the maximum # of terminals.");
|
toast.error("You reached the maximum # of terminals.");
|
||||||
|
@ -8,15 +8,12 @@ import { toast } from "sonner";
|
|||||||
import EditorTerminal from "./terminal";
|
import EditorTerminal from "./terminal";
|
||||||
import { useTerminal } from "@/context/TerminalContext";
|
import { useTerminal } from "@/context/TerminalContext";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { useSocket } from "@/context/SocketContext"
|
|
||||||
|
|
||||||
export default function Terminals() {
|
export default function Terminals() {
|
||||||
|
|
||||||
const { socket } = useSocket();
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
terminals,
|
terminals,
|
||||||
setTerminals,
|
setTerminals,
|
||||||
|
socket,
|
||||||
createNewTerminal,
|
createNewTerminal,
|
||||||
closeTerminal,
|
closeTerminal,
|
||||||
activeTerminalId,
|
activeTerminalId,
|
||||||
|
@ -1,63 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import React, { createContext, useContext, useEffect, useState } from 'react';
|
|
||||||
import { io, Socket } from 'socket.io-client';
|
|
||||||
|
|
||||||
interface SocketContextType {
|
|
||||||
socket: Socket | null;
|
|
||||||
setUserAndSandboxId: (userId: string, sandboxId: string) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
const SocketContext = createContext<SocketContextType | undefined>(undefined);
|
|
||||||
|
|
||||||
export const SocketProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
|
||||||
const [socket, setSocket] = useState<Socket | null>(null);
|
|
||||||
const [userId, setUserId] = useState<string | null>(null);
|
|
||||||
const [sandboxId, setSandboxId] = useState<string | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (userId && sandboxId) {
|
|
||||||
console.log("Initializing socket connection...");
|
|
||||||
const newSocket = io(`${process.env.NEXT_PUBLIC_SERVER_URL}?userId=${userId}&sandboxId=${sandboxId}`);
|
|
||||||
console.log("Socket instance:", newSocket);
|
|
||||||
setSocket(newSocket);
|
|
||||||
|
|
||||||
newSocket.on('connect', () => {
|
|
||||||
console.log("Socket connected:", newSocket.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
newSocket.on('disconnect', () => {
|
|
||||||
console.log("Socket disconnected");
|
|
||||||
});
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
console.log("Disconnecting socket...");
|
|
||||||
newSocket.disconnect();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}, [userId, sandboxId]);
|
|
||||||
|
|
||||||
const setUserAndSandboxId = (newUserId: string, newSandboxId: string) => {
|
|
||||||
setUserId(newUserId);
|
|
||||||
setSandboxId(newSandboxId);
|
|
||||||
};
|
|
||||||
|
|
||||||
const value = {
|
|
||||||
socket,
|
|
||||||
setUserAndSandboxId,
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<SocketContext.Provider value={ value }>
|
|
||||||
{children}
|
|
||||||
</SocketContext.Provider>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const useSocket = (): SocketContextType => {
|
|
||||||
const context = useContext(SocketContext);
|
|
||||||
if (!context) {
|
|
||||||
throw new Error('useSocket must be used within a SocketProvider');
|
|
||||||
}
|
|
||||||
return context;
|
|
||||||
};
|
|
@ -1,11 +1,12 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import React, { createContext, useContext, useState } from 'react';
|
import React, { createContext, useContext, useState, useEffect } from 'react';
|
||||||
|
import { io, Socket } from 'socket.io-client';
|
||||||
import { Terminal } from '@xterm/xterm';
|
import { Terminal } from '@xterm/xterm';
|
||||||
import { createTerminal as createTerminalHelper, closeTerminal as closeTerminalHelper } from '@/lib/terminal';
|
import { createTerminal as createTerminalHelper, closeTerminal as closeTerminalHelper } from '@/lib/terminal';
|
||||||
import { useSocket } from '@/context/SocketContext';
|
|
||||||
|
|
||||||
interface TerminalContextType {
|
interface TerminalContextType {
|
||||||
|
socket: Socket | null;
|
||||||
terminals: { id: string; terminal: Terminal | null }[];
|
terminals: { id: string; terminal: Terminal | null }[];
|
||||||
setTerminals: React.Dispatch<React.SetStateAction<{ id: string; terminal: Terminal | null }[]>>;
|
setTerminals: React.Dispatch<React.SetStateAction<{ id: string; terminal: Terminal | null }[]>>;
|
||||||
activeTerminalId: string;
|
activeTerminalId: string;
|
||||||
@ -14,16 +15,41 @@ interface TerminalContextType {
|
|||||||
setCreatingTerminal: React.Dispatch<React.SetStateAction<boolean>>;
|
setCreatingTerminal: React.Dispatch<React.SetStateAction<boolean>>;
|
||||||
createNewTerminal: (command?: string) => Promise<void>;
|
createNewTerminal: (command?: string) => Promise<void>;
|
||||||
closeTerminal: (id: string) => void;
|
closeTerminal: (id: string) => void;
|
||||||
|
setUserAndSandboxId: (userId: string, sandboxId: string) => void;
|
||||||
deploy: (callback: () => void) => void;
|
deploy: (callback: () => void) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const TerminalContext = createContext<TerminalContextType | undefined>(undefined);
|
const TerminalContext = createContext<TerminalContextType | undefined>(undefined);
|
||||||
|
|
||||||
export const TerminalProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
export const TerminalProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||||
const { socket } = useSocket();
|
const [socket, setSocket] = useState<Socket | null>(null);
|
||||||
const [terminals, setTerminals] = useState<{ id: string; terminal: Terminal | null }[]>([]);
|
const [terminals, setTerminals] = useState<{ id: string; terminal: Terminal | null }[]>([]);
|
||||||
const [activeTerminalId, setActiveTerminalId] = useState<string>('');
|
const [activeTerminalId, setActiveTerminalId] = useState<string>('');
|
||||||
const [creatingTerminal, setCreatingTerminal] = useState<boolean>(false);
|
const [creatingTerminal, setCreatingTerminal] = useState<boolean>(false);
|
||||||
|
const [userId, setUserId] = useState<string | null>(null);
|
||||||
|
const [sandboxId, setSandboxId] = useState<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (userId && sandboxId) {
|
||||||
|
console.log("Initializing socket connection...");
|
||||||
|
const newSocket = io(`${process.env.NEXT_PUBLIC_SERVER_URL}?userId=${userId}&sandboxId=${sandboxId}`);
|
||||||
|
console.log("Socket instance:", newSocket);
|
||||||
|
setSocket(newSocket);
|
||||||
|
|
||||||
|
newSocket.on('connect', () => {
|
||||||
|
console.log("Socket connected:", newSocket.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
newSocket.on('disconnect', () => {
|
||||||
|
console.log("Socket disconnected");
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
console.log("Disconnecting socket...");
|
||||||
|
newSocket.disconnect();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, [userId, sandboxId]);
|
||||||
|
|
||||||
const createNewTerminal = async (command?: string): Promise<void> => {
|
const createNewTerminal = async (command?: string): Promise<void> => {
|
||||||
if (!socket) return;
|
if (!socket) return;
|
||||||
@ -59,6 +85,11 @@ export const TerminalProvider: React.FC<{ children: React.ReactNode }> = ({ chil
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const setUserAndSandboxId = (newUserId: string, newSandboxId: string) => {
|
||||||
|
setUserId(newUserId);
|
||||||
|
setSandboxId(newSandboxId);
|
||||||
|
};
|
||||||
|
|
||||||
const deploy = (callback: () => void) => {
|
const deploy = (callback: () => void) => {
|
||||||
if (!socket) console.error("Couldn't deploy: No socket");
|
if (!socket) console.error("Couldn't deploy: No socket");
|
||||||
console.log("Deploying...")
|
console.log("Deploying...")
|
||||||
@ -68,6 +99,7 @@ export const TerminalProvider: React.FC<{ children: React.ReactNode }> = ({ chil
|
|||||||
}
|
}
|
||||||
|
|
||||||
const value = {
|
const value = {
|
||||||
|
socket,
|
||||||
terminals,
|
terminals,
|
||||||
setTerminals,
|
setTerminals,
|
||||||
activeTerminalId,
|
activeTerminalId,
|
||||||
@ -76,6 +108,7 @@ export const TerminalProvider: React.FC<{ children: React.ReactNode }> = ({ chil
|
|||||||
setCreatingTerminal,
|
setCreatingTerminal,
|
||||||
createNewTerminal,
|
createNewTerminal,
|
||||||
closeTerminal,
|
closeTerminal,
|
||||||
|
setUserAndSandboxId,
|
||||||
deploy
|
deploy
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user