-
-
Notifications
You must be signed in to change notification settings - Fork 833
Fix colour of avatar and colour matching with username #11470
Changes from 4 commits
e3df662
ec16765
e580440
45e9671
d8129e5
57e055b
f1ab043
c2ad167
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I have a comment explaining why this is OK to disable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment about a test as above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function starts with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, can we have that comment in the code please? |
||
const number = useIdColorHash(userId); | ||
return `mx_Username_color${number}`; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a test for this? I'd love to see one if not. Happy for you to merge and add it after if needed though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will do that! thank you 🙏