Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Use consistent colouring of username with avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
Germain committed Aug 24, 2023
1 parent 2d28419 commit e3df662
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/components/views/messages/DisambiguatedProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class DisambiguatedProfile extends React.Component<IProps> {

let colorClass: string | undefined;
if (colored) {
colorClass = getUserNameColorClass(fallbackName);
colorClass = getUserNameColorClass(mxid ?? "");
}

let mxidElement;
Expand Down
28 changes: 5 additions & 23 deletions src/utils/FormattingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
*/

import { ReactElement, ReactNode } from "react";
import { useIdColorHash } from "@vector-im/compound-web";

import { _t, getCurrentLanguage } from "../languageHandler";
import { jsxJoin } from "./ReactUtils";
Expand Down Expand Up @@ -73,30 +74,11 @@ export function formatBytes(bytes: number, decimals = 2): string {
export function formatCryptoKey(key: string): string {
return key.match(/.{1,4}/g)!.join(" ");
}
/**
* calculates a numeric hash for a given string
*
* @param {string} str string to hash
*
* @return {number}
*/
export function hashCode(str?: string): number {
let hash = 0;
let chr: number;
if (!str?.length) {
return hash;
}
for (let i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0;
}
return Math.abs(hash);
}

export function getUserNameColorClass(userId?: string): string {
const colorNumber = (hashCode(userId) % 8) + 1;
return `mx_Username_color${colorNumber}`;
export function getUserNameColorClass(userId: string): string {
// eslint-disable-next-line react-hooks/rules-of-hooks
const number = useIdColorHash(userId);
return `mx_Username_color${number}`;
}

/**
Expand Down

0 comments on commit e3df662

Please sign in to comment.