diff --git a/apps/mobile/src/j5/a2a/timeline.tsx b/apps/mobile/src/j5/a2a/timeline.tsx index 7d183b818dd75..d15df9ff83945 100644 --- a/apps/mobile/src/j5/a2a/timeline.tsx +++ b/apps/mobile/src/j5/a2a/timeline.tsx @@ -7,12 +7,14 @@ import type { J5UserMessagePresentation, } from "@t3tools/client-runtime/j5/user-message"; import { isSharedNote } from "@t3tools/client-runtime/j5/shared-conversation"; +import type { ReactNode } from "react"; import { Pressable, View } from "react-native"; import { AppText as Text } from "../../components/ui/AppText"; import { useOpenLinkedThread } from "../../features/threads/useOpenLinkedThread"; import { cn } from "../../lib/cn"; import { useQuietThreadShell } from "../../state/entities"; +import { PersonAvatar } from "../sharing/PersonAvatar"; import { SharedConversationNote } from "../sharing/SharedConversationNote"; import { presentJ5TimelineNotice, @@ -123,12 +125,12 @@ export function J5MessageAuthorLine(props: { ); } return ( - + {author.name ?? "Someone without a name"} {author.editedBy === null ? null : ` · edited by ${author.editedBy}`} - + ); } if (j5.group === "other-person") { @@ -138,15 +140,27 @@ export function J5MessageAuthorLine(props: { senderThreadTitle: undefined, }); return ( - + {name} · via Inbox - + ); } if (j5.group !== "participant") return null; return ; } +/** Another person's line above their message: their avatar, then their name and what follows. */ +function PersonLine(props: { readonly name: string | null; readonly children: ReactNode }) { + return ( + + + + {props.children} + + + ); +} + /** The identity line above a participant's letter: the sender's name and one tag or badge. */ function ParticipantAuthorLine(props: { readonly message: J5UserMessageInput & { readonly senderThreadId?: string | undefined }; diff --git a/apps/mobile/src/j5/sharing/PersonAvatar.tsx b/apps/mobile/src/j5/sharing/PersonAvatar.tsx new file mode 100644 index 0000000000000..566fbb3cbfbc9 --- /dev/null +++ b/apps/mobile/src/j5/sharing/PersonAvatar.tsx @@ -0,0 +1,46 @@ +import { presentPersonAvatar } from "@t3tools/client-runtime/j5/person-avatar"; +import type { ProjectIconColor } from "@t3tools/contracts"; +import { View } from "react-native"; + +import { AppText as Text } from "../../components/ui/AppText"; +import { cn } from "../../lib/cn"; + +// Literal class names, so the style compiler sees each one. +const BACKGROUND: Record, string> = { + red: "bg-red-500", + orange: "bg-orange-500", + amber: "bg-amber-500", + yellow: "bg-yellow-500", + lime: "bg-lime-500", + green: "bg-green-500", + emerald: "bg-emerald-500", + teal: "bg-teal-500", + cyan: "bg-cyan-500", + sky: "bg-sky-500", + blue: "bg-blue-500", + indigo: "bg-indigo-500", + violet: "bg-violet-500", + purple: "bg-purple-500", + fuchsia: "bg-fuchsia-500", + pink: "bg-pink-500", + rose: "bg-rose-500", +}; + +/** A person's initials on their colour, as web draws it beside their name. */ +export function PersonAvatar(props: { readonly name: string | null }) { + const avatar = presentPersonAvatar(props.name); + return ( + + {avatar === null ? null : ( + {avatar.initials} + )} + + ); +} diff --git a/apps/web/src/j5/sharing/PersonAvatar.tsx b/apps/web/src/j5/sharing/PersonAvatar.tsx index 630448934eca7..b6e7097d3c0b5 100644 --- a/apps/web/src/j5/sharing/PersonAvatar.tsx +++ b/apps/web/src/j5/sharing/PersonAvatar.tsx @@ -1,18 +1,11 @@ +import { presentPersonAvatar } from "@t3tools/client-runtime/j5/person-avatar"; + import { PROJECT_ICON_COLORS } from "../../projectIconColors"; -import { initialsOf } from "./shareLink.logic"; -// The project icon's swatches, gray aside, so a person's colour sits with the rest of the app. -const SWATCHES = PROJECT_ICON_COLORS.filter((color) => color.value !== "gray").map( - (color) => color.swatchClassName, +const SWATCH_BY_COLOR = new Map( + PROJECT_ICON_COLORS.map((color) => [color.value, color.swatchClassName] as const), ); -/** The same name is always the same colour, on every client. */ -function avatarSwatch(name: string): string { - let hash = 0; - for (const char of name.trim().toLowerCase()) hash = (hash * 31 + char.codePointAt(0)!) | 0; - return SWATCHES[Math.abs(hash) % SWATCHES.length]!; -} - const SIZES = { xs: "size-5 text-4xs", sm: "size-6 text-3xs", @@ -30,12 +23,13 @@ export function PersonAvatar({ readonly name: string | null; readonly size: keyof typeof SIZES; }) { + const avatar = presentPersonAvatar(name); return ( - {name === null ? "" : initialsOf(name)} + {avatar?.initials ?? ""} ); } diff --git a/apps/web/src/j5/sharing/shareLink.logic.ts b/apps/web/src/j5/sharing/shareLink.logic.ts index 118b049fdbc93..5ce2a8d8d27da 100644 --- a/apps/web/src/j5/sharing/shareLink.logic.ts +++ b/apps/web/src/j5/sharing/shareLink.logic.ts @@ -122,11 +122,3 @@ export function lifetimeLeft(expiresAtMs: number, nowMs: number): string | null const days = Math.round(hours / 24); return `${days} ${plural(days, "day", "days")}`; } - -/** The first one or two letters a person's avatar shows. */ -export function initialsOf(name: string): string { - const words = name.trim().split(/\s+/u).filter(Boolean); - const letters = - words.length > 1 ? `${words[0]![0]}${words[words.length - 1]![0]}` : (words[0]?.[0] ?? "?"); - return letters.toUpperCase(); -} diff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.json index 79c4f3bd92a93..29d686302dd4e 100644 --- a/packages/client-runtime/package.json +++ b/packages/client-runtime/package.json @@ -331,6 +331,10 @@ "types": "./src/state/threadUndo.ts", "default": "./src/state/threadUndo.ts" }, + "./j5/person-avatar": { + "types": "./src/j5/personAvatar.ts", + "default": "./src/j5/personAvatar.ts" + }, "./j5/participant-identity": { "types": "./src/j5/participantIdentity.ts", "default": "./src/j5/participantIdentity.ts" diff --git a/packages/client-runtime/src/j5/personAvatar.test.ts b/packages/client-runtime/src/j5/personAvatar.test.ts new file mode 100644 index 0000000000000..87f60731b55a6 --- /dev/null +++ b/packages/client-runtime/src/j5/personAvatar.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { initialsOf, presentPersonAvatar } from "./personAvatar.ts"; + +describe("presentPersonAvatar", () => { + it("gives the same name the same colour, whatever its case and spacing", () => { + expect(presentPersonAvatar("Priya")).toEqual(presentPersonAvatar(" priya ")); + expect(presentPersonAvatar("Priya")?.color).not.toBe("gray"); + }); + + it("keeps the colour web has always drawn for a name", () => { + // Web's palette is the project icon colours in contract order, gray aside. + expect(presentPersonAvatar("Priya")?.color).toBe("sky"); + expect(presentPersonAvatar("Test owner")?.color).toBe("yellow"); + }); + + it("draws nothing for someone without a name", () => { + expect(presentPersonAvatar(null)).toBeNull(); + }); +}); + +describe("initialsOf", () => { + it("takes the first and last words' first letters", () => { + expect(initialsOf("Priya Natarajan Rao")).toBe("PR"); + expect(initialsOf("priya")).toBe("P"); + expect(initialsOf(" ")).toBe("?"); + }); +}); diff --git a/packages/client-runtime/src/j5/personAvatar.ts b/packages/client-runtime/src/j5/personAvatar.ts new file mode 100644 index 0000000000000..6d8e1a59b2cea --- /dev/null +++ b/packages/client-runtime/src/j5/personAvatar.ts @@ -0,0 +1,28 @@ +import { ProjectIconColor } from "@t3tools/contracts"; + +/** The project icon's colours, gray aside, so a person's colour sits with the rest of the app. */ +const AVATAR_COLORS = ProjectIconColor.literals.filter( + (color): color is Exclude => color !== "gray", +); + +/** + * A person's avatar where a picture would go (Shared conversations AC16): their initials on a + * colour picked from their name, so the same name is the same colour on every client. Null for + * someone with no name yet, drawn as an empty grey circle. + */ +export function presentPersonAvatar(name: string | null) { + if (name === null) return null; + let hash = 0; + for (const char of name.trim().toLowerCase()) hash = (hash * 31 + char.codePointAt(0)!) | 0; + return { + initials: initialsOf(name), + color: AVATAR_COLORS[Math.abs(hash) % AVATAR_COLORS.length]!, + }; +} + +export function initialsOf(name: string): string { + const words = name.trim().split(/\s+/u).filter(Boolean); + const letters = + words.length > 1 ? `${words[0]![0]}${words[words.length - 1]![0]}` : (words[0]?.[0] ?? "?"); + return letters.toUpperCase(); +}