File size: 7,832 Bytes
2714d18
8af7605
 
 
 
2714d18
 
8af7605
 
 
 
 
 
 
 
2714d18
8af7605
 
 
 
 
2714d18
 
8af7605
 
b48b1fc
 
 
 
c1bd0d2
b48b1fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2714d18
 
8af7605
 
2714d18
8af7605
 
2714d18
 
814ee46
 
 
 
 
 
 
 
 
 
8af7605
814ee46
 
 
b48b1fc
814ee46
 
b48b1fc
 
 
814ee46
2714d18
 
 
 
 
 
 
 
 
814ee46
 
2714d18
 
814ee46
2714d18
 
 
3437bb4
5f09914
2714d18
 
 
 
 
 
 
 
 
b48b1fc
2714d18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
814ee46
2714d18
 
 
 
 
 
 
 
8af7605
 
2714d18
 
8af7605
2714d18
8af7605
 
2714d18
 
8af7605
c1bd0d2
 
 
 
 
8af7605
 
2714d18
8af7605
b48b1fc
8af7605
 
2714d18
8af7605
 
2714d18
b48b1fc
2714d18
b48b1fc
2714d18
8af7605
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* 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 };
})();