import {LitElement, html, unsafeHTML} from './lit-all.min.js';
import * as tfrpc from '/static/tfrpc.js';
import * as tfutils from './tf-utils.js';
import * as emojis from './emojis.js';
import {styles} from './tf-styles.js';
class TfMessageElement extends LitElement {
static get properties() {
return {
whoami: {type: String},
message: {type: Object},
users: {type: Object},
reply: {type: Boolean},
raw: {type: Boolean},
}
}
static styles = styles;
constructor() {
super();
let self = this;
this.whoami = null;
this.message = {};
this.users = {};
this.reply = false;
this.raw = false;
}
show_reply() {
this.reply = true;
}
render_votes() {
function normalize_expression(expression) {
if (expression === 'Like' || !expression) {
return '👍';
} else if (expression === 'Unlike') {
return '👎';
} else if (expression === 'heart') {
return '❤️';
} else {
return expression;
}
}
return html`
${(this.message.votes || []).map(vote => html`${normalize_expression(vote.content.vote.expression)}`)}
`;
}
render_raw() {
return html`${JSON.stringify(this.message, null, 2)}
`
}
vote(emoji) {
let reaction = emoji.emoji;
let message = this.message.id;
if (confirm('Are you sure you want to react with ' + reaction + ' to ' + message + '?')) {
tfrpc.rpc.appendMessage(
this.whoami,
{
type: 'vote',
vote: {
link: message,
value: 1,
expression: reaction,
},
}).catch(function(error) {
alert(error?.message);
});
}
}
react(event) {
emojis.picker(x => this.vote(x));
}
render() {
let content = this.message?.content;
let self = this;
let raw_button = this.raw ?
html` self.raw = false}>` :
html` self.raw = true}>`;
function small_frame(inner) {
return html`
% ${new Date(self.message.timestamp).toLocaleString()}
${raw_button}
${self.raw ? self.render_raw() : inner}
${self.render_votes()}
`
}
if (this.message.placeholder) {
return html`
${this.message.id} (placeholder)
${this.render_votes()}
${(this.message.child_messages || []).map(x => html`
`)}
`;
} else if (content.type == 'about') {
return small_frame(html`
Updated profile:
${JSON.stringify(content, null, 2)}
`);
} else if (content.type == 'contact') {
return small_frame(html`
is now
${
content.blocking === true ? 'blocking' :
content.blocking === false ? 'unblocking' :
content.following === true ? 'following' :
content.following === false ? 'unfollowing' :
'?'
}
`);
} else if (content.type == 'post') {
let reply = this.reply ? html`
this.reply = false}>
` : html`
`;
let self = this;
let body = this.raw ?
this.render_raw() :
unsafeHTML(tfutils.markdown(content.text));
return html`
% ${new Date(this.message.timestamp).toLocaleString()}
${raw_button}
${body}
${this.render_votes()}
${reply}
${(this.message.child_messages || []).map(x => html`
`)}
`;
} else if (typeof(this.message.content) == 'string') {
return small_frame(html`🔒`);
} else {
return small_frame(this.render_raw());
}
}
}
customElements.define('tf-message', TfMessageElement);