| 1 | "use client"; |
| 2 | |
| 3 | import Link from "next/link" |
| 4 | import { usePathname, useParams |
| 5 | import { useTranslations } |
| 6 | import { |
| 7 | Sidebar, |
| 8 | SidebarHeader, |
| 9 | SidebarContent, |
| 10 | SidebarGroup, |
| 11 | SidebarGroupContent, |
| 12 | SidebarMenu, |
| 13 | SidebarMenuItem, |
| 14 | SidebarMenuButton, |
| 15 | SidebarTrigger, |
| 16 | useSidebar, |
| 17 | } from "@struxa/ui/components/sidebar"; |
| 18 | import { |
| 19 | DropdownMenu, |
| 20 | DropdownMenuContent, |
| 21 | DropdownMenuItem, |
| 22 | DropdownMenuSeparator, |
| 23 | DropdownMenuTrigger, |
| 24 | } from "@struxa/ui/components/dropdown-menu"; |
| 25 | import { DitherAvatar } |
| 26 | import { |
| 27 | Server, |
| 28 | User, |
| 29 | Settings, |
| 30 | Terminal, |
| 31 | FolderOpen, |
| 32 | Database, |
| 33 | Clock, |
| 34 | Users, |
| 35 | Archive, |
| 36 | Globe, |
| 37 | Activity, |
| 38 | LogOut, |
| 39 | ShieldCheck, |
| 40 | ChevronLeft, |
| 41 | Wallet, |
| 42 | ShoppingBag, |
| 43 | Package, |
| 44 | Box, |
| 45 | } from "lucide-react"; |
| 46 | import { authClient } |
| 47 | import { parseEggFeatures } |
| 48 | import { useQuery } |
| 49 | import { orpc } |
| 50 | |
| 51 | export function PanelSidebar() |
| 52 | const t = |
| 53 | const tc = |
| 54 | const { state |
| 55 | const pathname = |
| 56 | const params = |
| 57 | const router = |
| 58 | const { data: |
| 59 | const user = |
| 60 | const avatarImage = |
| 61 | |
| 62 | async function handleLogout |
| 63 | await authClient.signOut |
| 64 | router.push("/login |
| 65 | } |
| 66 | |
| 67 | const isServerPage = |
| 68 | const serverId = |
| 69 | const activeServerTab = |
| 70 | |
| 71 | const { data: |
| 72 | const { data: |
| 73 | ...orpc.billing. |
| 74 | enabled: !!billingConfig?.enabled |
| 75 | }); |
| 76 | |
| 77 | const { data: |
| 78 | ...orpc.servers. |
| 79 | enabled: !!serverId, |
| 80 | select: (s) |
| 81 | }); |
| 82 | |
| 83 | function formatBalance(cents |
| 84 | try { |
| 85 | return new Intl |
| 86 | } catch { |
| 87 | return (cents / |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | const NAV_PRIMARY = |
| 92 | { key: "servers", |
| 93 | ...(billingConfig?.enabled |
| 94 | { key: "wallet", |
| 95 | { key: "shop", |
| 96 | ] : []), |
| 97 | { key: "account", |
| 98 | ]; |
| 99 | |
| 100 | const hasPlugins = |
| 101 | const hasMods = |
| 102 | |
| 103 | const NAV_SERVER = |
| 104 | { key: "console", |
| 105 | { key: "files", |
| 106 | ...(hasPlugins ? |
| 107 | ...(hasMods ? |
| 108 | { key: "databases", |
| 109 | { key: "schedules", |
| 110 | { key: "users", |
| 111 | { key: "backups", |
| 112 | { key: "network", |
| 113 | { key: "settings", |
| 114 | { key: "activity", |
| 115 | ]; |
| 116 | |
| 117 | return ( |
| 118 | <Sidebar collapsible=" |
| 119 | <SidebarHeader className=" |
| 120 | <div className="hidden group-data-[collapsible=icon]:flex justify-center py-2 |
| 121 | <DropdownMenu> |
| 122 | <DropdownMenuTrigger className=" |
| 123 | {avatarImage ? ( |
| 124 | // eslint-disable-next-line @next/next/no-img-element |
| 125 | <img src={avatarImage |
| 126 | ) : ( |
| 127 | <DitherAvatar name={ |
| 128 | )} |
| 129 | </DropdownMenuTrigger> |
| 130 | <DropdownMenuContent side=" |
| 131 | {user?.role === |
| 132 | <DropdownMenuItem |
| 133 | onClick={() => |
| 134 | className="flex cursor-pointer items-center gap-2.5 rounded-lg px-3 py-2 text-sm text-muted-foreground hover:bg-muted hover:text-foreground focus:bg-muted focus:text-foreground" |
| 135 | > |
| 136 | <ShieldCheck className=" |
| 137 | {tc("adminPanel") |
| 138 | </DropdownMenuItem> |
| 139 | )} |
| 140 | {user?.role === |
| 141 | <DropdownMenuItem |
| 142 | onClick={handleLogout} |
| 143 | className="flex cursor-pointer items-center gap-2.5 rounded-lg px-3 py-2 text-sm text-muted-foreground hover:bg-muted hover:text-foreground focus:bg-muted focus:text-foreground" |
| 144 | > |
| 145 | <LogOut className=" |
| 146 | {tc("signOut") |
| 147 | </DropdownMenuItem> |
| 148 | </DropdownMenuContent> |
| 149 | </DropdownMenu> |
| 150 | </div> |
| 151 | |
| 152 | <div className="group-data-[collapsible=icon]:hidden px-2 py-1.5 |
| 153 | <DropdownMenu> |
| 154 | <DropdownMenuTrigger className=" |
| 155 | <div className="flex h-6 w-6 shrink-0 items-center justify-center rounded-md bg-muted text-xs font-semibold text-foreground overflow-hidden |
| 156 | {avatarImage ? ( |
| 157 | // eslint-disable-next-line @next/next/no-img-element |
| 158 | <img src={avatarImage |
| 159 | ) : ( |
| 160 | <DitherAvatar name={ |
| 161 | )} |
| 162 | </div> |
| 163 | <div className="min-w-0 flex-1 text-left |
| 164 | <p className="truncate text-sm font-medium text-foreground |
| 165 | </div> |
| 166 | {billingConfig?.enabled && |
| 167 | <span className="shrink-0 text-[10px] font-semibold tabular-nums text-muted-foreground bg-muted rounded px-1.5 py-0.5 leading-none |
| 168 | {formatBalance(wallet.balanceCents |
| 169 | </span> |
| 170 | )} |
| 171 | </DropdownMenuTrigger> |
| 172 | <DropdownMenuContent side=" |
| 173 | {user?.role === |
| 174 | <DropdownMenuItem |
| 175 | onClick={() => |
| 176 | className="flex cursor-pointer items-center gap-2.5 rounded-lg px-3 py-2 text-sm text-muted-foreground hover:bg-muted hover:text-foreground focus:bg-muted focus:text-foreground" |
| 177 | > |
| 178 | <ShieldCheck className=" |
| 179 | {tc("adminPanel") |
| 180 | </DropdownMenuItem> |
| 181 | )} |
| 182 | {user?.role === |
| 183 | <DropdownMenuItem |
| 184 | onClick={handleLogout} |
| 185 | className="flex cursor-pointer items-center gap-2.5 rounded-lg px-3 py-2 text-sm text-muted-foreground hover:bg-muted hover:text-foreground focus:bg-muted focus:text-foreground" |
| 186 | > |
| 187 | <LogOut className=" |
| 188 | {tc("signOut") |
| 189 | </DropdownMenuItem> |
| 190 | </DropdownMenuContent> |
| 191 | </DropdownMenu> |
| 192 | </div> |
| 193 | </SidebarHeader> |
| 194 | |
| 195 | <SidebarContent className=" |
| 196 | {isServerPage ? ( |
| 197 | <SidebarGroup className=" |
| 198 | {state === "expanded |
| 199 | <Link |
| 200 | href="/" |
| 201 | className="mb-1 flex items-center gap-1.5 px-3 py-1.5 text-xs text-muted-foreground transition-colors hover:text-foreground" |
| 202 | > |
| 203 | <ChevronLeft className=" |
| 204 | {t("panel.allServers") |
| 205 | </Link> |
| 206 | )} |
| 207 | <SidebarGroupContent> |
| 208 | <SidebarMenu> |
| 209 | {NAV_SERVER.map(( |
| 210 | <SidebarMenuItem key={ |
| 211 | <SidebarMenuButton |
| 212 | render={ |
| 213 | <Link |
| 214 | href={ |
| 215 | item.key === |
| 216 | ? (`/servers/${serverId |
| 217 | : (`/servers/${serverId |
| 218 | } |
| 219 | /> |
| 220 | } |
| 221 | isActive={item.key |
| 222 | tooltip={item.label |
| 223 | className="h-auto gap-2 rounded-lg py-2 px-3 text-sm transition-colors duration-150" |
| 224 | > |
| 225 | <item.icon className=" |
| 226 | {item.label} |
| 227 | </SidebarMenuButton> |
| 228 | </SidebarMenuItem> |
| 229 | ))} |
| 230 | </SidebarMenu> |
| 231 | </SidebarGroupContent> |
| 232 | </SidebarGroup> |
| 233 | ) : ( |
| 234 | <> |
| 235 | <SidebarGroup className=" |
| 236 | <SidebarGroupContent> |
| 237 | <SidebarMenu> |
| 238 | {NAV_PRIMARY.map(( |
| 239 | <SidebarMenuItem key={ |
| 240 | <SidebarMenuButton |
| 241 | render={<Link |
| 242 | isActive={ |
| 243 | item.key === |
| 244 | item.key === |
| 245 | pathname.startsWith(item |
| 246 | } |
| 247 | tooltip={item.label |
| 248 | className="h-auto gap-2 rounded-lg py-2 px-3 text-sm transition-colors duration-150" |
| 249 | > |
| 250 | <item.icon className=" |
| 251 | {item.label} |
| 252 | </SidebarMenuButton> |
| 253 | </SidebarMenuItem> |
| 254 | ))} |
| 255 | </SidebarMenu> |
| 256 | </SidebarGroupContent> |
| 257 | </SidebarGroup> |
| 258 | |
| 259 | </> |
| 260 | )} |
| 261 | </SidebarContent> |
| 262 | |
| 263 | <div className="border-t border-sidebar-border |
| 264 | <div className="hidden group-data-[collapsible=icon]:flex justify-center py-3 |
| 265 | <SidebarTrigger className=" |
| 266 | </div> |
| 267 | <div className="group-data-[collapsible=icon]:hidden flex items-center gap-2 px-4 py-3 |
| 268 | <SidebarTrigger className=" |
| 269 | <div> |
| 270 | <p className="text-[10px] text-muted-foreground/50 |
| 271 | <p className="text-[10px] text-muted-foreground/40 |
| 272 | {process.env.NODE_ENV |
| 273 | ? "in-dev" |
| 274 | : [process.env |
| 275 | </p> |
| 276 | </div> |
| 277 | </div> |
| 278 | </div> |
| 279 | </Sidebar> |
| 280 | ); |
| 281 | } |