Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions apps/mobile/src/j5/a2a/timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -123,12 +125,12 @@ export function J5MessageAuthorLine(props: {
);
}
return (
<Text className="mb-1 pl-1 text-xs text-foreground-muted" numberOfLines={1}>
<PersonLine name={author.name}>
<Text className="font-t3-medium text-xs text-foreground">
{author.name ?? "Someone without a name"}
</Text>
{author.editedBy === null ? null : ` · edited by ${author.editedBy}`}
</Text>
</PersonLine>
);
}
if (j5.group === "other-person") {
Expand All @@ -138,15 +140,27 @@ export function J5MessageAuthorLine(props: {
senderThreadTitle: undefined,
});
return (
<Text className="mb-1 pl-1 text-xs text-foreground-muted" numberOfLines={1}>
<PersonLine name={j5.senderName ?? props.timeline.labels.get(j5.senderId) ?? null}>
<Text className="font-t3-medium text-xs text-foreground">{name}</Text> · via Inbox
</Text>
</PersonLine>
);
}
if (j5.group !== "participant") return null;
return <ParticipantAuthorLine {...props} j5={j5} />;
}

/** 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 (
<View className="mb-1 max-w-[85%] flex-row items-center gap-1.5 pl-1">
<PersonAvatar name={props.name} />
<Text className="shrink text-xs text-foreground-muted" numberOfLines={1}>
{props.children}
</Text>
</View>
);
}

/** 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 };
Expand Down
46 changes: 46 additions & 0 deletions apps/mobile/src/j5/sharing/PersonAvatar.tsx
Original file line number Diff line number Diff line change
@@ -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<Exclude<ProjectIconColor, "gray">, 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 (
<View
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
className={cn(
"size-5 items-center justify-center rounded-full",
avatar === null ? "bg-subtle-strong" : BACKGROUND[avatar.color],
)}
>
{avatar === null ? null : (
<Text className="font-t3-bold text-3xs text-white">{avatar.initials}</Text>
)}
</View>
);
}
20 changes: 7 additions & 13 deletions apps/web/src/j5/sharing/PersonAvatar.tsx
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -30,12 +23,13 @@ export function PersonAvatar({
readonly name: string | null;
readonly size: keyof typeof SIZES;
}) {
const avatar = presentPersonAvatar(name);
return (
<span
aria-hidden
className={`flex shrink-0 items-center justify-center rounded-full font-semibold text-white ${SIZES[size]} ${name === null ? "bg-muted" : avatarSwatch(name)}`}
className={`flex shrink-0 items-center justify-center rounded-full font-semibold text-white ${SIZES[size]} ${avatar === null ? "bg-muted" : SWATCH_BY_COLOR.get(avatar.color)}`}
>
{name === null ? "" : initialsOf(name)}
{avatar?.initials ?? ""}
</span>
);
}
8 changes: 0 additions & 8 deletions apps/web/src/j5/sharing/shareLink.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
4 changes: 4 additions & 0 deletions packages/client-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 28 additions & 0 deletions packages/client-runtime/src/j5/personAvatar.test.ts
Original file line number Diff line number Diff line change
@@ -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("?");
});
});
28 changes: 28 additions & 0 deletions packages/client-runtime/src/j5/personAvatar.ts
Original file line number Diff line number Diff line change
@@ -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<ProjectIconColor, "gray"> => 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();
}
Loading