refactor(app-sidebar): support external user prop and remove internal user data

This commit is contained in:
cfngc4594 2025-03-17 10:45:23 +08:00
parent 36c6dd138f
commit cb966297e1
2 changed files with 92 additions and 18 deletions

View File

@ -0,0 +1,73 @@
import {
SidebarInset,
SidebarProvider,
SidebarTrigger,
} from "@/components/ui/sidebar";
import { auth } from "@/lib/auth";
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbList,
BreadcrumbPage,
BreadcrumbSeparator,
} from "@/components/ui/breadcrumb";
import { User } from "@prisma/client";
import { redirect } from "next/navigation";
import { AppSidebar } from "@/components/app-sidebar";
import { Separator } from "@/components/ui/separator";
interface AdminDashboardLayoutProps {
children: React.ReactNode;
}
export default async function AdminDashboardLayout({
children,
}: AdminDashboardLayoutProps) {
const session = await auth();
if (!session?.user) {
redirect("/sign-in");
}
const user = (({ name, email, image }) => ({
name: name ?? "",
email: email ?? "",
avatar: image ?? "",
}))(session.user as User);
return (
<SidebarProvider>
<AppSidebar user={user} />
<SidebarInset>
<header className="flex h-16 shrink-0 items-center gap-2 transition-[width,height] ease-linear group-has-[[data-collapsible=icon]]/sidebar-wrapper:h-12">
<div className="flex items-center gap-2 px-4">
<SidebarTrigger className="-ml-1" />
<Separator orientation="vertical" className="mr-2 h-4" />
<Breadcrumb>
<BreadcrumbList>
<BreadcrumbItem className="hidden md:block">
<BreadcrumbLink href="#">
Building Your Application
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator className="hidden md:block" />
<BreadcrumbItem>
<BreadcrumbPage>Data Fetching</BreadcrumbPage>
</BreadcrumbItem>
</BreadcrumbList>
</Breadcrumb>
</div>
</header>
<div className="flex flex-1 flex-col gap-4 p-4 pt-0">
{children}
</div>
</SidebarInset>
</SidebarProvider>
);
}

View File

@ -1,6 +1,5 @@
"use client" "use client";
import * as React from "react"
import { import {
AudioWaveform, AudioWaveform,
BookOpen, BookOpen,
@ -12,27 +11,21 @@ import {
PieChart, PieChart,
Settings2, Settings2,
SquareTerminal, SquareTerminal,
} from "lucide-react" } from "lucide-react";
import * as React from "react";
import { NavMain } from "@/components/nav-main" import { NavMain } from "@/components/nav-main";
import { NavProjects } from "@/components/nav-projects" import { NavUser } from "@/components/nav-user";
import { NavUser } from "@/components/nav-user" import { NavProjects } from "@/components/nav-projects";
import { TeamSwitcher } from "@/components/team-switcher" import { TeamSwitcher } from "@/components/team-switcher";
import { import {
Sidebar, Sidebar,
SidebarContent, SidebarContent,
SidebarFooter, SidebarFooter,
SidebarHeader, SidebarHeader,
SidebarRail, SidebarRail,
} from "@/components/ui/sidebar" } from "@/components/ui/sidebar";
// This is sample data.
const data = { const data = {
user: {
name: "shadcn",
email: "m@example.com",
avatar: "/avatars/shadcn.jpg",
},
teams: [ teams: [
{ {
name: "Acme Inc", name: "Acme Inc",
@ -154,9 +147,17 @@ const data = {
icon: Map, icon: Map,
}, },
], ],
};
interface AppSidebarProps extends React.ComponentProps<typeof Sidebar> {
user: {
name: string;
email: string;
avatar: string;
};
} }
export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) { export function AppSidebar({ user, ...props }: AppSidebarProps) {
return ( return (
<Sidebar collapsible="icon" {...props}> <Sidebar collapsible="icon" {...props}>
<SidebarHeader> <SidebarHeader>
@ -167,9 +168,9 @@ export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
<NavProjects projects={data.projects} /> <NavProjects projects={data.projects} />
</SidebarContent> </SidebarContent>
<SidebarFooter> <SidebarFooter>
<NavUser user={data.user} /> <NavUser user={user} />
</SidebarFooter> </SidebarFooter>
<SidebarRail /> <SidebarRail />
</Sidebar> </Sidebar>
) );
} }