Kaleido-Desktop/src/screens/discover.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

81 lines
2.8 KiB
TypeScript

import * as api from '../plex/api';
import type { Hub, Item } from '../plex/types';
import type { Feature } from '../ui/Hero';
import type { PlexServer } from '../plex/client';
/** One shuffle per app session keeps features stable while browsing. */
const seed = Math.floor(Math.random() * 1e9);
function shuffled<T>(list: T[], salt = 0): T[] {
const out = [...list];
let s = (seed + salt) % 2147483647 || 1;
for (let i = out.length - 1; i > 0; i--) {
s = (s * 48271) % 2147483647;
const j = s % (i + 1);
[out[i], out[j]] = [out[j], out[i]];
}
return out;
}
const FEATURE_SOURCES: [RegExp, string][] = [
[/recentlyreleased|recentlyaired/, 'Recently released'],
[/topunwatched|toprated/, 'Highly rated'],
[/recentlyadded/, 'Recently added'],
[/startwatching/, 'Start watching'],
[/curated|genre|by\.actor|moreingenre/, 'Recommended'],
];
/**
* Features interleave recently released, highly rated unwatched and
* Plex-recommended titles; they need artwork and skip in-progress titles.
*/
export function buildFeatures(hubs: Hub[], limit = 8, salt = 0): Feature[] {
const pools: Feature[][] = FEATURE_SOURCES.map(([re, eyebrow]) =>
shuffled(
hubs
.filter((h) => re.test(h.id))
.flatMap((h) => h.items)
.filter((i) => (i.type === 'movie' || i.type === 'show') && i.art && !i.viewOffset && i.summary)
.map((item) => ({ item, eyebrow })),
salt,
),
);
const out: Feature[] = [];
const seen = new Set<string>();
for (let round = 0; out.length < limit && round < 20; round++) {
for (const pool of pools) {
const f = pool[round];
if (f && !seen.has(f.item.id) && out.length < limit) {
seen.add(f.item.id);
out.push(f);
}
}
}
return out;
}
/**
* New seasons, one card per show. Samples many seasons so a single series
* import cannot fill the row.
*/
export async function recentSeasons(s: PlexServer, sectionId: string): Promise<Item[]> {
const page = await api.libraryPage(s, sectionId, { type: 3, sort: 'addedAt:desc', start: 0, size: 100 });
const byShow = new Map<string, Item[]>();
for (const season of page.items) {
if (!season.parentId) continue;
const list = byShow.get(season.parentId) ?? [];
list.push(season);
byShow.set(season.parentId, list);
}
const out: Item[] = [];
for (const [, seasons] of byShow) {
if (out.length >= 18) break;
// Several new seasons: show the series; one: the season itself.
if (seasons.length > 1) {
const first = seasons[0];
out.push({ ...first, id: first.parentId, type: 'show', title: first.parentTitle, thumb: first.parentThumb || first.thumb, key: `/library/metadata/${first.parentId}/children` });
} else out.push(seasons[0]);
}
return out;
}
export const HIDDEN_HUBS = /continue|ondeck|inprogress|playlists|music|photos|videos\.recent/;