2022-09-11 13:42:41 -04:00
|
|
|
import {LitElement, html, unsafeHTML, until} from './lit-all.min.js';
|
|
|
|
import * as tfrpc from '/static/tfrpc.js';
|
|
|
|
import {styles} from './tf-styles.js';
|
|
|
|
|
2022-09-14 19:33:57 -04:00
|
|
|
class TfTabNewsElement extends LitElement {
|
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
whoami: {type: String},
|
|
|
|
users: {type: Object},
|
|
|
|
hash: {type: String},
|
|
|
|
unread: {type: Array},
|
|
|
|
following: {type: Array},
|
2023-01-20 19:16:18 -05:00
|
|
|
drafts: {type: Object},
|
2023-01-24 19:56:10 -05:00
|
|
|
expanded: {type: Object},
|
2023-03-29 18:02:12 -04:00
|
|
|
};
|
2022-09-14 19:33:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static styles = styles;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
let self = this;
|
|
|
|
this.whoami = null;
|
|
|
|
this.users = {};
|
|
|
|
this.hash = '#';
|
2022-09-11 13:42:41 -04:00
|
|
|
this.unread = [];
|
2022-09-14 19:33:57 -04:00
|
|
|
this.following = [];
|
|
|
|
this.cache = {};
|
2023-01-20 19:16:18 -05:00
|
|
|
this.drafts = {};
|
2023-01-24 19:56:10 -05:00
|
|
|
this.expanded = {};
|
2023-01-20 19:16:18 -05:00
|
|
|
tfrpc.rpc.localStorageGet('drafts').then(function (d) {
|
|
|
|
self.drafts = JSON.parse(d || '{}');
|
|
|
|
});
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
|
|
|
|
2023-05-02 12:47:27 -04:00
|
|
|
connectedCallback() {
|
|
|
|
super.connectedCallback();
|
|
|
|
document.body.addEventListener('keypress', this.on_keypress.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback() {
|
|
|
|
super.disconnectedCallback();
|
|
|
|
document.body.removeEventListener('keypress', this.on_keypress.bind(this));
|
|
|
|
}
|
|
|
|
|
2022-09-14 19:33:57 -04:00
|
|
|
show_more() {
|
|
|
|
let unread = this.unread;
|
2023-05-03 18:32:21 -04:00
|
|
|
let news = this.shadowRoot?.getElementById('news');
|
2022-09-14 19:33:57 -04:00
|
|
|
if (news) {
|
|
|
|
console.log('injecting messages', news.messages);
|
2023-09-01 22:06:29 -04:00
|
|
|
news.add_messages(
|
|
|
|
Object.values(Object.fromEntries(this.unread.map((x) => [x.id, x])))
|
|
|
|
);
|
2022-09-14 19:33:57 -04:00
|
|
|
this.dispatchEvent(new CustomEvent('refresh'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_messages_text() {
|
|
|
|
if (!this.unread?.length) {
|
|
|
|
return 'No new messages.';
|
|
|
|
}
|
|
|
|
let counts = {};
|
|
|
|
for (let message of this.unread) {
|
2022-09-25 08:33:54 -04:00
|
|
|
let type = 'private';
|
2022-09-14 19:33:57 -04:00
|
|
|
try {
|
|
|
|
type = JSON.parse(message.content).type || type;
|
|
|
|
} catch {}
|
|
|
|
counts[type] = (counts[type] || 0) + 1;
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
2024-01-11 23:23:31 -05:00
|
|
|
return (
|
|
|
|
'↻ Show New: ' +
|
|
|
|
Object.keys(counts)
|
|
|
|
.sort()
|
|
|
|
.map((x) => counts[x].toString() + ' ' + x + 's')
|
|
|
|
.join(', ')
|
|
|
|
);
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
|
|
|
|
2023-01-20 19:16:18 -05:00
|
|
|
draft(event) {
|
|
|
|
let id = event.detail.id || '';
|
2023-01-28 14:39:41 -05:00
|
|
|
let previous = this.drafts[id];
|
2023-01-20 19:16:18 -05:00
|
|
|
if (event.detail.draft !== undefined) {
|
2023-01-28 14:39:41 -05:00
|
|
|
this.drafts[id] = event.detail.draft;
|
2023-01-20 19:16:18 -05:00
|
|
|
} else {
|
|
|
|
delete this.drafts[id];
|
2023-01-28 14:39:41 -05:00
|
|
|
}
|
|
|
|
/* Only trigger a re-render if we're creating a new draft or discarding an old one. */
|
|
|
|
if ((previous !== undefined) != (event.detail.draft !== undefined)) {
|
2023-01-20 19:16:18 -05:00
|
|
|
this.drafts = Object.assign({}, this.drafts);
|
|
|
|
}
|
|
|
|
tfrpc.rpc.localStorageSet('drafts', JSON.stringify(this.drafts));
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:56:10 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 12:47:27 -04:00
|
|
|
on_keypress(event) {
|
|
|
|
if (event.target === document.body && event.key == '.') {
|
|
|
|
this.show_more();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 13:42:41 -04:00
|
|
|
render() {
|
|
|
|
let profile = this.hash.startsWith('#@')
|
|
|
|
? html`<tf-profile
|
|
|
|
id=${this.hash.substring(1)}
|
|
|
|
whoami=${this.whoami}
|
|
|
|
.users=${this.users}
|
|
|
|
></tf-profile>`
|
|
|
|
: undefined;
|
|
|
|
return html`
|
2024-01-12 22:03:03 -05:00
|
|
|
<p class="w3-bar">
|
|
|
|
<button
|
|
|
|
class="w3-bar-item w3-button w3-dark-grey"
|
|
|
|
@click=${this.show_more}
|
|
|
|
>
|
|
|
|
${this.new_messages_text()}
|
|
|
|
</button>
|
|
|
|
</p>
|
2022-09-11 13:42:41 -04:00
|
|
|
<div>
|
|
|
|
Welcome, <tf-user id=${this.whoami} .users=${this.users}></tf-user>!
|
|
|
|
</div>
|
2023-08-02 20:30:48 -04:00
|
|
|
<div>
|
|
|
|
<tf-compose
|
|
|
|
id="tf-compose"
|
|
|
|
whoami=${this.whoami}
|
|
|
|
.users=${this.users}
|
|
|
|
.drafts=${this.drafts}
|
|
|
|
@tf-draft=${this.draft}
|
|
|
|
></tf-compose>
|
|
|
|
</div>
|
2022-09-11 13:42:41 -04:00
|
|
|
${profile}
|
2023-01-24 19:56:10 -05:00
|
|
|
<tf-tab-news-feed
|
|
|
|
id="news"
|
|
|
|
whoami=${this.whoami}
|
|
|
|
.users=${this.users}
|
|
|
|
.following=${this.following}
|
|
|
|
hash=${this.hash}
|
|
|
|
.drafts=${this.drafts}
|
|
|
|
.expanded=${this.expanded}
|
|
|
|
@tf-draft=${this.draft}
|
|
|
|
@tf-expand=${this.on_expand}
|
|
|
|
></tf-tab-news-feed>
|
2022-09-11 13:42:41 -04:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 20:30:48 -04:00
|
|
|
customElements.define('tf-tab-news', TfTabNewsElement);
|