import {LitElement, html, unsafeHTML, until} from './lit-all.min.js'; import * as tfrpc from '/static/tfrpc.js'; import {styles} from './tf-styles.js'; class TfTabNewsElement extends LitElement { static get properties() { return { whoami: {type: String}, users: {type: Object}, hash: {type: String}, unread: {type: Array}, following: {type: Array}, drafts: {type: Object}, expanded: {type: Object}, loading: {type: Boolean}, channels: {type: Array}, channels_unread: {type: Object}, channels_latest: {type: Object}, }; } static styles = styles; constructor() { super(); let self = this; this.whoami = null; this.users = {}; this.hash = '#'; this.unread = []; this.following = []; this.cache = {}; this.drafts = {}; this.expanded = {}; this.channels_unread = {}; this.channels_latest = {}; this.channels = []; tfrpc.rpc.localStorageGet('drafts').then(function (d) { self.drafts = JSON.parse(d || '{}'); }); } connectedCallback() { super.connectedCallback(); document.body.addEventListener('keypress', this.on_keypress.bind(this)); } disconnectedCallback() { super.disconnectedCallback(); document.body.removeEventListener('keypress', this.on_keypress.bind(this)); } show_more() { let unread = this.unread; let news = this.shadowRoot?.getElementById('news'); if (news) { news.load_latest(); this.dispatchEvent(new CustomEvent('refresh')); } } new_messages_text() { if (!this.unread?.length) { return 'No new messages.'; } let counts = {}; for (let message of this.unread) { let type = 'private'; try { type = JSON.parse(message.content).type || type; } catch {} counts[type] = (counts[type] || 0) + 1; } return ( '↻ Show New: ' + Object.keys(counts) .sort() .map((x) => counts[x].toString() + ' ' + x + 's') .join(', ') ); } draft(event) { let id = event.detail.id || ''; let previous = this.drafts[id]; if (event.detail.draft !== undefined) { this.drafts[id] = event.detail.draft; } else { delete this.drafts[id]; } this.drafts = Object.assign({}, this.drafts); tfrpc.rpc.localStorageSet('drafts', JSON.stringify(this.drafts)); } on_expand(event) { if (event.detail.expanded) { let expand = {}; expand[event.detail.id] = true; this.expanded = Object.assign({}, this.expanded, expand); } else { delete this.expanded[event.detail.id]; this.expanded = Object.assign({}, this.expanded); } } on_keypress(event) { if (event.target === document.body && event.key == '.') { this.show_more(); } } unread_status(channel) { if ( this.channels_latest[channel] && (this.channels_unread[channel] === undefined || this.channels_unread[channel] <= this.channels_latest[channel]) ) { return 'πŸ”΅'; } } show_sidebar() { this.renderRoot.getElementById('sidebar').style.display = 'block'; this.renderRoot.getElementById('sidebar_overlay').style.display = 'block'; } hide_sidebar() { this.renderRoot.getElementById('sidebar').style.display = 'none'; this.renderRoot.getElementById('sidebar_overlay').style.display = 'none'; } async channel_toggle_subscribed() { let channel = this.hash.substring(2); let subscribed = this.channels.indexOf(channel) != -1; subscribed = !subscribed; await tfrpc.rpc.appendMessage(this.whoami, { type: 'channel', channel: channel, subscribed: subscribed, }); if (subscribed) { this.channels = [].concat([channel], this.channels).sort(); } else { this.channels = this.channels.filter((x) => x != channel); } } channel() { return this.hash.startsWith('##') ? this.hash.substring(2) : undefined; } render() { let profile = this.hash.startsWith('#@') && this.hash != '#@' ? html`` : undefined; let edit_profile; if ( !this.loading && this.users[this.whoami]?.name === undefined && this.hash.substring(1) != this.whoami ) { edit_profile = html`
ℹ️ Follow your identity link ☝️ above to edit your profile and set your name.
`; } return html`

${this.hash.startsWith('##') ? html` ` : undefined}

Welcome, ! ${edit_profile}
${profile}
`; } } customElements.define('tf-tab-news', TfTabNewsElement);