From fcd93f4a645713c8da79b32cc40c07432b93a0e9 Mon Sep 17 00:00:00 2001 From: Minit Jain Date: Mon, 16 Nov 2026 21:16:35 +0530 Subject: [PATCH] fix(web): use first whole character for dashboard avatar initial Avatar took name?.[0], which is a single UTF-16 code unit. Names starting with an emoji rendered a lone high surrogate, and names with a leading space rendered a blank initial instead of the "A" fallback. Trim the name and take the first code point instead. --- .../__tests__/unit/dashboard-avatar.test.ts | 42 +++++++++++++++++++ .../(org)/dashboard/_components/Avatar.tsx | 4 +- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 apps/web/__tests__/unit/dashboard-avatar.test.ts diff --git a/apps/web/__tests__/unit/dashboard-avatar.test.ts b/apps/web/__tests__/unit/dashboard-avatar.test.ts new file mode 100644 index 00000000000..aa8803da2cb --- /dev/null +++ b/apps/web/__tests__/unit/dashboard-avatar.test.ts @@ -0,0 +1,42 @@ +// @vitest-environment jsdom + +import { act, createElement } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { Avatar } from "@/app/(org)/dashboard/_components/Avatar"; + +describe("dashboard avatar initial", () => { + let root: Root; + let container: HTMLDivElement; + + beforeEach(() => { + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + container = document.createElement("div"); + document.body.append(container); + root = createRoot(container); + }); + + afterEach(async () => { + await act(async () => root.unmount()); + container.remove(); + vi.unstubAllGlobals(); + }); + + const renderInitial = async (name: string | null) => { + await act(async () => root.render(createElement(Avatar, { name }))); + return container.querySelector("span")?.textContent; + }; + + it("shows the whole emoji for names starting with an emoji", async () => { + expect(await renderInitial("\u{1F600} Team")).toBe("\u{1F600}"); + }); + + it("ignores leading whitespace", async () => { + expect(await renderInitial(" bob")).toBe("B"); + }); + + it("falls back to A for empty or missing names", async () => { + expect(await renderInitial("")).toBe("A"); + expect(await renderInitial(null)).toBe("A"); + }); +}); diff --git a/apps/web/app/(org)/dashboard/_components/Avatar.tsx b/apps/web/app/(org)/dashboard/_components/Avatar.tsx index b5e869631a8..da76a78d7a2 100644 --- a/apps/web/app/(org)/dashboard/_components/Avatar.tsx +++ b/apps/web/app/(org)/dashboard/_components/Avatar.tsx @@ -67,8 +67,8 @@ export const Avatar: React.FC<{ className?: string; letterClass?: string; }> = ({ name, className = "", letterClass = "text-xs" }) => { - const initial = name?.[0]?.toUpperCase() || "A"; - const charCode = initial.charCodeAt(0); + const initial = Array.from(name?.trim() ?? "")[0]?.toUpperCase() || "A"; + const charCode = initial.codePointAt(0) ?? 0; const isAlpha = charCode >= 65 && charCode <= 90; const colorIndex = isAlpha ? charCode - 65 : 25; // fallback to last color for non-alpha const bgColor = avatarDefaultPalette[colorIndex];