/* Solo online: benvenuto, menu, minuti finiti e avvisi d'errore comprensibili. */ (() => { "use strict"; const $ = (s) => document.querySelector(s); const WELCOME_KEY = "pixrick.benvenuto.v2"; const REPORT_URL = "https://huggingface.co/spaces/Algorick/pixrick/discussions/new"; function seenWelcome() { try { return localStorage.getItem(WELCOME_KEY) === "1"; } catch { return false; } } function markWelcome() { try { localStorage.setItem(WELCOME_KEY, "1"); } catch { /* storage bloccato */ } } /* ---------- menu ---------- */ function toggleMenu(open) { const menu = $("#account-menu"); const show = open ?? menu.hidden; menu.hidden = !show; $("#menu-btn").setAttribute("aria-expanded", String(show)); if (show) menu.querySelector("button, a")?.focus(); } /* ---------- account ---------- */ function renderAccount() { const who = window.PixBackend?.account?.(); $("#welcome-login-row").hidden = Boolean(who); $("#account-head").textContent = who ? who.nome || who.utente : "Gratis, senza account"; $("#account-note").textContent = who ? `Accesso con Hugging Face come @${who.utente}. Le generazioni usano la tua quota personale.` : "Qualche generazione gratuita al giorno. Le immagini restano nel tuo browser."; const pic = $("#account-pic"); pic.hidden = !who?.foto; if (who?.foto) pic.src = who.foto; $("#account-login").hidden = Boolean(who); $("#account-fine").hidden = Boolean(who); $("#account-logout").hidden = !who; } function logout() { window.PixBackend?.logout?.(); renderAccount(); toggleMenu(false); } function loginFeedback() { const result = window.PixBackend?.loginResult; if (result === "ok") { const who = window.PixBackend.account(); showNotice({ title: "Accesso fatto", text: `Ciao @${who?.utente}: da ora le generazioni usano la tua quota personale di Hugging Face.`, retry: false, ok: true }); } else if (result === "errore" || result === "non-disponibile") { showNotice({ title: "Accesso non riuscito", text: "Hugging Face non ha completato l'accesso. Riprova tra poco: intanto puoi continuare come ospite.", retry: false }); } } /* ---------- benvenuto ---------- */ function openWelcome() { toggleMenu(false); $("#welcome").showModal(); } /* ---------- minuti finiti ---------- */ function resetLabel(at) { const when = new Date(at); const today = new Date(); const tomorrow = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1); const time = `${when.getHours()}:${String(when.getMinutes()).padStart(2, "0")}`; if (when.toDateString() === today.toDateString()) return `Si riparte oggi alle ${time}.`; if (when.toDateString() === tomorrow.toDateString()) return `Si riparte domani alle ${time}.`; return "Si riparte entro 24 ore dalla prima generazione."; } function quota(retryIn) { const dialog = $("#quota"); if (dialog.open) return; const at = retryIn ? Date.now() + retryIn * 1000 : window.PixBackend?.quotaReset?.(); $("#quota-title").textContent = window.PixBackend?.account?.() ? "Quota di oggi finita" : "Prove gratuite finite per oggi"; $("#quota-text").textContent = "Hugging Face presta la GPU a PixRick e ogni giorno concede a ciascuno un po' di tempo e un numero limitato di generazioni. Le immagini che hai fatto restano nella galleria."; $("#quota-when").textContent = at ? resetLabel(at) : "Si riparte entro 24 ore dalla prima generazione."; const guest = !window.PixBackend?.account?.(); $("#quota-account").hidden = !guest; $("#quota-login").hidden = !guest; dialog.showModal(); } /* ---------- avvisi d'errore ---------- */ const HINT = /^(Scrivi|Aggiungi|Scegli|Dipingi|Servono|Al massimo|Write|Add|Choose|Pick|Paint|First|At most|A name)/; const BLOCKED = /bloccat|rifiutat|non si toccano|blocked|rejected|off limits/i; const BUSY = /GPU è piena|troppe persone|non risponde|GPU is full|Too many people|isn't responding/i; const LOST = /non c'è più|non sono arrivate|is gone|Not all starting/i; const QUOTA = /prove gratuite|free tries/i; function classify(text) { if (HINT.test(text)) return null; if (QUOTA.test(text)) return { quota: true }; if (BLOCKED.test(text)) return { title: "Questa richiesta non si può fare", text, retry: false }; if (BUSY.test(text)) return { title: "Il motore è sotto sforzo", text: "Tante persone stanno generando in questo momento. Riprova tra qualche secondo.", retry: true }; if (LOST.test(text)) return { title: "Manca un'immagine", text, retry: false }; if (/salvarla|couldn't save it/i.test(text)) return { title: "Immagine non salvata", text, retry: true }; return { title: "Non è andata", text: "Qualcosa si è inceppato mentre lavoravo. Riprova; se succede ancora, prova con l'altro modello o segnalacelo dal menu.", retry: true }; } let hideTimer = null; function showNotice(n) { clearTimeout(hideTimer); $("#notice-title").textContent = n.title; $("#notice-text").textContent = n.text; $("#notice-retry").hidden = !n.retry || !window.PixBackend?.lastPayload; const box = $("#notice"); box.dataset.tone = n.ok ? "ok" : ""; box.hidden = false; requestAnimationFrame(() => box.classList.add("show")); hideTimer = setTimeout(hideNotice, 12000); } function hideNotice() { clearTimeout(hideTimer); const box = $("#notice"); box.classList.remove("show"); setTimeout(() => { if (!box.classList.contains("show")) box.hidden = true; }, 220); } function watchErrors() { const field = $("#form-error"); let busy = false; new MutationObserver(() => { if (busy) return; const text = field.textContent.trim(); if (!text) return; const notice = classify(text); if (!notice) return; busy = true; field.textContent = ""; busy = false; if (!notice.quota) showNotice(notice); }).observe(field, { childList: true, characterData: true, subtree: true }); $("#notice-close").addEventListener("click", hideNotice); $("#notice-retry").addEventListener("click", () => { hideNotice(); const payload = window.PixBackend?.lastPayload; if (payload && typeof window.submit === "function") window.submit(payload); }); $("#generate-btn").addEventListener("click", hideNotice); } /* ---------- avvio ---------- */ function wire() { $("#menu-btn").addEventListener("click", (e) => { e.stopPropagation(); toggleMenu(); }); document.addEventListener("click", (e) => { if (!e.target.closest("#account-menu")) toggleMenu(false); }); document.addEventListener("keydown", (e) => { if (e.key === "Escape") toggleMenu(false); }); $("#menu-welcome").addEventListener("click", openWelcome); $("#menu-report").addEventListener("click", () => { toggleMenu(false); window.open(REPORT_URL, "_blank", "noopener"); }); const terms = $("#welcome-terms"); const sync = () => { $("#welcome-start").disabled = !terms.checked; $("#welcome-login-row").setAttribute("aria-disabled", String(!terms.checked)); }; $("#welcome-login").addEventListener("click", markWelcome); terms.addEventListener("change", sync); $("#welcome").addEventListener("cancel", (e) => { if (!seenWelcome()) e.preventDefault(); }); $("#welcome-form").addEventListener("submit", markWelcome); $("#quota-close").addEventListener("click", () => $("#quota").close()); $("#account-logout").addEventListener("click", logout); if (seenWelcome()) terms.checked = true; sync(); watchErrors(); } wire(); renderAccount(); if (!seenWelcome()) openWelcome(); else loginFeedback(); window.PixOnline = { quota, openWelcome }; })();