fffiloni commited on
Commit
bb8dbe6
·
verified ·
1 Parent(s): 63dc7a0

Add confirmed Banger, vote and report feedback animations

Browse files

Add optional motion feedback after successful actions, honor reduced motion, preserve voting and report permissions, and keep feedback attached to the clicked track. Wrap action controls on narrow mobile screens. Includes regression coverage for successful, failed, reversed and stale actions, optional animation failures and motion cleanup.

static/action-feedback.css ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Presentation only: vote state and permissions remain in the player. */
2
+ .radio-action-icon {
3
+ position: relative;
4
+ display: inline-block;
5
+ flex: 0 0 auto;
6
+ transform-origin: center;
7
+ }
8
+ .icon-button.like-button.liked {
9
+ background: linear-gradient(135deg, #a677e8, #c59af2);
10
+ border-color: #dfc3ff;
11
+ box-shadow: 0 8px 28px rgba(172, 121, 240, .25);
12
+ }
13
+ .icon-button.upvote-button.voted {
14
+ color: #9edfc5;
15
+ background: #20332f;
16
+ border-color: rgba(158, 223, 197, .42);
17
+ box-shadow: none;
18
+ }
19
+ .icon-button.downvote-button.voted {
20
+ color: #e7a0a7;
21
+ background: #38272f;
22
+ border-color: rgba(231, 160, 167, .42);
23
+ box-shadow: none;
24
+ }
25
+ .icon-button.report-button.reported {
26
+ color: #edc585;
27
+ background: #352e22;
28
+ border-color: rgba(237, 197, 133, .42);
29
+ }
30
+ .report-button.reported .radio-action-icon::after {
31
+ content: "✓";
32
+ position: absolute;
33
+ top: -5px;
34
+ right: -9px;
35
+ width: 11px;
36
+ height: 11px;
37
+ display: grid;
38
+ place-items: center;
39
+ border-radius: 50%;
40
+ background: #edc585;
41
+ color: #352e22;
42
+ font-size: 8px;
43
+ line-height: 1;
44
+ }
45
+ .radio-action-feedback {
46
+ position: fixed;
47
+ z-index: 100;
48
+ pointer-events: none;
49
+ color: var(--feedback-color);
50
+ font-family: system-ui, sans-serif;
51
+ }
52
+ .radio-feedback-ring {
53
+ position: absolute;
54
+ inset: 0;
55
+ border: 1px solid currentColor;
56
+ border-radius: 12px;
57
+ }
58
+ .radio-feedback-particle {
59
+ position: absolute;
60
+ left: 50%;
61
+ top: 50%;
62
+ width: 4px;
63
+ height: 4px;
64
+ margin: -2px;
65
+ border-radius: 50%;
66
+ background: currentColor;
67
+ }
68
+ .radio-feedback-particle.heart {
69
+ width: 12px;
70
+ height: 12px;
71
+ margin: -6px;
72
+ background: none;
73
+ font-size: 12px;
74
+ line-height: 1;
75
+ }
76
+ .radio-feedback-label {
77
+ position: absolute;
78
+ top: calc(100% + 8px);
79
+ left: 50%;
80
+ width: max-content;
81
+ padding: 5px 8px;
82
+ border: 1px solid color-mix(in srgb, currentColor 25%, transparent);
83
+ border-radius: 7px;
84
+ background: #191720;
85
+ font-size: 11px;
86
+ line-height: 1.3;
87
+ font-weight: 700;
88
+ white-space: nowrap;
89
+ }
90
+ .radio-feedback-announcement {
91
+ position: fixed;
92
+ width: 1px;
93
+ height: 1px;
94
+ overflow: hidden;
95
+ clip-path: inset(50%);
96
+ white-space: nowrap;
97
+ }
98
+ @media (max-width: 760px) {
99
+ .live-row { flex-direction: column; }
100
+ .track-actions { flex-wrap: wrap; max-width: 100%; }
101
+ }
102
+ @media (prefers-reduced-motion: reduce) {
103
+ .track-actions .icon-button { transition: none; }
104
+ .track-actions .icon-button:hover { transform: none; }
105
+ }
static/action-feedback.js ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (() => {
2
+ "use strict";
3
+ const colors = { banger: "#d5b0ff", up: "#9edfc5", down: "#e7a0a7", report: "#edc585" };
4
+ const labels = { banger: "BANGER!", up: "Upvote recorded", down: "Downvote recorded", report: "Report received" };
5
+ const active = new Map();
6
+ const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
7
+ let announcement;
8
+
9
+ function clear() {
10
+ for (const effect of Array.from(active.values())) effect.dispose();
11
+ if (announcement) announcement.textContent = "";
12
+ }
13
+
14
+ function play(button, kind) {
15
+ if (!button || !colors[kind] || !button.isConnected) return;
16
+ active.get(button)?.dispose();
17
+ if (!announcement) {
18
+ announcement = document.createElement("div");
19
+ announcement.className = "radio-feedback-announcement";
20
+ announcement.setAttribute("role", "status");
21
+ announcement.setAttribute("aria-live", "polite");
22
+ announcement.setAttribute("aria-atomic", "true");
23
+ document.body.appendChild(announcement);
24
+ }
25
+ announcement.textContent = labels[kind];
26
+
27
+ const rect = button.getBoundingClientRect();
28
+ const layer = document.createElement("div");
29
+ layer.className = "radio-action-feedback";
30
+ layer.setAttribute("aria-hidden", "true");
31
+ layer.style.setProperty("--feedback-color", colors[kind]);
32
+ Object.assign(layer.style, { left: `${rect.left}px`, top: `${rect.top}px`, width: `${rect.width}px`, height: `${rect.height}px` });
33
+ document.body.appendChild(layer);
34
+ const animations = [];
35
+ let timer;
36
+ const effect = {
37
+ dispose() {
38
+ clearTimeout(timer);
39
+ animations.forEach(animation => animation.cancel());
40
+ layer.remove();
41
+ active.delete(button);
42
+ },
43
+ };
44
+ active.set(button, effect);
45
+ // Register cleanup before optional animation work: feedback can never block voting.
46
+ timer = setTimeout(() => effect.dispose(), kind === "banger" ? 1100 : 1600);
47
+ const label = document.createElement("span");
48
+ label.className = "radio-feedback-label";
49
+ label.textContent = labels[kind];
50
+ layer.appendChild(label);
51
+ // Keep confirmations inside even the narrowest phone viewport.
52
+ const viewport = document.documentElement.clientWidth;
53
+ const labelWidth = label.getBoundingClientRect().width;
54
+ const labelLeft = Math.max(8, Math.min(rect.left + rect.width / 2 - labelWidth / 2, viewport - labelWidth - 8));
55
+ label.style.left = `${labelLeft - rect.left}px`;
56
+ if (reducedMotion.matches || typeof button.animate !== "function") return;
57
+
58
+ const animate = (target, frames, duration) => {
59
+ if (target) animations.push(target.animate(frames, { duration, easing: "cubic-bezier(.2,.75,.25,1)", fill: "none" }));
60
+ };
61
+ const icon = button.querySelector(".radio-action-icon");
62
+ if (kind === "banger") {
63
+ animate(button, [{ transform: "scale(1)" }, { transform: "scale(.94)", offset: .18 }, { transform: "scale(1.07)", offset: .48 }, { transform: "scale(1)" }], 500);
64
+ animate(icon, [{ transform: "scale(1)" }, { transform: "scale(1.5) rotate(-12deg)", offset: .35 }, { transform: "scale(1.12) rotate(6deg)", offset: .65 }, { transform: "scale(1)" }], 650);
65
+ } else if (kind === "report") {
66
+ animate(icon, [{ transform: "scale(1)" }, { transform: "scale(1.12)" }, { transform: "scale(1)" }], 300);
67
+ } else {
68
+ const up = kind === "up";
69
+ animate(button, [{ transform: "scale(1)" }, { transform: `scale(${up ? 1.035 : .97})` }, { transform: "scale(1)" }], up ? 520 : 420);
70
+ animate(icon, [{ transform: "translateY(0)" }, { transform: `translateY(${up ? -9 : 8}px) scale(${up ? 1.16 : 1})`, offset: .4 }, { transform: `translateY(${up ? 2 : -1}px)`, offset: .75 }, { transform: "translateY(0)" }], up ? 520 : 420);
71
+ }
72
+ if (kind !== "report") {
73
+ const ring = document.createElement("span");
74
+ ring.className = "radio-feedback-ring";
75
+ layer.appendChild(ring);
76
+ animate(ring, [{ transform: "scale(1)", opacity: .7 }, { transform: kind === "banger" ? "scale(1.45, 1.9)" : `translateY(${kind === "up" ? -7 : 6}px) scale(1.13)`, opacity: 0 }], kind === "banger" ? 780 : 580);
77
+ // Keep the finished ring invisible after its animation releases styles.
78
+ ring.style.opacity = "0";
79
+ }
80
+ const particleCount = kind === "banger" ? 10 : kind === "up" ? 3 : 0;
81
+ for (let i = 0; i < particleCount; i++) {
82
+ const particle = document.createElement("span");
83
+ const heart = kind === "banger" && i % 4 === 0;
84
+ particle.className = `radio-feedback-particle${heart ? " heart" : ""}`;
85
+ if (heart) particle.textContent = "♥";
86
+ if (kind === "banger" && i % 3 === 1) particle.style.color = "#efb69f";
87
+ layer.appendChild(particle);
88
+ const angle = Math.PI * 2 * i / particleCount;
89
+ const x = kind === "up" ? (i - 1) * 11 : Math.cos(angle) * (37 + i % 3 * 9);
90
+ const y = kind === "up" ? -24 - i % 2 * 7 : Math.sin(angle) * 30 - 13;
91
+ animate(particle, [{ transform: "translate(0, 0) scale(.5)", opacity: 0 }, { opacity: 1, offset: .18 }, { transform: `translate(${x}px, ${y}px) scale(.4)`, opacity: 0 }], kind === "up" ? 520 : 620 + i % 3 * 70);
92
+ particle.style.opacity = "0";
93
+ }
94
+ animate(label, [{ opacity: 0, transform: "translateY(3px)" }, { opacity: 1, transform: "translateY(0)", offset: .15 }, { opacity: 1, offset: .7 }, { opacity: 0, transform: "translateY(-5px)" }], kind === "banger" ? 1050 : 1550);
95
+ label.style.opacity = "0";
96
+ }
97
+
98
+ window.RadioActionFeedback = { play, clear };
99
+ window.addEventListener("resize", clear, { passive: true });
100
+ window.addEventListener("scroll", clear, { passive: true, capture: true });
101
+ window.addEventListener("pagehide", clear);
102
+ reducedMotion.addEventListener?.("change", clear);
103
+ })();
static/index.html CHANGED
@@ -1434,6 +1434,7 @@
1434
  .track-origin { margin-top: 10px; }
1435
  }
1436
  </style>
 
1437
 
1438
  <script src="https://cdn.jsdelivr.net/gh/foobar404/wave.js/dist/bundle.js"></script>
1439
  </head>
@@ -1695,6 +1696,7 @@
1695
  </details>
1696
  </main>
1697
 
 
1698
  <script>
1699
  const DEFAULT_COVER_URL =
1700
  "https://huggingface.co/datasets/Chunte/Huggy/resolve/main/modern%20Huggies/Huggy%20Hi.png";
@@ -2397,11 +2399,38 @@
2397
  }
2398
  }
2399
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2400
  function updateLikeButton(track) {
2401
  if (!track || !track.id) {
2402
  likeButton.disabled = true;
2403
  likeButton.classList.remove("liked");
2404
- likeButton.textContent = "♡ Banger 0";
2405
  likeButton.setAttribute("aria-label", "Banger unavailable");
2406
  setTooltip(likeButton, "Banger unavailable");
2407
  likeCount.textContent = "0";
@@ -2414,25 +2443,25 @@
2414
  if (!currentUser) {
2415
  likeButton.disabled = true;
2416
  likeButton.classList.remove("liked");
2417
- likeButton.textContent = `♡ Banger ${count}`;
2418
  likeButton.setAttribute("aria-label", "Sign in with Hugging Face to mark this song as a banger");
2419
  setTooltip(likeButton, "Sign in with Hugging Face to mark this as a banger");
2420
  } else if (hasReported(track.id)) {
2421
  likeButton.disabled = true;
2422
  likeButton.classList.remove("liked");
2423
- likeButton.textContent = `♡ Banger ${count}`;
2424
  likeButton.setAttribute("aria-label", "Cannot mark a reported song as a banger");
2425
  setTooltip(likeButton, "You reported this song");
2426
  } else if (hasLiked(track.id)) {
2427
  likeButton.disabled = true;
2428
  likeButton.classList.add("liked");
2429
- likeButton.textContent = `♥ Banger ${count}`;
2430
  likeButton.setAttribute("aria-label", `Marked as banger. ${count} Banger votes`);
2431
  setTooltip(likeButton, "Already marked as banger");
2432
  } else {
2433
  likeButton.disabled = false;
2434
  likeButton.classList.remove("liked");
2435
- likeButton.textContent = `♡ Banger ${count}`;
2436
  likeButton.setAttribute("aria-label", `Mark this song as a banger. ${count} banger likes`);
2437
  setTooltip(likeButton, "Only for absolute standouts");
2438
  }
@@ -2444,8 +2473,8 @@
2444
  downvoteButton.disabled = true;
2445
  upvoteButton.classList.remove("voted");
2446
  downvoteButton.classList.remove("voted");
2447
- upvoteButton.textContent = "↑ 0";
2448
- downvoteButton.textContent = "↓ 0";
2449
  upvoteButton.setAttribute("aria-label", "Upvote unavailable");
2450
  downvoteButton.setAttribute("aria-label", "Downvote unavailable");
2451
  setTooltip(upvoteButton, "Upvote unavailable");
@@ -2461,8 +2490,8 @@
2461
 
2462
  upvoteCount.textContent = String(upCount);
2463
  downvoteCount.textContent = String(downCount);
2464
- upvoteButton.textContent = `↑ ${upCount}`;
2465
- downvoteButton.textContent = `↓ ${downCount}`;
2466
  upvoteButton.classList.toggle("voted", currentVote === "up");
2467
  downvoteButton.classList.toggle("voted", currentVote === "down");
2468
 
@@ -2502,7 +2531,7 @@
2502
  if (!track || !track.id) {
2503
  reportButton.disabled = true;
2504
  reportButton.classList.remove("reported");
2505
- reportButton.textContent = "⚑";
2506
  reportButton.setAttribute("aria-label", "Report unavailable");
2507
  setTooltip(reportButton, "Report unavailable");
2508
  return;
@@ -2513,7 +2542,7 @@
2513
  if (!currentUser) {
2514
  reportButton.disabled = true;
2515
  reportButton.classList.remove("reported");
2516
- reportButton.textContent = "⚑";
2517
  reportButton.setAttribute("aria-label", "Sign in with Hugging Face to report this song");
2518
  setTooltip(reportButton, "Sign in with Hugging Face to report");
2519
  return;
@@ -2522,13 +2551,13 @@
2522
  if (hasReported(track.id)) {
2523
  reportButton.disabled = false;
2524
  reportButton.classList.add("reported");
2525
- reportButton.textContent = "⚑";
2526
  reportButton.setAttribute("aria-label", `Undo report. ${count} reports`);
2527
  setTooltip(reportButton, "Undo report");
2528
  } else {
2529
  reportButton.disabled = false;
2530
  reportButton.classList.remove("reported");
2531
- reportButton.textContent = "⚑";
2532
  reportButton.setAttribute("aria-label", `Report this song. ${count} reports`);
2533
  setTooltip(reportButton, "Report problematic track");
2534
  }
@@ -2713,6 +2742,7 @@
2713
 
2714
  const previousRenderedTrackKey = renderedTrackKey;
2715
  renderedTrackKey = nextTrackKey;
 
2716
 
2717
  if (!status.track) {
2718
  renderTrackSource(null);
@@ -2987,7 +3017,8 @@
2987
  }
2988
 
2989
  async function likeCurrentSong() {
2990
- if (!currentTrackId) {
 
2991
  return;
2992
  }
2993
 
@@ -2996,28 +3027,30 @@
2996
  return;
2997
  }
2998
 
2999
- if (hasLiked(currentTrackId) || hasReported(currentTrackId)) {
3000
  return;
3001
  }
3002
 
3003
  likeButton.disabled = true;
3004
 
3005
  try {
3006
- const result = await getJSON(`/api/like/${currentTrackId}`, {
3007
  method: "POST",
3008
  });
3009
 
3010
- markLiked(currentTrackId);
 
3011
 
3012
  if (result.status) {
3013
  renderStatus(result.status, { forceTrackUpdate: true });
3014
  } else {
3015
  likeCount.textContent = String(result.like_count || 0);
3016
  likeButton.classList.add("liked");
3017
- likeButton.textContent = `♥ Banger ${result.like_count || 0}`;
3018
  likeButton.setAttribute("aria-label", `Marked as banger. ${result.like_count || 0} Banger votes`);
3019
  setTooltip(likeButton, "Already marked as banger");
3020
  }
 
3021
  } catch (err) {
3022
  console.error(err);
3023
  const needsSignIn = String(err.message || "").includes("401");
@@ -3033,7 +3066,8 @@
3033
  }
3034
 
3035
  async function voteCurrentSong(nextVote) {
3036
- if (!currentTrackId) {
 
3037
  return;
3038
  }
3039
 
@@ -3042,19 +3076,19 @@
3042
  return;
3043
  }
3044
 
3045
- const previousVote = getVote(currentTrackId);
3046
  const calls = [];
3047
 
3048
  if (previousVote === nextVote) {
3049
- calls.push(`/api/un${nextVote}vote/${currentTrackId}`);
3050
  } else {
3051
  if (previousVote === "up") {
3052
- calls.push(`/api/unupvote/${currentTrackId}`);
3053
  } else if (previousVote === "down") {
3054
- calls.push(`/api/undownvote/${currentTrackId}`);
3055
  }
3056
 
3057
- calls.push(`/api/${nextVote}vote/${currentTrackId}`);
3058
  }
3059
 
3060
  upvoteButton.disabled = true;
@@ -3068,16 +3102,18 @@
3068
  }
3069
 
3070
  if (previousVote === nextVote) {
3071
- clearVote(currentTrackId);
3072
  } else {
3073
- markVote(currentTrackId, nextVote);
3074
  }
3075
 
 
 
3076
  if (result && result.status) {
3077
  renderStatus(result.status, { forceTrackUpdate: true });
3078
  } else if (result) {
3079
  const partialTrack = {
3080
- id: currentTrackId,
3081
  like_count: result.like_count || 0,
3082
  upvote_count: result.upvote_count || 0,
3083
  downvote_count: result.downvote_count || 0,
@@ -3088,6 +3124,7 @@
3088
  updateVoteButtons(partialTrack);
3089
  updateReportButton(partialTrack);
3090
  }
 
3091
  } catch (err) {
3092
  console.error(err);
3093
  const needsSignIn = String(err.message || "").includes("401");
@@ -3109,7 +3146,8 @@
3109
 
3110
 
3111
  async function reportCurrentSong() {
3112
- if (!currentTrackId) {
 
3113
  return;
3114
  }
3115
 
@@ -3118,10 +3156,10 @@
3118
  return;
3119
  }
3120
 
3121
- const shouldUndoReport = hasReported(currentTrackId);
3122
  const endpoint = shouldUndoReport
3123
- ? `/api/unreport/${currentTrackId}`
3124
- : `/api/report/${currentTrackId}`;
3125
 
3126
  reportButton.disabled = true;
3127
 
@@ -3131,16 +3169,18 @@
3131
  });
3132
 
3133
  if (shouldUndoReport) {
3134
- clearReported(currentTrackId);
3135
  } else {
3136
- markReported(currentTrackId);
3137
  }
3138
 
 
 
3139
  if (result.status) {
3140
  renderStatus(result.status, { forceTrackUpdate: true });
3141
  } else {
3142
  const partialTrack = {
3143
- id: currentTrackId,
3144
  like_count: result.like_count || 0,
3145
  upvote_count: result.upvote_count || 0,
3146
  downvote_count: result.downvote_count || 0,
@@ -3151,6 +3191,7 @@
3151
  updateVoteButtons(partialTrack);
3152
  updateReportButton(partialTrack);
3153
  }
 
3154
  } catch (err) {
3155
  console.error(err);
3156
  const errorMessage = String(err.message || "");
 
1434
  .track-origin { margin-top: 10px; }
1435
  }
1436
  </style>
1437
+ <link rel="stylesheet" href="/static/action-feedback.css?v=1" />
1438
 
1439
  <script src="https://cdn.jsdelivr.net/gh/foobar404/wave.js/dist/bundle.js"></script>
1440
  </head>
 
1696
  </details>
1697
  </main>
1698
 
1699
+ <script src="/static/action-feedback.js?v=1"></script>
1700
  <script>
1701
  const DEFAULT_COVER_URL =
1702
  "https://huggingface.co/datasets/Chunte/Huggy/resolve/main/modern%20Huggies/Huggy%20Hi.png";
 
2399
  }
2400
  }
2401
 
2402
+ function setActionButtonContent(button, icon, label = "") {
2403
+ let iconElement = button.querySelector(".radio-action-icon");
2404
+ let labelElement = button.querySelector(".radio-action-label");
2405
+ if (!iconElement || !labelElement) {
2406
+ iconElement = document.createElement("span");
2407
+ iconElement.className = "radio-action-icon";
2408
+ iconElement.setAttribute("aria-hidden", "true");
2409
+ labelElement = document.createElement("span");
2410
+ labelElement.className = "radio-action-label";
2411
+ button.replaceChildren(iconElement, labelElement);
2412
+ }
2413
+ iconElement.textContent = icon;
2414
+ labelElement.textContent = label;
2415
+ labelElement.hidden = !label;
2416
+ }
2417
+
2418
+ function playActionFeedback(kind, trackId) {
2419
+ if (!currentUser || currentTrackId !== trackId) return;
2420
+ const button = { banger: likeButton, up: upvoteButton, down: downvoteButton, report: reportButton }[kind];
2421
+ try {
2422
+ window.RadioActionFeedback?.play(button, kind);
2423
+ } catch (error) {
2424
+ // Optional visuals must never turn a saved vote into an apparent error.
2425
+ console.warn("Action feedback unavailable:", error);
2426
+ }
2427
+ }
2428
+
2429
  function updateLikeButton(track) {
2430
  if (!track || !track.id) {
2431
  likeButton.disabled = true;
2432
  likeButton.classList.remove("liked");
2433
+ setActionButtonContent(likeButton, "♡", "Banger 0");
2434
  likeButton.setAttribute("aria-label", "Banger unavailable");
2435
  setTooltip(likeButton, "Banger unavailable");
2436
  likeCount.textContent = "0";
 
2443
  if (!currentUser) {
2444
  likeButton.disabled = true;
2445
  likeButton.classList.remove("liked");
2446
+ setActionButtonContent(likeButton, "♡", `Banger ${count}`);
2447
  likeButton.setAttribute("aria-label", "Sign in with Hugging Face to mark this song as a banger");
2448
  setTooltip(likeButton, "Sign in with Hugging Face to mark this as a banger");
2449
  } else if (hasReported(track.id)) {
2450
  likeButton.disabled = true;
2451
  likeButton.classList.remove("liked");
2452
+ setActionButtonContent(likeButton, "♡", `Banger ${count}`);
2453
  likeButton.setAttribute("aria-label", "Cannot mark a reported song as a banger");
2454
  setTooltip(likeButton, "You reported this song");
2455
  } else if (hasLiked(track.id)) {
2456
  likeButton.disabled = true;
2457
  likeButton.classList.add("liked");
2458
+ setActionButtonContent(likeButton, "♥", `Banger ${count}`);
2459
  likeButton.setAttribute("aria-label", `Marked as banger. ${count} Banger votes`);
2460
  setTooltip(likeButton, "Already marked as banger");
2461
  } else {
2462
  likeButton.disabled = false;
2463
  likeButton.classList.remove("liked");
2464
+ setActionButtonContent(likeButton, "♡", `Banger ${count}`);
2465
  likeButton.setAttribute("aria-label", `Mark this song as a banger. ${count} banger likes`);
2466
  setTooltip(likeButton, "Only for absolute standouts");
2467
  }
 
2473
  downvoteButton.disabled = true;
2474
  upvoteButton.classList.remove("voted");
2475
  downvoteButton.classList.remove("voted");
2476
+ setActionButtonContent(upvoteButton, "↑", "0");
2477
+ setActionButtonContent(downvoteButton, "↓", "0");
2478
  upvoteButton.setAttribute("aria-label", "Upvote unavailable");
2479
  downvoteButton.setAttribute("aria-label", "Downvote unavailable");
2480
  setTooltip(upvoteButton, "Upvote unavailable");
 
2490
 
2491
  upvoteCount.textContent = String(upCount);
2492
  downvoteCount.textContent = String(downCount);
2493
+ setActionButtonContent(upvoteButton, "↑", String(upCount));
2494
+ setActionButtonContent(downvoteButton, "↓", String(downCount));
2495
  upvoteButton.classList.toggle("voted", currentVote === "up");
2496
  downvoteButton.classList.toggle("voted", currentVote === "down");
2497
 
 
2531
  if (!track || !track.id) {
2532
  reportButton.disabled = true;
2533
  reportButton.classList.remove("reported");
2534
+ setActionButtonContent(reportButton, "⚑");
2535
  reportButton.setAttribute("aria-label", "Report unavailable");
2536
  setTooltip(reportButton, "Report unavailable");
2537
  return;
 
2542
  if (!currentUser) {
2543
  reportButton.disabled = true;
2544
  reportButton.classList.remove("reported");
2545
+ setActionButtonContent(reportButton, "⚑");
2546
  reportButton.setAttribute("aria-label", "Sign in with Hugging Face to report this song");
2547
  setTooltip(reportButton, "Sign in with Hugging Face to report");
2548
  return;
 
2551
  if (hasReported(track.id)) {
2552
  reportButton.disabled = false;
2553
  reportButton.classList.add("reported");
2554
+ setActionButtonContent(reportButton, "⚑");
2555
  reportButton.setAttribute("aria-label", `Undo report. ${count} reports`);
2556
  setTooltip(reportButton, "Undo report");
2557
  } else {
2558
  reportButton.disabled = false;
2559
  reportButton.classList.remove("reported");
2560
+ setActionButtonContent(reportButton, "⚑");
2561
  reportButton.setAttribute("aria-label", `Report this song. ${count} reports`);
2562
  setTooltip(reportButton, "Report problematic track");
2563
  }
 
2742
 
2743
  const previousRenderedTrackKey = renderedTrackKey;
2744
  renderedTrackKey = nextTrackKey;
2745
+ if (nextTrackKey !== previousRenderedTrackKey) window.RadioActionFeedback?.clear();
2746
 
2747
  if (!status.track) {
2748
  renderTrackSource(null);
 
3017
  }
3018
 
3019
  async function likeCurrentSong() {
3020
+ const actionTrackId = currentTrackId;
3021
+ if (!actionTrackId) {
3022
  return;
3023
  }
3024
 
 
3027
  return;
3028
  }
3029
 
3030
+ if (hasLiked(actionTrackId) || hasReported(actionTrackId)) {
3031
  return;
3032
  }
3033
 
3034
  likeButton.disabled = true;
3035
 
3036
  try {
3037
+ const result = await getJSON(`/api/like/${actionTrackId}`, {
3038
  method: "POST",
3039
  });
3040
 
3041
+ markLiked(actionTrackId);
3042
+ if (currentTrackId !== actionTrackId) return;
3043
 
3044
  if (result.status) {
3045
  renderStatus(result.status, { forceTrackUpdate: true });
3046
  } else {
3047
  likeCount.textContent = String(result.like_count || 0);
3048
  likeButton.classList.add("liked");
3049
+ setActionButtonContent(likeButton, "♥", `Banger ${result.like_count || 0}`);
3050
  likeButton.setAttribute("aria-label", `Marked as banger. ${result.like_count || 0} Banger votes`);
3051
  setTooltip(likeButton, "Already marked as banger");
3052
  }
3053
+ playActionFeedback("banger", actionTrackId);
3054
  } catch (err) {
3055
  console.error(err);
3056
  const needsSignIn = String(err.message || "").includes("401");
 
3066
  }
3067
 
3068
  async function voteCurrentSong(nextVote) {
3069
+ const actionTrackId = currentTrackId;
3070
+ if (!actionTrackId) {
3071
  return;
3072
  }
3073
 
 
3076
  return;
3077
  }
3078
 
3079
+ const previousVote = getVote(actionTrackId);
3080
  const calls = [];
3081
 
3082
  if (previousVote === nextVote) {
3083
+ calls.push(`/api/un${nextVote}vote/${actionTrackId}`);
3084
  } else {
3085
  if (previousVote === "up") {
3086
+ calls.push(`/api/unupvote/${actionTrackId}`);
3087
  } else if (previousVote === "down") {
3088
+ calls.push(`/api/undownvote/${actionTrackId}`);
3089
  }
3090
 
3091
+ calls.push(`/api/${nextVote}vote/${actionTrackId}`);
3092
  }
3093
 
3094
  upvoteButton.disabled = true;
 
3102
  }
3103
 
3104
  if (previousVote === nextVote) {
3105
+ clearVote(actionTrackId);
3106
  } else {
3107
+ markVote(actionTrackId, nextVote);
3108
  }
3109
 
3110
+ if (currentTrackId !== actionTrackId) return;
3111
+
3112
  if (result && result.status) {
3113
  renderStatus(result.status, { forceTrackUpdate: true });
3114
  } else if (result) {
3115
  const partialTrack = {
3116
+ id: actionTrackId,
3117
  like_count: result.like_count || 0,
3118
  upvote_count: result.upvote_count || 0,
3119
  downvote_count: result.downvote_count || 0,
 
3124
  updateVoteButtons(partialTrack);
3125
  updateReportButton(partialTrack);
3126
  }
3127
+ if (previousVote !== nextVote) playActionFeedback(nextVote, actionTrackId);
3128
  } catch (err) {
3129
  console.error(err);
3130
  const needsSignIn = String(err.message || "").includes("401");
 
3146
 
3147
 
3148
  async function reportCurrentSong() {
3149
+ const actionTrackId = currentTrackId;
3150
+ if (!actionTrackId) {
3151
  return;
3152
  }
3153
 
 
3156
  return;
3157
  }
3158
 
3159
+ const shouldUndoReport = hasReported(actionTrackId);
3160
  const endpoint = shouldUndoReport
3161
+ ? `/api/unreport/${actionTrackId}`
3162
+ : `/api/report/${actionTrackId}`;
3163
 
3164
  reportButton.disabled = true;
3165
 
 
3169
  });
3170
 
3171
  if (shouldUndoReport) {
3172
+ clearReported(actionTrackId);
3173
  } else {
3174
+ markReported(actionTrackId);
3175
  }
3176
 
3177
+ if (currentTrackId !== actionTrackId) return;
3178
+
3179
  if (result.status) {
3180
  renderStatus(result.status, { forceTrackUpdate: true });
3181
  } else {
3182
  const partialTrack = {
3183
+ id: actionTrackId,
3184
  like_count: result.like_count || 0,
3185
  upvote_count: result.upvote_count || 0,
3186
  downvote_count: result.downvote_count || 0,
 
3191
  updateVoteButtons(partialTrack);
3192
  updateReportButton(partialTrack);
3193
  }
3194
+ if (!shouldUndoReport) playActionFeedback("report", actionTrackId);
3195
  } catch (err) {
3196
  console.error(err);
3197
  const errorMessage = String(err.message || "");
tests/test_action_feedback.js ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Exercise the real player handlers with controlled API responses; never cast live votes.
2
+ const assert = require('node:assert/strict');
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const vm = require('node:vm');
6
+ const html = fs.readFileSync(path.join(__dirname, '../static/index.html'), 'utf8');
7
+ const handlers = html.slice(html.indexOf(' async function likeCurrentSong()'), html.indexOf(' playToggleButton.addEventListener'));
8
+ const feedback = html.slice(html.indexOf(' function playActionFeedback('), html.indexOf(' function updateLikeButton('));
9
+
10
+ // Verify the real optional animation module, including cleanup and reduced motion.
11
+ const motionSource = fs.readFileSync(path.join(__dirname, '../static/action-feedback.js'), 'utf8');
12
+ for (const reduced of [false, true]) {
13
+ const children = new Set(), animations = [], timers = new Map(), events = {};
14
+ let timerId = 0;
15
+ const element = () => ({
16
+ isConnected: true, disabled: true, children: [], textContent: '',
17
+ style: { setProperty() {} }, setAttribute() {},
18
+ appendChild(child) { this.children.push(child); children.add(child); },
19
+ remove() { children.delete(this); },
20
+ getBoundingClientRect: () => ({ left: 270, top: 60, width: 48, height: 34 }),
21
+ animate() { const animation = { cancelled: false, cancel() { this.cancelled = true; } }; animations.push(animation); return animation; },
22
+ querySelector() { return this.icon; },
23
+ });
24
+ const context = {
25
+ window: { matchMedia: () => ({ matches: reduced, addEventListener: (type, cb) => { events.motionChange = cb; } }), addEventListener: (type, cb) => { events[type] = cb; } },
26
+ document: { createElement: element, body: element(), documentElement: { clientWidth: 320 } },
27
+ setTimeout: callback => { timers.set(++timerId, callback); return timerId; },
28
+ clearTimeout: id => timers.delete(id),
29
+ };
30
+ vm.createContext(context); vm.runInContext(motionSource, context);
31
+ const api = context.window.RadioActionFeedback;
32
+ for (const kind of ['banger', 'up', 'down', 'report']) {
33
+ const button = element(); button.icon = element();
34
+ const start = animations.length;
35
+ api.play(button, kind);
36
+ assert.equal(button.disabled, true, 'Visual feedback cannot alter button permissions');
37
+ assert.equal(timers.size, 1);
38
+ if (reduced) assert.equal(animations.length, start, 'Reduced motion must not start any animation');
39
+ else assert.ok(animations.length > start);
40
+ api.play(button, kind);
41
+ assert.equal(timers.size, 1, 'Repeated feedback replaces its previous cleanup timer');
42
+ api.clear(); api.clear();
43
+ assert.equal(timers.size, 0);
44
+ assert.equal([...children].filter(el => el.className === 'radio-action-feedback').length, 0);
45
+ assert.ok(animations.every(animation => animation.cancelled));
46
+ }
47
+ const button = element(); button.icon = element();
48
+ api.play(button, 'banger'); events.resize(); assert.equal(timers.size, 0);
49
+ api.play(button, 'up'); events.motionChange(); assert.equal(timers.size, 0);
50
+ api.play(button, 'report'); [...timers.values()][0](); assert.equal(timers.size, 0);
51
+ }
52
+
53
+ function setup({ vote = null, liked = false, reported = false, user = true, request } = {}) {
54
+ const calls = [], effects = [], messages = [], writes = [], rendered = [];
55
+ const button = () => ({ disabled: false, classList: { add() {} }, setAttribute() {} });
56
+ const context = {
57
+ currentTrackId: 'minimax-music3:a', currentUser: user ? { name: 'test' } : null,
58
+ likeButton: button(), upvoteButton: button(), downvoteButton: button(), reportButton: button(), likeCount: {},
59
+ hasLiked: () => liked, hasReported: () => reported, getVote: () => vote,
60
+ markLiked: id => { writes.push(['like', id]); liked = true; },
61
+ markReported: id => { writes.push(['report', id]); reported = true; },
62
+ clearReported: id => { writes.push(['unreport', id]); reported = false; },
63
+ markVote: (id, next) => { writes.push([next, id]); vote = next; },
64
+ clearVote: id => { writes.push(['unvote', id]); vote = null; },
65
+ setActionButtonContent() {}, setTooltip() {}, updateLikeButton() {}, updateVoteButtons() {}, updateReportButton() {},
66
+ showMessage: (...args) => messages.push(args), refreshStatus: async () => {},
67
+ renderStatus: status => { rendered.push(status); context.currentTrackId = status.track.id; },
68
+ getJSON: async (endpoint, options) => { calls.push([endpoint, options.method]); return request ? request(endpoint) : { like_count: 1, upvote_count: 1, downvote_count: 1, report_count: 1 }; },
69
+ window: { RadioActionFeedback: { play: (button, kind) => effects.push(kind) } },
70
+ console: { error() {}, warn() {} },
71
+ };
72
+ vm.createContext(context);
73
+ vm.runInContext(feedback + handlers, context);
74
+ return { context, calls, effects, messages, writes, rendered };
75
+ }
76
+
77
+ (async () => {
78
+ for (const [method, kind, endpoint] of [['likeCurrentSong', 'banger', 'like'], ['voteCurrentSong', 'up', 'upvote'], ['voteCurrentSong', 'down', 'downvote'], ['reportCurrentSong', 'report', 'report']]) {
79
+ let resolve;
80
+ const t = setup({ request: () => new Promise(done => { resolve = done; }) });
81
+ const pending = t.context[method](kind);
82
+ assert.deepEqual(t.effects, [], 'No effect before the server confirms');
83
+ assert.deepEqual(t.calls, [[`/api/${endpoint}/minimax-music3:a`, 'POST']]);
84
+ resolve({ like_count: 3 });
85
+ await pending;
86
+ assert.deepEqual(t.effects, [kind]);
87
+
88
+ for (const message of ['401', '403', '500', 'Network error']) {
89
+ const failed = setup({ request: () => { throw Error(message); } });
90
+ await failed.context[method](kind);
91
+ assert.deepEqual(failed.effects, []);
92
+ assert.deepEqual(failed.writes, []);
93
+ assert.equal(failed.messages.length, 1);
94
+ }
95
+ for (const eligibility of [{ user: false }, { user: true, noTrack: true }]) {
96
+ const denied = setup(eligibility);
97
+ if (eligibility.noTrack) denied.context.currentTrackId = null;
98
+ await denied.context[method](kind);
99
+ assert.deepEqual(denied.calls, []);
100
+ assert.deepEqual(denied.effects, []);
101
+ }
102
+ const changed = setup({ request: async () => {
103
+ changed.context.currentTrackId = 'ace-step:b';
104
+ return { status: { track: { id: 'minimax-music3:a' } } };
105
+ } });
106
+ await changed.context[method](kind);
107
+ assert.deepEqual(changed.effects, [], 'Never animate a different track');
108
+ assert.equal(changed.context.currentTrackId, 'ace-step:b');
109
+ assert.equal(changed.writes[0][1], 'minimax-music3:a', 'Persist the clicked track');
110
+ assert.equal(changed.rendered.length, 0, 'Do not restore an obsolete track');
111
+
112
+ const serverChanged = setup({ request: async () => ({ status: { track: { id: 'ace-step:b' } } }) });
113
+ await serverChanged.context[method](kind);
114
+ assert.deepEqual(serverChanged.effects, []);
115
+
116
+ const noAnimations = setup();
117
+ noAnimations.context.window.RadioActionFeedback = undefined;
118
+ await noAnimations.context[method](kind);
119
+ assert.equal(noAnimations.messages.length, 0, 'Optional animation bundle can be absent');
120
+ const brokenAnimation = setup();
121
+ brokenAnimation.context.window.RadioActionFeedback.play = () => { throw Error('unsupported browser'); };
122
+ await brokenAnimation.context[method](kind);
123
+ assert.equal(brokenAnimation.messages.length, 0, 'A visual failure must not masquerade as a failed vote');
124
+ }
125
+ for (const value of ['up', 'down']) {
126
+ const undo = setup({ vote: value });
127
+ await undo.context.voteCurrentSong(value);
128
+ assert.deepEqual(undo.effects, []);
129
+ assert.deepEqual(undo.calls, [[`/api/un${value}vote/minimax-music3:a`, 'POST']]);
130
+ const other = value === 'up' ? 'down' : 'up';
131
+ const switchVote = setup({ vote: other });
132
+ await switchVote.context.voteCurrentSong(value);
133
+ assert.deepEqual(switchVote.calls, [[`/api/un${other}vote/minimax-music3:a`, 'POST'], [`/api/${value}vote/minimax-music3:a`, 'POST']]);
134
+ assert.deepEqual(switchVote.effects, [value]);
135
+ const secondFails = setup({ vote: other, request: endpoint => {
136
+ if (endpoint === `/api/${value}vote/minimax-music3:a`) throw Error('500');
137
+ return {};
138
+ } });
139
+ await secondFails.context.voteCurrentSong(value);
140
+ assert.deepEqual(secondFails.effects, []);
141
+ }
142
+ const undoReport = setup({ reported: true });
143
+ await undoReport.context.reportCurrentSong();
144
+ assert.deepEqual(undoReport.effects, []);
145
+ assert.deepEqual(undoReport.calls, [['/api/unreport/minimax-music3:a', 'POST']]);
146
+ for (const state of [{ liked: true }, { reported: true }]) {
147
+ const duplicate = setup(state);
148
+ await duplicate.context.likeCurrentSong();
149
+ assert.deepEqual(duplicate.calls, []);
150
+ assert.deepEqual(duplicate.effects, []);
151
+ }
152
+ console.log('Action feedback: confirmed actions, errors, eligibility, reversals, track races and optional visuals OK');
153
+ })().catch(error => { console.error(error); process.exitCode = 1; });