rob-revisited/ide/ui/js/community.js

122 lines
5.7 KiB
JavaScript

// The Community window: the chat of jumphp.net (the same as its Community page), written with the IDE's account (no captcha), and
// the forum / the whole Community page in a window of the IDE (the site itself). Desktop IDE only: games/ide/src/Main.php
// community.* (the IDE's token on /api/ide/community/chat; read only as a visitor when not signed in).
import { backend } from './backend.js';
import { el, clear, icon, notify, t } from './ui.js';
export class CommunityPanel {
constructor(app) {
this.app = app;
this.node = el('div.community-view');
this.log = el('div.community-log');
this.input = el('input.input', { type: 'text', maxlength: 500, placeholder: t('Your message…'), onkeydown: (e) => { if (e.key === 'Enter') { e.preventDefault(); this.send(); } } });
this.state = { signedIn: false };
this.first = 0;
this.last = 0;
this.visible = false;
this.render();
setInterval(() => { if (this.visible && this.node.isConnected && document.visibilityState === 'visible') this.fresh(); }, 5000);
}
title() { return t('Community'); }
actions() {
return [
el('button', { title: t('The forum (in a window of the IDE)'), onclick: () => this.open('forum') }, icon('folder')),
el('button', { title: t('The Community page of jumphp.net (in a window of the IDE)'), onclick: () => this.open('') }, icon('globe')),
el('button', { title: t('Refresh'), onclick: () => this.fresh() }, icon('refresh')),
];
}
async shown() {
this.visible = true;
this.state = await backend.invoke('community.state').catch(() => this.state);
this.render();
await this.fresh();
}
hidden() { this.visible = false; }
async open(to) {
try { await backend.invoke('community.open', { to }); } catch (e) { notify(String(e.message || e), 'error', { title: t('Community') }); }
}
render() {
clear(this.node);
const older = el('button.btn.small', { onclick: () => this.older() }, t('Older messages'));
this.olderButton = older;
older.hidden = true;
this.log.replaceChildren(older);
this.first = this.last = 0;
const box = this.state.signedIn
? el('div.community-send', {}, this.input, el('button.btn.primary', { onclick: () => this.send() }, t('Send')))
: el('div.community-send', {}, el('span.pub-faint', {}, t('Sign in with jumphp.net to write (reading works without).')),
el('button.btn', { onclick: () => this.signIn() }, t('Sign in')));
this.node.append(
el('div.community-head', {}, el('b', {}, '💬 ' + t('Chat of jumphp.net')),
el('a', { href: '#', onclick: (e) => { e.preventDefault(); this.open('forum'); } }, '🗂️ ' + t('Forum'))),
this.log, box);
}
async signIn() {
try {
await backend.invoke('session.login', { another: false });
this.state = await backend.invoke('community.state');
this.render();
this.fresh();
} catch (e) { if (!/cancel/i.test(e.message)) notify(e.message, 'error', { title: t('Sign in') }); }
}
line(m) {
const who = el(m.profile ? 'a.community-who' : 'span.community-who', m.profile ? { href: '#', onclick: (e) => { e.preventDefault(); this.app.openUrl(m.profile); } } : {},
m.avatar ? el('img', { src: m.avatar, alt: '', width: 18, height: 18 }) : null, m.name, m.guest ? el('span.badge', {}, t('guest')) : null);
const when = el('time.pub-faint', {}, new Date(m.at).toLocaleString([], { day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit' }));
const reply = this.state.signedIn ? el('button.community-reply', { title: t('Reply to this person'), onclick: () => {
const tag = m.login ? '@' + m.login : m.name;
if (!this.input.value.includes(tag)) this.input.value = tag + ' ' + this.input.value.trimStart();
this.input.focus();
} }, '↩') : null;
return el('div.community-msg', { dataset: { id: m.id } }, el('div', {}, who, ' ', when, reply), el('p', {}, m.text));
}
async fresh() {
try {
const r = await backend.invoke('community.chat', this.last ? { after: this.last } : {});
const list = r.messages || [];
if (!list.length) {
if (!this.last && !this.log.querySelector('.community-empty')) this.log.append(el('p.pub-faint.community-empty', {}, t('No messages yet: start the conversation!')));
return;
}
this.log.querySelector('.community-empty')?.remove();
const stick = this.log.scrollHeight - this.log.scrollTop - this.log.clientHeight < 40 || !this.last;
for (const m of list) if (!this.log.querySelector(`[data-id="${m.id}"]`)) this.log.append(this.line(m));
if (!this.first) { this.first = list[0].id; this.olderButton.hidden = list.length < 50; }
this.last = list[list.length - 1].id;
if (stick) this.log.scrollTop = this.log.scrollHeight;
} catch (e) { /* offline: the next time */ }
}
async older() {
const r = await backend.invoke('community.chat', { before: this.first }).catch(() => ({ messages: [] }));
const list = r.messages || [];
if (!list.length) { this.olderButton.hidden = true; return; }
const height = this.log.scrollHeight;
this.olderButton.after(...list.map((m) => this.line(m)));
this.first = list[0].id;
this.olderButton.hidden = list.length < 50;
this.log.scrollTop += this.log.scrollHeight - height;
}
async send() {
const text = this.input.value.trim();
if (!text) return;
this.input.disabled = true;
try {
await backend.invoke('community.post', { text });
this.input.value = '';
await this.fresh();
this.log.scrollTop = this.log.scrollHeight;
} catch (e) { notify(String(e.message || e), 'error', { title: t('Chat of jumphp.net') }); }
finally { this.input.disabled = false; this.input.focus(); }
}
}