refactor: sidebar

This commit is contained in:
2025-12-18 22:52:00 +01:00
parent 5244275264
commit a37554d205
3 changed files with 115 additions and 87 deletions

View File

@@ -1,5 +1,6 @@
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';
import { cookies } from 'next/headers';
import '../globals.css';
import Navbar from '@/components/app/NavBar/NavBar';
import { SessionProvider } from '@/lib/providers/SessionProvider';
@@ -31,6 +32,9 @@ export default async function RootLayout({
children: React.ReactNode;
}>) {
const sessionData = await validateRequest();
const cookieStore = await cookies();
const defaultOpen = cookieStore.get('sidebar:state')?.value === 'true';
return (
<html lang="en">
<body className={cn('flex flex-col h-screen', inter.className)}>
@@ -47,7 +51,7 @@ export default async function RootLayout({
/>
<ConfirmDialogProvider>
<NuqsAdapter>
<SidebarProvider>
<SidebarProvider defaultOpen={defaultOpen}>
<StreamInfoProvider>
{/* this promise is ugly but i'm lazy to fix the type errors */}
<Navbar editLivestream={Promise.resolve(<EditLivestream />)} />

View File

@@ -1,8 +1,8 @@
'use client';
import * as React from 'react';
import { ChevronDown, ChevronUp } from 'lucide-react';
import { Avatar } from '@/components/ui/avatar';
import { ChevronDown, ChevronUp, Radio } from 'lucide-react';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import {
Sidebar as UISidebar,
SidebarContent,
@@ -13,131 +13,153 @@ import {
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarTrigger,
useSidebar,
} from '@/components/ui/sidebar';
import { StreamInfoResponse, useStreams } from '@/lib/providers/StreamInfoProvider';
import { useRouter } from 'next/navigation';
import { Skeleton } from '@/components/ui/skeleton';
import { useAllChannels } from '@/lib/hooks/useUserList';
import { cn } from '@/lib/utils';
import { Separator } from '@/components/ui/separator';
export default function Sidebar({ ...props }: React.ComponentProps<typeof UISidebar>) {
const { channels: stream, isLoading } = useAllChannels(5000);
const [followedExpanded, setFollowedExpanded] = React.useState(true);
const { state } = useSidebar();
const isCollapsed = state === 'collapsed';
if (isLoading) return <SidebarSkeleton />;
if (isLoading) return <SidebarSkeleton {...props} />;
const liveStreamers = stream?.filter((s) => s.isLive) || [];
const offlineStreamers = stream?.filter((s) => !s.isLive) || [];
return (
<UISidebar {...props}>
<UISidebar collapsible="icon" {...props}>
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel asChild>
<button
onClick={() => setFollowedExpanded(!followedExpanded)}
className="w-full flex items-center justify-between"
>
<span>Live Channels</span>
{followedExpanded ? (
<ChevronUp className="h-4 w-4" />
) : (
<ChevronDown className="h-4 w-4" />
)}
</button>
<SidebarGroupLabel className="flex items-center justify-between px-2 py-1.5">
<span className="text-xs font-semibold uppercase text-muted-foreground group-data-[collapsible=icon]:opacity-0 transition-opacity duration-200">
Live Channels
</span>
<span className="text-xs text-muted-foreground group-data-[collapsible=icon]:opacity-0 transition-opacity duration-200">
{liveStreamers.length}
</span>
</SidebarGroupLabel>
{followedExpanded && (
<SidebarGroupContent>
<SidebarMenu>
{liveStreamers.map((streamer) => (
<StreamerItem key={streamer.id} streamer={streamer} />
))}
</SidebarMenu>
</SidebarGroupContent>
)}
<SidebarGroupContent>
<SidebarMenu>
{liveStreamers.length === 0 && !isCollapsed && (
<div className="px-4 py-2 text-sm text-muted-foreground">
No channels live
</div>
)}
{liveStreamers.map((streamer) => (
<StreamerItem key={streamer.id} streamer={streamer} isCollapsed={isCollapsed} />
))}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
{offlineStreamers.length > 0 && (
<SidebarGroup>
<SidebarGroupLabel>Offline Channels</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
{offlineStreamers.map((streamer) => (
<StreamerItem key={streamer.id} streamer={streamer} />
))}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
)}
<Separator className="group-data-[collapsible=icon]:block hidden" />
<SidebarGroup>
<SidebarGroupLabel className="flex items-center justify-between px-2 py-1.5">
<span className="text-xs font-semibold uppercase text-muted-foreground group-data-[collapsible=icon]:opacity-0 transition-opacity duration-200">
Offline Channels
</span>
</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
{offlineStreamers.map((streamer) => (
<StreamerItem key={streamer.id} streamer={streamer} isCollapsed={isCollapsed} />
))}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
</UISidebar>
);
}
function StreamerItem({ streamer }: { streamer: StreamInfoResponse[0] }) {
function StreamerItem({ streamer, isCollapsed }: { streamer: StreamInfoResponse[0], isCollapsed: boolean }) {
const router = useRouter();
return (
<SidebarMenuItem key={streamer.id} className={streamer.isLive ? '' : '*:text-muted-foreground'}>
<SidebarMenuButton className="flex items-center gap-3 h-full" onClick={() => {
router.push(`/${streamer.username}`);
}}>
<div className="relative">
<Avatar className="h-9 w-9">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={streamer.channel.pfpUrl}
alt={streamer.username}
width={36}
height={36}
className="rounded-full h-9 w-9 object-cover"
loading="lazy"
/>
</Avatar>
{streamer.isLive && (
<span className="absolute -top-1 -right-1 w-3 h-3 bg-primary rounded-full border-2 border-black" />
<SidebarMenuItem>
<SidebarMenuButton
asChild
tooltip={streamer.username}
className="h-12"
onClick={() => router.push(`/${streamer.username}`)}
>
<button className="flex w-full items-center gap-3">
<div className="relative flex-shrink-0">
<Avatar className="h-8 w-8">
<AvatarImage src={streamer.channel.pfpUrl} alt={streamer.username} className="object-cover" />
<AvatarFallback>{streamer.username[0]?.toUpperCase()}</AvatarFallback>
</Avatar>
{streamer.isLive && (
<span className="absolute -bottom-0.5 -right-0.5 flex h-3 w-3 items-center justify-center rounded-full bg-background ring-2 ring-background">
<span className="h-2 w-2 rounded-full bg-red-500 animate-pulse" />
</span>
)}
</div>
{!isCollapsed && (
<div className="flex flex-1 flex-col items-start overflow-hidden">
<div className="flex w-full items-center justify-between">
<span className="truncate font-medium text-sm leading-none">
{streamer.username}
</span>
{streamer.isLive && (
<div className="flex items-center gap-1 text-xs text-red-500">
<span className="h-1.5 w-1.5 rounded-full bg-red-500" />
<span>{streamer.viewers}</span>
</div>
)}
</div>
<span className="truncate text-xs text-muted-foreground w-full text-left">
{streamer.isLive ? streamer.title || streamer.category || 'Live' : 'Offline'}
</span>
</div>
)}
</div>
<div className="flex-1">
<p className="font-medium truncate">{streamer.username}</p>
<p className="text-sm truncate">{streamer.category}</p>
{streamer.isLive && (
<p className="text-sm">
{streamer.viewers} viewer{streamer.viewers === 1 ? '' : 's'}
</p>
)}
</div>
</button>
</SidebarMenuButton>
</SidebarMenuItem>
);
}
function SidebarSkeleton({ ...props }: React.ComponentProps<typeof UISidebar>) {
const { state } = useSidebar();
const isCollapsed = state === 'collapsed';
return (
<UISidebar {...props}>
<UISidebar collapsible="icon" {...props}>
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel asChild>
<button className="w-full flex items-center justify-between">
<span>Live Channels</span>
<ChevronUp className="h-4 w-4" />
</button>
<SidebarGroupLabel className="px-2 py-1.5">
<Skeleton className="h-4 w-24 group-data-[collapsible=icon]:opacity-0 transition-opacity duration-200" />
</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
{Array(3).fill(0).map((_, i) => (
<StreamerItemSkeleton key={i} />
<StreamerItemSkeleton key={i} isCollapsed={isCollapsed} />
))}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
<Separator className="group-data-[collapsible=icon]:block hidden" />
<SidebarGroup>
<SidebarGroupLabel>Offline Channels</SidebarGroupLabel>
<SidebarGroupLabel className="px-2 py-1.5">
<Skeleton className="h-4 w-24 group-data-[collapsible=icon]:opacity-0 transition-opacity duration-200" />
</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
{Array(5).fill(0).map((_, i) => (
<StreamerItemSkeleton key={i} />
<StreamerItemSkeleton key={i} isCollapsed={isCollapsed} />
))}
</SidebarMenu>
</SidebarGroupContent>
@@ -147,16 +169,18 @@ function SidebarSkeleton({ ...props }: React.ComponentProps<typeof UISidebar>) {
);
}
function StreamerItemSkeleton() {
function StreamerItemSkeleton({ isCollapsed }: { isCollapsed: boolean }) {
return (
<SidebarMenuItem>
<SidebarMenuButton className="flex items-center gap-3 h-full">
<div className="relative">
<Skeleton className="h-9 w-9 rounded-full" />
</div>
<div className="flex-1 space-y-2">
<Skeleton className="h-4 w-24" />
<Skeleton className="h-3 w-16" />
<SidebarMenuButton className="h-12">
<div className="flex w-full items-center gap-3">
<Skeleton className="h-8 w-8 rounded-full flex-shrink-0" />
{!isCollapsed && (
<div className="flex-1 space-y-1.5">
<Skeleton className="h-3.5 w-24" />
<Skeleton className="h-3 w-16" />
</div>
)}
</div>
</SidebarMenuButton>
</SidebarMenuItem>

View File

@@ -23,7 +23,7 @@ const SIDEBAR_COOKIE_NAME = "sidebar:state"
const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7
const SIDEBAR_WIDTH = "16rem"
const SIDEBAR_WIDTH_MOBILE = "18rem"
const SIDEBAR_WIDTH_ICON = "3rem"
const SIDEBAR_WIDTH_ICON = "4rem"
const SIDEBAR_KEYBOARD_SHORTCUT = "b"
type SidebarContext = {
@@ -512,7 +512,7 @@ const SidebarMenuItem = React.forwardRef<
SidebarMenuItem.displayName = "SidebarMenuItem"
const sidebarMenuButtonVariants = cva(
"peer/menu-button flex w-full items-center gap-2 overflow-hidden rounded-md p-2 text-left text-sm outline-none ring-sidebar-ring transition-[width,height,padding] hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 active:bg-sidebar-accent active:text-sidebar-accent-foreground disabled:pointer-events-none disabled:opacity-50 group-has-[[data-sidebar=menu-action]]/menu-item:pr-8 aria-disabled:pointer-events-none aria-disabled:opacity-50 data-[active=true]:bg-sidebar-accent data-[active=true]:font-medium data-[active=true]:text-sidebar-accent-foreground data-[state=open]:hover:bg-sidebar-accent data-[state=open]:hover:text-sidebar-accent-foreground group-data-[collapsible=icon]:!size-8 group-data-[collapsible=icon]:!p-2 [&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0",
"peer/menu-button flex w-full items-center gap-2 overflow-hidden rounded-md p-2 text-left text-sm outline-none ring-sidebar-ring transition-[width,height,padding] hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 active:bg-sidebar-accent active:text-sidebar-accent-foreground disabled:pointer-events-none disabled:opacity-50 group-has-[[data-sidebar=menu-action]]/menu-item:pr-8 aria-disabled:pointer-events-none aria-disabled:opacity-50 data-[active=true]:bg-sidebar-accent data-[active=true]:font-medium data-[active=true]:text-sidebar-accent-foreground data-[state=open]:hover:bg-sidebar-accent data-[state=open]:hover:text-sidebar-accent-foreground group-data-[collapsible=icon]:!size-12 group-data-[collapsible=icon]:!p-2 [&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0",
{
variants: {
variant: {