colorize user names under messages

This commit is contained in:
or-else
2021-08-18 16:00:47 -07:00
parent fb3de109e0
commit 529486feb3
3 changed files with 15 additions and 9 deletions
+6 -1
View File
@@ -39,7 +39,7 @@ export function bytesToHumanSize(bytes) {
// Get 32 bit integer hash value for a string. Ideally it should produce the same value // Get 32 bit integer hash value for a string. Ideally it should produce the same value
// as Java's String#hash(). // as Java's String#hash().
export function stringHash(value) { function stringToColorHash(value) {
let hash = 0; let hash = 0;
value = '' + value; value = '' + value;
for (let i = 0; i < value.length; i++) { for (let i = 0; i < value.length; i++) {
@@ -48,3 +48,8 @@ export function stringHash(value) {
} }
return hash; return hash;
} }
// Converts user or topic ID to a CSS color class. Ideally it should produce the same color value as Java version.
export function idToColorClass(string, light) {
return (light ? 'light-color' : 'dark-color') + (Math.abs(stringToColorHash(string)) % 16);
}
+7 -5
View File
@@ -7,6 +7,8 @@ import Attachment from './attachment.jsx';
import LetterTile from './letter-tile.jsx'; import LetterTile from './letter-tile.jsx';
import ReceivedMarker from './received-marker.jsx' import ReceivedMarker from './received-marker.jsx'
import UploadingImage from './uploading-image.jsx' import UploadingImage from './uploading-image.jsx'
import { idToColorClass } from '../lib/strformat.js';
import { sanitizeImageUrl, sanitizeUrl } from '../lib/utils.js'; import { sanitizeImageUrl, sanitizeUrl } from '../lib/utils.js';
export default class ChatMessage extends React.Component { export default class ChatMessage extends React.Component {
@@ -152,11 +154,11 @@ export default class ChatMessage extends React.Component {
} }
</div> </div>
{fullDisplay ? {fullDisplay ?
<div className="author"> <div className={'author' + (this.props.userName ? idToColorClass(this.props.userFrom) : '')}>
<FormattedMessage id="user_not_found" defaultMessage="Not found" {this.props.userName ||
description="In place of a user's full name when the user is not found.">{ <i><FormattedMessage id="user_not_found" defaultMessage="Not found"
(notFound) => {return this.props.userName || <i>{notFound}</i>} description="In place of a user's full name when the user is not found." /></i>
}</FormattedMessage> }
</div> </div>
: null : null
} }
+2 -3
View File
@@ -2,7 +2,7 @@ import React from 'react';
import Tinode from 'tinode-sdk'; import Tinode from 'tinode-sdk';
import { stringHash } from '../lib/strformat.js'; import { idToColorClass } from '../lib/strformat.js';
import { sanitizeImageUrl } from '../lib/utils.js'; import { sanitizeImageUrl } from '../lib/utils.js';
export default class LetterTile extends React.PureComponent { export default class LetterTile extends React.PureComponent {
@@ -10,8 +10,7 @@ export default class LetterTile extends React.PureComponent {
let avatar; let avatar;
if (this.props.avatar === true) { if (this.props.avatar === true) {
const isGroup = (Tinode.topicType(this.props.topic) == 'grp'); const isGroup = (Tinode.topicType(this.props.topic) == 'grp');
const iconColor = (isGroup ? 'light-color' : 'dark-color') const iconColor = idToColorClass(this.props.topic, isGroup);
+ (Math.abs(stringHash(this.props.topic)) % 16);
if (this.props.topic && this.props.title && this.props.title.trim()) { if (this.props.topic && this.props.title && this.props.title.trim()) {
const letter = this.props.title.trim().charAt(0); const letter = this.props.title.trim().charAt(0);
const className = 'lettertile ' + iconColor; const className = 'lettertile ' + iconColor;