From e935f8aa8e97d31febd936b3784e4da6085a4aa0 Mon Sep 17 00:00:00 2001 From: Daisy Date: Tue, 28 Jul 2026 17:54:57 +0100 Subject: [PATCH] Add PMP name colors --- .changeset/add_persona_name_colors.md | 5 ++ src/app/components/user-avatar/UserAvatar.tsx | 15 ++++- src/app/features/room/message/Message.tsx | 18 +++++- .../room/persona-picker/PersonaPicker.tsx | 16 ++++- .../Persona/PerMessageProfileEditor.tsx | 62 ++++++++++++++++++- .../Persona/PerMessageProfileEditorView.tsx | 6 ++ .../Persona/PerMessageProfileListItem.tsx | 15 ++++- .../Persona/PerMessageProfileOverview.tsx | 2 + .../settings/Persona/ProfilesPage.tsx | 2 + .../settings/account/NameColorEditor.tsx | 51 ++++++++++----- .../features/settings/cosmetics/Cosmetics.tsx | 11 ++++ src/app/features/settings/settingsLink.ts | 1 + src/app/hooks/usePerMessageProfile.ts | 17 ++++- src/app/state/settings.ts | 2 + src/unstable/prefixes/msc/profile.ts | 5 ++ 15 files changed, 203 insertions(+), 25 deletions(-) create mode 100644 .changeset/add_persona_name_colors.md diff --git a/.changeset/add_persona_name_colors.md b/.changeset/add_persona_name_colors.md new file mode 100644 index 000000000..e647b21ae --- /dev/null +++ b/.changeset/add_persona_name_colors.md @@ -0,0 +1,5 @@ +--- +default: minor +--- + +# Add persona name colors diff --git a/src/app/components/user-avatar/UserAvatar.tsx b/src/app/components/user-avatar/UserAvatar.tsx index ff1e91bbb..9c7a66e7c 100644 --- a/src/app/components/user-avatar/UserAvatar.tsx +++ b/src/app/components/user-avatar/UserAvatar.tsx @@ -11,6 +11,7 @@ type UserAvatarProps = { userId: string; src?: string; alt?: string; + fallbackColor?: string; renderFallback: () => ReactNode; }; @@ -18,7 +19,14 @@ const handleImageLoad: ReactEventHandler = (evt) => { evt.currentTarget.setAttribute('data-image-loaded', 'true'); }; -export function UserAvatar({ className, userId, src, alt, renderFallback }: UserAvatarProps) { +export function UserAvatar({ + className, + userId, + src, + alt, + fallbackColor, + renderFallback, +}: UserAvatarProps) { const [error, setError] = useState(false); const resolvedSrc = useRenderableMediaUrl(src); @@ -29,7 +37,10 @@ export function UserAvatar({ className, userId, src, alt, renderFallback }: User if (!src || error) { return ( {renderFallback()} diff --git a/src/app/features/room/message/Message.tsx b/src/app/features/room/message/Message.tsx index 3bb3604e6..5644a52de 100644 --- a/src/app/features/room/message/Message.tsx +++ b/src/app/features/room/message/Message.tsx @@ -131,6 +131,7 @@ export type MessageProps = { }; import { useMenuAnchor } from '$hooks/useMenuAnchor'; +import { ThemeKind, useActiveTheme } from '$hooks/useTheme'; const clamp = (str: string, len: number) => (str.length > len ? `${str.slice(0, len)}...` : str); @@ -367,6 +368,8 @@ function MessageInternal( const messageRef = useRef(null); useImperativeHandle(ref, () => messageRef.current as HTMLDivElement); const [isVisible, setIsVisible] = useState(() => typeof IntersectionObserver === 'undefined'); + const activeTheme = useActiveTheme(); + const [renderPersonaColors] = useSetting(settingsAtom, 'renderPersonaColors'); useEffect(() => { const element = messageRef.current; @@ -450,6 +453,14 @@ function MessageInternal( return convertBeeperFormatToOurPerMessageProfile(pmp); }, [pmp]); + const pmpNameColor = useMemo(() => { + if (!renderPersonaColors) return undefined; + const pmpNameColorLight = parsedPMPContent?.nameColorLightTheme; + const pmpNameColorDark = parsedPMPContent?.nameColorDarkTheme; + + return activeTheme.kind === ThemeKind.Dark ? pmpNameColorDark : pmpNameColorLight; + }, [parsedPMPContent, activeTheme, renderPersonaColors]); + /** * boolean to indicate wheather we should indicate to the user that it is a pmp * We want to not show it, when the name is unset, or whitespace only @@ -574,7 +585,7 @@ function MessageInternal( {showPronouns && ( - + )} {showPmPInfo && ( diff --git a/src/app/features/room/persona-picker/PersonaPicker.tsx b/src/app/features/room/persona-picker/PersonaPicker.tsx index 55e6d3e3c..63f920d5b 100644 --- a/src/app/features/room/persona-picker/PersonaPicker.tsx +++ b/src/app/features/room/persona-picker/PersonaPicker.tsx @@ -37,6 +37,7 @@ import { useCallback, useEffect, useRef, useState, type FormEvent } from 'react' import * as css from './PersonaPicker.css.ts'; import { InfoCard } from '$components/info-card/InfoCard.tsx'; import { InfoIcon } from '@phosphor-icons/react'; +import { ThemeKind, useActiveTheme } from '$hooks/useTheme.ts'; const pillStyles = { cursor: 'pointer', @@ -63,6 +64,7 @@ export function PersonaPicker({ onTabChange, }: PersonaPickerProps) { const useAuthentication = useMediaAuthentication(); + const activeTheme = useActiveTheme(); const [AddPersonaMenuAnchor, setAddPersonaMenuAnchor] = useState(); const [profiles, setProfiles] = useState(undefined); const [selectedGlobalPersona, setSelectedGlobalPersona] = useState( @@ -75,6 +77,14 @@ export function PersonaPicker({ return persona.id === selectedPersona?.id ? true : undefined; }; + const nameColor = useCallback( + (persona: PerMessageProfile) => + activeTheme.kind === ThemeKind.Dark + ? persona.nameColorDarkTheme + : persona.nameColorLightTheme, + [activeTheme] + ); + const defactoPersona = () => selectedRoomPersona ?? selectedGlobalPersona; const searchInputRef = useRef(null); @@ -256,6 +266,7 @@ export function PersonaPicker({ ( {nameInitials(profile.name)} @@ -266,7 +277,10 @@ export function PersonaPicker({ } > - + {profile.name} diff --git a/src/app/features/settings/Persona/PerMessageProfileEditor.tsx b/src/app/features/settings/Persona/PerMessageProfileEditor.tsx index e0f46fab1..1564b1890 100644 --- a/src/app/features/settings/Persona/PerMessageProfileEditor.tsx +++ b/src/app/features/settings/Persona/PerMessageProfileEditor.tsx @@ -28,6 +28,7 @@ import { parsePronounsStringToPronounsSetArray } from '$utils/pronouns'; import { generateShortId } from '$utils/shortIdGen'; import { SettingTile } from '$components/setting-tile'; import { type AsyncState, AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback'; +import { NameColorEditor } from '../account/NameColorEditor'; const constructProxyString = (s: Shorthand) => { return `${s.prefix ?? ''}text${s.suffix ?? ''}`; @@ -319,6 +320,8 @@ type PerMessageProfileEditorProps = { avatarMxcUrl?: string; displayName?: string; pronouns?: PronounSet[]; + nameColorLightTheme?: string; + nameColorDarkTheme?: string; shorthands?: Shorthand[]; onDelete?: (profileId: string) => void; }; @@ -329,6 +332,8 @@ export function PerMessageProfileEditor({ avatarMxcUrl, displayName, pronouns = Array(), + nameColorLightTheme, + nameColorDarkTheme, onDelete, }: Readonly) { const useAuthentication = useMediaAuthentication(); @@ -353,6 +358,12 @@ export function PerMessageProfileEditor({ return pronounsString; }); + // Name color + const [currentNameColorLight, setCurrentNameColorLight] = useState(nameColorLightTheme ?? null); + const [newNameColorLight, setNewNameColorLight] = useState(nameColorLightTheme ?? null); + const [currentNameColorDark, setCurrentNameColorDark] = useState(nameColorDarkTheme ?? null); + const [newNameColorDark, setNewNameColorDark] = useState(nameColorDarkTheme ?? null); + const [newDisplayName, setNewDisplayName] = useState(currentDisplayName); const [imageFile, setImageFile] = useState(); const [imageHasChanges, setImageHasChanges] = useState(false); @@ -396,6 +407,8 @@ export function PerMessageProfileEditor({ () => newDisplayName !== (currentDisplayName ?? '') || newPronounsString !== currentPronounsString || + newNameColorLight !== currentNameColorLight || + newNameColorDark !== currentNameColorDark || hasIdChange || imageHasChanges, [ @@ -403,6 +416,10 @@ export function PerMessageProfileEditor({ currentDisplayName, newPronounsString, currentPronounsString, + newNameColorLight, + currentNameColorLight, + newNameColorDark, + currentNameColorDark, hasIdChange, imageHasChanges, ] @@ -433,10 +450,14 @@ export function PerMessageProfileEditor({ name: newDisplayName, avatarUrl: avatarMxc, pronouns: newPronouns, + nameColorLightTheme: newNameColorLight ?? undefined, + nameColorDarkTheme: newNameColorDark ?? undefined, }); setCurrentDisplayName(newDisplayName); setCurrentPronouns(newPronouns); + setCurrentNameColorLight(newNameColorLight); + setCurrentNameColorDark(newNameColorDark); setImageHasChanges(false); setChangingDisplayName(false); setDisableSetDisplayname(false); @@ -445,7 +466,17 @@ export function PerMessageProfileEditor({ setCurrentId(newId); }); } - }, [mx, profileId, newDisplayName, avatarMxc, newPronouns, hasIdChange, newId]) + }, [ + mx, + profileId, + newDisplayName, + avatarMxc, + newPronouns, + newNameColorLight, + newNameColorDark, + hasIdChange, + newId, + ]) ); const [deleteState, handleDelete] = useAsyncCallback( @@ -649,6 +680,35 @@ export function PerMessageProfileEditor({ } > + + + + + + + void; onDelete?: (profileId: string) => void; requestClose: () => void; @@ -21,6 +23,8 @@ export function PerMessageProfileEditorView({ avatarMxcUrl, displayName, pronouns = Array(), + nameColorLightTheme, + nameColorDarkTheme, requestClose, }: Readonly) { return ( @@ -53,6 +57,8 @@ export function PerMessageProfileEditorView({ avatarMxcUrl={avatarMxcUrl} displayName={displayName} pronouns={pronouns} + nameColorLightTheme={nameColorLightTheme} + nameColorDarkTheme={nameColorDarkTheme} onDelete={requestClose} /> diff --git a/src/app/features/settings/Persona/PerMessageProfileListItem.tsx b/src/app/features/settings/Persona/PerMessageProfileListItem.tsx index 40fa89dc6..eee92ffbd 100644 --- a/src/app/features/settings/Persona/PerMessageProfileListItem.tsx +++ b/src/app/features/settings/Persona/PerMessageProfileListItem.tsx @@ -9,6 +9,7 @@ import type { PronounSet } from '$utils/pronouns'; import { SettingTile } from '$components/setting-tile'; import { toSettingsFocusIdPart } from '../settingsLink'; import { Pronouns } from '$features/room/message'; +import { ThemeKind, useActiveTheme } from '$hooks/useTheme'; /** * the props we use for the per-message profile list item, which is used to display a per-message profile. This is used in the settings page when the user wants to view all of their profiles. @@ -18,6 +19,8 @@ type PerMessageProfileListItemProps = { profileId: string; avatarMxcUrl?: string; displayName?: string; + nameColorLight?: string; + nameColorDark?: string; pronouns?: PronounSet[]; onOpenEditor: (profileId: string) => void; }; @@ -28,9 +31,16 @@ export function PerMessageProfileListItem({ avatarMxcUrl, displayName, pronouns = Array(), + nameColorLight, + nameColorDark, onOpenEditor, }: Readonly) { const useAuthentication = useMediaAuthentication(); + const activeTheme = useActiveTheme(); + const nameColor = useMemo( + () => (activeTheme.kind === ThemeKind.Dark ? nameColorDark : nameColorLight), + [activeTheme, nameColorLight, nameColorDark] + ); const avatarUrl = useMemo(() => { if (avatarMxcUrl) { @@ -46,9 +56,9 @@ export function PerMessageProfileListItem({ focusId={`persona-${toSettingsFocusIdPart(profileId)}`} showSettingLinkAction={false} title={ - + {displayName ?? 'Unnamed Profile'} - + } description={profileId} @@ -69,6 +79,7 @@ export function PerMessageProfileListItem({ ( diff --git a/src/app/features/settings/Persona/PerMessageProfileOverview.tsx b/src/app/features/settings/Persona/PerMessageProfileOverview.tsx index 68dcf3961..63b1f5055 100644 --- a/src/app/features/settings/Persona/PerMessageProfileOverview.tsx +++ b/src/app/features/settings/Persona/PerMessageProfileOverview.tsx @@ -96,6 +96,8 @@ export function PerMessageProfileOverview({ avatarMxcUrl={profile.avatarUrl} displayName={profile.name} pronouns={profile.pronouns} + nameColorLight={profile.nameColorLightTheme} + nameColorDark={profile.nameColorDarkTheme} onOpenEditor={handleEdit} /> diff --git a/src/app/features/settings/Persona/ProfilesPage.tsx b/src/app/features/settings/Persona/ProfilesPage.tsx index f3bb8add9..b0841eb87 100644 --- a/src/app/features/settings/Persona/ProfilesPage.tsx +++ b/src/app/features/settings/Persona/ProfilesPage.tsx @@ -29,6 +29,8 @@ export function PerMessageProfilePage({ requestBack, requestClose }: PerMessageP avatarMxcUrl={editingProfile.avatarUrl} displayName={editingProfile.name} pronouns={editingProfile.pronouns} + nameColorLightTheme={editingProfile.nameColorLightTheme} + nameColorDarkTheme={editingProfile.nameColorDarkTheme} requestClose={handleEditorClose} /> ); diff --git a/src/app/features/settings/account/NameColorEditor.tsx b/src/app/features/settings/account/NameColorEditor.tsx index 71d524e42..cde8bc4d2 100644 --- a/src/app/features/settings/account/NameColorEditor.tsx +++ b/src/app/features/settings/account/NameColorEditor.tsx @@ -1,5 +1,5 @@ import { useState, useEffect } from 'react'; -import { Box, Button, config, Text, Input, IconButton } from 'folds'; +import { Box, Button, config, color, Text, Input, IconButton } from 'folds'; import { menuIcon, X } from '$components/icons/phosphor'; import { HexColorPicker } from 'react-colorful'; import { SettingTile } from '$components/setting-tile'; @@ -10,7 +10,8 @@ type NameColorEditorProps = { description?: string; focusId?: string; current?: string; - onSave: (color: string | null) => void; + onChange?: (color: string | null) => void; + onSave?: (color: string | null) => void; disabled?: boolean; }; @@ -19,44 +20,62 @@ const stripQuotes = (str?: string) => { // to solve the silly tuwunel return str.replaceAll(/^["']|["']$/g, ''); }; +const colorPattern = /^#[0-9A-F]{6}$/i; + export function NameColorEditor({ title, description, focusId, current, + onChange, onSave, disabled, }: Readonly) { - const [tempColor, setTempColor] = useState(stripQuotes(current) || '#FFFFFF'); + const [tempColor, setTempColor] = useState(stripQuotes(current) || undefined); const [hasChanged, setHasChanged] = useState(false); useEffect(() => { const sanitized = stripQuotes(current); if (sanitized) setTempColor(sanitized); - else setTempColor('#FFFFFF'); + else setTempColor(undefined); }, [current]); + // Valid in the sense that it should look like an error during input. + // It's disruptive to the user to immediately present their input as an error. + // We should only present an error if they fail the regex test :) + // (Also this change makes the fallback "undefined" instead of #fff so this is required) + const validInput = (testColor: string | undefined) => { + if (!testColor) return true; + return colorPattern.test(testColor); + }; + const handleUpdate = (newColor: string) => { let sanitized = stripQuotes(newColor); sanitized = sanitized.startsWith('#') ? sanitized : `#${sanitized}`; setTempColor(sanitized); - const currentSanitized = stripQuotes(current) || '#FFFFFF'; + const currentSanitized = stripQuotes(current) || '#ffffff'; setHasChanged(sanitized.toUpperCase() !== currentSanitized.toUpperCase()); }; const handleSave = () => { - if (/^#[0-9A-F]{6}$/i.test(tempColor)) { + if (!!onSave && !!tempColor && colorPattern.test(tempColor)) { onSave(tempColor); setHasChanged(false); } }; const handleReset = () => { - onSave(null); + if (onChange) onChange(null); + if (onSave) onSave(null); setHasChanged(false); - setTempColor('#FFFFFF'); + setTempColor(undefined); }; + useEffect(() => { + const validColor = tempColor !== undefined && colorPattern.test(tempColor); + if (onChange) onChange(validColor ? tempColor : null); + }, [onChange, tempColor]); + return ( - {hasChanged && ( + {onSave && hasChanged && ( )} } + picker={} > {(onOpen, opened) => (