2024-05-06 22:59:49 -07:00
|
|
|
"use client";
|
2024-04-06 19:03:04 -04:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
import monaco from "monaco-editor";
|
|
|
|
import Editor, { BeforeMount, OnMount } from "@monaco-editor/react";
|
|
|
|
import { io } from "socket.io-client";
|
|
|
|
import { toast } from "sonner";
|
|
|
|
import { useClerk } from "@clerk/nextjs";
|
2024-05-03 13:53:21 -07:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
import * as Y from "yjs";
|
|
|
|
import LiveblocksProvider from "@liveblocks/yjs";
|
|
|
|
import { MonacoBinding } from "y-monaco";
|
|
|
|
import { Awareness } from "y-protocols/awareness";
|
|
|
|
import { TypedLiveblocksProvider, useRoom } from "@/liveblocks.config";
|
2024-04-06 19:03:04 -04:00
|
|
|
|
|
|
|
import {
|
|
|
|
ResizableHandle,
|
|
|
|
ResizablePanel,
|
|
|
|
ResizablePanelGroup,
|
2024-05-06 22:59:49 -07:00
|
|
|
} from "@/components/ui/resizable";
|
2024-05-08 23:52:08 -07:00
|
|
|
import { FileJson, Loader2, TerminalSquare } from "lucide-react";
|
2024-05-06 22:59:49 -07:00
|
|
|
import Tab from "../ui/tab";
|
|
|
|
import Sidebar from "./sidebar";
|
|
|
|
import GenerateInput from "./generate";
|
2024-05-08 23:52:08 -07:00
|
|
|
import { Sandbox, User, TFile, TFolder, TTab } from "@/lib/types";
|
2024-05-11 18:03:42 -07:00
|
|
|
import { addNew, processFileType, validateName } from "@/lib/utils";
|
2024-05-06 22:59:49 -07:00
|
|
|
import { Cursors } from "./live/cursors";
|
|
|
|
import { Terminal } from "@xterm/xterm";
|
2024-05-07 21:19:32 -07:00
|
|
|
import DisableAccessModal from "./live/disableModal";
|
|
|
|
import Loading from "./loading";
|
2024-05-08 23:52:08 -07:00
|
|
|
import PreviewWindow from "./preview";
|
|
|
|
import Terminals from "./terminals";
|
2024-05-09 22:16:56 -07:00
|
|
|
import { ImperativePanelHandle } from "react-resizable-panels";
|
2024-04-28 20:06:47 -04:00
|
|
|
|
2024-04-26 22:34:56 -04:00
|
|
|
export default function CodeEditor({
|
2024-05-01 01:53:49 -04:00
|
|
|
userData,
|
2024-05-05 00:06:10 -07:00
|
|
|
sandboxData,
|
2024-05-08 23:52:08 -07:00
|
|
|
}: // isSharedUser,
|
|
|
|
{
|
2024-05-06 22:59:49 -07:00
|
|
|
userData: User;
|
|
|
|
sandboxData: Sandbox;
|
|
|
|
isSharedUser: boolean;
|
2024-04-26 22:34:56 -04:00
|
|
|
}) {
|
2024-05-08 23:52:08 -07:00
|
|
|
const socket = io(
|
|
|
|
`http://localhost:4000?userId=${userData.id}&sandboxId=${sandboxData.id}`
|
|
|
|
);
|
|
|
|
|
2024-05-09 22:16:56 -07:00
|
|
|
const [isPreviewCollapsed, setIsPreviewCollapsed] = useState(
|
|
|
|
sandboxData.type !== "react"
|
|
|
|
);
|
|
|
|
const [disableAccess, setDisableAccess] = useState({
|
|
|
|
isDisabled: false,
|
|
|
|
message: "",
|
|
|
|
});
|
|
|
|
|
|
|
|
// File state
|
2024-05-06 22:59:49 -07:00
|
|
|
const [files, setFiles] = useState<(TFolder | TFile)[]>([]);
|
|
|
|
const [tabs, setTabs] = useState<TTab[]>([]);
|
|
|
|
const [activeFileId, setActiveFileId] = useState<string>("");
|
2024-05-06 23:34:45 -07:00
|
|
|
const [activeFileContent, setActiveFileContent] = useState("");
|
2024-05-11 17:23:45 -07:00
|
|
|
const [deletingFolderId, setDeletingFolderId] = useState("");
|
2024-05-09 22:16:56 -07:00
|
|
|
|
|
|
|
// Editor state
|
|
|
|
const [editorLanguage, setEditorLanguage] = useState("plaintext");
|
2024-05-06 22:59:49 -07:00
|
|
|
const [cursorLine, setCursorLine] = useState(0);
|
2024-05-09 22:16:56 -07:00
|
|
|
const [editorRef, setEditorRef] =
|
|
|
|
useState<monaco.editor.IStandaloneCodeEditor>();
|
|
|
|
|
|
|
|
// AI Copilot state
|
|
|
|
const [ai, setAi] = useState(false);
|
2024-05-02 17:38:37 -07:00
|
|
|
const [generate, setGenerate] = useState<{
|
2024-05-06 22:59:49 -07:00
|
|
|
show: boolean;
|
|
|
|
id: string;
|
|
|
|
line: number;
|
|
|
|
widget: monaco.editor.IContentWidget | undefined;
|
|
|
|
pref: monaco.editor.ContentWidgetPositionPreference[];
|
|
|
|
width: number;
|
|
|
|
}>({ show: false, line: 0, id: "", widget: undefined, pref: [], width: 0 });
|
2024-05-02 15:25:24 -07:00
|
|
|
const [decorations, setDecorations] = useState<{
|
2024-05-06 22:59:49 -07:00
|
|
|
options: monaco.editor.IModelDeltaDecoration[];
|
|
|
|
instance: monaco.editor.IEditorDecorationsCollection | undefined;
|
|
|
|
}>({ options: [], instance: undefined });
|
2024-05-09 22:16:56 -07:00
|
|
|
|
|
|
|
// Terminal state
|
2024-05-06 22:59:49 -07:00
|
|
|
const [terminals, setTerminals] = useState<
|
|
|
|
{
|
|
|
|
id: string;
|
|
|
|
terminal: Terminal | null;
|
|
|
|
}[]
|
|
|
|
>([]);
|
2024-05-06 23:34:45 -07:00
|
|
|
const [activeTerminalId, setActiveTerminalId] = useState("");
|
|
|
|
const [creatingTerminal, setCreatingTerminal] = useState(false);
|
2024-05-08 01:04:03 -07:00
|
|
|
const [closingTerminal, setClosingTerminal] = useState("");
|
2024-05-09 22:16:56 -07:00
|
|
|
const activeTerminal = terminals.find((t) => t.id === activeTerminalId);
|
2024-05-06 22:59:49 -07:00
|
|
|
|
|
|
|
const isOwner = sandboxData.userId === userData.id;
|
|
|
|
const clerk = useClerk();
|
2024-05-09 22:16:56 -07:00
|
|
|
|
|
|
|
// Liveblocks hooks
|
2024-05-06 22:59:49 -07:00
|
|
|
const room = useRoom();
|
2024-05-09 22:16:56 -07:00
|
|
|
const [provider, setProvider] = useState<TypedLiveblocksProvider>();
|
2024-04-28 20:06:47 -04:00
|
|
|
|
2024-05-09 22:16:56 -07:00
|
|
|
// Refs for libraries / features
|
2024-05-06 22:59:49 -07:00
|
|
|
const editorContainerRef = useRef<HTMLDivElement>(null);
|
|
|
|
const monacoRef = useRef<typeof monaco | null>(null);
|
|
|
|
const generateRef = useRef<HTMLDivElement>(null);
|
|
|
|
const generateWidgetRef = useRef<HTMLDivElement>(null);
|
2024-05-09 22:16:56 -07:00
|
|
|
const previewPanelRef = useRef<ImperativePanelHandle>(null);
|
2024-05-02 00:00:35 -07:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// Resize observer tracks editor width for generate widget
|
|
|
|
const resizeObserver = new ResizeObserver((entries) => {
|
|
|
|
for (const entry of entries) {
|
|
|
|
const { width } = entry.contentRect;
|
|
|
|
setGenerate((prev) => {
|
|
|
|
return { ...prev, width };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Pre-mount editor keybindings
|
2024-05-02 00:00:35 -07:00
|
|
|
const handleEditorWillMount: BeforeMount = (monaco) => {
|
|
|
|
monaco.editor.addKeybindingRules([
|
|
|
|
{
|
|
|
|
keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyG,
|
|
|
|
command: "null",
|
|
|
|
},
|
2024-05-06 22:59:49 -07:00
|
|
|
]);
|
|
|
|
};
|
2024-04-06 19:03:04 -04:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// Post-mount editor keybindings and actions
|
2024-04-27 00:20:17 -04:00
|
|
|
const handleEditorMount: OnMount = (editor, monaco) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
setEditorRef(editor);
|
|
|
|
monacoRef.current = monaco;
|
2024-05-02 00:00:35 -07:00
|
|
|
|
|
|
|
editor.onDidChangeCursorPosition((e) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
const { column, lineNumber } = e.position;
|
|
|
|
if (lineNumber === cursorLine) return;
|
|
|
|
setCursorLine(lineNumber);
|
2024-05-02 15:25:24 -07:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
const model = editor.getModel();
|
|
|
|
const endColumn = model?.getLineContent(lineNumber).length || 0;
|
2024-05-02 15:25:24 -07:00
|
|
|
|
|
|
|
setDecorations((prev) => {
|
|
|
|
return {
|
|
|
|
...prev,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
range: new monaco.Range(
|
|
|
|
lineNumber,
|
|
|
|
column,
|
|
|
|
lineNumber,
|
|
|
|
endColumn
|
|
|
|
),
|
|
|
|
options: {
|
|
|
|
afterContentClassName: "inline-decoration",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
2024-05-02 00:00:35 -07:00
|
|
|
|
2024-05-02 17:38:37 -07:00
|
|
|
editor.onDidBlurEditorText((e) => {
|
|
|
|
setDecorations((prev) => {
|
|
|
|
return {
|
|
|
|
...prev,
|
|
|
|
options: [],
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-02 00:00:35 -07:00
|
|
|
editor.addAction({
|
|
|
|
id: "generate",
|
|
|
|
label: "Generate",
|
|
|
|
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyG],
|
|
|
|
precondition:
|
|
|
|
"editorTextFocus && !suggestWidgetVisible && !renameInputVisible && !inSnippetMode && !quickFixWidgetVisible",
|
2024-05-02 15:25:24 -07:00
|
|
|
run: () => {
|
|
|
|
setGenerate((prev) => {
|
2024-05-02 17:38:37 -07:00
|
|
|
return {
|
|
|
|
...prev,
|
|
|
|
show: !prev.show,
|
|
|
|
pref: [monaco.editor.ContentWidgetPositionPreference.BELOW],
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
|
|
|
});
|
2024-05-02 15:25:24 -07:00
|
|
|
},
|
2024-05-06 22:59:49 -07:00
|
|
|
});
|
|
|
|
};
|
2024-04-06 19:03:04 -04:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// Generate widget effect
|
2024-05-02 00:00:35 -07:00
|
|
|
useEffect(() => {
|
2024-05-04 01:50:33 -07:00
|
|
|
if (!ai) {
|
|
|
|
setGenerate((prev) => {
|
|
|
|
return {
|
|
|
|
...prev,
|
|
|
|
show: false,
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
return;
|
2024-05-04 01:50:33 -07:00
|
|
|
}
|
2024-05-02 15:25:24 -07:00
|
|
|
if (generate.show) {
|
2024-05-04 20:50:00 -07:00
|
|
|
editorRef?.changeViewZones(function (changeAccessor) {
|
2024-05-06 22:59:49 -07:00
|
|
|
if (!generateRef.current) return;
|
2024-05-02 00:00:35 -07:00
|
|
|
const id = changeAccessor.addZone({
|
|
|
|
afterLineNumber: cursorLine,
|
|
|
|
heightInLines: 3,
|
|
|
|
domNode: generateRef.current,
|
2024-05-06 22:59:49 -07:00
|
|
|
});
|
2024-05-02 15:25:24 -07:00
|
|
|
setGenerate((prev) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
return { ...prev, id, line: cursorLine };
|
|
|
|
});
|
|
|
|
});
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
if (!generateWidgetRef.current) return;
|
|
|
|
const widgetElement = generateWidgetRef.current;
|
2024-05-02 17:38:37 -07:00
|
|
|
|
|
|
|
const contentWidget = {
|
|
|
|
getDomNode: () => {
|
2024-05-06 22:59:49 -07:00
|
|
|
return widgetElement;
|
2024-05-02 17:38:37 -07:00
|
|
|
},
|
|
|
|
getId: () => {
|
2024-05-06 22:59:49 -07:00
|
|
|
return "generate.widget";
|
2024-05-02 17:38:37 -07:00
|
|
|
},
|
|
|
|
getPosition: () => {
|
|
|
|
return {
|
|
|
|
position: {
|
|
|
|
lineNumber: cursorLine,
|
|
|
|
column: 1,
|
|
|
|
},
|
|
|
|
preference: generate.pref,
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
2024-05-02 17:38:37 -07:00
|
|
|
},
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
2024-05-02 17:38:37 -07:00
|
|
|
|
|
|
|
setGenerate((prev) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
return { ...prev, widget: contentWidget };
|
|
|
|
});
|
|
|
|
editorRef?.addContentWidget(contentWidget);
|
2024-05-02 17:38:37 -07:00
|
|
|
|
|
|
|
if (generateRef.current && generateWidgetRef.current) {
|
2024-05-06 22:59:49 -07:00
|
|
|
editorRef?.applyFontInfo(generateRef.current);
|
|
|
|
editorRef?.applyFontInfo(generateWidgetRef.current);
|
2024-05-02 17:38:37 -07:00
|
|
|
}
|
2024-05-02 00:00:35 -07:00
|
|
|
} else {
|
2024-05-04 20:50:00 -07:00
|
|
|
editorRef?.changeViewZones(function (changeAccessor) {
|
2024-05-06 22:59:49 -07:00
|
|
|
changeAccessor.removeZone(generate.id);
|
2024-05-02 15:25:24 -07:00
|
|
|
setGenerate((prev) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
return { ...prev, id: "" };
|
|
|
|
});
|
|
|
|
});
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
if (!generate.widget) return;
|
|
|
|
editorRef?.removeContentWidget(generate.widget);
|
2024-05-02 17:38:37 -07:00
|
|
|
setGenerate((prev) => {
|
|
|
|
return {
|
|
|
|
...prev,
|
|
|
|
widget: undefined,
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
|
|
|
});
|
2024-05-02 15:25:24 -07:00
|
|
|
}
|
2024-05-06 22:59:49 -07:00
|
|
|
}, [generate.show]);
|
2024-05-02 15:25:24 -07:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// Decorations effect for generate widget tips
|
2024-05-02 15:25:24 -07:00
|
|
|
useEffect(() => {
|
2024-05-02 17:38:37 -07:00
|
|
|
if (decorations.options.length === 0) {
|
2024-05-06 22:59:49 -07:00
|
|
|
decorations.instance?.clear();
|
2024-05-02 17:38:37 -07:00
|
|
|
}
|
2024-05-02 15:25:24 -07:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
if (!ai) return;
|
2024-05-04 01:50:33 -07:00
|
|
|
|
2024-05-02 15:25:24 -07:00
|
|
|
if (decorations.instance) {
|
2024-05-06 22:59:49 -07:00
|
|
|
decorations.instance.set(decorations.options);
|
2024-05-02 15:25:24 -07:00
|
|
|
} else {
|
2024-05-06 22:59:49 -07:00
|
|
|
const instance = editorRef?.createDecorationsCollection();
|
|
|
|
instance?.set(decorations.options);
|
2024-05-02 15:25:24 -07:00
|
|
|
|
|
|
|
setDecorations((prev) => {
|
|
|
|
return {
|
|
|
|
...prev,
|
|
|
|
instance,
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
|
|
|
});
|
2024-05-02 00:00:35 -07:00
|
|
|
}
|
2024-05-06 22:59:49 -07:00
|
|
|
}, [decorations.options]);
|
2024-04-26 22:34:56 -04:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// Save file keybinding logic effect
|
2024-04-27 16:41:25 -04:00
|
|
|
useEffect(() => {
|
|
|
|
const down = (e: KeyboardEvent) => {
|
|
|
|
if (e.key === "s" && (e.metaKey || e.ctrlKey)) {
|
2024-05-06 22:59:49 -07:00
|
|
|
e.preventDefault();
|
2024-04-27 16:41:25 -04:00
|
|
|
|
|
|
|
setTabs((prev) =>
|
|
|
|
prev.map((tab) =>
|
2024-05-06 22:59:49 -07:00
|
|
|
tab.id === activeFileId ? { ...tab, saved: true } : tab
|
2024-04-27 16:41:25 -04:00
|
|
|
)
|
2024-05-06 22:59:49 -07:00
|
|
|
);
|
2024-04-27 16:41:25 -04:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
socket.emit("saveFile", activeFileId, editorRef?.getValue());
|
2024-04-27 16:41:25 -04:00
|
|
|
}
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
|
|
|
document.addEventListener("keydown", down);
|
2024-04-27 16:41:25 -04:00
|
|
|
|
|
|
|
return () => {
|
2024-05-06 22:59:49 -07:00
|
|
|
document.removeEventListener("keydown", down);
|
|
|
|
};
|
|
|
|
}, [tabs, activeFileId]);
|
2024-04-27 16:41:25 -04:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// Liveblocks live collaboration setup effect
|
2024-05-04 20:50:00 -07:00
|
|
|
useEffect(() => {
|
2024-05-06 22:59:49 -07:00
|
|
|
const tab = tabs.find((t) => t.id === activeFileId);
|
|
|
|
const model = editorRef?.getModel();
|
2024-05-04 20:50:00 -07:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
if (!editorRef || !tab || !model) return;
|
2024-05-04 20:50:00 -07:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
const yDoc = new Y.Doc();
|
|
|
|
const yText = yDoc.getText(tab.id);
|
|
|
|
const yProvider: any = new LiveblocksProvider(room, yDoc);
|
2024-05-04 20:50:00 -07:00
|
|
|
|
|
|
|
const onSync = (isSynced: boolean) => {
|
|
|
|
if (isSynced) {
|
2024-05-06 22:59:49 -07:00
|
|
|
const text = yText.toString();
|
2024-05-04 20:50:00 -07:00
|
|
|
if (text === "") {
|
2024-05-06 22:59:49 -07:00
|
|
|
if (activeFileContent) {
|
|
|
|
yText.insert(0, activeFileContent);
|
2024-05-04 20:50:00 -07:00
|
|
|
} else {
|
|
|
|
setTimeout(() => {
|
2024-05-06 22:59:49 -07:00
|
|
|
yText.insert(0, editorRef.getValue());
|
|
|
|
}, 0);
|
2024-05-04 20:50:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
2024-05-04 20:50:00 -07:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
yProvider.on("sync", onSync);
|
2024-05-04 20:50:00 -07:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
setProvider(yProvider);
|
2024-05-04 20:50:00 -07:00
|
|
|
|
|
|
|
const binding = new MonacoBinding(
|
|
|
|
yText,
|
|
|
|
model,
|
|
|
|
new Set([editorRef]),
|
|
|
|
yProvider.awareness as Awareness
|
2024-05-06 22:59:49 -07:00
|
|
|
);
|
2024-05-04 20:50:00 -07:00
|
|
|
|
|
|
|
return () => {
|
2024-05-06 22:59:49 -07:00
|
|
|
yDoc.destroy();
|
|
|
|
yProvider.destroy();
|
|
|
|
binding.destroy();
|
|
|
|
yProvider.off("sync", onSync);
|
|
|
|
};
|
|
|
|
}, [editorRef, room, activeFileContent]);
|
2024-05-03 13:53:21 -07:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// Connection/disconnection effect + resizeobserver
|
2024-04-26 22:34:56 -04:00
|
|
|
useEffect(() => {
|
2024-05-06 22:59:49 -07:00
|
|
|
socket.connect();
|
2024-04-26 22:34:56 -04:00
|
|
|
|
2024-05-02 17:38:37 -07:00
|
|
|
if (editorContainerRef.current) {
|
2024-05-06 22:59:49 -07:00
|
|
|
resizeObserver.observe(editorContainerRef.current);
|
2024-05-02 17:38:37 -07:00
|
|
|
}
|
|
|
|
|
2024-04-26 22:34:56 -04:00
|
|
|
return () => {
|
2024-05-06 22:59:49 -07:00
|
|
|
socket.disconnect();
|
|
|
|
resizeObserver.disconnect();
|
|
|
|
};
|
|
|
|
}, []);
|
2024-04-26 22:34:56 -04:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// Socket event listener effect
|
2024-04-26 22:34:56 -04:00
|
|
|
useEffect(() => {
|
2024-05-08 23:52:08 -07:00
|
|
|
const onConnect = () => {};
|
2024-04-28 20:06:47 -04:00
|
|
|
|
2024-05-07 22:40:59 -07:00
|
|
|
const onDisconnect = () => {
|
2024-05-07 23:52:14 -07:00
|
|
|
setTerminals([]);
|
2024-05-07 22:40:59 -07:00
|
|
|
};
|
2024-04-28 20:06:47 -04:00
|
|
|
|
|
|
|
const onLoadedEvent = (files: (TFolder | TFile)[]) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
setFiles(files);
|
|
|
|
};
|
2024-04-26 22:34:56 -04:00
|
|
|
|
2024-05-05 14:33:09 -07:00
|
|
|
const onRateLimit = (message: string) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
toast.error(message);
|
|
|
|
};
|
2024-04-28 20:06:47 -04:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
const onTerminalResponse = (response: { id: string; data: string }) => {
|
2024-05-08 01:04:03 -07:00
|
|
|
const term = terminals.find((t) => t.id === response.id);
|
|
|
|
if (term && term.terminal) {
|
|
|
|
term.terminal.write(response.data);
|
|
|
|
}
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
|
|
|
|
2024-05-07 22:40:59 -07:00
|
|
|
const onDisableAccess = (message: string) => {
|
2024-05-09 22:16:56 -07:00
|
|
|
if (!isOwner)
|
|
|
|
setDisableAccess({
|
|
|
|
isDisabled: true,
|
|
|
|
message,
|
|
|
|
});
|
2024-05-07 22:40:59 -07:00
|
|
|
};
|
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
socket.on("connect", onConnect);
|
|
|
|
socket.on("disconnect", onDisconnect);
|
|
|
|
socket.on("loaded", onLoadedEvent);
|
|
|
|
socket.on("rateLimit", onRateLimit);
|
|
|
|
socket.on("terminalResponse", onTerminalResponse);
|
2024-05-07 22:40:59 -07:00
|
|
|
socket.on("disableAccess", onDisableAccess);
|
2024-04-26 22:34:56 -04:00
|
|
|
|
|
|
|
return () => {
|
2024-05-06 22:59:49 -07:00
|
|
|
socket.off("connect", onConnect);
|
|
|
|
socket.off("disconnect", onDisconnect);
|
|
|
|
socket.off("loaded", onLoadedEvent);
|
|
|
|
socket.off("rateLimit", onRateLimit);
|
|
|
|
socket.off("terminalResponse", onTerminalResponse);
|
2024-05-07 22:40:59 -07:00
|
|
|
socket.off("disableAccess", onDisableAccess);
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
2024-05-08 01:04:03 -07:00
|
|
|
// }, []);
|
|
|
|
}, [terminals]);
|
2024-04-26 22:34:56 -04:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// Helper functions for tabs:
|
2024-05-06 22:59:49 -07:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// Select file and load content
|
2024-04-27 11:08:19 -04:00
|
|
|
const selectFile = (tab: TTab) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
if (tab.id === activeFileId) return;
|
|
|
|
const exists = tabs.find((t) => t.id === tab.id);
|
2024-05-05 12:55:34 -07:00
|
|
|
|
2024-04-26 00:10:53 -04:00
|
|
|
setTabs((prev) => {
|
|
|
|
if (exists) {
|
2024-05-06 22:59:49 -07:00
|
|
|
setActiveFileId(exists.id);
|
|
|
|
return prev;
|
2024-04-26 00:10:53 -04:00
|
|
|
}
|
2024-05-06 22:59:49 -07:00
|
|
|
return [...prev, tab];
|
|
|
|
});
|
2024-05-04 01:18:06 -07:00
|
|
|
|
2024-04-27 00:20:17 -04:00
|
|
|
socket.emit("getFile", tab.id, (response: string) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
setActiveFileContent(response);
|
|
|
|
});
|
|
|
|
setEditorLanguage(processFileType(tab.name));
|
|
|
|
setActiveFileId(tab.id);
|
|
|
|
};
|
2024-04-26 00:10:53 -04:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// Close tab and remove from tabs
|
2024-05-11 17:23:45 -07:00
|
|
|
const closeTab = (id: string) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
const numTabs = tabs.length;
|
2024-05-11 17:23:45 -07:00
|
|
|
const index = tabs.findIndex((t) => t.id === id);
|
|
|
|
|
|
|
|
console.log("closing tab", id, index);
|
2024-04-30 01:56:43 -04:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
if (index === -1) return;
|
2024-04-30 01:56:43 -04:00
|
|
|
|
2024-04-27 00:20:17 -04:00
|
|
|
const nextId =
|
2024-05-11 17:23:45 -07:00
|
|
|
activeFileId === id
|
2024-04-27 00:20:17 -04:00
|
|
|
? numTabs === 1
|
|
|
|
? null
|
|
|
|
: index < numTabs - 1
|
|
|
|
? tabs[index + 1].id
|
|
|
|
: tabs[index - 1].id
|
2024-05-06 22:59:49 -07:00
|
|
|
: activeFileId;
|
2024-04-27 00:20:17 -04:00
|
|
|
|
2024-05-11 17:23:45 -07:00
|
|
|
setTabs((prev) => prev.filter((t) => t.id !== id));
|
2024-05-04 01:18:06 -07:00
|
|
|
|
|
|
|
if (!nextId) {
|
2024-05-06 22:59:49 -07:00
|
|
|
setActiveFileId("");
|
2024-05-04 01:18:06 -07:00
|
|
|
} else {
|
2024-05-06 22:59:49 -07:00
|
|
|
const nextTab = tabs.find((t) => t.id === nextId);
|
2024-05-04 01:18:06 -07:00
|
|
|
if (nextTab) {
|
2024-05-06 22:59:49 -07:00
|
|
|
selectFile(nextTab);
|
2024-05-04 01:18:06 -07:00
|
|
|
}
|
|
|
|
}
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
2024-04-26 00:10:53 -04:00
|
|
|
|
2024-05-11 17:23:45 -07:00
|
|
|
const closeTabs = (ids: string[]) => {
|
|
|
|
const numTabs = tabs.length;
|
|
|
|
|
|
|
|
if (numTabs === 0) return;
|
|
|
|
|
|
|
|
const allIndexes = ids.map((id) => tabs.findIndex((t) => t.id === id));
|
|
|
|
|
|
|
|
const indexes = allIndexes.filter((index) => index !== -1);
|
|
|
|
if (indexes.length === 0) return;
|
|
|
|
|
|
|
|
console.log("closing tabs", ids, indexes);
|
|
|
|
|
|
|
|
const activeIndex = tabs.findIndex((t) => t.id === activeFileId);
|
|
|
|
|
|
|
|
const newTabs = tabs.filter((t) => !ids.includes(t.id));
|
|
|
|
setTabs(newTabs);
|
|
|
|
|
|
|
|
if (indexes.length === numTabs) {
|
|
|
|
setActiveFileId("");
|
|
|
|
} else {
|
|
|
|
const nextTab =
|
|
|
|
newTabs.length > activeIndex
|
|
|
|
? newTabs[activeIndex]
|
|
|
|
: newTabs[newTabs.length - 1];
|
|
|
|
if (nextTab) {
|
|
|
|
selectFile(nextTab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-04-27 14:23:09 -04:00
|
|
|
const handleRename = (
|
|
|
|
id: string,
|
|
|
|
newName: string,
|
|
|
|
oldName: string,
|
|
|
|
type: "file" | "folder"
|
|
|
|
) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
const valid = validateName(newName, oldName, type);
|
2024-05-05 12:55:34 -07:00
|
|
|
if (!valid.status) {
|
2024-05-06 22:59:49 -07:00
|
|
|
if (valid.message) toast.error("Invalid file name.");
|
|
|
|
return false;
|
2024-04-30 01:56:43 -04:00
|
|
|
}
|
2024-04-27 14:23:09 -04:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
socket.emit("renameFile", id, newName);
|
2024-04-27 11:08:19 -04:00
|
|
|
setTabs((prev) =>
|
|
|
|
prev.map((tab) => (tab.id === id ? { ...tab, name: newName } : tab))
|
2024-05-06 22:59:49 -07:00
|
|
|
);
|
2024-04-27 14:23:09 -04:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
return true;
|
|
|
|
};
|
2024-04-27 11:08:19 -04:00
|
|
|
|
2024-04-30 01:56:43 -04:00
|
|
|
const handleDeleteFile = (file: TFile) => {
|
|
|
|
socket.emit("deleteFile", file.id, (response: (TFolder | TFile)[]) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
setFiles(response);
|
|
|
|
});
|
2024-05-11 17:23:45 -07:00
|
|
|
closeTab(file.id);
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
2024-04-30 01:56:43 -04:00
|
|
|
|
|
|
|
const handleDeleteFolder = (folder: TFolder) => {
|
2024-05-11 17:23:45 -07:00
|
|
|
setDeletingFolderId(folder.id);
|
|
|
|
console.log("deleting folder", folder.id);
|
|
|
|
|
|
|
|
socket.emit("getFolder", folder.id, (response: string[]) =>
|
|
|
|
closeTabs(response)
|
|
|
|
);
|
|
|
|
|
|
|
|
socket.emit("deleteFolder", folder.id, (response: (TFolder | TFile)[]) => {
|
|
|
|
setFiles(response);
|
|
|
|
setDeletingFolderId("");
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
setDeletingFolderId("");
|
|
|
|
}, 3000);
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
2024-04-30 01:56:43 -04:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
// On disabled access for shared users, show un-interactable loading placeholder + info modal
|
|
|
|
if (disableAccess.isDisabled)
|
2024-05-07 21:19:32 -07:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DisableAccessModal
|
|
|
|
message={disableAccess.message}
|
|
|
|
open={disableAccess.isDisabled}
|
|
|
|
setOpen={() => {}}
|
|
|
|
/>
|
|
|
|
<Loading />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
2024-04-06 19:03:04 -04:00
|
|
|
return (
|
|
|
|
<>
|
2024-05-08 23:52:08 -07:00
|
|
|
{/* Copilot DOM elements */}
|
2024-05-02 17:38:37 -07:00
|
|
|
<div ref={generateRef} />
|
|
|
|
<div className="z-50 p-1" ref={generateWidgetRef}>
|
2024-05-04 01:50:33 -07:00
|
|
|
{generate.show && ai ? (
|
2024-05-02 17:38:37 -07:00
|
|
|
<GenerateInput
|
2024-05-05 16:51:30 -07:00
|
|
|
user={userData}
|
2024-05-03 00:52:01 -07:00
|
|
|
socket={socket}
|
2024-05-02 17:38:37 -07:00
|
|
|
width={generate.width - 90}
|
2024-05-03 00:52:01 -07:00
|
|
|
data={{
|
2024-05-06 22:59:49 -07:00
|
|
|
fileName: tabs.find((t) => t.id === activeFileId)?.name ?? "",
|
2024-05-04 20:50:00 -07:00
|
|
|
code: editorRef?.getValue() ?? "",
|
2024-05-03 00:52:01 -07:00
|
|
|
line: generate.line,
|
|
|
|
}}
|
|
|
|
editor={{
|
|
|
|
language: editorLanguage,
|
|
|
|
}}
|
2024-05-02 17:38:37 -07:00
|
|
|
onExpand={() => {
|
2024-05-04 20:50:00 -07:00
|
|
|
editorRef?.changeViewZones(function (changeAccessor) {
|
2024-05-06 22:59:49 -07:00
|
|
|
changeAccessor.removeZone(generate.id);
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
if (!generateRef.current) return;
|
2024-05-02 17:38:37 -07:00
|
|
|
const id = changeAccessor.addZone({
|
|
|
|
afterLineNumber: cursorLine,
|
|
|
|
heightInLines: 12,
|
|
|
|
domNode: generateRef.current,
|
2024-05-06 22:59:49 -07:00
|
|
|
});
|
2024-05-02 17:38:37 -07:00
|
|
|
setGenerate((prev) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
return { ...prev, id };
|
|
|
|
});
|
|
|
|
});
|
2024-05-02 17:38:37 -07:00
|
|
|
}}
|
2024-05-02 18:05:18 -07:00
|
|
|
onAccept={(code: string) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
const line = generate.line;
|
2024-05-02 18:05:18 -07:00
|
|
|
setGenerate((prev) => {
|
|
|
|
return {
|
|
|
|
...prev,
|
|
|
|
show: !prev.show,
|
2024-05-06 22:59:49 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
const file = editorRef?.getValue();
|
|
|
|
|
|
|
|
const lines = file?.split("\n") || [];
|
|
|
|
lines.splice(line - 1, 0, code);
|
|
|
|
const updatedFile = lines.join("\n");
|
|
|
|
editorRef?.setValue(updatedFile);
|
2024-05-02 18:05:18 -07:00
|
|
|
}}
|
2024-05-02 17:38:37 -07:00
|
|
|
/>
|
|
|
|
) : null}
|
2024-05-02 00:00:35 -07:00
|
|
|
</div>
|
2024-05-02 17:38:37 -07:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
{/* Main editor components */}
|
2024-04-27 14:23:09 -04:00
|
|
|
<Sidebar
|
2024-05-10 00:12:41 -07:00
|
|
|
sandboxData={sandboxData}
|
2024-04-27 14:23:09 -04:00
|
|
|
files={files}
|
|
|
|
selectFile={selectFile}
|
|
|
|
handleRename={handleRename}
|
2024-04-30 01:56:43 -04:00
|
|
|
handleDeleteFile={handleDeleteFile}
|
|
|
|
handleDeleteFolder={handleDeleteFolder}
|
2024-04-29 00:50:25 -04:00
|
|
|
socket={socket}
|
2024-05-10 00:12:41 -07:00
|
|
|
setFiles={setFiles}
|
2024-05-11 18:03:42 -07:00
|
|
|
addNew={(name, type) => addNew(name, type, setFiles, sandboxData)}
|
2024-05-11 17:23:45 -07:00
|
|
|
deletingFolderId={deletingFolderId}
|
2024-05-08 23:52:08 -07:00
|
|
|
// AI Copilot Toggle
|
2024-05-04 01:50:33 -07:00
|
|
|
ai={ai}
|
|
|
|
setAi={setAi}
|
2024-04-27 14:23:09 -04:00
|
|
|
/>
|
2024-05-08 23:52:08 -07:00
|
|
|
|
|
|
|
{/* Shadcn resizeable panels: https://ui.shadcn.com/docs/components/resizable */}
|
2024-04-06 19:03:04 -04:00
|
|
|
<ResizablePanelGroup direction="horizontal">
|
|
|
|
<ResizablePanel
|
|
|
|
className="p-2 flex flex-col"
|
2024-05-09 22:16:56 -07:00
|
|
|
maxSize={80}
|
2024-04-06 19:03:04 -04:00
|
|
|
minSize={30}
|
|
|
|
defaultSize={60}
|
|
|
|
>
|
2024-05-07 00:16:02 -07:00
|
|
|
<div className="h-10 w-full flex gap-2 overflow-auto tab-scroll">
|
2024-05-08 23:52:08 -07:00
|
|
|
{/* File tabs */}
|
2024-04-26 00:10:53 -04:00
|
|
|
{tabs.map((tab) => (
|
|
|
|
<Tab
|
|
|
|
key={tab.id}
|
2024-04-27 11:08:19 -04:00
|
|
|
saved={tab.saved}
|
2024-05-06 22:59:49 -07:00
|
|
|
selected={activeFileId === tab.id}
|
2024-05-04 01:18:06 -07:00
|
|
|
onClick={(e) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
selectFile(tab);
|
2024-04-27 00:20:17 -04:00
|
|
|
}}
|
2024-05-11 17:23:45 -07:00
|
|
|
onClose={() => closeTab(tab.id)}
|
2024-04-26 00:10:53 -04:00
|
|
|
>
|
|
|
|
{tab.name}
|
|
|
|
</Tab>
|
|
|
|
))}
|
2024-04-06 19:03:04 -04:00
|
|
|
</div>
|
2024-05-08 23:52:08 -07:00
|
|
|
{/* Monaco editor */}
|
2024-05-02 17:38:37 -07:00
|
|
|
<div
|
|
|
|
ref={editorContainerRef}
|
2024-05-03 14:27:45 -07:00
|
|
|
className="grow w-full overflow-hidden rounded-md relative"
|
2024-05-02 17:38:37 -07:00
|
|
|
>
|
2024-05-06 22:59:49 -07:00
|
|
|
{!activeFileId ? (
|
2024-04-26 00:10:53 -04:00
|
|
|
<>
|
2024-05-05 14:33:09 -07:00
|
|
|
<div className="w-full h-full flex items-center justify-center text-xl font-medium text-muted-foreground/50 select-none">
|
2024-04-27 11:08:19 -04:00
|
|
|
<FileJson className="w-6 h-6 mr-3" />
|
2024-04-26 00:10:53 -04:00
|
|
|
No file selected.
|
|
|
|
</div>
|
|
|
|
</>
|
2024-05-08 23:52:08 -07:00
|
|
|
) : // Note clerk.loaded is required here due to a bug: https://github.com/clerk/javascript/issues/1643
|
|
|
|
clerk.loaded ? (
|
2024-05-03 14:27:45 -07:00
|
|
|
<>
|
|
|
|
{provider ? <Cursors yProvider={provider} /> : null}
|
|
|
|
<Editor
|
|
|
|
height="100%"
|
|
|
|
language={editorLanguage}
|
|
|
|
beforeMount={handleEditorWillMount}
|
|
|
|
onMount={handleEditorMount}
|
|
|
|
onChange={(value) => {
|
2024-05-06 22:59:49 -07:00
|
|
|
if (value === activeFileContent) {
|
2024-05-04 20:50:00 -07:00
|
|
|
setTabs((prev) =>
|
|
|
|
prev.map((tab) =>
|
2024-05-06 22:59:49 -07:00
|
|
|
tab.id === activeFileId
|
|
|
|
? { ...tab, saved: true }
|
|
|
|
: tab
|
2024-05-04 20:50:00 -07:00
|
|
|
)
|
2024-05-06 22:59:49 -07:00
|
|
|
);
|
2024-05-04 20:50:00 -07:00
|
|
|
} else {
|
|
|
|
setTabs((prev) =>
|
|
|
|
prev.map((tab) =>
|
2024-05-06 22:59:49 -07:00
|
|
|
tab.id === activeFileId
|
|
|
|
? { ...tab, saved: false }
|
|
|
|
: tab
|
2024-05-04 20:50:00 -07:00
|
|
|
)
|
2024-05-06 22:59:49 -07:00
|
|
|
);
|
2024-05-04 20:50:00 -07:00
|
|
|
}
|
2024-05-03 14:27:45 -07:00
|
|
|
}}
|
|
|
|
options={{
|
|
|
|
minimap: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
padding: {
|
|
|
|
bottom: 4,
|
|
|
|
top: 4,
|
|
|
|
},
|
|
|
|
scrollBeyondLastLine: false,
|
|
|
|
fixedOverflowWidgets: true,
|
|
|
|
fontFamily: "var(--font-geist-mono)",
|
|
|
|
}}
|
|
|
|
theme="vs-dark"
|
2024-05-06 23:34:45 -07:00
|
|
|
value={activeFileContent}
|
2024-05-03 14:27:45 -07:00
|
|
|
/>
|
|
|
|
</>
|
2024-05-05 14:33:09 -07:00
|
|
|
) : (
|
|
|
|
<div className="w-full h-full flex items-center justify-center text-xl font-medium text-muted-foreground/50 select-none">
|
|
|
|
<Loader2 className="animate-spin w-6 h-6 mr-3" />
|
|
|
|
Waiting for Clerk to load...
|
|
|
|
</div>
|
|
|
|
)}
|
2024-04-06 19:03:04 -04:00
|
|
|
</div>
|
|
|
|
</ResizablePanel>
|
|
|
|
<ResizableHandle />
|
|
|
|
<ResizablePanel defaultSize={40}>
|
|
|
|
<ResizablePanelGroup direction="vertical">
|
|
|
|
<ResizablePanel
|
2024-05-09 22:16:56 -07:00
|
|
|
ref={previewPanelRef}
|
2024-04-06 19:03:04 -04:00
|
|
|
defaultSize={50}
|
2024-05-09 22:16:56 -07:00
|
|
|
collapsedSize={4}
|
|
|
|
minSize={25}
|
|
|
|
collapsible
|
2024-04-06 19:03:04 -04:00
|
|
|
className="p-2 flex flex-col"
|
2024-05-09 22:16:56 -07:00
|
|
|
onCollapse={() => setIsPreviewCollapsed(true)}
|
|
|
|
onExpand={() => setIsPreviewCollapsed(false)}
|
2024-04-06 19:03:04 -04:00
|
|
|
>
|
2024-05-09 22:16:56 -07:00
|
|
|
<PreviewWindow
|
|
|
|
collapsed={isPreviewCollapsed}
|
|
|
|
open={() => {
|
|
|
|
previewPanelRef.current?.expand();
|
|
|
|
setIsPreviewCollapsed(false);
|
|
|
|
}}
|
|
|
|
/>
|
2024-04-06 19:03:04 -04:00
|
|
|
</ResizablePanel>
|
|
|
|
<ResizableHandle />
|
|
|
|
<ResizablePanel
|
|
|
|
defaultSize={50}
|
|
|
|
minSize={20}
|
|
|
|
className="p-2 flex flex-col"
|
|
|
|
>
|
2024-05-05 14:33:09 -07:00
|
|
|
{isOwner ? (
|
2024-05-08 23:52:08 -07:00
|
|
|
<Terminals
|
|
|
|
terminals={terminals}
|
|
|
|
setTerminals={setTerminals}
|
|
|
|
activeTerminalId={activeTerminalId}
|
|
|
|
setActiveTerminalId={setActiveTerminalId}
|
|
|
|
socket={socket}
|
|
|
|
activeTerminal={activeTerminal}
|
|
|
|
creatingTerminal={creatingTerminal}
|
|
|
|
setCreatingTerminal={setCreatingTerminal}
|
|
|
|
closingTerminal={closingTerminal}
|
|
|
|
setClosingTerminal={setClosingTerminal}
|
|
|
|
/>
|
2024-05-05 14:33:09 -07:00
|
|
|
) : (
|
|
|
|
<div className="w-full h-full flex items-center justify-center text-lg font-medium text-muted-foreground/50 select-none">
|
|
|
|
<TerminalSquare className="w-4 h-4 mr-2" />
|
|
|
|
No terminal access.
|
|
|
|
</div>
|
|
|
|
)}
|
2024-04-06 19:03:04 -04:00
|
|
|
</ResizablePanel>
|
|
|
|
</ResizablePanelGroup>
|
|
|
|
</ResizablePanel>
|
|
|
|
</ResizablePanelGroup>
|
|
|
|
</>
|
2024-05-06 22:59:49 -07:00
|
|
|
);
|
2024-04-06 19:03:04 -04:00
|
|
|
}
|