Move usePersonaCosmetics + add name color lightness correction

This commit is contained in:
Josie committed 2026-08-09 16:16:54 +01:00
1 parent aa86c1a87b
commit 7cd09e9e94
4 files changed
+44 -37

No files matched your search

+2 -2
View File
@@ -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
@@ -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 = () => {
@@ -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<boolean>,
@@ -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<HTMLDivElement>(null);
+37
View File
@@ -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<PerMessageProfileMsc4461 | undefined> {
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 };
}