Add Back Spin feature
Browse files
app.py
CHANGED
|
@@ -268,16 +268,19 @@ def _why_note() -> str:
|
|
| 268 |
</div>"""
|
| 269 |
|
| 270 |
|
| 271 |
-
def _placeholder(msg: str = "") -> str:
|
| 272 |
inner = msg or "Pull the lever โ hit <strong>SPIN</strong> to discover a random project!"
|
|
|
|
|
|
|
|
|
|
| 273 |
return f"""
|
| 274 |
-
<div class="ph-wrap">
|
| 275 |
<div class="ph-dice">๐ฐ</div>
|
| 276 |
<p class="ph-txt">{inner}</p>
|
| 277 |
</div>"""
|
| 278 |
|
| 279 |
|
| 280 |
-
def _card(sp: dict, spin_count: int,
|
| 281 |
e = _html.escape
|
| 282 |
cat = sp["category"]
|
| 283 |
em = CAT_EMOJI.get(cat, "๐")
|
|
@@ -307,7 +310,7 @@ def _card(sp: dict, spin_count: int, position: int, bucket_total: int) -> str:
|
|
| 307 |
<div class="card-chips">{chips}</div>
|
| 308 |
<div class="card-foot">
|
| 309 |
<a href="{sp['url']}" target="_blank" rel="noopener" class="open-btn">๐ Open Space</a>
|
| 310 |
-
<span class="draw-note">
|
| 311 |
</div>
|
| 312 |
</div>"""
|
| 313 |
|
|
@@ -338,19 +341,28 @@ def _browse_list(spaces: list, cat: str, seen: list) -> str:
|
|
| 338 |
|
| 339 |
# โโ Callbacks โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 340 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
def on_load():
|
| 342 |
spaces = load_spaces()
|
| 343 |
n = len(spaces)
|
| 344 |
msg = (f"Loaded <strong>{n}</strong> projects from the {HACKATHON_NAME} โ ready to roll! ๐"
|
| 345 |
if n else "Couldn't reach the Hub right now โ try refreshing in a moment.")
|
| 346 |
-
|
|
|
|
|
|
|
| 347 |
|
| 348 |
|
| 349 |
-
def on_spin(seen: list, cat: str, spin_count: int):
|
| 350 |
spaces = load_spaces()
|
|
|
|
| 351 |
bucket = _bucket(spaces, cat)
|
| 352 |
if not bucket:
|
| 353 |
-
|
|
|
|
|
|
|
| 354 |
|
| 355 |
seen_set = set(seen)
|
| 356 |
unseen = [s for s in bucket if s["id"] not in seen_set]
|
|
@@ -361,21 +373,49 @@ def on_spin(seen: list, cat: str, spin_count: int):
|
|
| 361 |
unseen = bucket
|
| 362 |
banner = '<div class="reset-note">๐ You\'ve seen every project here โ reshuffling the deck!</div>'
|
| 363 |
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
new_seen = list(seen) + [pick["id"]]
|
| 366 |
-
new_count = spin_count + 1
|
| 367 |
position = sum(1 for s in bucket if s["id"] in set(new_seen))
|
| 368 |
|
| 369 |
-
|
| 370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
|
| 373 |
def on_category(cat: str, seen: list):
|
|
|
|
|
|
|
| 374 |
spaces = load_spaces()
|
| 375 |
-
|
| 376 |
-
n = len(_bucket(spaces, cat))
|
| 377 |
-
msg = f"{em} <strong>{_html.escape(cat)}</strong> โ {n} project{'s' if n != 1 else ''}. Hit SPIN!"
|
| 378 |
-
return cat, _stats(seen, spaces, cat), _placeholder(msg)
|
| 379 |
|
| 380 |
|
| 381 |
def on_browse(cat: str, seen: list):
|
|
@@ -459,6 +499,15 @@ footer { display: none !important; }
|
|
| 459 |
#spin-btn.spinning, #spin-btn.spinning button { animation: glow-fast .45s ease-in-out infinite; }
|
| 460 |
@keyframes glow-fast { 0%,100%{ box-shadow:0 4px 26px rgba(124,58,237,.7); } 50%{ box-shadow:0 6px 44px rgba(244,114,182,.85), 0 0 28px rgba(96,165,250,.7); } }
|
| 461 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
#cat-dd label span { color:#50508a!important; font-size:.78rem!important; }
|
| 463 |
#cat-dd input, #cat-dd .wrap { background:#0f0f28!important; border-color:#1e1e48!important; color:#c0c0e8!important; }
|
| 464 |
|
|
@@ -478,6 +527,7 @@ footer { display: none !important; }
|
|
| 478 |
.reel::after { right:0; background:linear-gradient(270deg,#0c0c22,transparent); }
|
| 479 |
.reel-strip { display:inline-block; white-space:nowrap; font-size:2.7rem; line-height:78px;
|
| 480 |
animation: reel-roll .42s linear infinite; filter: blur(.3px); will-change: transform; }
|
|
|
|
| 481 |
@keyframes reel-roll { from { transform:translateX(0); } to { transform:translateX(-2em); } }
|
| 482 |
.reel-txt { color:#7c7cb8; margin-top:.85rem; font-size:.92rem; letter-spacing:.05em; animation: pulse-txt 1s ease-in-out infinite; }
|
| 483 |
@keyframes pulse-txt { 0%,100%{ opacity:.45; } 50%{ opacity:1; } }
|
|
@@ -578,6 +628,12 @@ HEAD_HTML = r"""<script>
|
|
| 578 |
step();
|
| 579 |
};
|
| 580 |
RL.stopSpinSound = () => { if (spinTimer) { clearTimeout(spinTimer); spinTimer = null; } };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
|
| 582 |
// confetti burst on a fixed overlay canvas
|
| 583 |
let cv, cx, parts = [], raf = null;
|
|
@@ -604,63 +660,88 @@ HEAD_HTML = r"""<script>
|
|
| 604 |
};
|
| 605 |
|
| 606 |
const MIN_SPIN = 1100; // keep the wheel visibly spinning even when the draw is instant
|
|
|
|
| 607 |
const REEL = '<div class="reel-wrap"><div class="reel"><div class="reel-strip">๐ฎ ๐จ ๐๏ธ ๐ฉบ ๐ผ ๐ โ๏ธ ๐ ๐ฌ ๐ก๏ธ ๐ธ ๐ ๐ฎ ๐จ ๐๏ธ ๐ฉบ<\/div><\/div><p class="reel-txt">spinning the wheelโฆ<\/p><\/div>';
|
|
|
|
| 608 |
// The reel is an OVERLAY on top of #card-out โ never the card's own DOM. Two
|
| 609 |
// bugs this fixes: (1) mutating Gradio's Svelte-managed card node broke every
|
| 610 |
// spin after the first; (2) when a cached draw returns instantly the reel/
|
| 611 |
// sound used to flash by invisibly โ now the overlay (and ticks) stay up for
|
| 612 |
// at least MIN_SPIN before the reveal.
|
| 613 |
-
|
|
|
|
|
|
|
|
|
|
| 614 |
ac();
|
|
|
|
| 615 |
if (RL._poll) { clearInterval(RL._poll); RL._poll = null; }
|
| 616 |
if (RL._revealTimer) { clearTimeout(RL._revealTimer); RL._revealTimer = null; }
|
| 617 |
RL.stopSpinSound();
|
| 618 |
RL._spinStart = performance.now();
|
| 619 |
-
|
|
|
|
|
|
|
| 620 |
RL._prevSpin = prev ? prev.getAttribute('data-spin') : '-1';
|
| 621 |
const host = document.getElementById('card-out');
|
| 622 |
if (host) {
|
| 623 |
host.style.position = 'relative';
|
| 624 |
let ov = host.querySelector('.rl-reel-overlay');
|
| 625 |
if (!ov) { ov = document.createElement('div'); ov.className = 'rl-reel-overlay'; host.appendChild(ov); }
|
| 626 |
-
ov.innerHTML =
|
| 627 |
}
|
| 628 |
-
const b = document.getElementById(
|
| 629 |
-
|
| 630 |
-
// Poll for the new
|
| 631 |
-
// arrived AND the wheel has spun for at least
|
| 632 |
const t0 = performance.now();
|
| 633 |
RL._poll = setInterval(() => {
|
| 634 |
-
const cur = document.querySelector('#card-out
|
| 635 |
const arrived = cur && cur.getAttribute('data-spin') !== RL._prevSpin;
|
| 636 |
if (arrived || performance.now() - t0 > 6000) {
|
| 637 |
clearInterval(RL._poll); RL._poll = null;
|
| 638 |
-
const wait = Math.max(0,
|
| 639 |
RL._revealTimer = setTimeout(() => { RL._revealTimer = null; RL.land(); }, wait);
|
| 640 |
}
|
| 641 |
}, 40);
|
| 642 |
};
|
|
|
|
|
|
|
| 643 |
RL.land = () => {
|
| 644 |
RL.stopSpinSound();
|
| 645 |
-
const b = document.getElementById(
|
| 646 |
const host = document.getElementById('card-out');
|
| 647 |
const ov = host && host.querySelector('.rl-reel-overlay');
|
| 648 |
-
if (ov) ov.remove(); // reveal the
|
| 649 |
const card = document.querySelector('#card-out .space-card');
|
| 650 |
-
if (card) {
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
|
|
|
|
|
|
| 655 |
};
|
| 656 |
|
| 657 |
-
// delegated listeners survive Gradio re-renders
|
|
|
|
|
|
|
|
|
|
| 658 |
document.addEventListener('click', (e) => {
|
| 659 |
-
|
| 660 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 661 |
if (m) { RL.muted = !RL.muted; m.textContent = RL.muted ? '๐' : '๐'; m.classList.toggle('muted', RL.muted); if (RL.muted) RL.stopSpinSound(); }
|
| 662 |
}, true);
|
| 663 |
-
document.addEventListener('pointerover', (e) => {
|
|
|
|
|
|
|
| 664 |
})();
|
| 665 |
</script>
|
| 666 |
"""
|
|
@@ -672,6 +753,8 @@ with gr.Blocks(css=CSS, title="๐ฐ Hackathon Roulette", theme=gr.themes.Base(),
|
|
| 672 |
seen_st = gr.State([])
|
| 673 |
cat_st = gr.State("All")
|
| 674 |
spin_ctr = gr.State(0)
|
|
|
|
|
|
|
| 675 |
|
| 676 |
gr.HTML(f"""
|
| 677 |
<div class="app-header">
|
|
@@ -685,22 +768,31 @@ with gr.Blocks(css=CSS, title="๐ฐ Hackathon Roulette", theme=gr.themes.Base(),
|
|
| 685 |
|
| 686 |
with gr.Row():
|
| 687 |
cat_dd = gr.Dropdown(choices=ALL_CATEGORIES, value="All", label="Browse by category",
|
| 688 |
-
elem_id="cat-dd",
|
| 689 |
-
|
|
|
|
|
|
|
|
|
|
| 690 |
|
| 691 |
card_out = gr.HTML(elem_id="card-out")
|
| 692 |
|
| 693 |
with gr.Accordion("๐ Browse all in this category", open=False, elem_id="browse-acc") as browse_acc:
|
| 694 |
browse_out = gr.HTML()
|
| 695 |
|
| 696 |
-
# events
|
| 697 |
-
demo.load(on_load,
|
|
|
|
| 698 |
spin_btn.click(
|
| 699 |
on_spin,
|
| 700 |
-
inputs=[seen_st, cat_st, spin_ctr],
|
| 701 |
-
outputs=[card_out, seen_st, spin_ctr, stats_out],
|
| 702 |
-
)
|
| 703 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 704 |
# keep the browse list in sync whenever the category changes or the panel opens
|
| 705 |
cat_dd.change(on_browse, inputs=[cat_dd, seen_st], outputs=[browse_out])
|
| 706 |
browse_acc.expand(on_browse, inputs=[cat_st, seen_st], outputs=[browse_out])
|
|
|
|
| 268 |
</div>"""
|
| 269 |
|
| 270 |
|
| 271 |
+
def _placeholder(msg: str = "", spin_count: int | None = None) -> str:
|
| 272 |
inner = msg or "Pull the lever โ hit <strong>SPIN</strong> to discover a random project!"
|
| 273 |
+
# When a placeholder is the *result* of a spin/back (overlay is up), carry
|
| 274 |
+
# data-spin so the client reveal poller resolves instead of timing out.
|
| 275 |
+
ds = f' data-spin="{spin_count}"' if spin_count is not None else ""
|
| 276 |
return f"""
|
| 277 |
+
<div class="ph-wrap"{ds}>
|
| 278 |
<div class="ph-dice">๐ฐ</div>
|
| 279 |
<p class="ph-txt">{inner}</p>
|
| 280 |
</div>"""
|
| 281 |
|
| 282 |
|
| 283 |
+
def _card(sp: dict, spin_count: int, footer: str = "") -> str:
|
| 284 |
e = _html.escape
|
| 285 |
cat = sp["category"]
|
| 286 |
em = CAT_EMOJI.get(cat, "๐")
|
|
|
|
| 310 |
<div class="card-chips">{chips}</div>
|
| 311 |
<div class="card-foot">
|
| 312 |
<a href="{sp['url']}" target="_blank" rel="noopener" class="open-btn">๐ Open Space</a>
|
| 313 |
+
<span class="draw-note">{footer}</span>
|
| 314 |
</div>
|
| 315 |
</div>"""
|
| 316 |
|
|
|
|
| 341 |
|
| 342 |
# โโ Callbacks โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 343 |
|
| 344 |
+
def _find(spaces: list, sid: str):
|
| 345 |
+
return next((s for s in spaces if s["id"] == sid), None)
|
| 346 |
+
|
| 347 |
+
|
| 348 |
def on_load():
|
| 349 |
spaces = load_spaces()
|
| 350 |
n = len(spaces)
|
| 351 |
msg = (f"Loaded <strong>{n}</strong> projects from the {HACKATHON_NAME} โ ready to roll! ๐"
|
| 352 |
if n else "Couldn't reach the Hub right now โ try refreshing in a moment.")
|
| 353 |
+
# state: seen, cat, spin_ctr, hist, pos + stats, card, back-button(disabled)
|
| 354 |
+
return ([], "All", 0, [], -1,
|
| 355 |
+
_stats([], spaces, "All"), _placeholder(msg), gr.update(interactive=False))
|
| 356 |
|
| 357 |
|
| 358 |
+
def on_spin(seen: list, cat: str, spin_count: int, hist: list, pos: int):
|
| 359 |
spaces = load_spaces()
|
| 360 |
+
new_count = spin_count + 1
|
| 361 |
bucket = _bucket(spaces, cat)
|
| 362 |
if not bucket:
|
| 363 |
+
# data-spin on the placeholder so the client reveal poller still resolves
|
| 364 |
+
return (_placeholder("No projects in this category.", new_count), seen, new_count,
|
| 365 |
+
_stats(seen, spaces, cat), hist, pos, gr.update(interactive=pos > 0))
|
| 366 |
|
| 367 |
seen_set = set(seen)
|
| 368 |
unseen = [s for s in bucket if s["id"] not in seen_set]
|
|
|
|
| 373 |
unseen = bucket
|
| 374 |
banner = '<div class="reset-note">๐ You\'ve seen every project here โ reshuffling the deck!</div>'
|
| 375 |
|
| 376 |
+
# Don't redraw the project already on screen (only reachable on the reshuffle
|
| 377 |
+
# path, where the current card is back in the pool) unless it's the only one.
|
| 378 |
+
cur_id = hist[pos] if 0 <= pos < len(hist) else None
|
| 379 |
+
pool = [s for s in unseen if s["id"] != cur_id] or unseen
|
| 380 |
+
pick = random.choice(pool)
|
| 381 |
new_seen = list(seen) + [pick["id"]]
|
|
|
|
| 382 |
position = sum(1 for s in bucket if s["id"] in set(new_seen))
|
| 383 |
|
| 384 |
+
# Browser-style history: drop anything "forward" of the current position,
|
| 385 |
+
# then append this draw. Back Spin walks this stack; it's global, so it
|
| 386 |
+
# survives category switches.
|
| 387 |
+
new_hist = list(hist[:pos + 1]) + [pick["id"]]
|
| 388 |
+
new_pos = len(new_hist) - 1
|
| 389 |
+
|
| 390 |
+
card = banner + _card(pick, new_count, f"draw #{position} of {len(bucket)}")
|
| 391 |
+
return (card, new_seen, new_count, _stats(new_seen, spaces, cat),
|
| 392 |
+
new_hist, new_pos, gr.update(interactive=new_pos > 0))
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def on_back(hist: list, pos: int, spin_count: int):
|
| 396 |
+
"""Re-show the project viewed just before the current one."""
|
| 397 |
+
spaces = load_spaces()
|
| 398 |
+
new_count = spin_count + 1 # always bump so the client poller resolves
|
| 399 |
+
# Step back when possible; if already at the start (button should be disabled,
|
| 400 |
+
# but a click can slip through before the disabled state propagates) just
|
| 401 |
+
# re-show the current card rather than hanging the reel.
|
| 402 |
+
new_pos = pos - 1 if (pos > 0 and hist) else max(pos, 0)
|
| 403 |
+
if not hist:
|
| 404 |
+
return _placeholder("Spin first!", new_count), pos, new_count, gr.update(interactive=False)
|
| 405 |
+
|
| 406 |
+
sp = _find(spaces, hist[new_pos]) if 0 <= new_pos < len(hist) else None
|
| 407 |
+
if sp is None:
|
| 408 |
+
card = _placeholder("That project is no longer available.", new_count)
|
| 409 |
+
else:
|
| 410 |
+
card = _card(sp, new_count, f"โฉ back-spin ยท {new_pos + 1} of {len(hist)}")
|
| 411 |
+
return card, new_pos, new_count, gr.update(interactive=new_pos > 0)
|
| 412 |
|
| 413 |
|
| 414 |
def on_category(cat: str, seen: list):
|
| 415 |
+
# Keep the current card on screen (don't reset it) so Back Spin always has the
|
| 416 |
+
# last project to return to, and so switching filters never wipes your view.
|
| 417 |
spaces = load_spaces()
|
| 418 |
+
return cat, _stats(seen, spaces, cat)
|
|
|
|
|
|
|
|
|
|
| 419 |
|
| 420 |
|
| 421 |
def on_browse(cat: str, seen: list):
|
|
|
|
| 499 |
#spin-btn.spinning, #spin-btn.spinning button { animation: glow-fast .45s ease-in-out infinite; }
|
| 500 |
@keyframes glow-fast { 0%,100%{ box-shadow:0 4px 26px rgba(124,58,237,.7); } 50%{ box-shadow:0 6px 44px rgba(244,114,182,.85), 0 0 28px rgba(96,165,250,.7); } }
|
| 501 |
|
| 502 |
+
/* โโ Back Spin button โ subtler sibling of SPIN, disabled until there's history โโ */
|
| 503 |
+
#back-btn, #back-btn button { background:#13132e!important; color:#a78bfa!important; border:1px solid #34346e!important;
|
| 504 |
+
border-radius:14px!important; font-size:1rem!important; font-weight:800!important; letter-spacing:.04em!important;
|
| 505 |
+
transition:transform .12s, box-shadow .2s, opacity .2s, border-color .2s!important; min-height:54px!important; }
|
| 506 |
+
#back-btn:not(:disabled):hover, #back-btn:not(:disabled) button:hover { transform:translateY(-2px)!important; border-color:#7c3aed!important; box-shadow:0 6px 22px rgba(124,58,237,.4)!important; }
|
| 507 |
+
#back-btn:not(:disabled):active, #back-btn:not(:disabled) button:active { transform:translateY(0) scale(.97)!important; }
|
| 508 |
+
#back-btn:disabled, #back-btn button:disabled, #back-btn[disabled] { opacity:.38!important; cursor:not-allowed!important; box-shadow:none!important; transform:none!important; }
|
| 509 |
+
#back-btn.spinning, #back-btn.spinning button { animation: glow-fast .4s ease-in-out infinite; border-color:#7c3aed!important; }
|
| 510 |
+
|
| 511 |
#cat-dd label span { color:#50508a!important; font-size:.78rem!important; }
|
| 512 |
#cat-dd input, #cat-dd .wrap { background:#0f0f28!important; border-color:#1e1e48!important; color:#c0c0e8!important; }
|
| 513 |
|
|
|
|
| 527 |
.reel::after { right:0; background:linear-gradient(270deg,#0c0c22,transparent); }
|
| 528 |
.reel-strip { display:inline-block; white-space:nowrap; font-size:2.7rem; line-height:78px;
|
| 529 |
animation: reel-roll .42s linear infinite; filter: blur(.3px); will-change: transform; }
|
| 530 |
+
.reel.reel-back .reel-strip { animation-direction: reverse; } /* rewind scrolls the other way */
|
| 531 |
@keyframes reel-roll { from { transform:translateX(0); } to { transform:translateX(-2em); } }
|
| 532 |
.reel-txt { color:#7c7cb8; margin-top:.85rem; font-size:.92rem; letter-spacing:.05em; animation: pulse-txt 1s ease-in-out infinite; }
|
| 533 |
@keyframes pulse-txt { 0%,100%{ opacity:.45; } 50%{ opacity:1; } }
|
|
|
|
| 628 |
step();
|
| 629 |
};
|
| 630 |
RL.stopSpinSound = () => { if (spinTimer) { clearTimeout(spinTimer); spinTimer = null; } };
|
| 631 |
+
RL.startBackSound = () => { // descending ticks โ a "rewind" counterpart to the spin
|
| 632 |
+
if (RL.muted || !ac()) return;
|
| 633 |
+
let f = 700, delay = 46;
|
| 634 |
+
const step = () => { blip(f, ctx.currentTime, 0.05, 0.04, 'square'); f = Math.max(170, f * 0.85); delay = Math.min(150, delay * 1.06); spinTimer = setTimeout(step, delay); };
|
| 635 |
+
step();
|
| 636 |
+
};
|
| 637 |
|
| 638 |
// confetti burst on a fixed overlay canvas
|
| 639 |
let cv, cx, parts = [], raf = null;
|
|
|
|
| 660 |
};
|
| 661 |
|
| 662 |
const MIN_SPIN = 1100; // keep the wheel visibly spinning even when the draw is instant
|
| 663 |
+
const MIN_BACK = 600; // back-spin rewinds quicker than a fresh spin
|
| 664 |
const REEL = '<div class="reel-wrap"><div class="reel"><div class="reel-strip">๐ฎ ๐จ ๐๏ธ ๐ฉบ ๐ผ ๐ โ๏ธ ๐ ๐ฌ ๐ก๏ธ ๐ธ ๐ ๐ฎ ๐จ ๐๏ธ ๐ฉบ<\/div><\/div><p class="reel-txt">spinning the wheelโฆ<\/p><\/div>';
|
| 665 |
+
const REEL_BACK = '<div class="reel-wrap"><div class="reel reel-back"><div class="reel-strip">๐ฉบ ๐๏ธ ๐จ ๐ฎ ๐ ๐ธ ๐ก๏ธ ๐ฌ ๐ โ๏ธ ๐ ๐ผ ๐ฉบ ๐๏ธ ๐จ ๐ฎ<\/div><\/div><p class="reel-txt">โฉ rewindingโฆ<\/p><\/div>';
|
| 666 |
// The reel is an OVERLAY on top of #card-out โ never the card's own DOM. Two
|
| 667 |
// bugs this fixes: (1) mutating Gradio's Svelte-managed card node broke every
|
| 668 |
// spin after the first; (2) when a cached draw returns instantly the reel/
|
| 669 |
// sound used to flash by invisibly โ now the overlay (and ticks) stay up for
|
| 670 |
// at least MIN_SPIN before the reveal.
|
| 671 |
+
// Shared overlay+poll routine for both forward spins and back-spins. opts:
|
| 672 |
+
// { reel, sound, min, btn } โ what to show, what to play, how long to hold, and
|
| 673 |
+
// which button glows.
|
| 674 |
+
RL._begin = (opts) => {
|
| 675 |
ac();
|
| 676 |
+
RL._busy = true; // serialize: ignore clicks until land()
|
| 677 |
if (RL._poll) { clearInterval(RL._poll); RL._poll = null; }
|
| 678 |
if (RL._revealTimer) { clearTimeout(RL._revealTimer); RL._revealTimer = null; }
|
| 679 |
RL.stopSpinSound();
|
| 680 |
RL._spinStart = performance.now();
|
| 681 |
+
RL._min = opts.min;
|
| 682 |
+
// Poll/compare against any data-spin holder (real card OR a result placeholder).
|
| 683 |
+
const prev = document.querySelector('#card-out [data-spin]');
|
| 684 |
RL._prevSpin = prev ? prev.getAttribute('data-spin') : '-1';
|
| 685 |
const host = document.getElementById('card-out');
|
| 686 |
if (host) {
|
| 687 |
host.style.position = 'relative';
|
| 688 |
let ov = host.querySelector('.rl-reel-overlay');
|
| 689 |
if (!ov) { ov = document.createElement('div'); ov.className = 'rl-reel-overlay'; host.appendChild(ov); }
|
| 690 |
+
ov.innerHTML = opts.reel; ov.style.display = 'flex';
|
| 691 |
}
|
| 692 |
+
const b = document.getElementById(opts.btn); if (b) b.classList.add('spinning');
|
| 693 |
+
opts.sound();
|
| 694 |
+
// Poll for the new result (data-spin changes on every draw AND back-spin);
|
| 695 |
+
// reveal once it has arrived AND the wheel has spun for at least its minimum.
|
| 696 |
const t0 = performance.now();
|
| 697 |
RL._poll = setInterval(() => {
|
| 698 |
+
const cur = document.querySelector('#card-out [data-spin]');
|
| 699 |
const arrived = cur && cur.getAttribute('data-spin') !== RL._prevSpin;
|
| 700 |
if (arrived || performance.now() - t0 > 6000) {
|
| 701 |
clearInterval(RL._poll); RL._poll = null;
|
| 702 |
+
const wait = Math.max(0, RL._min - (performance.now() - RL._spinStart));
|
| 703 |
RL._revealTimer = setTimeout(() => { RL._revealTimer = null; RL.land(); }, wait);
|
| 704 |
}
|
| 705 |
}, 40);
|
| 706 |
};
|
| 707 |
+
RL.startSpin = () => RL._begin({ reel: REEL, sound: RL.startSpinSound, min: MIN_SPIN, btn: 'spin-btn' });
|
| 708 |
+
RL.startBack = () => RL._begin({ reel: REEL_BACK, sound: RL.startBackSound, min: MIN_BACK, btn: 'back-btn' });
|
| 709 |
RL.land = () => {
|
| 710 |
RL.stopSpinSound();
|
| 711 |
+
['spin-btn', 'back-btn'].forEach(id => { const b = document.getElementById(id); if (b) b.classList.remove('spinning'); });
|
| 712 |
const host = document.getElementById('card-out');
|
| 713 |
const ov = host && host.querySelector('.rl-reel-overlay');
|
| 714 |
+
if (ov) ov.remove(); // reveal the result underneath
|
| 715 |
const card = document.querySelector('#card-out .space-card');
|
| 716 |
+
if (card) { // celebrate real cards only (not placeholders)
|
| 717 |
+
card.style.animation = 'none'; void card.offsetWidth; card.style.animation = '';
|
| 718 |
+
RL.chime();
|
| 719 |
+
const r = card.getBoundingClientRect();
|
| 720 |
+
RL.burst(r.left + r.width / 2, r.top + Math.min(120, r.height * 0.35));
|
| 721 |
+
}
|
| 722 |
+
RL._busy = false; // ready for the next click
|
| 723 |
};
|
| 724 |
|
| 725 |
+
// delegated listeners survive Gradio re-renders. Capture phase so we can
|
| 726 |
+
// suppress Gradio's own click handler when a draw is mid-flight (prevents
|
| 727 |
+
// overlapping spin/back draws from desyncing the history state) or when Back
|
| 728 |
+
// is disabled (its click would otherwise no-op on the server).
|
| 729 |
document.addEventListener('click', (e) => {
|
| 730 |
+
const closest = (sel) => (e.target.closest && e.target.closest(sel)) || null;
|
| 731 |
+
const spin = closest('#spin-btn');
|
| 732 |
+
const back = closest('#back-btn');
|
| 733 |
+
if (spin || back) {
|
| 734 |
+
const backOff = back && (back.disabled || back.getAttribute('aria-disabled') === 'true');
|
| 735 |
+
if (RL._busy || backOff) { e.preventDefault(); e.stopImmediatePropagation(); return; }
|
| 736 |
+
if (spin) RL.startSpin(); else RL.startBack();
|
| 737 |
+
return;
|
| 738 |
+
}
|
| 739 |
+
const m = closest('#rl-mute');
|
| 740 |
if (m) { RL.muted = !RL.muted; m.textContent = RL.muted ? '๐' : '๐'; m.classList.toggle('muted', RL.muted); if (RL.muted) RL.stopSpinSound(); }
|
| 741 |
}, true);
|
| 742 |
+
document.addEventListener('pointerover', (e) => {
|
| 743 |
+
if (e.target.closest && (e.target.closest('#spin-btn') || e.target.closest('#back-btn'))) RL.hover();
|
| 744 |
+
}, true);
|
| 745 |
})();
|
| 746 |
</script>
|
| 747 |
"""
|
|
|
|
| 753 |
seen_st = gr.State([])
|
| 754 |
cat_st = gr.State("All")
|
| 755 |
spin_ctr = gr.State(0)
|
| 756 |
+
hist_st = gr.State([]) # chronological ids of viewed projects (global, all categories)
|
| 757 |
+
pos_st = gr.State(-1) # index into hist_st of the currently shown card
|
| 758 |
|
| 759 |
gr.HTML(f"""
|
| 760 |
<div class="app-header">
|
|
|
|
| 768 |
|
| 769 |
with gr.Row():
|
| 770 |
cat_dd = gr.Dropdown(choices=ALL_CATEGORIES, value="All", label="Browse by category",
|
| 771 |
+
elem_id="cat-dd", interactive=True, filterable=False)
|
| 772 |
+
with gr.Row():
|
| 773 |
+
back_btn = gr.Button("โฉ Back Spin", elem_id="back-btn", scale=2,
|
| 774 |
+
interactive=False, min_width=120)
|
| 775 |
+
spin_btn = gr.Button("๐ฐ SPIN", elem_id="spin-btn", scale=3, min_width=160)
|
| 776 |
|
| 777 |
card_out = gr.HTML(elem_id="card-out")
|
| 778 |
|
| 779 |
with gr.Accordion("๐ Browse all in this category", open=False, elem_id="browse-acc") as browse_acc:
|
| 780 |
browse_out = gr.HTML()
|
| 781 |
|
| 782 |
+
# events (the reveal โ chime + confetti โ is driven by the data-spin poller in HEAD_HTML)
|
| 783 |
+
demo.load(on_load,
|
| 784 |
+
outputs=[seen_st, cat_st, spin_ctr, hist_st, pos_st, stats_out, card_out, back_btn])
|
| 785 |
spin_btn.click(
|
| 786 |
on_spin,
|
| 787 |
+
inputs=[seen_st, cat_st, spin_ctr, hist_st, pos_st],
|
| 788 |
+
outputs=[card_out, seen_st, spin_ctr, stats_out, hist_st, pos_st, back_btn],
|
| 789 |
+
)
|
| 790 |
+
back_btn.click(
|
| 791 |
+
on_back,
|
| 792 |
+
inputs=[hist_st, pos_st, spin_ctr],
|
| 793 |
+
outputs=[card_out, pos_st, spin_ctr, back_btn],
|
| 794 |
+
)
|
| 795 |
+
cat_dd.change(on_category, inputs=[cat_dd, seen_st], outputs=[cat_st, stats_out])
|
| 796 |
# keep the browse list in sync whenever the category changes or the panel opens
|
| 797 |
cat_dd.change(on_browse, inputs=[cat_dd, seen_st], outputs=[browse_out])
|
| 798 |
browse_acc.expand(on_browse, inputs=[cat_st, seen_st], outputs=[browse_out])
|