mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-23 19:05:16 +02:00
refactor: extract user avatar color and initials logic into a new utility module, update related components to use the new functions
This commit is contained in:
parent
87caa4b6d0
commit
e0ecea61f8
6 changed files with 65 additions and 63 deletions
34
surfsense_web/lib/user-avatar.ts
Normal file
34
surfsense_web/lib/user-avatar.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
const USER_AVATAR_COLORS = [
|
||||
"#6366f1",
|
||||
"#8b5cf6",
|
||||
"#a855f7",
|
||||
"#d946ef",
|
||||
"#ec4899",
|
||||
"#f43f5e",
|
||||
"#ef4444",
|
||||
"#f97316",
|
||||
"#eab308",
|
||||
"#84cc16",
|
||||
"#22c55e",
|
||||
"#14b8a6",
|
||||
"#06b6d4",
|
||||
"#0ea5e9",
|
||||
"#3b82f6",
|
||||
];
|
||||
|
||||
export function getUserAvatarColor(email: string): string {
|
||||
let hash = 0;
|
||||
for (let i = 0; i < email.length; i++) {
|
||||
hash = email.charCodeAt(i) + ((hash << 5) - hash);
|
||||
}
|
||||
return USER_AVATAR_COLORS[Math.abs(hash) % USER_AVATAR_COLORS.length];
|
||||
}
|
||||
|
||||
export function getUserInitials(email: string): string {
|
||||
const name = email.split("@")[0];
|
||||
const parts = name.split(/[._-]/);
|
||||
if (parts.length >= 2 && parts[0]?.[0] && parts[1]?.[0]) {
|
||||
return (parts[0][0] + parts[1][0]).toUpperCase();
|
||||
}
|
||||
return name.slice(0, 2).toUpperCase();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue