From 2b9576c3fee8f82655b3576f6c6b4944b60b9e68 Mon Sep 17 00:00:00 2001 From: Ishaan Dey Date: Thu, 2 May 2024 00:00:35 -0700 Subject: [PATCH] start gen ai logic --- frontend/components/editor/index.tsx | 73 ++++++++++++++++--- frontend/components/editor/terminal/index.tsx | 4 +- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/frontend/components/editor/index.tsx b/frontend/components/editor/index.tsx index 089488a..45b3c18 100644 --- a/frontend/components/editor/index.tsx +++ b/frontend/components/editor/index.tsx @@ -1,8 +1,8 @@ "use client" -import Editor, { OnMount } from "@monaco-editor/react" +import Editor, { BeforeMount, OnMount } from "@monaco-editor/react" import monaco from "monaco-editor" -import { useEffect, useRef, useState } from "react" +import { use, useEffect, useRef, useState } from "react" // import theme from "./theme.json" import { @@ -39,20 +39,69 @@ export default function CodeEditor({ userData: User sandboxId: string }) { - const clerk = useClerk() - - 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 [terminals, setTerminals] = useState([]) + const [showGenerate, setShowGenerate] = useState(false) + const [generateId, setGenerateId] = useState("") + const [cursorLine, setCursorLine] = useState(0) + + const clerk = useClerk() + + const editorRef = useRef(null) + const monacoRef = useRef(null) + const generateRef = useRef(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( `http://localhost:4000?userId=${userData.id}&sandboxId=${sandboxId}` @@ -191,6 +240,9 @@ export default function CodeEditor({ return ( <> +
+ {showGenerate ? "HELLO" : null} +
{ setTabs((prev) => diff --git a/frontend/components/editor/terminal/index.tsx b/frontend/components/editor/terminal/index.tsx index efe4b40..8156275 100644 --- a/frontend/components/editor/terminal/index.tsx +++ b/frontend/components/editor/terminal/index.tsx @@ -33,10 +33,10 @@ export default function EditorTerminal({ socket }: { socket: Socket }) { if (!term) return const onConnect = () => { - // console.log("Connected to server", socket.connected) + console.log("Connected to server", socket.connected) setTimeout(() => { socket.emit("createTerminal", { id: "testId" }) - }, 500) + }, 2000) } const onTerminalResponse = (response: { data: string }) => {