feat(playground): integrate Gitea API for file tree retrieval and update PlaygroundSidebar component
This commit is contained in:
parent
a872a33052
commit
08831481b8
3
.gitignore
vendored
3
.gitignore
vendored
@ -44,3 +44,6 @@ cache
|
|||||||
|
|
||||||
# bun
|
# bun
|
||||||
bun.lockb
|
bun.lockb
|
||||||
|
|
||||||
|
# vscode
|
||||||
|
.vscode
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
"@radix-ui/react-tooltip": "^1.1.4",
|
"@radix-ui/react-tooltip": "^1.1.4",
|
||||||
"class-variance-authority": "^0.7.1",
|
"class-variance-authority": "^0.7.1",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
|
"cross-fetch": "^4.0.0",
|
||||||
"gitea-js": "^1.22.0",
|
"gitea-js": "^1.22.0",
|
||||||
"jotai": "^2.10.3",
|
"jotai": "^2.10.3",
|
||||||
"lucide-react": "^0.468.0",
|
"lucide-react": "^0.468.0",
|
||||||
|
22
src/app/actions/index.ts
Normal file
22
src/app/actions/index.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import api from "@/lib/gitea";
|
||||||
|
import { GitEntry, APIError } from "gitea-js";
|
||||||
|
|
||||||
|
export async function retrieveTreeStructure(
|
||||||
|
owner: string,
|
||||||
|
repo: string,
|
||||||
|
sha: string
|
||||||
|
): Promise<GitEntry[]> {
|
||||||
|
try {
|
||||||
|
const response = await api.repos.getTree(owner, repo, sha, {
|
||||||
|
recursive: true,
|
||||||
|
});
|
||||||
|
return response.data.tree || [];
|
||||||
|
} catch (error) {
|
||||||
|
if ((error as APIError).message) {
|
||||||
|
console.error("Gitea API Error", error);
|
||||||
|
} else {
|
||||||
|
console.error("Unexpected Error", error);
|
||||||
|
}
|
||||||
|
throw new Error("Failed to retrieve tree structure");
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
import { auth } from "@/auth";
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import {
|
import {
|
||||||
Sidebar,
|
Sidebar,
|
||||||
@ -11,45 +12,78 @@ import {
|
|||||||
SidebarMenuSub,
|
SidebarMenuSub,
|
||||||
SidebarRail,
|
SidebarRail,
|
||||||
} from "@/components/ui/sidebar";
|
} from "@/components/ui/sidebar";
|
||||||
|
import { GitEntry } from "gitea-js";
|
||||||
import {
|
import {
|
||||||
Collapsible,
|
Collapsible,
|
||||||
CollapsibleContent,
|
CollapsibleContent,
|
||||||
CollapsibleTrigger,
|
CollapsibleTrigger,
|
||||||
} from "@/components/ui/collapsible";
|
} from "@/components/ui/collapsible";
|
||||||
|
import { retrieveTreeStructure } from "@/app/actions";
|
||||||
import { ChevronRight, File, Folder } from "lucide-react";
|
import { ChevronRight, File, Folder } from "lucide-react";
|
||||||
|
|
||||||
const data = {
|
interface FileTree {
|
||||||
tree: [
|
name: string;
|
||||||
[
|
type: "blob" | "tree";
|
||||||
"app",
|
children?: { [key: string]: FileTree };
|
||||||
[
|
}
|
||||||
"api",
|
|
||||||
["hello", ["route.ts"]],
|
|
||||||
"page.tsx",
|
|
||||||
"layout.tsx",
|
|
||||||
["blog", ["page.tsx"]],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
[
|
|
||||||
"components",
|
|
||||||
["ui", "button.tsx", "card.tsx"],
|
|
||||||
"header.tsx",
|
|
||||||
"footer.tsx",
|
|
||||||
],
|
|
||||||
["lib", ["util.ts"]],
|
|
||||||
["public", "favicon.ico", "vercel.svg"],
|
|
||||||
".eslintrc.json",
|
|
||||||
".gitignore",
|
|
||||||
"next.config.js",
|
|
||||||
"tailwind.config.js",
|
|
||||||
"package.json",
|
|
||||||
"README.md",
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
export function PlaygroundSidebar({
|
export function buildFileTree(tree: GitEntry[]): FileTree {
|
||||||
|
const root: FileTree = { name: "", type: "tree", children: {} };
|
||||||
|
|
||||||
|
tree.forEach((entry: GitEntry) => {
|
||||||
|
if (!entry.path) return;
|
||||||
|
|
||||||
|
const pathParts = entry.path.split("/");
|
||||||
|
let currentLevel = root;
|
||||||
|
|
||||||
|
pathParts.forEach((part: string) => {
|
||||||
|
if (!currentLevel.children) {
|
||||||
|
currentLevel.children = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!currentLevel.children[part]) {
|
||||||
|
currentLevel.children[part] = {
|
||||||
|
name: part,
|
||||||
|
type: entry.type as "blob" | "tree",
|
||||||
|
children: {},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
currentLevel = currentLevel.children[part];
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (root.children) {
|
||||||
|
const sortedChildren = Object.values(root.children).sort((a, b) => {
|
||||||
|
if (a.type === "tree" && b.type === "blob") return -1;
|
||||||
|
if (a.type === "blob" && b.type === "tree") return 1;
|
||||||
|
return a.name.localeCompare(b.name);
|
||||||
|
});
|
||||||
|
|
||||||
|
root.children = sortedChildren.reduce((acc, child) => {
|
||||||
|
acc[child.name] = child;
|
||||||
|
return acc;
|
||||||
|
}, {} as { [key: string]: FileTree });
|
||||||
|
}
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function PlaygroundSidebar({
|
||||||
...props
|
...props
|
||||||
}: React.ComponentProps<typeof Sidebar>) {
|
}: React.ComponentProps<typeof Sidebar>) {
|
||||||
|
const session = await auth();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Sidebar {...props}>
|
<Sidebar {...props}>
|
||||||
<SidebarContent>
|
<SidebarContent>
|
||||||
@ -57,9 +91,10 @@ export function PlaygroundSidebar({
|
|||||||
<SidebarGroupLabel>Files</SidebarGroupLabel>
|
<SidebarGroupLabel>Files</SidebarGroupLabel>
|
||||||
<SidebarGroupContent>
|
<SidebarGroupContent>
|
||||||
<SidebarMenu>
|
<SidebarMenu>
|
||||||
{data.tree.map((item, index) => (
|
{fileTree.children &&
|
||||||
<Tree key={index} item={item} />
|
Object.values(fileTree.children).map((item, index) => (
|
||||||
))}
|
<Tree key={index} item={item} />
|
||||||
|
))}
|
||||||
</SidebarMenu>
|
</SidebarMenu>
|
||||||
</SidebarGroupContent>
|
</SidebarGroupContent>
|
||||||
</SidebarGroup>
|
</SidebarGroup>
|
||||||
@ -69,25 +104,20 @@ export function PlaygroundSidebar({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function Tree({ item }: { item: string | any[] }) {
|
function Tree({ item }: { item: FileTree }) {
|
||||||
const [name, ...items] = Array.isArray(item) ? item : [item];
|
const { name, type, children } = item;
|
||||||
if (!items.length) {
|
if (type === "blob") {
|
||||||
return (
|
return (
|
||||||
<SidebarMenuButton
|
<SidebarMenuButton className="data-[active=true]:bg-transparent">
|
||||||
isActive={name === "button.tsx"}
|
|
||||||
className="data-[active=true]:bg-transparent"
|
|
||||||
>
|
|
||||||
<File />
|
<File />
|
||||||
{name}
|
{name}
|
||||||
</SidebarMenuButton>
|
</SidebarMenuButton>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SidebarMenuItem>
|
<SidebarMenuItem>
|
||||||
<Collapsible
|
<Collapsible className="group/collapsible [&[data-state=open]>button>svg:first-child]:rotate-90">
|
||||||
className="group/collapsible [&[data-state=open]>button>svg:first-child]:rotate-90"
|
|
||||||
defaultOpen={name === "components" || name === "ui"}
|
|
||||||
>
|
|
||||||
<CollapsibleTrigger asChild>
|
<CollapsibleTrigger asChild>
|
||||||
<SidebarMenuButton>
|
<SidebarMenuButton>
|
||||||
<ChevronRight className="transition-transform" />
|
<ChevronRight className="transition-transform" />
|
||||||
@ -97,9 +127,10 @@ function Tree({ item }: { item: string | any[] }) {
|
|||||||
</CollapsibleTrigger>
|
</CollapsibleTrigger>
|
||||||
<CollapsibleContent>
|
<CollapsibleContent>
|
||||||
<SidebarMenuSub>
|
<SidebarMenuSub>
|
||||||
{items.map((subItem, index) => (
|
{children &&
|
||||||
<Tree key={index} item={subItem} />
|
Object.values(children).map((subItem, index) => (
|
||||||
))}
|
<Tree key={index} item={subItem} />
|
||||||
|
))}
|
||||||
</SidebarMenuSub>
|
</SidebarMenuSub>
|
||||||
</CollapsibleContent>
|
</CollapsibleContent>
|
||||||
</Collapsible>
|
</Collapsible>
|
||||||
|
13
src/lib/gitea.ts
Normal file
13
src/lib/gitea.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import fetch from "cross-fetch";
|
||||||
|
import { giteaApi } from "gitea-js";
|
||||||
|
|
||||||
|
if (!process.env.GITEA_URL) {
|
||||||
|
throw new Error("GITEA_URL environment variable is not defined");
|
||||||
|
}
|
||||||
|
|
||||||
|
const api = giteaApi(process.env.GITEA_URL, {
|
||||||
|
token: process.env.GITEA_TOKEN,
|
||||||
|
customFetch: fetch,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default api;
|
Loading…
Reference in New Issue
Block a user