feat: make role switching drive navigation and view changes
This commit is contained in:
+31
-21
@@ -40,18 +40,37 @@ const roleLabels: Record<UserRole, string> = {
|
|||||||
owner: "店主",
|
owner: "店主",
|
||||||
}
|
}
|
||||||
|
|
||||||
const navLinks = [
|
|
||||||
{ href: "/", label: "首页" },
|
|
||||||
{ href: "/community", label: "社区" },
|
|
||||||
{ href: "/orders", label: "订单" },
|
|
||||||
{ href: "/chat", label: "消息" },
|
|
||||||
]
|
|
||||||
|
|
||||||
export function Header() {
|
export function Header() {
|
||||||
const [mobileOpen, setMobileOpen] = useState(false)
|
const [mobileOpen, setMobileOpen] = useState(false)
|
||||||
const pathname = usePathname()
|
const pathname = usePathname()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { isAuthenticated, currentRole, switchRole, logout } = useAuthStore()
|
const { isAuthenticated, currentRole, verifiedRoles, switchRole, logout } = useAuthStore()
|
||||||
|
|
||||||
|
const navLinks =
|
||||||
|
currentRole === "consumer"
|
||||||
|
? [
|
||||||
|
{ href: "/", label: "首页" },
|
||||||
|
{ href: "/search", label: "找陪玩" },
|
||||||
|
{ href: "/community", label: "社区" },
|
||||||
|
{ href: "/chat", label: "消息" },
|
||||||
|
]
|
||||||
|
: [
|
||||||
|
{ href: "/", label: "首页" },
|
||||||
|
{ href: "/community", label: "社区" },
|
||||||
|
{ href: "/orders", label: "订单" },
|
||||||
|
{ href: "/chat", label: "消息" },
|
||||||
|
{ href: "/dashboard", label: "管理后台" },
|
||||||
|
]
|
||||||
|
|
||||||
|
const availableRoles = (Object.entries(roleLabels) as [UserRole, string][]).filter(([role]) =>
|
||||||
|
verifiedRoles.includes(role),
|
||||||
|
)
|
||||||
|
|
||||||
|
const handleRoleSwitch = (role: UserRole) => {
|
||||||
|
switchRole(role)
|
||||||
|
router.push(role === "consumer" ? "/" : "/dashboard")
|
||||||
|
setMobileOpen(false)
|
||||||
|
}
|
||||||
|
|
||||||
const unreadCount = mockNotifications.filter((n) => !n.read).length
|
const unreadCount = mockNotifications.filter((n) => !n.read).length
|
||||||
|
|
||||||
@@ -106,10 +125,10 @@ export function Header() {
|
|||||||
<DropdownMenuContent align="end">
|
<DropdownMenuContent align="end">
|
||||||
<DropdownMenuLabel>切换身份</DropdownMenuLabel>
|
<DropdownMenuLabel>切换身份</DropdownMenuLabel>
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
{(Object.entries(roleLabels) as [UserRole, string][]).map(([role, label]) => (
|
{availableRoles.map(([role, label]) => (
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
key={role}
|
key={role}
|
||||||
onClick={() => switchRole(role)}
|
onClick={() => handleRoleSwitch(role)}
|
||||||
className={cn(currentRole === role && "bg-accent")}
|
className={cn(currentRole === role && "bg-accent")}
|
||||||
>
|
>
|
||||||
{label}
|
{label}
|
||||||
@@ -275,15 +294,6 @@ export function Header() {
|
|||||||
>
|
>
|
||||||
设置
|
设置
|
||||||
</Link>
|
</Link>
|
||||||
{(currentRole === "player" || currentRole === "owner") && (
|
|
||||||
<Link
|
|
||||||
href="/dashboard"
|
|
||||||
onClick={() => setMobileOpen(false)}
|
|
||||||
className="px-3 py-2 rounded-md text-sm font-medium text-muted-foreground hover:text-foreground hover:bg-accent/50"
|
|
||||||
>
|
|
||||||
管理后台
|
|
||||||
</Link>
|
|
||||||
)}
|
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</nav>
|
</nav>
|
||||||
@@ -292,13 +302,13 @@ export function Header() {
|
|||||||
<div className="border-t pt-4">
|
<div className="border-t pt-4">
|
||||||
<p className="px-3 text-xs text-muted-foreground mb-2">切换身份</p>
|
<p className="px-3 text-xs text-muted-foreground mb-2">切换身份</p>
|
||||||
<div className="flex gap-1 px-3">
|
<div className="flex gap-1 px-3">
|
||||||
{(Object.entries(roleLabels) as [UserRole, string][]).map(([role, label]) => (
|
{availableRoles.map(([role, label]) => (
|
||||||
<Button
|
<Button
|
||||||
key={role}
|
key={role}
|
||||||
variant={currentRole === role ? "default" : "outline"}
|
variant={currentRole === role ? "default" : "outline"}
|
||||||
size="sm"
|
size="sm"
|
||||||
className="text-xs flex-1"
|
className="text-xs flex-1"
|
||||||
onClick={() => switchRole(role)}
|
onClick={() => handleRoleSwitch(role)}
|
||||||
>
|
>
|
||||||
{label}
|
{label}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user