feat(playlists): integrate Navidrome smart playlist flow into playlists page

Move smart playlist creation and management into Playlists, including Navidrome-only gating, smart-name/icon presentation, and smarter refresh handling while server-side smart rules are being applied.
This commit is contained in:
Maxim Isaev
2026-04-24 16:41:17 +03:00
parent 1c761682f4
commit 27a59f6b23
8 changed files with 785 additions and 15 deletions
+114
View File
@@ -0,0 +1,114 @@
import { invoke } from '@tauri-apps/api/core';
import { useAuthStore } from '../store/authStore';
import { ndLogin } from './navidromeAdmin';
export type SmartRuleOperator =
| 'is'
| 'isNot'
| 'contains'
| 'notContains'
| 'startsWith'
| 'endsWith'
| 'gt'
| 'lt'
| 'inTheRange';
export interface SmartRuleCondition {
field: string;
operator: SmartRuleOperator;
value: string | number | boolean | [number, number];
}
export interface NdSmartPlaylist {
id: string;
name: string;
songCount: number;
duration?: number;
rules?: Record<string, unknown>;
sync?: boolean;
updatedAt?: string;
}
let authCache: {
key: string;
token: string;
expiresAt: number;
} | null = null;
async function getNavidromeAuth(): Promise<{ serverUrl: string; token: string }> {
const s = useAuthStore.getState();
const server = s.getActiveServer();
const serverUrl = s.getBaseUrl();
if (!serverUrl || !server?.username || !server?.password) {
throw new Error('No active server credentials');
}
const key = `${serverUrl}|${server.username}|${server.password}`;
if (authCache && authCache.key === key && Date.now() < authCache.expiresAt) {
return { serverUrl, token: authCache.token };
}
const login = await ndLogin(serverUrl, server.username, server.password);
authCache = {
key,
token: login.token,
expiresAt: Date.now() + 10 * 60 * 1000,
};
return { serverUrl, token: login.token };
}
function conditionToRule(c: SmartRuleCondition): Record<string, unknown> {
return { [c.operator]: { [c.field]: c.value } };
}
export function buildSmartRules(conditions: SmartRuleCondition[], opts?: { limit?: number; sort?: string }) {
const all = conditions.map(conditionToRule);
const rules: Record<string, unknown> = { all };
if (typeof opts?.limit === 'number' && opts.limit > 0) rules.limit = opts.limit;
if (opts?.sort) rules.sort = opts.sort;
return rules;
}
export async function ndListSmartPlaylists(): Promise<NdSmartPlaylist[]> {
const { serverUrl, token } = await getNavidromeAuth();
const raw = await invoke<unknown>('nd_list_playlists', { serverUrl, token, smart: true });
const list = Array.isArray(raw)
? raw
: (raw && typeof raw === 'object' && Array.isArray((raw as { items?: unknown[] }).items))
? (raw as { items: unknown[] }).items
: [];
return list.map((v) => {
const o = (v as Record<string, unknown>) ?? {};
return {
id: String(o.id ?? ''),
name: String(o.name ?? ''),
songCount: Number(o.songCount ?? 0),
duration: typeof o.duration === 'number' ? o.duration : undefined,
rules: typeof o.rules === 'object' && o.rules ? (o.rules as Record<string, unknown>) : undefined,
sync: typeof o.sync === 'boolean' ? o.sync : undefined,
updatedAt: typeof o.updatedAt === 'string' ? o.updatedAt : undefined,
};
});
}
export async function ndCreateSmartPlaylist(name: string, rules: Record<string, unknown>, sync = true): Promise<NdSmartPlaylist> {
const { serverUrl, token } = await getNavidromeAuth();
const raw = await invoke<unknown>('nd_create_playlist', {
serverUrl,
token,
body: { name, rules, sync },
});
const o = (raw as Record<string, unknown>) ?? {};
return {
id: String(o.id ?? ''),
name: String(o.name ?? name),
songCount: Number(o.songCount ?? 0),
duration: typeof o.duration === 'number' ? o.duration : undefined,
rules: typeof o.rules === 'object' && o.rules ? (o.rules as Record<string, unknown>) : undefined,
sync: typeof o.sync === 'boolean' ? o.sync : undefined,
updatedAt: typeof o.updatedAt === 'string' ? o.updatedAt : undefined,
};
}
export async function ndDeletePlaylist(id: string): Promise<void> {
const { serverUrl, token } = await getNavidromeAuth();
await invoke('nd_delete_playlist', { serverUrl, token, id });
}
+25 -1
View File
@@ -499,6 +499,30 @@ export async function getRandomSongs(size = 50, genre?: string, timeout = 15000)
return data.randomSongs?.song ?? [];
}
export interface RandomSongsFilters {
size?: number;
genre?: string;
fromYear?: number;
toYear?: number;
}
/** Extended random song fetch with server-side year/genre filtering. */
export async function getRandomSongsFiltered(
filters: RandomSongsFilters,
timeout = 15000,
): Promise<SubsonicSong[]> {
const params: Record<string, string | number> = {
size: filters.size ?? 50,
_t: Date.now(),
...libraryFilterParams(),
};
if (filters.genre) params.genre = filters.genre;
if (typeof filters.fromYear === 'number') params.fromYear = filters.fromYear;
if (typeof filters.toYear === 'number') params.toYear = filters.toYear;
const data = await api<{ randomSongs: { song: SubsonicSong[] } }>('getRandomSongs.view', params, timeout);
return data.randomSongs?.song ?? [];
}
export async function getSong(id: string): Promise<SubsonicSong | null> {
try {
const data = await api<{ song: SubsonicSong }>('getSong.view', { id });
@@ -995,7 +1019,7 @@ export function buildDownloadUrl(id: string): string {
// ─── Playlists ────────────────────────────────────────────────
export async function getPlaylists(): Promise<SubsonicPlaylist[]> {
const data = await api<{ playlists: { playlist: SubsonicPlaylist[] } }>('getPlaylists.view');
const data = await api<{ playlists: { playlist: SubsonicPlaylist[] } }>('getPlaylists.view', { _t: Date.now() });
return data.playlists?.playlist ?? [];
}