"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, FileJson, RotateCw, TerminalSquare, } from "lucide-react" import Tab from "../ui/tab" import Sidebar from "./sidebar" import { useClerk } from "@clerk/nextjs" import { TFile, TFileData, TFolder, TTab } from "./sidebar/types" import { io } from "socket.io-client" import { processFileType } from "@/lib/utils" import { toast } from "sonner" 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 [editorLanguage, setEditorLanguage] = useState("plaintext") const [activeFile, setActiveFile] = useState(null) const [tabs, setTabs] = useState([]) const [activeId, setActiveId] = useState(null) const socket = io( `http://localhost:4000?userId=${userId}&sandboxId=${sandboxId}` ) // connection/disconnection effect useEffect(() => { console.log("connecting") socket.connect() return () => { socket.disconnect() } }, []) // event listener effect useEffect(() => { function onLoadedEvent(files: (TFolder | TFile)[]) { console.log("onLoadedEvent") setFiles(files) } function onFileEvent() { console.log("onFileEvent") // setActiveFile(file) } socket.on("loaded", onLoadedEvent) socket.on("file", onFileEvent) return () => { socket.off("loaded", onLoadedEvent) socket.off("file", onFileEvent) } }, []) const clerk = useClerk() const selectFile = (tab: TTab) => { setTabs((prev) => { const exists = prev.find((t) => t.id === tab.id) if (exists) { // console.log("exists") setActiveId(exists.id) return prev } return [...prev, tab] }) socket.emit("getFile", tab.id, (response: string) => { setActiveFile(response) }) setEditorLanguage(processFileType(tab.name)) setActiveId(tab.id) } const closeTab = (tab: TFile) => { const numTabs = tabs.length const index = tabs.findIndex((t) => t.id === tab.id) const nextId = activeId === tab.id ? numTabs === 1 ? null : index < numTabs - 1 ? tabs[index + 1].id : tabs[index - 1].id : activeId const nextTab = tabs.find((t) => t.id === nextId) if (nextTab) selectFile(nextTab) else setActiveId(null) setTabs((prev) => prev.filter((t) => t.id !== tab.id)) } const handleRename = ( id: string, newName: string, oldName: string, type: "file" | "folder" ) => { // Validation if (newName === oldName) { return false } if ( newName.includes("/") || newName.includes("\\") || newName.includes(" ") || (type === "file" && !newName.includes(".")) || (type === "folder" && newName.includes(".")) ) { toast.error("Invalid file name.") return false } // Action socket.emit("renameFile", id, newName) setTabs((prev) => prev.map((tab) => (tab.id === id ? { ...tab, name: newName } : tab)) ) return true } return ( <>
{tabs.map((tab) => ( { selectFile(tab) }} onClose={() => closeTab(tab)} > {tab.name} ))}
{activeId === null ? ( <>
No file selected.
) : clerk.loaded ? ( { setTabs((prev) => prev.map((tab) => tab.id === activeId ? { ...tab, saved: false } : tab ) ) }} options={{ minimap: { enabled: false, }, padding: { bottom: 4, top: 4, }, scrollBeyondLastLine: false, fixedOverflowWidgets: true, }} theme="vs-dark" value={activeFile ?? ""} /> ) : null}
Preview
Node Console
) }