fix: garbled text displayed on avatars (#6575)
Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
parent
e0f2869374
commit
306e133651
@ -20,9 +20,13 @@ export const getClientColors = (clientId: string, appState: AppState) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getClientInitials = (userName?: string | null) => {
|
/**
|
||||||
if (!userName?.trim()) {
|
* returns first char, capitalized
|
||||||
return "?";
|
*/
|
||||||
}
|
export const getNameInitial = (name?: string | null) => {
|
||||||
return userName.trim()[0].toUpperCase();
|
// first char can be a surrogate pair, hence using codePointAt
|
||||||
|
const firstCodePoint = name?.trim()?.codePointAt(0);
|
||||||
|
return (
|
||||||
|
firstCodePoint ? String.fromCodePoint(firstCodePoint) : "?"
|
||||||
|
).toUpperCase();
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import "./Avatar.scss";
|
import "./Avatar.scss";
|
||||||
|
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { getClientInitials } from "../clients";
|
import { getNameInitial } from "../clients";
|
||||||
|
|
||||||
type AvatarProps = {
|
type AvatarProps = {
|
||||||
onClick: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
|
onClick: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
|
||||||
@ -12,7 +12,7 @@ type AvatarProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const Avatar = ({ color, onClick, name, src }: AvatarProps) => {
|
export const Avatar = ({ color, onClick, name, src }: AvatarProps) => {
|
||||||
const shortName = getClientInitials(name);
|
const shortName = getNameInitial(name);
|
||||||
const [error, setError] = useState(false);
|
const [error, setError] = useState(false);
|
||||||
const loadImg = !error && src;
|
const loadImg = !error && src;
|
||||||
const style = loadImg ? undefined : { background: color };
|
const style = loadImg ? undefined : { background: color };
|
||||||
|
@ -1,44 +1,39 @@
|
|||||||
import { getClientInitials } from "../clients";
|
import { getNameInitial } from "../clients";
|
||||||
|
|
||||||
describe("getClientInitials", () => {
|
describe("getClientInitials", () => {
|
||||||
it("returns substring if one name provided", () => {
|
it("returns substring if one name provided", () => {
|
||||||
const result = getClientInitials("Alan");
|
expect(getNameInitial("Alan")).toBe("A");
|
||||||
expect(result).toBe("A");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns initials", () => {
|
it("returns initials", () => {
|
||||||
const result = getClientInitials("John Doe");
|
expect(getNameInitial("John Doe")).toBe("J");
|
||||||
expect(result).toBe("J");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns correct initials if many names provided", () => {
|
it("returns correct initials if many names provided", () => {
|
||||||
const result = getClientInitials("John Alan Doe");
|
expect(getNameInitial("John Alan Doe")).toBe("J");
|
||||||
expect(result).toBe("J");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns single initial if 1 letter provided", () => {
|
it("returns single initial if 1 letter provided", () => {
|
||||||
const result = getClientInitials("z");
|
expect(getNameInitial("z")).toBe("Z");
|
||||||
expect(result).toBe("Z");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("trims trailing whitespace", () => {
|
it("trims trailing whitespace", () => {
|
||||||
const result = getClientInitials(" q ");
|
expect(getNameInitial(" q ")).toBe("Q");
|
||||||
expect(result).toBe("Q");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns "?" if falsey value provided', () => {
|
it('returns "?" if falsey value provided', () => {
|
||||||
let result = getClientInitials("");
|
expect(getNameInitial("")).toBe("?");
|
||||||
expect(result).toBe("?");
|
expect(getNameInitial(undefined)).toBe("?");
|
||||||
|
expect(getNameInitial(null)).toBe("?");
|
||||||
result = getClientInitials(undefined);
|
|
||||||
expect(result).toBe("?");
|
|
||||||
|
|
||||||
result = getClientInitials(null);
|
|
||||||
expect(result).toBe("?");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns "?" when value is blank', () => {
|
it('returns "?" when value is blank', () => {
|
||||||
const result = getClientInitials(" ");
|
expect(getNameInitial(" ")).toBe("?");
|
||||||
expect(result).toBe("?");
|
});
|
||||||
|
|
||||||
|
it("works with multibyte strings", () => {
|
||||||
|
expect(getNameInitial("😀")).toBe("😀");
|
||||||
|
// but doesn't work with emoji ZWJ sequences
|
||||||
|
expect(getNameInitial("👨👩👦")).toBe("👨");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user