"use client" import Editor, { OnMount } from "@monaco-editor/react" import monaco from "monaco-editor" import { useEffect, useRef, useState } from "react" // import theme from "./theme.json" import { ResizableHandle, ResizablePanel, ResizablePanelGroup, } from "@/components/ui/resizable" import { ChevronLeft, ChevronRight, RotateCw, TerminalSquare, } from "lucide-react" import Tab from "../ui/tab" import Sidebar from "./sidebar" import { useClerk } from "@clerk/nextjs" import { TFile, TFolder } from "./sidebar/types" import { io } from "socket.io-client" export default function CodeEditor({ userId, sandboxId, }: { userId: string sandboxId: string }) { // const editorRef = useRef(null) // const handleEditorMount: OnMount = (editor, monaco) => { // editorRef.current = editor // } const [files, setFiles] = useState<(TFolder | TFile)[]>([]) const socket = io( `http://localhost:4000?userId=${userId}&sandboxId=${sandboxId}` ) // connection/disconnection effect useEffect(() => { socket.connect() return () => { socket.disconnect() } }, []) // event listener effect useEffect(() => { function onLoadedEvent(files: (TFolder | TFile)[]) { setFiles(files) } socket.on("loaded", onLoadedEvent) return () => { socket.off("loaded", onLoadedEvent) } }, []) // use the dependency array! const clerk = useClerk() const [tabs, setTabs] = useState([]) const [activeId, setActiveId] = useState(null) const selectFile = (tab: TFile) => { setTabs((prev) => { const exists = prev.find((t) => t.id === tab.id) if (exists) { setActiveId(exists.id) return prev } return [...prev, tab] }) setActiveId(tab.id) } const closeTab = (tab: TFile) => { const numTabs = tabs.length const index = tabs.findIndex((t) => t.id === tab.id) setActiveId((prev) => { const next = prev === tab.id ? numTabs === 1 ? null : index < numTabs - 1 ? tabs[index + 1].id : tabs[index - 1].id : prev return next }) setTabs((prev) => prev.filter((t) => t.id !== tab.id)) } return ( <>
{tabs.map((tab) => ( setActiveId(tab.id)} onClose={() => closeTab(tab)} > {tab.name} ))}
{activeId === null ? ( <>
No file selected.
) : clerk.loaded ? ( ) : null}
Preview
Node Console
) }