feat: integrate dnd-kit for sidebar customization and reordering functionality

- Added dnd-kit dependencies to package.json and pnpm-lock.yaml.
- Implemented SidebarCustomization component using dnd-kit for drag-and-drop reordering of sidebar items.
- Created SortableItem component for individual sidebar items with visibility toggle.
- Enhanced SidebarCustomizer component with drag-and-drop support using react-beautiful-dnd.
- Updated sidebar-new component to include dynamic shortcuts for recently played albums and playlists.
- Improved user experience with import/export settings functionality and toast notifications.
This commit is contained in:
2025-07-10 17:46:59 +00:00
committed by GitHub
parent 59aae6ea31
commit 3734f67100
10 changed files with 1168 additions and 457 deletions

View File

@@ -0,0 +1,310 @@
'use client';
import React, { useState } from 'react';
import {
DndContext,
closestCenter,
KeyboardSensor,
PointerSensor,
useSensor,
useSensors,
DragEndEvent,
} from '@dnd-kit/core';
import {
arrayMove,
SortableContext,
sortableKeyboardCoordinates,
verticalListSortingStrategy,
} from '@dnd-kit/sortable';
import {
useSortable,
} from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Label } from '@/components/ui/label';
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
import { Switch } from '@/components/ui/switch';
import { Input } from '@/components/ui/input';
import {
GripVertical,
Eye,
EyeOff,
Download,
Upload,
RotateCcw,
Search,
Home,
List,
Radio,
Users,
Disc,
Music,
Heart,
Grid3X3,
Clock,
Settings
} from 'lucide-react';
import { useSidebarLayout, SidebarItem, SidebarItemType } from '@/hooks/use-sidebar-layout';
// Icon mapping
const iconMap: Record<string, React.ReactNode> = {
search: <Search className="h-4 w-4" />,
home: <Home className="h-4 w-4" />,
queue: <List className="h-4 w-4" />,
radio: <Radio className="h-4 w-4" />,
artists: <Users className="h-4 w-4" />,
albums: <Disc className="h-4 w-4" />,
playlists: <Music className="h-4 w-4" />,
favorites: <Heart className="h-4 w-4" />,
browse: <Grid3X3 className="h-4 w-4" />,
songs: <Music className="h-4 w-4" />,
history: <Clock className="h-4 w-4" />,
settings: <Settings className="h-4 w-4" />,
};
interface SortableItemProps {
item: SidebarItem;
onToggleVisibility: (id: SidebarItemType) => void;
showIcons: boolean;
}
function SortableItem({ item, onToggleVisibility, showIcons }: SortableItemProps) {
const {
attributes,
listeners,
setNodeRef,
transform,
transition,
isDragging,
} = useSortable({ id: item.id });
const style = {
transform: CSS.Transform.toString(transform),
transition,
opacity: isDragging ? 0.5 : 1,
};
return (
<div
ref={setNodeRef}
style={style}
className="flex items-center justify-between p-3 bg-secondary/50 rounded-lg border"
>
<div className="flex items-center gap-3">
<div
className="cursor-grab hover:cursor-grabbing text-muted-foreground"
{...attributes}
{...listeners}
>
<GripVertical className="h-4 w-4" />
</div>
{showIcons && (
<div className="text-muted-foreground">
{iconMap[item.icon] || <div className="h-4 w-4" />}
</div>
)}
<span className={`font-medium ${!item.visible ? 'text-muted-foreground line-through' : ''}`}>
{item.label}
</span>
</div>
<Button
variant="ghost"
size="sm"
onClick={() => onToggleVisibility(item.id)}
className="h-8 w-8 p-0"
>
{item.visible ? (
<Eye className="h-4 w-4" />
) : (
<EyeOff className="h-4 w-4" />
)}
</Button>
</div>
);
}
export function SidebarCustomization() {
const {
settings,
reorderItems,
toggleItemVisibility,
updateShortcuts,
updateShowIcons,
exportSettings,
importSettings,
resetToDefaults,
} = useSidebarLayout();
const [importFile, setImportFile] = useState<File | null>(null);
const [importing, setImporting] = useState(false);
const [importError, setImportError] = useState<string | null>(null);
const sensors = useSensors(
useSensor(PointerSensor),
useSensor(KeyboardSensor, {
coordinateGetter: sortableKeyboardCoordinates,
})
);
const handleDragEnd = (event: DragEndEvent) => {
const { active, over } = event;
if (over && active.id !== over.id) {
reorderItems(active.id as string, over.id as string);
}
};
const handleImportFile = async () => {
if (!importFile) return;
setImporting(true);
setImportError(null);
try {
await importSettings(importFile);
setImportFile(null);
// Reset file input
const fileInput = document.getElementById('settings-import') as HTMLInputElement;
if (fileInput) fileInput.value = '';
} catch (error) {
setImportError(error instanceof Error ? error.message : 'Failed to import settings');
} finally {
setImporting(false);
}
};
return (
<Card>
<CardHeader>
<CardTitle>Sidebar Customization</CardTitle>
<CardDescription>
Customize the sidebar layout, reorder items, and manage visibility settings.
</CardDescription>
</CardHeader>
<CardContent className="space-y-6">
{/* Show Icons Toggle */}
<div className="flex items-center justify-between">
<div className="space-y-0.5">
<Label>Show Icons</Label>
<div className="text-sm text-muted-foreground">
Display icons next to navigation items
</div>
</div>
<Switch
checked={settings.showIcons}
onCheckedChange={updateShowIcons}
/>
</div>
{/* Shortcut Type */}
<div className="space-y-3">
<Label>Sidebar Shortcuts</Label>
<RadioGroup
value={settings.shortcuts}
onValueChange={(value: 'albums' | 'playlists' | 'both') => updateShortcuts(value)}
>
<div className="flex items-center space-x-2">
<RadioGroupItem value="albums" id="shortcuts-albums" />
<Label htmlFor="shortcuts-albums">Albums only</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="playlists" id="shortcuts-playlists" />
<Label htmlFor="shortcuts-playlists">Playlists only</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="both" id="shortcuts-both" />
<Label htmlFor="shortcuts-both">Both albums and playlists</Label>
</div>
</RadioGroup>
</div>
{/* Navigation Items Order */}
<div className="space-y-3">
<Label>Navigation Items</Label>
<div className="text-sm text-muted-foreground mb-3">
Drag to reorder, click the eye icon to show/hide items
</div>
<DndContext
sensors={sensors}
collisionDetection={closestCenter}
onDragEnd={handleDragEnd}
>
<SortableContext items={settings.items.map(item => item.id)} strategy={verticalListSortingStrategy}>
<div className="space-y-2">
{settings.items.map((item) => (
<SortableItem
key={item.id}
item={item}
onToggleVisibility={toggleItemVisibility}
showIcons={settings.showIcons}
/>
))}
</div>
</SortableContext>
</DndContext>
</div>
{/* Settings Import/Export */}
<div className="space-y-3 pt-4 border-t">
<Label>Settings Management</Label>
<div className="flex flex-wrap gap-2">
<Button onClick={exportSettings} variant="outline">
<Download className="h-4 w-4 mr-2" />
Export Settings
</Button>
<div className="flex items-center gap-2">
<Input
id="settings-import"
type="file"
accept=".json"
onChange={(e) => setImportFile(e.target.files?.[0] || null)}
className="hidden"
/>
<Button
variant="outline"
onClick={() => document.getElementById('settings-import')?.click()}
>
<Upload className="h-4 w-4 mr-2" />
Select File
</Button>
{importFile && (
<Button
onClick={handleImportFile}
disabled={importing}
variant="default"
>
{importing ? 'Importing...' : 'Import'}
</Button>
)}
</div>
<Button onClick={resetToDefaults} variant="outline">
<RotateCcw className="h-4 w-4 mr-2" />
Reset to Default
</Button>
</div>
{importFile && (
<div className="text-sm text-muted-foreground">
Selected: {importFile.name}
</div>
)}
{importError && (
<div className="text-sm text-destructive">
Error: {importError}
</div>
)}
</div>
</CardContent>
</Card>
);
}

View File

@@ -0,0 +1,321 @@
'use client';
import React, { useState } from 'react';
import { DragDropContext, Droppable, Draggable, DropResult } from 'react-beautiful-dnd';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Switch } from '@/components/ui/switch';
import { Label } from '@/components/ui/label';
import { Badge } from '@/components/ui/badge';
import { GripVertical, Eye, EyeOff, Download, Upload, RotateCcw } from 'lucide-react';
import { useSidebarLayout, SidebarItem } from '@/hooks/use-sidebar-layout';
import { Input } from '@/components/ui/input';
import { useToast } from '@/hooks/use-toast';
export function SidebarCustomizer() {
const {
settings,
updateItemOrder,
toggleItemVisibility,
updateShortcuts,
updateShowIcons,
exportSettings,
importSettings,
resetToDefaults
} = useSidebarLayout();
const { toast } = useToast();
const [dragEnabled, setDragEnabled] = useState(false);
const handleDragEnd = (result: DropResult) => {
if (!result.destination) return;
const items = Array.from(settings.items);
const [reorderedItem] = items.splice(result.source.index, 1);
items.splice(result.destination.index, 0, reorderedItem);
updateItemOrder(items);
};
const handleFileImport = async (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (!file) return;
try {
await importSettings(file);
toast({
title: "Settings imported",
description: "Your sidebar settings have been imported successfully.",
});
} catch (error) {
toast({
title: "Import failed",
description: "Failed to import settings. Please check the file format.",
variant: "destructive",
});
}
// Reset the input
event.target.value = '';
};
const handleExport = () => {
exportSettings();
toast({
title: "Settings exported",
description: "Your settings have been downloaded as a JSON file.",
});
};
const handleReset = () => {
resetToDefaults();
toast({
title: "Settings reset",
description: "Sidebar settings have been reset to defaults.",
});
};
const getSidebarIcon = (iconId: string) => {
const iconMap: Record<string, React.ReactElement> = {
search: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<circle cx="11" cy="11" r="8" />
<path d="m21 21-4.35-4.35" />
</svg>
),
home: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<circle cx="12" cy="12" r="10" />
<polygon points="10 8 16 12 10 16 10 8" />
</svg>
),
queue: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="M3 6h18M3 12h18M3 18h18" />
</svg>
),
radio: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="M4.9 19.1C1 15.2 1 8.8 4.9 4.9"/>
<circle cx="12" cy="12" r="2"/>
</svg>
),
artists: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="m12 8-9.04 9.06a2.82 2.82 0 1 0 3.98 3.98L16 12" />
<circle cx="17" cy="7" r="5" />
</svg>
),
albums: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="m16 6 4 14" />
<path d="M12 6v14" />
<path d="M8 8v12" />
<path d="M4 4v16" />
</svg>
),
playlists: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="M21 15V6" />
<path d="M18.5 18a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5Z" />
<path d="M12 12H3" />
</svg>
),
favorites: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z" />
</svg>
),
browse: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<rect width="7" height="7" x="3" y="3" rx="1" />
<rect width="7" height="7" x="14" y="3" rx="1" />
</svg>
),
songs: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<circle cx="8" cy="18" r="4" />
<path d="M12 18V2l7 4" />
</svg>
),
history: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="M12 2C6.48 2 2 6.48 2 12c0 5.52 4.48 10 10 10 5.52 0 10-4.48 10-10 0-5.52-4.48-10-10-10Z" />
<path d="M12 8v4l4 2" />
</svg>
),
settings: (
<svg className="h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z" />
<circle cx="12" cy="12" r="3" />
</svg>
),
};
return iconMap[iconId] || iconMap.home;
};
return (
<div className="space-y-6">
{/* Global Settings */}
<Card>
<CardHeader>
<CardTitle>Sidebar Settings</CardTitle>
<CardDescription>
Customize your sidebar appearance and behavior
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="flex items-center space-x-2">
<Switch
id="show-icons"
checked={settings.showIcons}
onCheckedChange={updateShowIcons}
/>
<Label htmlFor="show-icons">Show icons</Label>
</div>
<div className="space-y-2">
<Label>Sidebar shortcuts</Label>
<div className="flex gap-2">
<Button
variant={settings.shortcuts === 'both' ? 'default' : 'outline'}
size="sm"
onClick={() => updateShortcuts('both')}
>
Both
</Button>
<Button
variant={settings.shortcuts === 'albums' ? 'default' : 'outline'}
size="sm"
onClick={() => updateShortcuts('albums')}
>
Albums only
</Button>
<Button
variant={settings.shortcuts === 'playlists' ? 'default' : 'outline'}
size="sm"
onClick={() => updateShortcuts('playlists')}
>
Playlists only
</Button>
</div>
</div>
</CardContent>
</Card>
{/* Item Management */}
<Card>
<CardHeader>
<CardTitle>Sidebar Items</CardTitle>
<CardDescription>
Drag to reorder items and toggle visibility
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex items-center space-x-2 mb-4">
<Switch
id="drag-enabled"
checked={dragEnabled}
onCheckedChange={setDragEnabled}
/>
<Label htmlFor="drag-enabled">Enable drag to reorder</Label>
</div>
<DragDropContext onDragEnd={handleDragEnd}>
<Droppable droppableId="sidebar-items" isDropDisabled={!dragEnabled}>
{(provided) => (
<div
{...provided.droppableProps}
ref={provided.innerRef}
className="space-y-2"
>
{settings.items.map((item, index) => (
<Draggable
key={item.id}
draggableId={item.id}
index={index}
isDragDisabled={!dragEnabled}
>
{(provided, snapshot) => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
className={`flex items-center justify-between p-3 border rounded-lg ${
snapshot.isDragging ? 'bg-accent' : 'bg-background'
} ${!item.visible ? 'opacity-50' : ''}`}
>
<div className="flex items-center space-x-3">
<div
{...provided.dragHandleProps}
className={`${dragEnabled ? 'cursor-grab' : 'cursor-default'}`}
>
<GripVertical className="h-4 w-4 text-muted-foreground" />
</div>
{settings.showIcons && getSidebarIcon(item.icon)}
<span className="font-medium">{item.label}</span>
{!item.visible && <Badge variant="secondary">Hidden</Badge>}
</div>
<Button
variant="ghost"
size="sm"
onClick={() => toggleItemVisibility(item.id)}
>
{item.visible ? (
<Eye className="h-4 w-4" />
) : (
<EyeOff className="h-4 w-4" />
)}
</Button>
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</Droppable>
</DragDropContext>
</CardContent>
</Card>
{/* Import/Export */}
<Card>
<CardHeader>
<CardTitle>Settings Management</CardTitle>
<CardDescription>
Export, import, or reset your settings
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="flex gap-2">
<Button onClick={handleExport} variant="outline">
<Download className="h-4 w-4 mr-2" />
Export Settings
</Button>
<div>
<Input
type="file"
accept=".json"
onChange={handleFileImport}
className="hidden"
id="import-settings"
/>
<Label htmlFor="import-settings">
<Button variant="outline" asChild>
<span>
<Upload className="h-4 w-4 mr-2" />
Import Settings
</span>
</Button>
</Label>
</div>
<Button onClick={handleReset} variant="destructive">
<RotateCcw className="h-4 w-4 mr-2" />
Reset to Defaults
</Button>
</div>
</CardContent>
</Card>
</div>
);
}

View File

@@ -79,11 +79,9 @@ const Ihateserverside: React.FC<IhateserversideProps> = ({ children }) => {
<Sidebar
playlists={playlists}
className="h-full overflow-y-auto"
collapsed={true}
visible={isSidebarVisible}
favoriteAlbums={favoriteAlbums}
onToggle={toggleSidebarCollapse}
onTransitionEnd={handleTransitionEnd}
onRemoveFavoriteAlbum={removeFavoriteAlbum}
/>
</div>
)}
@@ -123,11 +121,8 @@ const Ihateserverside: React.FC<IhateserversideProps> = ({ children }) => {
<Sidebar
playlists={playlists}
className="h-full overflow-y-auto"
collapsed={true}
visible={isSidebarVisible}
favoriteAlbums={favoriteAlbums}
onToggle={toggleSidebarCollapse}
onTransitionEnd={handleTransitionEnd}
onRemoveFavoriteAlbum={removeFavoriteAlbum}
/>
</div>

View File

@@ -0,0 +1,244 @@
'use client';
import { useState, useEffect } from 'react';
import { cn } from "@/lib/utils";
import { usePathname } from 'next/navigation';
import { Button } from "../../components/ui/button";
import { ScrollArea } from "../../components/ui/scroll-area";
import Link from "next/link";
import Image from "next/image";
import { Playlist, Album } from "@/lib/navidrome";
import {
Search,
Home,
List,
Radio,
Users,
Disc,
Music,
Heart,
Grid3X3,
Clock,
Settings,
Circle
} from "lucide-react";
import { useNavidrome } from "./NavidromeContext";
import { useRecentlyPlayedAlbums } from "@/hooks/use-recently-played-albums";
import { useSidebarShortcuts } from "@/hooks/use-sidebar-shortcuts";
import { useSidebarLayout, SidebarItem } from "@/hooks/use-sidebar-layout";
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuTrigger,
} from "@/components/ui/context-menu";
// Icon mapping for sidebar items
const iconMap: Record<string, React.ReactNode> = {
search: <Search className="h-4 w-4" />,
home: <Home className="h-4 w-4" />,
queue: <List className="h-4 w-4" />,
radio: <Radio className="h-4 w-4" />,
artists: <Users className="h-4 w-4" />,
albums: <Disc className="h-4 w-4" />,
playlists: <Music className="h-4 w-4" />,
favorites: <Heart className="h-4 w-4" />,
browse: <Grid3X3 className="h-4 w-4" />,
songs: <Circle className="h-4 w-4" />,
history: <Clock className="h-4 w-4" />,
settings: <Settings className="h-4 w-4" />,
};
interface SidebarProps extends React.HTMLAttributes<HTMLDivElement> {
playlists: Playlist[];
visible?: boolean;
favoriteAlbums?: Array<{id: string, name: string, artist: string, coverArt?: string}>;
onRemoveFavoriteAlbum?: (albumId: string) => void;
}
export function Sidebar({ className, playlists, visible = true, favoriteAlbums = [], onRemoveFavoriteAlbum }: SidebarProps) {
const pathname = usePathname();
const { api } = useNavidrome();
const { recentAlbums } = useRecentlyPlayedAlbums();
const { shortcutType } = useSidebarShortcuts();
const { settings } = useSidebarLayout();
if (!visible) {
return null;
}
// Check if a route is active
const isRouteActive = (href: string): boolean => {
if (href === '/') return pathname === '/';
return pathname.startsWith(href);
};
// Get visible navigation items
const visibleItems = settings.items.filter(item => item.visible);
return (
<div className={cn("pb-23 relative w-16", className)}>
<div className="space-y-4 py-4 pt-6">
<div className="px-3 py-2">
<div className="space-y-1">
{/* Main Navigation Items */}
{visibleItems.map((item) => (
<Link key={item.id} href={item.href}>
<Button
variant={isRouteActive(item.href) ? "secondary" : "ghost"}
className="w-full justify-center px-2"
title={item.label}
>
{settings.showIcons && (iconMap[item.icon] || <div className="h-4 w-4" />)}
</Button>
</Link>
))}
{/* Dynamic Shortcuts Section */}
{(shortcutType === 'albums' || shortcutType === 'both') && favoriteAlbums.length > 0 && (
<>
<div className="border-t my-2"></div>
{favoriteAlbums.slice(0, 5).map((album) => (
<ContextMenu key={album.id}>
<ContextMenuTrigger>
<Link href={`/album/${album.id}`}>
<Button
variant="ghost"
className="w-full justify-center px-2"
title={`${album.name} by ${album.artist}`}
>
{album.coverArt && api ? (
<Image
src={api.getCoverArtUrl(album.coverArt, 32)}
alt={album.name}
width={16}
height={16}
className="rounded"
/>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="m16 6 4 14" />
<path d="M12 6v14" />
<path d="M8 8v12" />
<path d="M4 4v16" />
</svg>
)}
</Button>
</Link>
</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
onRemoveFavoriteAlbum?.(album.id);
}}
className="text-destructive focus:text-destructive"
>
Remove from favorites
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
))}
</>
)}
{/* Recently Played Albums */}
{(shortcutType === 'albums' || shortcutType === 'both') && recentAlbums.length > 0 && (
<>
<div className="border-t my-2"></div>
{recentAlbums.slice(0, 5).map((album) => (
<Link key={album.id} href={`/album/${album.id}`}>
<Button
variant="ghost"
className="w-full justify-center px-2"
title={`${album.name} by ${album.artist} (Recently Played)`}
>
{album.coverArt && api ? (
<Image
src={api.getCoverArtUrl(album.coverArt, 32)}
alt={album.name}
width={16}
height={16}
className="rounded opacity-70"
/>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4 opacity-70"
>
<path d="m16 6 4 14" />
<path d="M12 6v14" />
<path d="M8 8v12" />
<path d="M4 4v16" />
</svg>
)}
</Button>
</Link>
))}
</>
)}
{/* Playlists Section */}
{(shortcutType === 'playlists' || shortcutType === 'both') && playlists.length > 0 && (
<>
<div className="border-t my-2"></div>
{playlists.slice(0, 5).map((playlist) => (
<Link key={playlist.id} href={`/playlist/${playlist.id}`}>
<Button
variant="ghost"
className="w-full justify-center px-2"
title={`${playlist.name} by ${playlist.owner} - ${playlist.songCount} songs`}
>
{playlist.coverArt && api ? (
<Image
src={api.getCoverArtUrl(playlist.coverArt, 32)}
alt={playlist.name}
width={16}
height={16}
className="rounded"
/>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="M21 15V6" />
<path d="M18.5 18a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5Z" />
<path d="M12 12H3" />
<path d="M16 6H3" />
<path d="M12 18H3" />
</svg>
)}
</Button>
</Link>
))}
</>
)}
</div>
</div>
</div>
</div>
);
}

View File

@@ -8,11 +8,24 @@ import { ScrollArea } from "../../components/ui/scroll-area";
import Link from "next/link";
import Image from "next/image";
import { Playlist, Album } from "@/lib/navidrome";
import { ChevronLeft, ChevronRight } from "lucide-react";
import {
Search,
Home,
List,
Radio,
Users,
Disc,
Music,
Heart,
Grid3X3,
Clock,
Settings,
Circle
} from "lucide-react";
import { useNavidrome } from "./NavidromeContext";
import { useRecentlyPlayedAlbums } from "@/hooks/use-recently-played-albums";
import { useSidebarShortcuts } from "@/hooks/use-sidebar-shortcuts";
import { useSidebarLayout, SidebarItemType } from "@/hooks/use-sidebar-layout";
import { useSidebarLayout, SidebarItem } from "@/hooks/use-sidebar-layout";
import {
ContextMenu,
ContextMenuContent,
@@ -20,474 +33,209 @@ import {
ContextMenuTrigger,
} from "@/components/ui/context-menu";
// Icon mapping for sidebar items
const iconMap: Record<string, React.ReactNode> = {
search: <Search className="h-4 w-4" />,
home: <Home className="h-4 w-4" />,
queue: <List className="h-4 w-4" />,
radio: <Radio className="h-4 w-4" />,
artists: <Users className="h-4 w-4" />,
albums: <Disc className="h-4 w-4" />,
playlists: <Music className="h-4 w-4" />,
favorites: <Heart className="h-4 w-4" />,
browse: <Grid3X3 className="h-4 w-4" />,
songs: <Circle className="h-4 w-4" />,
history: <Clock className="h-4 w-4" />,
settings: <Settings className="h-4 w-4" />,
};
interface SidebarProps extends React.HTMLAttributes<HTMLDivElement> {
playlists: Playlist[];
collapsed?: boolean;
onToggle?: () => void;
visible?: boolean;
favoriteAlbums?: Array<{id: string, name: string, artist: string, coverArt?: string}>;
onRemoveFavoriteAlbum?: (albumId: string) => void;
}
export function Sidebar({ className, playlists, collapsed = false, onToggle, visible = true, favoriteAlbums = [], onRemoveFavoriteAlbum }: SidebarProps) {
export function Sidebar({ className, playlists, visible = true, favoriteAlbums = [], onRemoveFavoriteAlbum }: SidebarProps) {
const pathname = usePathname();
const { api } = useNavidrome();
const { recentAlbums } = useRecentlyPlayedAlbums();
const { showPlaylists, showAlbums } = useSidebarShortcuts();
const { shortcutType } = useSidebarShortcuts();
const { settings } = useSidebarLayout();
if (!visible) {
return null;
}
// Define all routes and their active states
const routes = {
isRoot: pathname === "/",
isBrowse: pathname === "/browse",
isSearch: pathname === "/search",
isQueue: pathname === "/queue",
isRadio: pathname === "/radio",
isPlaylists: pathname === "/library/playlists",
isSongs: pathname === "/library/songs",
isArtists: pathname === "/library/artists",
isAlbums: pathname === "/library/albums",
isHistory: pathname === "/history",
isFavorites: pathname === "/favorites",
isSettings: pathname === "/settings",
// Handle dynamic routes
isAlbumPage: pathname.startsWith("/album/"),
isArtistPage: pathname.startsWith("/artist/"),
isPlaylistPage: pathname.startsWith("/playlist/"),
isNewPage: pathname === "/new",
// Check if a route is active
const isRouteActive = (href: string): boolean => {
if (href === '/') return pathname === '/';
return pathname.startsWith(href);
};
// Helper function to determine if any sidebar route is active
// This prevents highlights on pages not defined in sidebar
const isAnySidebarRouteActive = Object.values(routes).some(Boolean);
// Get visible navigation items
const visibleItems = settings.items.filter(item => item.visible);
return (
<div className={cn("pb-23 relative w-16", className)}>
<div className="space-y-4 py-4 pt-6">
<div className="px-3 py-2">
<p className={cn("mb-2 px-4 text-lg font-semibold tracking-tight", collapsed && "sr-only")}>
Navigation
</p>
<div className="space-y-1">
{/* Search */}
<Link href="/search">
<Button
variant={routes.isSearch ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="Search"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<circle cx="11" cy="11" r="8" />
<path d="m21 21-4.35-4.35" />
</svg>
</Button>
</Link>
{/* Home */}
<Link href="/">
<Button
variant={routes.isRoot ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="Home"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<circle cx="12" cy="12" r="10" />
<polygon points="10 8 16 12 10 16 10 8" />
</svg>
</Button>
</Link>
{/* Queue */}
<Link href="/queue">
<Button
variant={routes.isQueue ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="Queue"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="M3 6h18M3 12h18M3 18h18" />
</svg>
</Button>
</Link>
{/* Radio */}
<Link href="/radio">
<Button
variant={routes.isRadio ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="Radio"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="M4.9 19.1C1 15.2 1 8.8 4.9 4.9"/>
<path d="M7.8 16.2c-2.3-2.3-2.3-6.1 0-8.5"/>
<circle cx="12" cy="12" r="2"/>
<path d="M16.2 7.8c2.3 2.3 2.3 6.1 0 8.5"/>
<path d="M19.1 4.9C23 8.8 23 15.2 19.1 19.1"/>
</svg>
</Button>
</Link>
{/* Artists */}
<Link href="/library/artists">
<Button
variant={routes.isArtists ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="Artists"
>
<svg
className="h-4 w-4"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="m12 8-9.04 9.06a2.82 2.82 0 1 0 3.98 3.98L16 12" />
<circle cx="17" cy="7" r="5" />
</svg>
</Button>
</Link>
{/* Albums */}
<Link href="/library/albums">
<Button
variant={routes.isAlbums ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="Albums"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="m16 6 4 14" />
<path d="M12 6v14" />
<path d="M8 8v12" />
<path d="M4 4v16" />
</svg>
</Button>
</Link>
{/* Playlists */}
<Link href="/library/playlists">
<Button
variant={routes.isPlaylists ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="Playlists"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="M21 15V6" />
<path d="M18.5 18a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5Z" />
<path d="M12 12H3" />
<path d="M16 6H3" />
<path d="M12 18H3" />
</svg>
</Button>
</Link>
{/* Favorites */}
<Link href="/favorites">
<Button
variant={routes.isFavorites ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="Favorites"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z" />
</svg>
</Button>
</Link>
</div>
</div>
<div>
<div className="px-3 py-0 pt-0">
<p className={cn("mb-2 px-4 text-lg font-semibold tracking-tight sr-only")}>
Library
</p>
<div className="space-y-1">
{/* Browse */}
<Link href="/browse">
{/* Main Navigation Items */}
{visibleItems.map((item) => (
<Link key={item.id} href={item.href}>
<Button
variant={routes.isBrowse ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="Browse"
variant={isRouteActive(item.href) ? "secondary" : "ghost"}
className="w-full justify-center px-2"
title={item.label}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<rect width="7" height="7" x="3" y="3" rx="1" />
<rect width="7" height="7" x="14" y="3" rx="1" />
<rect width="7" height="7" x="14" y="14" rx="1" />
<rect width="7" height="7" x="3" y="14" rx="1" />
</svg>
{settings.showIcons && (iconMap[item.icon] || <div className="h-4 w-4" />)}
</Button>
</Link>
{/* Songs */}
<Link href="/library/songs">
<Button
variant={routes.isSongs ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="Songs"
>
<svg
className="h-4 w-4"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<circle cx="8" cy="18" r="4" />
<path d="M12 18V2l7 4" />
</svg>
</Button>
</Link>
{/* History */}
<Link href="/history">
<Button
variant={routes.isHistory ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="History"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="M12 2C6.48 2 2 6.48 2 12c0 5.52 4.48 10 10 10 5.52 0 10-4.48 10-10 0-5.52-4.48-10-10-10Z" />
<path d="M12 8v4l4 2" />
</svg>
</Button>
</Link>
{/* Favorite Albums Section */}
{showAlbums && favoriteAlbums.length > 0 && (
<>
<div className="border-t my-2"></div>
{favoriteAlbums.slice(0, 5).map((album) => (
<ContextMenu key={album.id}>
<ContextMenuTrigger asChild>
<Link href={`/album/${album.id}`}>
<Button
variant="ghost"
className="w-full justify-center px-2"
title={`${album.name} - ${album.artist}`}
>
{album.coverArt ? (
<Image
src={album.coverArt}
alt={album.name}
width={16}
height={16}
className="w-4 h-4 rounded"
/>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="m16 6 4 14" />
<path d="M12 6v14" />
<path d="M8 8v12" />
<path d="M4 4v16" />
</svg>
)}
</Button>
</Link>
</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
onRemoveFavoriteAlbum?.(album.id);
}}
className="text-destructive focus:text-destructive"
>
Remove from favorites
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
))}
</>
)}
{/* Recently Played Albums Section */}
{showAlbums && recentAlbums.length > 0 && (
<>
<div className="border-t my-2"></div>
{recentAlbums.slice(0, 5).map((album) => {
const albumImageUrl = album.coverArt && api
? api.getCoverArtUrl(album.coverArt, 32)
: '/play.png';
return (
<Link key={album.id} href={`/album/${album.id}`}>
))}
{/* Dynamic Shortcuts Section */}
{(shortcutType === 'albums' || shortcutType === 'both') && favoriteAlbums.length > 0 && (
<>
<div className="border-t my-2"></div>
{favoriteAlbums.slice(0, 5).map((album) => (
<ContextMenu key={album.id}>
<ContextMenuTrigger>
<Link href={`/album/${album.id}`}>
<Button
variant="ghost"
className="w-full justify-center px-2 h-10"
title={`${album.name} - ${album.artist} (Recently Played)`}
className="w-full justify-center px-2"
title={`${album.name} by ${album.artist}`}
>
<div className="w-6 h-6 rounded-sm overflow-hidden">
{album.coverArt && api ? (
<Image
src={albumImageUrl}
src={api.getCoverArtUrl(album.coverArt, 32)}
alt={album.name}
width={24}
height={24}
className="w-full h-full object-cover"
width={16}
height={16}
className="rounded"
/>
</div>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="m16 6 4 14" />
<path d="M12 6v14" />
<path d="M8 8v12" />
<path d="M4 4v16" />
</svg>
)}
</Button>
</Link>
);
})}
</>
)}
{/* Playlists Section */}
{showPlaylists && playlists.length > 0 && (
<>
<div className="border-t my-2"></div>
{playlists.slice(0, 5).map((playlist) => {
const playlistImageUrl = playlist.coverArt && api
? api.getCoverArtUrl(playlist.coverArt, 32)
: '/play.png'; // fallback to a music icon
return (
<Link key={playlist.id} href={`/playlist/${playlist.id}`}>
<Button
variant="ghost"
className="w-full justify-center px-2 h-10"
title={`${playlist.name} by ${playlist.owner} - ${playlist.songCount} songs`}
</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
onRemoveFavoriteAlbum?.(album.id);
}}
className="text-destructive focus:text-destructive"
>
Remove from favorites
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
))}
</>
)}
{/* Recently Played Albums */}
{(shortcutType === 'albums' || shortcutType === 'both') && recentAlbums.length > 0 && (
<>
<div className="border-t my-2"></div>
{recentAlbums.slice(0, 5).map((album) => (
<Link key={album.id} href={`/album/${album.id}`}>
<Button
variant="ghost"
className="w-full justify-center px-2"
title={`${album.name} by ${album.artist} (Recently Played)`}
>
{album.coverArt && api ? (
<Image
src={api.getCoverArtUrl(album.coverArt, 32)}
alt={album.name}
width={16}
height={16}
className="rounded opacity-70"
/>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4 opacity-70"
>
<div className="w-6 h-6 rounded-sm overflow-hidden">
<Image
src={playlistImageUrl}
alt={playlist.name}
width={24}
height={24}
className="w-full h-full object-cover"
/>
</div>
</Button>
</Link>
);
})}
</>
)}
</div>
</div>
</div>
<div className="px-3">
<div className="space-y-0">
<Link href="/settings">
<Button
variant={routes.isSettings ? "secondary" : "ghost"}
className={cn("w-full justify-center px-2")}
title="Settings"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z" />
<circle cx="12" cy="12" r="3" />
</svg>
</Button>
</Link>
<path d="m16 6 4 14" />
<path d="M12 6v14" />
<path d="M8 8v12" />
<path d="M4 4v16" />
</svg>
)}
</Button>
</Link>
))}
</>
)}
{/* Playlists Section */}
{(shortcutType === 'playlists' || shortcutType === 'both') && playlists.length > 0 && (
<>
<div className="border-t my-2"></div>
{playlists.slice(0, 5).map((playlist) => (
<Link key={playlist.id} href={`/playlist/${playlist.id}`}>
<Button
variant="ghost"
className="w-full justify-center px-2"
title={`${playlist.name} by ${playlist.owner} - ${playlist.songCount} songs`}
>
{playlist.coverArt && api ? (
<Image
src={api.getCoverArtUrl(playlist.coverArt, 32)}
alt={playlist.name}
width={16}
height={16}
className="rounded"
/>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
>
<path d="M21 15V6" />
<path d="M18.5 18a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5Z" />
<path d="M12 12H3" />
<path d="M16 6H3" />
<path d="M12 18H3" />
</svg>
)}
</Button>
</Link>
))}
</>
)}
</div>
</div>
</div>

View File

@@ -11,6 +11,7 @@ import { useNavidromeConfig } from '@/app/components/NavidromeConfigContext';
import { useToast } from '@/hooks/use-toast';
import { useStandaloneLastFm } from '@/hooks/use-standalone-lastfm';
import { useSidebarShortcuts, SidebarShortcutType } from '@/hooks/use-sidebar-shortcuts';
import { SidebarCustomization } from '@/app/components/SidebarCustomization';
import { FaServer, FaUser, FaLock, FaCheck, FaTimes, FaLastfm, FaCog } from 'react-icons/fa';
import { Settings, ExternalLink } from 'lucide-react';
@@ -693,6 +694,9 @@ const SettingsPage = () => {
</CardContent>
</Card>
{/* Sidebar Customization */}
<SidebarCustomization />
<Card className="break-inside-avoid mb-6">
<CardHeader>
<CardTitle>Appearance</CardTitle>