Kaleido-Desktop/src/app/format.ts
Elijah 00d72e70f4 Create Kaleido Desktop, a cinematic Plex client for Windows
Tauri 2 shell with a React/TypeScript interface and libmpv playback composed
beneath the web view (DirectComposition on Windows). Shared code with per-OS
platform modules and configs so macOS and Linux can follow.

- Plex sign-in (browser, plex.tv/link or manual), server selection, Plex Home
- Home, Movies and TV Shows (Discover, Library, Collections), search, people
- Title pages with logos, ratings, cast, extras, seasons and episodes
- Direct play with Plex conversion fallback, tracks, markers, autoplay
- Metadata editing, artwork, fix match, refresh, analyze and deletion
- Resumable offline downloads
- App settings and Plex server administration with a live dashboard
- Signed updater feed and Forgejo CI

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 16:26:37 -07:00

190 lines
7.2 KiB
TypeScript

import type { Item, Media } from '../plex/types';
export function duration(ms: number): string {
if (!ms || ms < 0) return '';
const m = Math.round(ms / 60000);
if (m < 60) return `${m}m`;
const h = Math.floor(m / 60);
return m % 60 ? `${h}h ${m % 60}m` : `${h}h`;
}
export function clock(seconds: number, forceHours = false): string {
if (!Number.isFinite(seconds) || seconds < 0) seconds = 0;
const s = Math.floor(seconds % 60);
const m = Math.floor((seconds / 60) % 60);
const h = Math.floor(seconds / 3600);
const pad = (n: number) => String(n).padStart(2, '0');
return h || forceHours ? `${h}:${pad(m)}:${pad(s)}` : `${m}:${pad(s)}`;
}
export function remaining(item: Item): string {
if (!item.duration || !item.viewOffset) return '';
const left = Math.max(0, item.duration - item.viewOffset);
const m = Math.round(left / 60000);
if (m < 1) return 'Almost done';
return m >= 60 ? `${Math.floor(m / 60)}h ${m % 60}m left` : `${m}m left`;
}
export function episodeLabel(item: Item, long = false): string {
if (item.type !== 'episode') return '';
if (long) return `Season ${item.parentIndex} · Episode ${item.index}`;
return `S${item.parentIndex} · E${item.index}`;
}
export function progress(item: Item): number {
if (!item.duration || !item.viewOffset) return 0;
return Math.min(1, Math.max(0, item.viewOffset / item.duration));
}
export function isWatched(item: Item, percent = 90): boolean {
if (item.type === 'show' || item.type === 'season') return item.leafCount > 0 && item.viewedLeafCount >= item.leafCount;
if (item.viewOffset > 0 && item.duration > 0) return item.viewCount > 0 && item.viewOffset / item.duration >= percent / 100;
return item.viewCount > 0;
}
export function remainingEpisodes(item: Item): number {
return Math.max(0, item.leafCount - item.viewedLeafCount);
}
export function displayTitle(item: Item): string {
if (item.type === 'episode') return item.grandparentTitle || item.title;
if (item.type === 'season') return item.parentTitle ? `${item.parentTitle}` : item.title;
return item.title;
}
export function subtitleFor(item: Item): string {
switch (item.type) {
case 'episode':
return `${episodeLabel(item)} ${item.title}`;
case 'season':
return item.title + (item.leafCount ? ` · ${item.leafCount} episodes` : '');
case 'show':
return [item.year || '', item.childCount ? `${item.childCount} season${item.childCount === 1 ? '' : 's'}` : ''].filter(Boolean).join(' · ');
case 'collection':
return item.childCount ? `${item.childCount} titles` : 'Collection';
case 'clip':
return [extraLabel(item), duration(item.duration)].filter(Boolean).join(' · ');
default:
return [item.year || '', duration(item.duration)].filter(Boolean).join(' · ');
}
}
export function extraLabel(item: Item): string {
const map: Record<string, string> = {
trailer: 'Trailer',
behindTheScenes: 'Behind the scenes',
deletedScene: 'Deleted scene',
featurette: 'Featurette',
interview: 'Interview',
sceneOrSample: 'Scene',
short: 'Short',
};
return map[item.subtype] ?? (item.type === 'clip' ? 'Extra' : '');
}
/** Image path choices per presentation. */
export const art = {
poster(item: Item): string {
if (item.type === 'episode') return item.grandparentThumb || item.parentThumb || item.thumb;
if (item.type === 'season') return item.thumb || item.parentThumb;
return item.thumb;
},
landscape(item: Item): string {
if (item.type === 'episode' || item.type === 'clip') return item.thumb || item.art;
return item.art || item.grandparentArt || item.thumb;
},
backdrop(item: Item): string {
if (item.type === 'episode') return item.grandparentArt || item.art || item.thumb;
return item.art || item.grandparentArt || item.thumb;
},
};
export function resolutionLabel(m?: Media): string {
if (!m) return '';
const r = m.videoResolution.toLowerCase();
if (r === '4k') return '4K';
if (r === 'sd') return 'SD';
if (/^\d+$/.test(r)) return `${r}p`;
if (m.height >= 2000) return '4K';
if (m.height >= 1000) return '1080p';
if (m.height >= 700) return '720p';
return r.toUpperCase();
}
export function channelLabel(n: number): string {
return n === 1 ? 'Mono' : n === 2 ? 'Stereo' : n === 6 ? '5.1' : n === 8 ? '7.1' : n ? `${n} ch` : '';
}
/** Format badges: 4K, HDR flavor, Atmos/5.1 — the facts that matter at a glance. */
export function formatBadges(item: Item): string[] {
const m = item.media[0];
if (!m) return [];
const out: string[] = [];
const res = resolutionLabel(m);
if (res === '4K' || res === '1080p' || res === '720p') out.push(res.toUpperCase());
if (m.dynamicRange === 'Dolby Vision') out.push('DOLBY VISION');
else if (m.dynamicRange) out.push(m.dynamicRange === 'HDR10' ? 'HDR' : m.dynamicRange);
if (m.atmos) out.push('ATMOS');
else if (m.audioChannels >= 6) out.push(channelLabel(m.audioChannels));
return out;
}
export function technicalLine(m?: Media): string {
if (!m) return '';
return [
m.videoCodec.toUpperCase(),
[m.audioCodec.toUpperCase(), channelLabel(m.audioChannels)].filter(Boolean).join(' '),
m.bitrate ? `${Math.round(m.bitrate / 1000)} Mbps` : '',
]
.filter(Boolean)
.join(' · ');
}
export function fileSize(bytes: number): string {
if (!bytes) return '';
if (bytes >= 1e12) return `${(bytes / 1e12).toFixed(2)} TB`;
if (bytes >= 1e9) return `${(bytes / 1e9).toFixed(1)} GB`;
if (bytes >= 1e6) return `${Math.round(bytes / 1e6)} MB`;
return `${Math.round(bytes / 1e3)} KB`;
}
export function bitrate(kbps: number): string {
if (!kbps) return '';
return kbps >= 1000 ? `${(kbps / 1000).toFixed(kbps >= 10000 ? 0 : 1)} Mbps` : `${kbps} kbps`;
}
export function date(value: string | number): string {
if (!value) return '';
const d = typeof value === 'number' ? new Date(value * 1000) : new Date(value);
if (Number.isNaN(d.getTime())) return '';
return d.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' });
}
export function relativeTime(seconds: number): string {
if (!seconds) return '';
const diff = Date.now() / 1000 - seconds;
if (diff < 60) return 'just now';
if (diff < 3600) return `${Math.floor(diff / 60)} min ago`;
if (diff < 86400) return `${Math.floor(diff / 3600)} hr ago`;
if (diff < 86400 * 30) return `${Math.floor(diff / 86400)} days ago`;
return date(seconds);
}
export function plural(n: number, word: string, pluralWord = `${word}s`) {
return `${n.toLocaleString()} ${n === 1 ? word : pluralWord}`;
}
/** Hex UltraBlur colors → CSS gradient layers for the artwork colour wash. */
export function washGradient(u: Item['ultraBlur'], strength = 1): string {
if (!u) return '';
const c = (hex: string, a: number) => {
const n = parseInt(hex, 16);
return `rgba(${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}, ${(a * strength).toFixed(3)})`;
};
return [
`radial-gradient(ellipse at 100% 0%, ${c(u.topRight, 0.95)} 0%, transparent 62%)`,
`radial-gradient(ellipse at 0% 0%, ${c(u.topLeft, 0.7)} 0%, transparent 55%)`,
`radial-gradient(ellipse at 100% 100%, ${c(u.bottomRight, 0.55)} 0%, transparent 60%)`,
`radial-gradient(ellipse at 0% 100%, ${c(u.bottomLeft, 0.45)} 0%, transparent 60%)`,
].join(', ');
}