From 1b6bd019895f7ee1eb04baaef377764fd96b141b Mon Sep 17 00:00:00 2001 From: Ishaan Dey Date: Sat, 27 Apr 2024 00:28:00 -0400 Subject: [PATCH] sandbox data route + cosmetic changes --- backend/database/src/index.ts | 14 ++++++++++++-- frontend/app/(app)/code/[id]/page.tsx | 11 +++++++++-- frontend/components/editor/index.tsx | 3 ++- frontend/components/editor/navbar/index.tsx | 12 +++++++++--- frontend/lib/utils.ts | 10 ++++++++++ 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/backend/database/src/index.ts b/backend/database/src/index.ts index 1423579..8630cf7 100644 --- a/backend/database/src/index.ts +++ b/backend/database/src/index.ts @@ -46,8 +46,18 @@ export default { }); return success; - - // } else if (path === "/api/sandbox/files") { + } else if (path === "/api/sandbox" && method === "GET") { + const params = url.searchParams; + if (params.has("id")) { + const id = params.get("id") as string; + const res = await db.query.sandbox.findFirst({ + where: (sandbox, { eq }) => eq(sandbox.id, id), + }); + return json(res ?? {}); + } else { + const res = await db.select().from(sandbox).all(); + return json(res ?? {}); + } } else if (path === "/api/user") { if (method === "GET") { const params = url.searchParams; diff --git a/frontend/app/(app)/code/[id]/page.tsx b/frontend/app/(app)/code/[id]/page.tsx index d6a5442..b0d646d 100644 --- a/frontend/app/(app)/code/[id]/page.tsx +++ b/frontend/app/(app)/code/[id]/page.tsx @@ -1,5 +1,5 @@ import Navbar from "@/components/editor/navbar" -import { User } from "@/lib/types" +import { Sandbox, User } from "@/lib/types" import { currentUser } from "@clerk/nextjs" import dynamic from "next/dynamic" import { redirect } from "next/navigation" @@ -14,6 +14,12 @@ const getUserData = async (id: string) => { return userData } +const getSandboxData = async (id: string) => { + const sandboxRes = await fetch(`http://localhost:8787/api/sandbox?id=${id}`) + const sandboxData: Sandbox = await sandboxRes.json() + return sandboxData +} + export default async function CodePage({ params }: { params: { id: string } }) { const user = await currentUser() const sandboxId = params.id @@ -23,10 +29,11 @@ export default async function CodePage({ params }: { params: { id: string } }) { } const userData = await getUserData(user.id) + const sandboxData = await getSandboxData(sandboxId) return (
- +
diff --git a/frontend/components/editor/index.tsx b/frontend/components/editor/index.tsx index 003454f..0be779e 100644 --- a/frontend/components/editor/index.tsx +++ b/frontend/components/editor/index.tsx @@ -23,6 +23,7 @@ import { TFile, TFileData, TFolder } from "./sidebar/types" import { io } from "socket.io-client" import { set } from "zod" +import { processFileType } from "@/lib/utils" export default function CodeEditor({ userId, @@ -92,7 +93,7 @@ export default function CodeEditor({ socket.emit("getFile", tab.id, (response: string) => { setActiveFile(response) }) - setEditorLanguage(tab.name.split(".").pop() ?? "plaintext") + setEditorLanguage(processFileType(tab.name)) setActiveId(tab.id) } diff --git a/frontend/components/editor/navbar/index.tsx b/frontend/components/editor/navbar/index.tsx index 0afd848..11363d3 100644 --- a/frontend/components/editor/navbar/index.tsx +++ b/frontend/components/editor/navbar/index.tsx @@ -2,10 +2,16 @@ import Image from "next/image" import Logo from "@/assets/logo.svg" import { Pencil } from "lucide-react" import Link from "next/link" -import { User } from "@/lib/types" +import { Sandbox, User } from "@/lib/types" import UserButton from "@/components/ui/userButton" -export default function Navbar({ userData }: { userData: User }) { +export default function Navbar({ + userData, + sandboxData, +}: { + userData: User + sandboxData: Sandbox +}) { return (
@@ -16,7 +22,7 @@ export default function Navbar({ userData }: { userData: User }) { Logo
- My React Project{" "} + {sandboxData.name}
diff --git a/frontend/lib/utils.ts b/frontend/lib/utils.ts index d084cca..e0a02f3 100644 --- a/frontend/lib/utils.ts +++ b/frontend/lib/utils.ts @@ -4,3 +4,13 @@ import { twMerge } from "tailwind-merge" export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + +export function processFileType(file: string) { + const ending = file.split(".").pop() + + if (ending === "ts" || ending === "tsx") return "typescript" + if (ending === "js" || ending === "jsx") return "javascript" + + if (ending) return ending + return "plaintext" +}