refactoring

This commit is contained in:
Ahmad Karlam
2020-10-04 09:08:05 +07:00
parent d27d4a798f
commit 2abde9186c
2 changed files with 19 additions and 19 deletions

View File

@@ -1,25 +1,13 @@
import {Component, h} from 'https://unpkg.com/preact?module'; import { h, Component } from 'https://unpkg.com/preact?module';
import htm from 'https://unpkg.com/htm?module'; import htm from 'https://unpkg.com/htm?module';
import {messageBubbleColorForString} from '../../utils/user-colors.js';
import {formatMessageText} from '../../utils/chat.js';
import {generateAvatar} from '../../utils/helpers.js';
import {SOCKET_MESSAGE_TYPES} from '../../utils/websocket.js';
const html = htm.bind(h); const html = htm.bind(h);
import { messageBubbleColorForString } from '../../utils/user-colors.js';
import { formatMessageText, formatTimestamp } from '../../utils/chat.js';
import { generateAvatar } from '../../utils/helpers.js';
import { SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js';
export default class Message extends Component { export default class Message extends Component {
formatTimestamp(sentAt) {
sentAt = new Date(sentAt);
let diffInDays = ((new Date()) - sentAt) / (24 * 3600 * 1000);
if (diffInDays >= -1) {
return `${sentAt.toLocaleDateString('en-US', {dateStyle: 'medium'})} at ` +
sentAt.toLocaleTimeString();
}
return sentAt.toLocaleTimeString();
}
render(props) { render(props) {
const { message, username } = props; const { message, username } = props;
const { type } = message; const { type } = message;
@@ -47,7 +35,7 @@ export default class Message extends Component {
</div> </div>
<div <div
class="message-text text-gray-300 font-normal overflow-y-hidden" class="message-text text-gray-300 font-normal overflow-y-hidden"
title=${`Sent at ${this.formatTimestamp(timestamp)}`} title=${`Sent at ${formatTimestamp(timestamp)}`}
dangerouslySetInnerHTML=${ dangerouslySetInnerHTML=${
{ __html: formattedMessage } { __html: formattedMessage }
} }

View File

@@ -278,3 +278,15 @@ export function convertOnPaste( event = { preventDefault() {} }) {
document.execCommand('insertText', false, value); document.execCommand('insertText', false, value);
} }
} }
export function formatTimestamp(sentAt) {
sentAt = new Date(sentAt);
let diffInDays = ((new Date()) - sentAt) / (24 * 3600 * 1000);
if (diffInDays >= -1) {
return `${sentAt.toLocaleDateString('en-US', {dateStyle: 'medium'})} at ` +
sentAt.toLocaleTimeString();
}
return sentAt.toLocaleTimeString();
}