From 20d790c5e0f47549156eac316c79be085146835a Mon Sep 17 00:00:00 2001 From: ngc2207 Date: Sat, 14 Dec 2024 18:49:35 +0800 Subject: [PATCH] feat(playground): add session management and folder open icon in PlaygroundSidebar component --- package.json | 1 + src/{app => }/actions/index.ts | 2 + .../components/playground-sidebar.tsx | 65 ++++++++++++++----- src/app/playground/layout.tsx | 53 ++++++++------- 4 files changed, 79 insertions(+), 42 deletions(-) rename src/{app => }/actions/index.ts (97%) diff --git a/package.json b/package.json index 97d8984..99012cd 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "cross-fetch": "^4.0.0", + "devicons-react": "^1.4.0", "gitea-js": "^1.22.0", "jotai": "^2.10.3", "lucide-react": "^0.468.0", diff --git a/src/app/actions/index.ts b/src/actions/index.ts similarity index 97% rename from src/app/actions/index.ts rename to src/actions/index.ts index 171e1f0..b7af701 100644 --- a/src/app/actions/index.ts +++ b/src/actions/index.ts @@ -1,3 +1,5 @@ +"use server"; + import api from "@/lib/gitea"; import { GitEntry, APIError } from "gitea-js"; diff --git a/src/app/playground/components/playground-sidebar.tsx b/src/app/playground/components/playground-sidebar.tsx index 4cee51f..17e7aaf 100644 --- a/src/app/playground/components/playground-sidebar.tsx +++ b/src/app/playground/components/playground-sidebar.tsx @@ -1,4 +1,5 @@ -import { auth } from "@/auth"; +"use client"; + import * as React from "react"; import { Sidebar, @@ -18,8 +19,10 @@ import { CollapsibleContent, CollapsibleTrigger, } from "@/components/ui/collapsible"; -import { retrieveTreeStructure } from "@/app/actions"; -import { ChevronRight, File, Folder } from "lucide-react"; +import { useSession } from "next-auth/react"; +import { retrieveTreeStructure } from "@/actions"; +import { COriginal, JavaOriginal } from "devicons-react"; +import { ChevronRight, File, Folder, FolderOpen } from "lucide-react"; interface FileTree { name: string; @@ -69,20 +72,32 @@ export function buildFileTree(tree: GitEntry[]): FileTree { return root; } -export async function PlaygroundSidebar({ +export function PlaygroundSidebar({ ...props }: React.ComponentProps) { - const session = await auth(); + const { data: session } = useSession(); + const [fileTree, setFileTree] = React.useState({ + name: "", + type: "tree", + children: {}, + }); + const username = session?.user?.name ?? ""; - let fileTree: FileTree = { name: "", type: "tree", children: {} }; - if (username) { - const tree: GitEntry[] = await retrieveTreeStructure( - username, - "playground", - "main" - ); - fileTree = buildFileTree(tree); - } + + React.useEffect(() => { + async function fetchFileTree() { + if (username) { + const tree: GitEntry[] = await retrieveTreeStructure( + username, + "playground", + "main" + ); + const newFileTree = buildFileTree(tree); + setFileTree(newFileTree); + } + } + fetchFileTree(); + }, [username]); return ( @@ -106,10 +121,22 @@ export async function PlaygroundSidebar({ function Tree({ item }: { item: FileTree }) { const { name, type, children } = item; + const fileExtension = name.split(".").pop(); + + let fileIcon = ; + + if (fileExtension === "c") { + fileIcon = ; + } else if (fileExtension === "java") { + fileIcon = ; + } + + const [isOpen, setIsOpen] = React.useState(false); + if (type === "blob") { return ( - + {fileIcon} {name} ); @@ -117,11 +144,15 @@ function Tree({ item }: { item: FileTree }) { return ( - + - + {isOpen ? : } {name} diff --git a/src/app/playground/layout.tsx b/src/app/playground/layout.tsx index fc90e5b..0b2961f 100644 --- a/src/app/playground/layout.tsx +++ b/src/app/playground/layout.tsx @@ -9,6 +9,7 @@ import { BreadcrumbList, BreadcrumbPage, } from "@/components/ui/breadcrumb"; +import { SessionProvider } from "next-auth/react"; import { Separator } from "@/components/ui/separator"; import { ModeSwitcher } from "@/components/mode-switcher"; import LanguageSwitcher from "@/components/language-switcher"; @@ -18,30 +19,32 @@ export default function PlaygroundLayout({ children, }: Readonly<{ children: React.ReactNode }>) { return ( - - - -
-
- - - - - - - - Project Management & Task Tracking - - - - -
-
- -
-
- {children} -
-
+ + + + +
+
+ + + + + + + + Project Management & Task Tracking + + + + +
+
+ +
+
+ {children} +
+
+
); }