65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
// Presentation helpers shared across views — ported from web/app.js.
|
|
|
|
import type { Track } from './types';
|
|
|
|
export function fmtMs(ms: number | undefined | null): string {
|
|
const s = Math.max(0, Math.floor((ms || 0) / 1000));
|
|
const m = Math.floor(s / 60);
|
|
return `${m}:${String(s % 60).padStart(2, '0')}`;
|
|
}
|
|
|
|
export function cover(seed: number): string {
|
|
const h = (seed * 47) % 360;
|
|
const h2 = (h + 40) % 360;
|
|
return `linear-gradient(135deg, hsl(${h} 55% 42%), hsl(${h2} 60% 26%))`;
|
|
}
|
|
|
|
export function seedOf(t: Track | null | undefined): number {
|
|
const s = (t && (t.id || t.title)) || '?';
|
|
let n = 0;
|
|
for (let i = 0; i < s.length; i++) n = (n * 31 + s.charCodeAt(i)) & 0xffff;
|
|
return n;
|
|
}
|
|
|
|
// Prefer source-provided artwork; otherwise derive the YouTube thumbnail from
|
|
// the video id (covers tracks stored before thumbnails were persisted).
|
|
export function thumbFor(t: Track | null | undefined): string | null {
|
|
if (!t) return null;
|
|
if (t.thumbnailUrl) return t.thumbnailUrl;
|
|
const m = (t.url || '').match(/(?:v=|youtu\.be\/|\/embed\/)([\w-]{11})/);
|
|
if (m) return `https://i.ytimg.com/vi/${m[1]}/mqdefault.jpg`;
|
|
if (t.source === 1 && /^[\w-]{11}$/.test(t.id || '')) {
|
|
return `https://i.ytimg.com/vi/${t.id}/mqdefault.jpg`;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// TrackSource enum: 1 = YouTube, 6 = Yandex (see EchoionApi TrackTypes.hpp).
|
|
export function sourceInfo(
|
|
t: Track | null | undefined
|
|
): { label: string; cls: string } | null {
|
|
const url = (t && t.url) || '';
|
|
if ((t && t.source === 6) || url.includes('yandex')) {
|
|
return { label: 'YANDEX', cls: 'src-badge yandex' };
|
|
}
|
|
if ((t && t.source === 1) || url.includes('youtu')) {
|
|
return { label: 'YOUTUBE', cls: 'src-badge' };
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function avatarColor(name: string | undefined | null): string {
|
|
let n = 0;
|
|
const s = name || '';
|
|
for (let i = 0; i < s.length; i++) n = (n * 31 + s.charCodeAt(i)) & 0xffff;
|
|
const h = n % 360;
|
|
return `linear-gradient(135deg, hsl(${h} 60% 55%), hsl(${(h + 45) % 360} 55% 40%))`;
|
|
}
|
|
|
|
export function coverStyle(t: Track | null | undefined): string {
|
|
const thumb = thumbFor(t);
|
|
if (thumb) return `url("${thumb}") center/cover`;
|
|
if (t) return cover(seedOf(t));
|
|
return 'linear-gradient(135deg, #2a1a4a, #0d0d1f)';
|
|
}
|