feat: implement recently played albums and sidebar shortcut preferences
This commit is contained in:
36
hooks/use-recently-played-albums.ts
Normal file
36
hooks/use-recently-played-albums.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Album } from '@/lib/navidrome';
|
||||
import { useNavidrome } from '@/app/components/NavidromeContext';
|
||||
|
||||
export function useRecentlyPlayedAlbums() {
|
||||
const [recentAlbums, setRecentAlbums] = useState<Album[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const { api } = useNavidrome();
|
||||
|
||||
const fetchRecentAlbums = async () => {
|
||||
if (!api) return;
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
const albums = await api.getAlbums('recent', 5, 0);
|
||||
setRecentAlbums(albums);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch recent albums:', error);
|
||||
setRecentAlbums([]);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchRecentAlbums();
|
||||
}, [api]);
|
||||
|
||||
return {
|
||||
recentAlbums,
|
||||
loading,
|
||||
refetch: fetchRecentAlbums
|
||||
};
|
||||
}
|
||||
29
hooks/use-sidebar-shortcuts.ts
Normal file
29
hooks/use-sidebar-shortcuts.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
export type SidebarShortcutType = 'playlists' | 'albums' | 'both';
|
||||
|
||||
export function useSidebarShortcuts() {
|
||||
const [shortcutType, setShortcutType] = useState<SidebarShortcutType>('both');
|
||||
|
||||
useEffect(() => {
|
||||
// Load preference from localStorage
|
||||
const savedType = localStorage.getItem('sidebar-shortcut-type');
|
||||
if (savedType && ['playlists', 'albums', 'both'].includes(savedType)) {
|
||||
setShortcutType(savedType as SidebarShortcutType);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const updateShortcutType = (type: SidebarShortcutType) => {
|
||||
setShortcutType(type);
|
||||
localStorage.setItem('sidebar-shortcut-type', type);
|
||||
};
|
||||
|
||||
return {
|
||||
shortcutType,
|
||||
updateShortcutType,
|
||||
showPlaylists: shortcutType === 'playlists' || shortcutType === 'both',
|
||||
showAlbums: shortcutType === 'albums' || shortcutType === 'both'
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user