Files
jellytau/src/lib/utils/heroMix.ts
T
dtourolle ad48d89dfe chore(format): run prettier over src/ and scripts/
Formatting was configured but never enforced: `bun run format:check`
reported 199 unformatted files and ran in no workflow and in no git hook,
so .prettierrc (printWidth 100, trailing commas) described an intention
rather than the tree.

This is the one-time sweep that makes the check gateable. Whitespace and
token-reflow only -- no behavioural change: `bun run check` reports 0
errors and all 1053 frontend tests pass before and after.

Kept out of every other commit on purpose. A 199-file diff mixed with
real changes is unreviewable, and the next commit turns format:check
into a hard CI gate so this cannot silently accumulate again.
2026-08-21 17:41:44 +02:00

60 lines
2.1 KiB
TypeScript

// Hero banner mix builder, shared by the movies/TV/music landing stores.
//
// The hero used to show the first N items of fixed pools (continue watching,
// recently played, latest), which made it identical on every visit. Instead we
// sample a couple of items at random from each pool — pools are ordered most
// personal first — and shuffle the rotation, so the banner is a fresh mix each
// time while still leading with something personal.
// TRACES: UR-034 | DR-038
import type { MediaItem } from "$lib/api/types";
/** Fisher-Yates shuffle; returns a new array, input is untouched. */
export function shuffle<T>(items: T[]): T[] {
const result = [...items];
for (let i = result.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[result[i], result[j]] = [result[j], result[i]];
}
return result;
}
/**
* Build a varied hero rotation from ordered pools (most personal first).
*
* Takes up to `perPool` random items from each pool, de-duplicated by id and
* filtered to items with usable artwork. The first pick from the first
* non-empty pool leads the rotation (so a resumable/recent item greets the
* user), the rest are shuffled. If that yields fewer than `count` items, the
* remainder is backfilled from whatever is left across all pools.
*/
export function buildHeroMix(
pools: MediaItem[][],
hasArt: (item: MediaItem) => boolean,
count = 6,
perPool = 2,
): MediaItem[] {
const seen = new Set<string>();
const usable = pools.map((pool) =>
pool.filter((item) => {
if (!hasArt(item) || seen.has(item.id)) return false;
seen.add(item.id);
return true;
}),
);
const picked = new Set<string>();
const picks: MediaItem[] = [];
for (const pool of usable) {
for (const item of shuffle(pool).slice(0, perPool)) {
picked.add(item.id);
picks.push(item);
}
}
if (picks.length === 0) return [];
const [leader, ...rest] = picks;
const leftovers = shuffle(usable.flat().filter((item) => !picked.has(item.id)));
return [leader, ...shuffle(rest), ...leftovers].slice(0, count);
}