From 7cd09e9e94c7213fc8d416d3b41d66459ea59c58 Mon Sep 17 00:00:00 2001 From: Josie Date: Sun, 9 Aug 2026 16:15:10 +0100 Subject: [PATCH] Move usePersonaCosmetics + add name color lightness correction --- src/app/components/user-profile/UserHero.tsx | 4 +- .../user-profile/UserRoomProfile.tsx | 4 +- .../room/persona-picker/PersonaPicker.tsx | 36 ++---------------- src/app/hooks/usePerMessageProfile.ts | 37 +++++++++++++++++++ 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/app/components/user-profile/UserHero.tsx b/src/app/components/user-profile/UserHero.tsx index 7e766b966..7f9b421c9 100644 --- a/src/app/components/user-profile/UserHero.tsx +++ b/src/app/components/user-profile/UserHero.tsx @@ -44,8 +44,8 @@ import { CopyIcon, CrossIcon } from '@phosphor-icons/react'; import { useOpenSettings } from '$features/settings'; import { ModalOverlay } from '$components/modal-overlay/ModalOverlay'; import type { Persona } from '$app/persona'; -import { useProfileCosmetics } from '$features/room/persona-picker/PersonaPicker'; import type { MatrixClient } from 'matrix-js-sdk'; +import { usePersonaCosmetics } from '$hooks/usePerMessageProfile'; type UserHeroProps = { userId: string; @@ -381,7 +381,7 @@ export function UserHeroName({ const nick = useNickname(userId); // personas - const { nameColor: getPmpNameColor } = useProfileCosmetics(mx); + const { nameColor: getPmpNameColor } = usePersonaCosmetics(mx); const pmpNameColor = pmp?.['eu.she-a.color'] ? getPmpNameColor?.(pmp) : null; // Sable username color and fonts diff --git a/src/app/components/user-profile/UserRoomProfile.tsx b/src/app/components/user-profile/UserRoomProfile.tsx index 8e582b331..0ef7fef1d 100644 --- a/src/app/components/user-profile/UserRoomProfile.tsx +++ b/src/app/components/user-profile/UserRoomProfile.tsx @@ -63,7 +63,7 @@ import { useRoomMemberHydration } from '$hooks/useRoomMemberHydration'; import * as css from './styles.css'; import * as prefix from '$unstable/prefixes'; import type { Persona } from '$app/persona'; -import { useProfileCosmetics } from '$features/room/persona-picker/PersonaPicker'; +import { usePersonaCosmetics } from '$hooks/usePerMessageProfile'; const KNOWN_KEYS = new Set([ prefix.MATRIX_SABLE_UNSTABLE_PROFILE_BIOGRAPHY_PROPERTY_NAME, @@ -463,7 +463,7 @@ export function UserRoomProfile({ useRoomMemberHydration(room, userId); const [pmp, setPmp] = useState(initialPmp); - const { avatarUrl: getPmpAvatarUrl } = useProfileCosmetics(mx); + const { avatarUrl: getPmpAvatarUrl } = usePersonaCosmetics(mx); const pmpAvatarUrl = pmp?.avatar_url ? getPmpAvatarUrl?.(pmp) : null; const handleClearPmp = () => { diff --git a/src/app/features/room/persona-picker/PersonaPicker.tsx b/src/app/features/room/persona-picker/PersonaPicker.tsx index 9bacf8f49..4807af0f0 100644 --- a/src/app/features/room/persona-picker/PersonaPicker.tsx +++ b/src/app/features/room/persona-picker/PersonaPicker.tsx @@ -6,10 +6,8 @@ import { } from '$components/icons/phosphor'; import { ResponsiveMenu } from '$components/ResponsiveMenu'; import { UserAvatar } from '$components/user-avatar/UserAvatar.tsx'; -import { useMediaAuthentication } from '$hooks/useMediaAuthentication.ts'; import type { PerMessageProfileMsc4461, Persona } from '$app/persona'; import { ProfileCatalog } from '$app/persona/catalog'; -import { mxcUrlToHttp } from '$utils/matrix.ts'; import { isMobileOrTablet } from '$utils/platform'; import { nameInitials } from '$utils/common'; import { @@ -40,7 +38,7 @@ import { 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'; +import { usePersonaCosmetics } from '$hooks/usePerMessageProfile.ts'; const pillStyles = { cursor: 'pointer', @@ -78,34 +76,6 @@ type TemporaryPersonaPickerProps = { anchor?: RectCords; }; -export function useProfileCosmetics(mx: MatrixClient | undefined) { - const useAuthentication = useMediaAuthentication(); - const activeTheme = useActiveTheme(); - - const nameColor = useCallback( - (persona: PerMessageProfileMsc4461) => - activeTheme.kind === ThemeKind.Dark - ? persona['eu.she-a.color']?.on_dark - : persona['eu.she-a.color']?.on_light, - [activeTheme] - ); - - const avatarUrl = useCallback( - (profile: PerMessageProfileMsc4461) => { - if (profile.avatar_url !== undefined) { - return ( - mxcUrlToHttp(mx!, profile.avatar_url, useAuthentication, 96, 96, 'crop') ?? undefined - ); - } else { - return undefined; - } - }, - [mx, useAuthentication] - ); - - if (!mx) return { avatarUrl: undefined, nameColor: undefined }; - return { nameColor, avatarUrl }; -} function useProfiles( mx: MatrixClient, mountedRef: MutableRefObject, @@ -314,7 +284,7 @@ export function PersonaPicker({ return persona.id === selectedPersona?.id ? true : undefined; }; - const { nameColor, avatarUrl } = useProfileCosmetics(mx); + const { nameColor, avatarUrl } = usePersonaCosmetics(mx); const defactoPersona = () => selectedRoomPersona ?? selectedGlobalPersona; @@ -528,7 +498,7 @@ export function TemporaryPersonaPicker({ profileFetchGenerationRef ); - const { nameColor, avatarUrl } = useProfileCosmetics(mx); + const { nameColor, avatarUrl } = usePersonaCosmetics(mx); const scrollRef = useRef(null); diff --git a/src/app/hooks/usePerMessageProfile.ts b/src/app/hooks/usePerMessageProfile.ts index b6d881f3a..280308ef6 100644 --- a/src/app/hooks/usePerMessageProfile.ts +++ b/src/app/hooks/usePerMessageProfile.ts @@ -10,6 +10,11 @@ import { resolvePersonaProxy } from '$app/persona/proxy'; import { resolvePersona } from '$app/persona/selection'; import { ProfileCatalog } from '$app/persona/catalog'; import type { PerMessageProfileMsc4461, Persona } from '$app/persona'; +import { useMediaAuthentication } from './useMediaAuthentication'; +import { mxcUrlToHttp } from '$app/utils/mediaUrl'; +import { useCallback } from 'react'; +import { useAccessibleNameColor } from './useAccessibleNameColor'; +import { useActiveTheme, ThemeKind } from './useTheme'; // Compatibility exports for existing callers while persona persistence lives in the catalog. export * from '$app/persona/catalog'; @@ -87,3 +92,35 @@ export async function getCurrentlyUsedPerMessageProfileForAccount( ): Promise { return (await new ProfileCatalog(mx).getSelection('account'))?.persona; } + +export function usePersonaCosmetics(mx: MatrixClient | undefined) { + const useAuthentication = useMediaAuthentication(); + const activeTheme = useActiveTheme(); + const accessibleNameColor = useAccessibleNameColor(activeTheme.kind); + + const nameColor = useCallback( + (persona: PerMessageProfileMsc4461) => + accessibleNameColor( + activeTheme.kind === ThemeKind.Dark + ? persona['eu.she-a.color']?.on_dark + : persona['eu.she-a.color']?.on_light + ), + [activeTheme, accessibleNameColor] + ); + + const avatarUrl = useCallback( + (profile: PerMessageProfileMsc4461) => { + if (profile.avatar_url !== undefined) { + return ( + mxcUrlToHttp(mx!, profile.avatar_url, useAuthentication, 96, 96, 'crop') ?? undefined + ); + } else { + return undefined; + } + }, + [mx, useAuthentication] + ); + + if (!mx) return { avatarUrl: undefined, nameColor: undefined }; + return { nameColor, avatarUrl }; +}