jevstrudel.git / website / src / jev / radioHistory.mjs
1// A signed-in listener's radio history, kept by the Worker
2// (worker/src/radio.ts, GET and POST /jev/me/radio), so the radio avoids
3// repeats across sessions and devices. Anonymous radio never calls these:
4// its history lasts the tab (JevPicks.jsx).
5export const RADIO_HISTORY = '/jev/me/radio';
6
7// Song ids, newest first; [] when it cannot be read (the radio then picks
8// as it would anonymously).
9export async function radioHistory() {
10  try {
11    const res = await fetch(RADIO_HISTORY, { credentials: 'same-origin' });
12    if (!res.ok) return [];
13    const { plays } = await res.json();
14    return Array.isArray(plays) ? plays.map((p) => p.song).filter((id) => typeof id === 'string') : [];
15  } catch {
16    return [];
17  }
18}
19
20// The radio started `songId`; fire and forget: a lost record costs only a
21// possible repeat.
22export function recordRadioPlay(songId) {
23  fetch(RADIO_HISTORY, {
24    method: 'POST',
25    headers: { 'Content-Type': 'application/json' },
26    body: JSON.stringify({ song: songId }),
27    credentials: 'same-origin',
28  }).catch(() => {});
29}