199 lines
6.0 KiB
TypeScript
Raw Normal View History

2024-04-06 19:03:04 -04:00
"use client"
import Editor, { OnMount } from "@monaco-editor/react"
import monaco from "monaco-editor"
2024-04-26 22:34:56 -04:00
import { useEffect, useRef, useState } from "react"
2024-04-26 00:10:53 -04:00
// import theme from "./theme.json"
2024-04-06 19:03:04 -04:00
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable"
2024-04-08 23:40:42 -04:00
import {
ChevronLeft,
ChevronRight,
RotateCw,
TerminalSquare,
} from "lucide-react"
import Tab from "../ui/tab"
2024-04-09 00:50:48 -04:00
import Sidebar from "./sidebar"
2024-04-18 15:32:27 -04:00
import { useClerk } from "@clerk/nextjs"
2024-04-26 00:10:53 -04:00
import { TFile, TFolder } from "./sidebar/types"
2024-04-06 19:03:04 -04:00
2024-04-26 22:34:56 -04:00
import { io } from "socket.io-client"
export default function CodeEditor({
userId,
sandboxId,
}: {
userId: string
sandboxId: string
}) {
2024-04-26 00:10:53 -04:00
// const editorRef = useRef<null | monaco.editor.IStandaloneCodeEditor>(null)
2024-04-06 19:03:04 -04:00
2024-04-18 15:32:27 -04:00
// const handleEditorMount: OnMount = (editor, monaco) => {
// editorRef.current = editor
// }
2024-04-06 19:03:04 -04:00
2024-04-26 22:34:56 -04:00
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!
2024-04-18 15:32:27 -04:00
const clerk = useClerk()
2024-04-06 19:03:04 -04:00
2024-04-26 00:10:53 -04:00
const [tabs, setTabs] = useState<TFile[]>([])
const [activeId, setActiveId] = useState<string | null>(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))
}
2024-04-06 19:03:04 -04:00
return (
<>
2024-04-26 00:10:53 -04:00
<Sidebar files={files} selectFile={selectFile} />
2024-04-06 19:03:04 -04:00
<ResizablePanelGroup direction="horizontal">
<ResizablePanel
className="p-2 flex flex-col"
maxSize={75}
minSize={30}
defaultSize={60}
>
<div className="h-10 w-full flex gap-2">
2024-04-26 00:10:53 -04:00
{tabs.map((tab) => (
<Tab
key={tab.id}
selected={activeId === tab.id}
onClick={() => setActiveId(tab.id)}
onClose={() => closeTab(tab)}
>
{tab.name}
</Tab>
))}
2024-04-06 19:03:04 -04:00
</div>
2024-04-08 23:40:42 -04:00
<div className="grow w-full overflow-hidden rounded-md">
2024-04-26 00:10:53 -04:00
{activeId === null ? (
<>
<div className="w-full h-full flex items-center justify-center text-xl font-medium text-secondary select-none">
No file selected.
</div>
</>
) : clerk.loaded ? (
2024-04-18 15:32:27 -04:00
<Editor
height="100%"
defaultLanguage="typescript"
// onMount={handleEditorMount}
options={{
minimap: {
enabled: false,
},
padding: {
bottom: 4,
top: 4,
},
scrollBeyondLastLine: false,
}}
theme="vs-dark"
/>
) : null}
2024-04-06 19:03:04 -04:00
</div>
</ResizablePanel>
<ResizableHandle />
<ResizablePanel defaultSize={40}>
<ResizablePanelGroup direction="vertical">
<ResizablePanel
defaultSize={50}
minSize={20}
className="p-2 flex flex-col"
>
2024-04-08 23:40:42 -04:00
<div className="h-10 select-none w-full flex gap-2">
<div className="h-8 rounded-md px-3 text-xs bg-secondary flex items-center w-full justify-between">
Preview
<div className="flex space-x-1 translate-x-1">
2024-04-09 00:50:48 -04:00
<div className="p-0.5 h-5 w-5 ml-0.5 flex items-center justify-center transition-colors bg-transparent hover:bg-muted-foreground/25 cursor-pointer rounded-sm">
2024-04-08 23:40:42 -04:00
<TerminalSquare className="w-4 h-4" />
</div>
2024-04-09 00:50:48 -04:00
<div className="p-0.5 h-5 w-5 ml-0.5 flex items-center justify-center transition-colors bg-transparent hover:bg-muted-foreground/25 cursor-pointer rounded-sm">
2024-04-08 23:40:42 -04:00
<ChevronLeft className="w-4 h-4" />
</div>
2024-04-09 00:50:48 -04:00
<div className="p-0.5 h-5 w-5 ml-0.5 flex items-center justify-center transition-colors bg-transparent hover:bg-muted-foreground/25 cursor-pointer rounded-sm">
2024-04-08 23:40:42 -04:00
<ChevronRight className="w-4 h-4" />
</div>
2024-04-09 00:50:48 -04:00
<div className="p-0.5 h-5 w-5 ml-0.5 flex items-center justify-center transition-colors bg-transparent hover:bg-muted-foreground/25 cursor-pointer rounded-sm">
2024-04-08 23:40:42 -04:00
<RotateCw className="w-3 h-3" />
</div>
</div>
</div>
2024-04-06 19:03:04 -04:00
</div>
2024-04-08 23:40:42 -04:00
<div className="w-full grow rounded-md bg-foreground"></div>
2024-04-06 19:03:04 -04:00
</ResizablePanel>
<ResizableHandle />
<ResizablePanel
defaultSize={50}
minSize={20}
className="p-2 flex flex-col"
>
<div className="h-10 w-full flex gap-2">
2024-04-18 15:38:38 -04:00
<Tab selected>Node</Tab>
2024-04-08 23:40:42 -04:00
<Tab>Console</Tab>
2024-04-06 19:03:04 -04:00
</div>
2024-04-08 23:40:42 -04:00
<div className="w-full grow rounded-md bg-secondary"></div>
2024-04-06 19:03:04 -04:00
</ResizablePanel>
</ResizablePanelGroup>
</ResizablePanel>
</ResizablePanelGroup>
</>
)
}