222 lines
6.9 KiB
TypeScript
222 lines
6.9 KiB
TypeScript
"use client"
|
|
|
|
import {
|
|
Disc3,
|
|
House,
|
|
LayoutGrid,
|
|
ListMusic,
|
|
LogOut,
|
|
Mic,
|
|
Music2,
|
|
Search,
|
|
Settings,
|
|
Star,
|
|
} from "lucide-react"
|
|
|
|
import { usePathname, useRouter } from "next/navigation"
|
|
import dynamic from "next/dynamic"
|
|
import { useAuthActions } from "@convex-dev/auth/react"
|
|
|
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu"
|
|
import {
|
|
Sidebar,
|
|
SidebarContent,
|
|
SidebarFooter,
|
|
SidebarGroup,
|
|
SidebarGroupContent,
|
|
SidebarGroupLabel,
|
|
SidebarHeader,
|
|
SidebarMenu,
|
|
SidebarMenuButton,
|
|
SidebarMenuItem,
|
|
useSidebar,
|
|
} from "@/components/ui/sidebar"
|
|
import { ThemeToggle } from "@/components/theme-toggle"
|
|
import Image from "next/image"
|
|
import Link from "next/link"
|
|
import { useTheme } from "next-themes"
|
|
import * as React from "react"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import { useCachedQuery } from "@/hooks/use-cached-query"
|
|
import { useMounted } from "@/hooks/use-mounted"
|
|
import { clearPersistentCache } from "@/lib/persistent-cache"
|
|
import { resetPlaybackStores } from "@otunes/convex/client/playback-store"
|
|
|
|
const OptionsModal = dynamic(() =>
|
|
import("@/components/options-modal").then((module) => module.OptionsModal)
|
|
)
|
|
|
|
const browse = [
|
|
{ title: "Home", path: "/", icon: House },
|
|
{ title: "Search", path: "/search", icon: Search },
|
|
]
|
|
|
|
const library = [
|
|
{ title: "Albums", path: "/library/albums", icon: Disc3 },
|
|
{ title: "Artists", path: "/library/artists", icon: Mic },
|
|
{ title: "Songs", path: "/library/songs", icon: Music2 },
|
|
]
|
|
|
|
export function AppSidebar() {
|
|
const { setOpenMobile } = useSidebar()
|
|
const pathname = usePathname()
|
|
const router = useRouter()
|
|
const { signOut } = useAuthActions()
|
|
const user = useCachedQuery(api.auth.currentUser)
|
|
const sidebarPlaylists = useCachedQuery(api.playlists.listSidebarPlaylists)
|
|
const fallbackLetters = user?.name?.match(/\b(\w)/g)?.join("") || "NA"
|
|
|
|
const { resolvedTheme } = useTheme()
|
|
const mounted = useMounted()
|
|
const [optionsOpen, setOptionsOpen] = React.useState(false)
|
|
|
|
async function handleSignOut() {
|
|
await signOut()
|
|
resetPlaybackStores()
|
|
await clearPersistentCache()
|
|
router.push("/signin")
|
|
}
|
|
|
|
const isDark = mounted && resolvedTheme === "dark"
|
|
|
|
const playlistNav = [
|
|
{ title: "All Playlists", path: "/playlists", icon: LayoutGrid },
|
|
{ title: "Favorites", path: "/playlist/favorites", icon: Star },
|
|
...(sidebarPlaylists?.owned ?? []).map((playlist) => ({
|
|
title: playlist.title,
|
|
path: `/playlist/${playlist.id}`,
|
|
icon: ListMusic,
|
|
})),
|
|
...(sidebarPlaylists?.subscribed ?? []).map((playlist) => ({
|
|
title: playlist.title,
|
|
path: `/playlist/${playlist.id}`,
|
|
icon: ListMusic,
|
|
})),
|
|
]
|
|
|
|
return (
|
|
<Sidebar>
|
|
<SidebarHeader>
|
|
<div className="flex items-center gap-1 px-2 py-1">
|
|
<Image
|
|
src={isDark ? "/logo-dark.png" : "/logo.png"}
|
|
alt={"oTunes"}
|
|
width={20}
|
|
height={20}
|
|
className="size-5"
|
|
/>
|
|
<span className="text-lg font-semibold tracking-tight">oTunes</span>
|
|
</div>
|
|
</SidebarHeader>
|
|
|
|
<SidebarContent>
|
|
<SidebarGroup>
|
|
<SidebarGroupContent>
|
|
<SidebarMenu>
|
|
{browse.map((item) => (
|
|
<SidebarMenuItem key={item.title}>
|
|
<SidebarMenuButton isActive={pathname === item.path} asChild>
|
|
<Link href={item.path} onClick={() => setOpenMobile(false)}>
|
|
<item.icon />
|
|
<span>{item.title}</span>
|
|
</Link>
|
|
</SidebarMenuButton>
|
|
</SidebarMenuItem>
|
|
))}
|
|
</SidebarMenu>
|
|
</SidebarGroupContent>
|
|
</SidebarGroup>
|
|
|
|
<SidebarGroup>
|
|
<SidebarGroupLabel>Library</SidebarGroupLabel>
|
|
<SidebarGroupContent>
|
|
<SidebarMenu>
|
|
{library.map((item) => (
|
|
<SidebarMenuItem key={item.title}>
|
|
<SidebarMenuButton
|
|
isActive={
|
|
pathname === item.path ||
|
|
pathname.startsWith(`${item.path}/`)
|
|
}
|
|
asChild
|
|
>
|
|
<Link href={item.path} onClick={() => setOpenMobile(false)}>
|
|
<item.icon />
|
|
<span>{item.title}</span>
|
|
</Link>
|
|
</SidebarMenuButton>
|
|
</SidebarMenuItem>
|
|
))}
|
|
</SidebarMenu>
|
|
</SidebarGroupContent>
|
|
</SidebarGroup>
|
|
|
|
<SidebarGroup>
|
|
<SidebarGroupLabel>Playlists</SidebarGroupLabel>
|
|
<SidebarGroupContent>
|
|
<SidebarMenu>
|
|
{playlistNav.map((item) => (
|
|
<SidebarMenuItem key={item.path}>
|
|
<SidebarMenuButton isActive={pathname === item.path} asChild>
|
|
<Link href={item.path} onClick={() => setOpenMobile(false)}>
|
|
<item.icon />
|
|
<span>{item.title}</span>
|
|
</Link>
|
|
</SidebarMenuButton>
|
|
</SidebarMenuItem>
|
|
))}
|
|
</SidebarMenu>
|
|
</SidebarGroupContent>
|
|
</SidebarGroup>
|
|
</SidebarContent>
|
|
|
|
<SidebarFooter>
|
|
<SidebarMenu>
|
|
<SidebarMenuItem className="flex items-center justify-around gap-1">
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<SidebarMenuButton size="lg" className="min-w-0 flex-1">
|
|
<Avatar size="sm" key={user?.image ?? "fallback"}>
|
|
{user?.image ? (
|
|
<AvatarImage src={user.image} alt={user.name} />
|
|
) : null}
|
|
<AvatarFallback>{fallbackLetters}</AvatarFallback>
|
|
</Avatar>
|
|
<span className="truncate">{user?.name || "N/A"}</span>
|
|
</SidebarMenuButton>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent side="top" align="start" className="w-40">
|
|
<DropdownMenuItem
|
|
variant="destructive"
|
|
className="justify-between"
|
|
onClick={handleSignOut}
|
|
>
|
|
Sign Out
|
|
<LogOut />
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem
|
|
className="justify-between"
|
|
onClick={() => setOptionsOpen(true)}
|
|
>
|
|
Options
|
|
<Settings />
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
{optionsOpen ? (
|
|
<OptionsModal open onOpenChange={setOptionsOpen} />
|
|
) : null}
|
|
<ThemeToggle />
|
|
</SidebarMenuItem>
|
|
</SidebarMenu>
|
|
</SidebarFooter>
|
|
</Sidebar>
|
|
)
|
|
}
|