start gen ai logic

This commit is contained in:
Ishaan Dey 2024-05-02 00:00:35 -07:00
parent e6e5247a3d
commit 2b9576c3fe
2 changed files with 65 additions and 12 deletions

View File

@ -1,8 +1,8 @@
"use client" "use client"
import Editor, { OnMount } from "@monaco-editor/react" import Editor, { BeforeMount, OnMount } from "@monaco-editor/react"
import monaco from "monaco-editor" import monaco from "monaco-editor"
import { useEffect, useRef, useState } from "react" import { use, useEffect, useRef, useState } from "react"
// import theme from "./theme.json" // import theme from "./theme.json"
import { import {
@ -39,20 +39,69 @@ export default function CodeEditor({
userData: User userData: User
sandboxId: string sandboxId: string
}) { }) {
const clerk = useClerk()
const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null)
const handleEditorMount: OnMount = (editor, monaco) => {
editorRef.current = editor
}
const [files, setFiles] = useState<(TFolder | TFile)[]>([]) const [files, setFiles] = useState<(TFolder | TFile)[]>([])
const [editorLanguage, setEditorLanguage] = useState("plaintext") const [editorLanguage, setEditorLanguage] = useState("plaintext")
const [activeFile, setActiveFile] = useState<string | null>(null) const [activeFile, setActiveFile] = useState<string | null>(null)
const [tabs, setTabs] = useState<TTab[]>([]) const [tabs, setTabs] = useState<TTab[]>([])
const [activeId, setActiveId] = useState<string | null>(null) const [activeId, setActiveId] = useState<string | null>(null)
const [terminals, setTerminals] = useState<string[]>([]) const [terminals, setTerminals] = useState<string[]>([])
const [showGenerate, setShowGenerate] = useState(false)
const [generateId, setGenerateId] = useState<string>("")
const [cursorLine, setCursorLine] = useState(0)
const clerk = useClerk()
const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null)
const monacoRef = useRef<typeof monaco | null>(null)
const generateRef = useRef<HTMLDivElement>(null)
const handleEditorWillMount: BeforeMount = (monaco) => {
monaco.editor.addKeybindingRules([
{
keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyG,
command: "null",
// when: "textInputFocus",
},
])
}
const handleEditorMount: OnMount = (editor, monaco) => {
editorRef.current = editor
monacoRef.current = monaco
editor.onDidChangeCursorPosition((e) => {
setCursorLine(e.position.lineNumber)
})
editor.addAction({
id: "generate",
label: "Generate",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyG],
precondition:
"editorTextFocus && !suggestWidgetVisible && !renameInputVisible && !inSnippetMode && !quickFixWidgetVisible",
run: () => setShowGenerate((prev) => !prev),
})
}
useEffect(() => {
if (showGenerate) {
editorRef.current?.changeViewZones(function (changeAccessor) {
if (!generateRef.current) return
const id = changeAccessor.addZone({
afterLineNumber: cursorLine,
heightInLines: 3,
domNode: generateRef.current,
})
setGenerateId(id)
})
} else {
editorRef.current?.changeViewZones(function (changeAccessor) {
if (!generateRef.current) return
changeAccessor.removeZone(generateId)
setGenerateId("")
})
}
}, [showGenerate])
const socket = io( const socket = io(
`http://localhost:4000?userId=${userData.id}&sandboxId=${sandboxId}` `http://localhost:4000?userId=${userData.id}&sandboxId=${sandboxId}`
@ -191,6 +240,9 @@ export default function CodeEditor({
return ( return (
<> <>
<div className="bg-blue-500" ref={generateRef}>
{showGenerate ? "HELLO" : null}
</div>
<Sidebar <Sidebar
files={files} files={files}
selectFile={selectFile} selectFile={selectFile}
@ -246,6 +298,7 @@ export default function CodeEditor({
height="100%" height="100%"
// defaultLanguage="typescript" // defaultLanguage="typescript"
language={editorLanguage} language={editorLanguage}
beforeMount={handleEditorWillMount}
onMount={handleEditorMount} onMount={handleEditorMount}
onChange={(value) => { onChange={(value) => {
setTabs((prev) => setTabs((prev) =>

View File

@ -33,10 +33,10 @@ export default function EditorTerminal({ socket }: { socket: Socket }) {
if (!term) return if (!term) return
const onConnect = () => { const onConnect = () => {
// console.log("Connected to server", socket.connected) console.log("Connected to server", socket.connected)
setTimeout(() => { setTimeout(() => {
socket.emit("createTerminal", { id: "testId" }) socket.emit("createTerminal", { id: "testId" })
}, 500) }, 2000)
} }
const onTerminalResponse = (response: { data: string }) => { const onTerminalResponse = (response: { data: string }) => {