diff --git a/src/app/components/user-profile/UserRoomProfile.tsx b/src/app/components/user-profile/UserRoomProfile.tsx index ef234a01..e92763a8 100644 --- a/src/app/components/user-profile/UserRoomProfile.tsx +++ b/src/app/components/user-profile/UserRoomProfile.tsx @@ -30,6 +30,7 @@ import { UserProfile, useUserProfile } from '../../hooks/useUserProfile'; import { RenderBody } from '../message'; import { factoryRenderLinkifyWithMention, getReactCustomHtmlParser, LINKIFY_OPTS, makeMentionCustomProps, renderMatrixMention } from '../../plugins/react-custom-html-parser'; import { useSpoilerClickHandler } from '../../hooks/useSpoilerClickHandler'; +import { userRoomProfileAtom } from '../../state/userRoomProfile'; type UserExtendedSectionProps = { profile: UserProfile; @@ -180,6 +181,8 @@ export function UserRoomProfile({ userId }: UserRoomProfileProps) { const presence = useUserPresence(userId); + const extendedProfile = useUserProfile(userId); + const handleMessage = () => { closeUserRoomProfile(); const directSearchParam: DirectCreateSearchParams = { @@ -188,8 +191,6 @@ export function UserRoomProfile({ userId }: UserRoomProfileProps) { navigate(withSearchParam(getDirectCreatePath(), directSearchParam)); }; - const extendedProfile = useUserProfile(userId); - // Todo eventually maybe const mentionClickHandler = useCallback((e: React.SyntheticEvent) => { e.preventDefault(); diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index 315207b5..7c2f046f 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -29,7 +29,7 @@ import classNames from 'classnames'; import { ReactEditor } from 'slate-react'; import { Editor } from 'slate'; import to from 'await-to-js'; -import { useAtomValue, useSetAtom } from 'jotai'; +import { useAtom, useAtomValue, useSetAtom } from 'jotai'; import { as, Badge, Box, Chip, color, config, ContainerColor, Icon, Icons, Line, Scroll, Text, toRem, } from 'folds'; import { isKeyHotkey } from 'is-hotkey'; import { Opts as LinkifyOpts } from 'linkifyjs'; @@ -107,6 +107,8 @@ import { useSpaceOptionally } from '../../hooks/useSpace'; import { useRoomCreators } from '../../hooks/useRoomCreators'; import { useRoomPermissions } from '../../hooks/useRoomPermissions'; import { useGetMemberPowerTag } from '../../hooks/useMemberPowerTag'; +import { profilesCacheAtom } from '../../state/userRoomProfile'; +import { UserProfile } from '../../hooks/useUserProfile'; const TimelineFloat = as<'div', css.TimelineFloatVariants>( ({ position, className, ...props }, ref) => ( @@ -443,7 +445,9 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli const canSendReaction = permissions.event(MessageEvent.Reaction, mx.getSafeUserId()); const canPinEvent = permissions.stateEvent(StateEvent.RoomPinnedEvents, mx.getSafeUserId()); const [editId, setEditId] = useState(); + const [profileCache, setProfileCache] = useState>({}); + const [globalProfiles, setGlobalProfiles] = useAtom(profilesCacheAtom); const roomToParents = useAtomValue(roomToParentsAtom); const unread = useRoomUnread(room.roomId, roomToUnreadAtom); @@ -902,7 +906,6 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli } }, [scrollToElement, editId]); - // fetch user profiles, specifically pronouns useEffect(() => { const visibleIndices = getItems(); const sendersToFetch = new Set(); @@ -921,14 +924,28 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli setProfileCache(prev => ({ ...prev, [userId]: { loading: true } })); const [err, info] = await to(mx.getProfileInfo(userId)); - if (err || !info) return; + + if (err || !info) { + setProfileCache(prev => { + const newState = { ...prev }; + delete newState[userId]; + return newState; + }); + return; + } + + const normalized: UserProfile = { + avatarUrl: info.avatar_url, + displayName: info.displayname, + pronouns: (info as any)['io.fsky.nyx.pronouns'], + timezone: (info as any)['us.cloke.msc4175.tz'] || (info as any)['m.tz'], + bio: (info as any)['moe.sable.app.bio'], + extended: info, + }; setProfileCache((prev) => ({ ...prev, - [userId]: { - pronouns: (info as any)['io.fsky.nyx.pronouns'], - loading: false - }, + [userId]: normalized, })); }); }, [timeline.range, timeline.linkedTimelines, mx, profileCache, getItems]); @@ -967,16 +984,22 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli evt.preventDefault(); evt.stopPropagation(); const userId = evt.currentTarget.getAttribute('data-user-id'); - if (!userId) { - throw new Error('Button should have "data-user-id" attribute!'); - } + if (!userId) return; + + const cachedData = profileCache[userId]; openUserRoomProfile( room.roomId, space?.roomId, userId, evt.currentTarget.getBoundingClientRect(), - profileCache[userId]?.pronouns + undefined, + { + pronouns: cachedData?.pronouns, + bio: cachedData?.bio, + timezone: cachedData?.timezone, + extended: cachedData?.extended, + } ); }, [room, space, openUserRoomProfile, profileCache] diff --git a/src/app/hooks/useUserProfile.ts b/src/app/hooks/useUserProfile.ts index 2070055f..97f9f1a0 100644 --- a/src/app/hooks/useUserProfile.ts +++ b/src/app/hooks/useUserProfile.ts @@ -1,70 +1,78 @@ -import { useEffect, useState } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { UserEvent, UserEventHandlerMap } from 'matrix-js-sdk'; +import { useAtom } from 'jotai'; import { useMatrixClient } from './useMatrixClient'; +import { profilesCacheAtom } from '../state/userRoomProfile'; export type UserProfile = { avatarUrl?: string; displayName?: string; - - pronouns?: any[]; // io.fsky.nyx.pronouns - timezone?: string; // us.cloke.msc4175.tz - bio?: string; // moe.sable.app.bio - extended?: Record; // any other fields + pronouns?: any[]; + timezone?: string; + bio?: string; + extended?: Record; }; -export const useUserProfile = (userId: string): UserProfile => { +export const useUserProfile = (userId: string, initialProfile?: Partial): UserProfile => { const mx = useMatrixClient(); + const [globalProfiles, setGlobalProfiles] = useAtom(profilesCacheAtom); + + const normalizeInfo = useCallback((info: any): UserProfile => { + const normalized: UserProfile = { + avatarUrl: info.avatar_url, + displayName: info.displayname, + pronouns: info['io.fsky.nyx.pronouns'], + timezone: info['us.cloke.msc4175.tz'] || info['m.tz'], + bio: info['moe.sable.app.bio'], + extended: {}, + }; + + const knownKeys = [ + 'avatar_url', + 'displayname', + 'io.fsky.nyx.pronouns', + 'us.cloke.msc4175.tz', 'm.tz', + 'moe.sable.app.bio' + ]; + + Object.keys(info).forEach((key) => { + if (!knownKeys.includes(key)) { + normalized.extended![key] = info[key]; + } + }); + + return normalized; + }, []); const [profile, setProfile] = useState(() => { const user = mx.getUser(userId); + const cached = globalProfiles[userId]; return { - avatarUrl: user?.avatarUrl, - displayName: user?.displayName, + avatarUrl: cached?.avatarUrl ?? initialProfile?.avatarUrl ?? user?.avatarUrl, + displayName: cached?.displayName ?? initialProfile?.displayName ?? user?.displayName, + pronouns: cached?.pronouns ?? initialProfile?.pronouns, + timezone: cached?.timezone ?? initialProfile?.timezone, + bio: cached?.bio ?? initialProfile?.bio, + extended: cached?.extended ?? initialProfile?.extended, }; }); useEffect(() => { const user = mx.getUser(userId); - const onAvatarChange: UserEventHandlerMap[UserEvent.AvatarUrl] = (event, myUser) => { - setProfile((cp) => ({ - ...cp, - avatarUrl: myUser.avatarUrl, - })); - }; - const onDisplayNameChange: UserEventHandlerMap[UserEvent.DisplayName] = (event, myUser) => { - setProfile((cp) => ({ - ...cp, - displayName: myUser.displayName, - })); - }; mx.getProfileInfo(userId).then((info: any) => { - const normalized: UserProfile = { - avatarUrl: info.avatar_url, - displayName: info.displayname, - pronouns: info['io.fsky.nyx.pronouns'], - timezone: info['us.cloke.msc4175.tz'] || info['m.tz'], - bio: info['moe.sable.app.bio'], - extended: {}, - }; - - const knownKeys = [ - 'avatar_url', - 'displayname', - 'io.fsky.nyx.pronouns', - 'us.cloke.msc4175.tz', - 'm.tz', - 'moe.sable.app.bio' - ]; - Object.keys(info).forEach((key) => { - if (!knownKeys.includes(key)) { - normalized.extended![key] = info[key]; - } - }); - + const normalized = normalizeInfo(info); setProfile(normalized); + setGlobalProfiles((prev) => ({ ...prev, [userId]: normalized })); }); + const onAvatarChange: UserEventHandlerMap[UserEvent.AvatarUrl] = (event, myUser) => { + setProfile((cp) => ({ ...cp, avatarUrl: myUser.avatarUrl })); + }; + const onDisplayNameChange: UserEventHandlerMap[UserEvent.DisplayName] = (event, myUser) => { + setProfile((cp) => ({ ...cp, displayName: myUser.displayName })); + }; + user?.on(UserEvent.AvatarUrl, onAvatarChange); user?.on(UserEvent.DisplayName, onDisplayNameChange); @@ -72,7 +80,7 @@ export const useUserProfile = (userId: string): UserProfile => { user?.removeListener(UserEvent.AvatarUrl, onAvatarChange); user?.removeListener(UserEvent.DisplayName, onDisplayNameChange); }; - }, [mx, userId]); + }, [mx, userId, normalizeInfo, setGlobalProfiles]); return profile; -}; +}; \ No newline at end of file diff --git a/src/app/state/hooks/userRoomProfile.ts b/src/app/state/hooks/userRoomProfile.ts index 7fed0c08..64872f17 100644 --- a/src/app/state/hooks/userRoomProfile.ts +++ b/src/app/state/hooks/userRoomProfile.ts @@ -2,10 +2,10 @@ import { useCallback } from 'react'; import { useAtomValue, useSetAtom } from 'jotai'; import { Position, RectCords } from 'folds'; import { userRoomProfileAtom, UserRoomProfileState } from '../userRoomProfile'; +import { UserProfile } from '../../hooks/useUserProfile'; export const useUserRoomProfileState = (): UserRoomProfileState | undefined => { const data = useAtomValue(userRoomProfileAtom); - return data; }; @@ -25,17 +25,26 @@ type OpenCallback = ( spaceId: string | undefined, userId: string, cords: RectCords, - position?: Position + position?: Position, + initialProfile?: Partial ) => void; + export const useOpenUserRoomProfile = (): OpenCallback => { const setUserRoomProfile = useSetAtom(userRoomProfileAtom); const open: OpenCallback = useCallback( - (roomId, spaceId, userId, cords, position) => { - setUserRoomProfile({ roomId, spaceId, userId, cords, position }); + (roomId, spaceId, userId, cords, position, initialProfile) => { + setUserRoomProfile({ + roomId, + spaceId, + userId, + cords, + position, + initialProfile + }); }, [setUserRoomProfile] ); return open; -}; +}; \ No newline at end of file diff --git a/src/app/state/userRoomProfile.ts b/src/app/state/userRoomProfile.ts index 47d49cba..58f41067 100644 --- a/src/app/state/userRoomProfile.ts +++ b/src/app/state/userRoomProfile.ts @@ -1,5 +1,6 @@ import { Position, RectCords } from 'folds'; import { atom } from 'jotai'; +import { UserProfile } from '../hooks/useUserProfile'; export type UserRoomProfileState = { userId: string; @@ -7,7 +8,9 @@ export type UserRoomProfileState = { spaceId?: string; cords: RectCords; position?: Position; - pronouns?: any[]; + initialProfile?: Partial; }; -export const userRoomProfileAtom = atom(undefined); \ No newline at end of file +export const userRoomProfileAtom = atom(undefined); + +export const profilesCacheAtom = atom>({}); \ No newline at end of file