- Add backend endpoint for /discover/playlists API - Add PlaylistTagFilter dropdown component for filtering by genre - Add QobuzPlaylistCard with CSS blur background effect (color-thief style) - Add HorizontalScrollRow header snippet for custom headers with scroll controls - Add session cache store for caching API responses - Add i18n keys for playlist filter and discover section cc
385 lines
9.8 KiB
TypeScript
385 lines
9.8 KiB
TypeScript
/**
|
|
* Home Settings Store
|
|
*
|
|
* Manages Home view personalization settings with localStorage persistence.
|
|
* Handles section ordering, visibility, and greeting customization.
|
|
*/
|
|
|
|
// Section identifiers
|
|
export type HomeSectionId =
|
|
| 'newReleases'
|
|
| 'pressAwards'
|
|
| 'mostStreamed'
|
|
| 'qobuzissimes'
|
|
| 'editorPicks'
|
|
| 'qobuzPlaylists'
|
|
| 'essentialDiscography'
|
|
| 'recentAlbums'
|
|
| 'continueTracks'
|
|
| 'topArtists'
|
|
| 'favoriteAlbums';
|
|
|
|
export interface HomeSection {
|
|
id: HomeSectionId;
|
|
label: string;
|
|
visible: boolean;
|
|
source?: 'qobuz' | 'ml'; // qobuz = editorial, ml = ML-powered recommendations
|
|
limit?: number; // configurable item count
|
|
}
|
|
|
|
export interface HomeSettings {
|
|
sections: HomeSection[];
|
|
greeting: {
|
|
enabled: boolean;
|
|
customText: string | null; // null = use default time-based greeting
|
|
};
|
|
limits: {
|
|
recentAlbums: number;
|
|
continueTracks: number;
|
|
topArtists: number;
|
|
favoriteAlbums: number;
|
|
favoriteTracks: number;
|
|
featuredAlbums: number;
|
|
};
|
|
}
|
|
|
|
const STORAGE_KEY = 'qbz-home-settings';
|
|
|
|
// Default section order and visibility
|
|
const DEFAULT_SECTIONS: HomeSection[] = [
|
|
{ id: 'newReleases', label: 'New Releases', visible: true, source: 'qobuz' },
|
|
{ id: 'pressAwards', label: 'Press Awards', visible: true, source: 'qobuz' },
|
|
{ id: 'mostStreamed', label: 'Popular Albums', visible: false, source: 'qobuz' },
|
|
{ id: 'qobuzissimes', label: 'Qobuzissimes', visible: false, source: 'qobuz' },
|
|
{ id: 'editorPicks', label: "Editor's Picks", visible: false, source: 'qobuz' },
|
|
{ id: 'qobuzPlaylists', label: 'Qobuz Playlists', visible: true, source: 'qobuz' },
|
|
{ id: 'essentialDiscography', label: 'Essential Discography', visible: true, source: 'qobuz' },
|
|
{ id: 'recentAlbums', label: 'Recently Played', visible: true, source: 'ml' },
|
|
{ id: 'continueTracks', label: 'Continue Listening', visible: true, source: 'ml' },
|
|
{ id: 'topArtists', label: 'Your Top Artists', visible: true, source: 'ml' },
|
|
{ id: 'favoriteAlbums', label: 'More From Favorites', visible: true, source: 'ml' },
|
|
];
|
|
|
|
const DEFAULT_SETTINGS: HomeSettings = {
|
|
sections: DEFAULT_SECTIONS,
|
|
greeting: {
|
|
enabled: true,
|
|
customText: null
|
|
},
|
|
limits: {
|
|
recentAlbums: 20,
|
|
continueTracks: 10,
|
|
topArtists: 20,
|
|
favoriteAlbums: 12,
|
|
favoriteTracks: 10,
|
|
featuredAlbums: 12
|
|
}
|
|
};
|
|
|
|
// State
|
|
let settings: HomeSettings = loadSettings();
|
|
const listeners = new Set<() => void>();
|
|
|
|
function notifyListeners(): void {
|
|
for (const listener of listeners) {
|
|
listener();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load settings from localStorage
|
|
*/
|
|
function loadSettings(): HomeSettings {
|
|
try {
|
|
const saved = localStorage.getItem(STORAGE_KEY);
|
|
if (saved) {
|
|
const parsed = JSON.parse(saved) as Partial<HomeSettings>;
|
|
// Merge with defaults to handle new sections added later
|
|
return mergeWithDefaults(parsed);
|
|
}
|
|
} catch (err) {
|
|
console.error('Failed to load home settings:', err);
|
|
}
|
|
return { ...DEFAULT_SETTINGS, sections: [...DEFAULT_SECTIONS] };
|
|
}
|
|
|
|
/**
|
|
* Merge saved settings with defaults (handles new sections)
|
|
*/
|
|
function mergeWithDefaults(saved: Partial<HomeSettings>): HomeSettings {
|
|
const result: HomeSettings = {
|
|
sections: [],
|
|
greeting: saved.greeting ?? DEFAULT_SETTINGS.greeting,
|
|
limits: { ...DEFAULT_SETTINGS.limits, ...(saved.limits || {}) }
|
|
};
|
|
|
|
// Build section map from saved
|
|
const savedSectionMap = new Map<HomeSectionId, HomeSection>();
|
|
if (saved.sections) {
|
|
for (const section of saved.sections) {
|
|
savedSectionMap.set(section.id, section);
|
|
}
|
|
}
|
|
|
|
// Use saved order, adding any missing sections at the end
|
|
const usedIds = new Set<HomeSectionId>();
|
|
|
|
if (saved.sections) {
|
|
for (const section of saved.sections) {
|
|
const defaultSection = DEFAULT_SECTIONS.find(s => s.id === section.id);
|
|
if (defaultSection) {
|
|
result.sections.push({
|
|
id: section.id,
|
|
label: defaultSection.label, // Always use current label
|
|
visible: section.visible,
|
|
source: defaultSection.source // Always use default source
|
|
});
|
|
usedIds.add(section.id);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Add any new sections that weren't in saved settings
|
|
for (const defaultSection of DEFAULT_SECTIONS) {
|
|
if (!usedIds.has(defaultSection.id)) {
|
|
result.sections.push({ ...defaultSection });
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Save settings to localStorage
|
|
*/
|
|
function saveSettings(): void {
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
|
|
} catch (err) {
|
|
console.error('Failed to save home settings:', err);
|
|
}
|
|
}
|
|
|
|
// ============ Public API ============
|
|
|
|
/**
|
|
* Subscribe to settings changes
|
|
*/
|
|
export function subscribe(listener: () => void): () => void {
|
|
listeners.add(listener);
|
|
// Don't call immediately - caller should use getSettings() for initial state
|
|
return () => listeners.delete(listener);
|
|
}
|
|
|
|
/**
|
|
* Get current settings
|
|
*/
|
|
export function getSettings(): HomeSettings {
|
|
return settings;
|
|
}
|
|
|
|
/**
|
|
* Get visible sections in order
|
|
*/
|
|
export function getVisibleSections(): HomeSection[] {
|
|
return settings.sections.filter(s => s.visible);
|
|
}
|
|
|
|
/**
|
|
* Toggle section visibility
|
|
*/
|
|
export function toggleSectionVisibility(sectionId: HomeSectionId): void {
|
|
const section = settings.sections.find(s => s.id === sectionId);
|
|
if (section) {
|
|
section.visible = !section.visible;
|
|
saveSettings();
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set section visibility
|
|
*/
|
|
export function setSectionVisibility(sectionId: HomeSectionId, visible: boolean): void {
|
|
const section = settings.sections.find(s => s.id === sectionId);
|
|
if (section) {
|
|
section.visible = visible;
|
|
saveSettings();
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Move section up in order
|
|
*/
|
|
export function moveSectionUp(sectionId: HomeSectionId): void {
|
|
const index = settings.sections.findIndex(s => s.id === sectionId);
|
|
if (index > 0) {
|
|
const temp = settings.sections[index];
|
|
settings.sections[index] = settings.sections[index - 1];
|
|
settings.sections[index - 1] = temp;
|
|
saveSettings();
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Move section down in order
|
|
*/
|
|
export function moveSectionDown(sectionId: HomeSectionId): void {
|
|
const index = settings.sections.findIndex(s => s.id === sectionId);
|
|
if (index >= 0 && index < settings.sections.length - 1) {
|
|
const temp = settings.sections[index];
|
|
settings.sections[index] = settings.sections[index + 1];
|
|
settings.sections[index + 1] = temp;
|
|
saveSettings();
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reorder sections by providing new order of IDs
|
|
*/
|
|
export function reorderSections(newOrder: HomeSectionId[]): void {
|
|
const sectionMap = new Map(settings.sections.map(s => [s.id, s]));
|
|
const reordered: HomeSection[] = [];
|
|
|
|
for (const id of newOrder) {
|
|
const section = sectionMap.get(id);
|
|
if (section) {
|
|
reordered.push(section);
|
|
}
|
|
}
|
|
|
|
settings.sections = reordered;
|
|
saveSettings();
|
|
notifyListeners();
|
|
}
|
|
|
|
/**
|
|
* Toggle greeting visibility
|
|
*/
|
|
export function toggleGreeting(): void {
|
|
settings.greeting.enabled = !settings.greeting.enabled;
|
|
saveSettings();
|
|
notifyListeners();
|
|
}
|
|
|
|
/**
|
|
* Set greeting enabled state
|
|
*/
|
|
export function setGreetingEnabled(enabled: boolean): void {
|
|
settings.greeting.enabled = enabled;
|
|
saveSettings();
|
|
notifyListeners();
|
|
}
|
|
|
|
/**
|
|
* Set custom greeting text (null = use default time-based)
|
|
*/
|
|
export function setCustomGreeting(text: string | null): void {
|
|
settings.greeting.customText = text && text.trim() ? text.trim() : null;
|
|
saveSettings();
|
|
notifyListeners();
|
|
}
|
|
|
|
/**
|
|
* Get greeting text for display (legacy - use getGreetingInfo for i18n support)
|
|
*/
|
|
export function getGreetingText(userName: string): string {
|
|
if (settings.greeting.customText) {
|
|
return settings.greeting.customText.replace('{name}', getFirstName(userName));
|
|
}
|
|
return getTimeBasedGreeting(userName);
|
|
}
|
|
|
|
/**
|
|
* Get greeting info for i18n-aware display
|
|
* Returns either custom text or a translation key with params
|
|
*/
|
|
export function getGreetingInfo(userName: string): { type: 'custom'; text: string } | { type: 'key'; key: string; name: string } {
|
|
const firstName = getFirstName(userName);
|
|
|
|
if (settings.greeting.customText) {
|
|
return {
|
|
type: 'custom',
|
|
text: settings.greeting.customText.replace('{name}', firstName)
|
|
};
|
|
}
|
|
|
|
return {
|
|
type: 'key',
|
|
key: getTimeBasedGreetingKey(),
|
|
name: firstName
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Get time-based greeting translation key
|
|
*/
|
|
export function getTimeBasedGreetingKey(): string {
|
|
const hour = new Date().getHours();
|
|
|
|
if (hour >= 5 && hour < 12) {
|
|
return 'homeSettings.goodMorning';
|
|
} else if (hour >= 12 && hour < 17) {
|
|
return 'homeSettings.goodAfternoon';
|
|
} else if (hour >= 17 && hour < 21) {
|
|
return 'homeSettings.goodEvening';
|
|
} else {
|
|
return 'homeSettings.goodNight';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get time-based greeting (legacy)
|
|
*/
|
|
function getTimeBasedGreeting(userName: string): string {
|
|
const hour = new Date().getHours();
|
|
const firstName = getFirstName(userName);
|
|
|
|
if (hour >= 5 && hour < 12) {
|
|
return `Good morning, ${firstName}`;
|
|
} else if (hour >= 12 && hour < 17) {
|
|
return `Good afternoon, ${firstName}`;
|
|
} else if (hour >= 17 && hour < 21) {
|
|
return `Good evening, ${firstName}`;
|
|
} else {
|
|
return `Good night, ${firstName}`;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Extract first name from full name
|
|
*/
|
|
function getFirstName(fullName: string): string {
|
|
return fullName.split(' ')[0] || fullName;
|
|
}
|
|
|
|
/**
|
|
* Reset to defaults
|
|
*/
|
|
export function resetToDefaults(): void {
|
|
settings = { ...DEFAULT_SETTINGS, sections: DEFAULT_SECTIONS.map(s => ({ ...s })) };
|
|
saveSettings();
|
|
notifyListeners();
|
|
}
|
|
|
|
/**
|
|
* Update a specific limit
|
|
*/
|
|
export function updateLimit(key: keyof HomeSettings['limits'], value: number): void {
|
|
if (value >= 1 && value <= 100) {
|
|
settings.limits[key] = value;
|
|
saveSettings();
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get current limits
|
|
*/
|
|
export function getLimits(): HomeSettings['limits'] {
|
|
return settings.limits;
|
|
}
|