Cache profile data
This commit is contained in:
1 parent
ac39469eee
commit
defff8c487
5 files changed
+110
-66
No files matched your search
@@ -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<HTMLElement>) => {
|
||||
e.preventDefault();
|
||||
|
||||
@@ -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<string>();
|
||||
|
||||
const [profileCache, setProfileCache] = useState<Record<string, any>>({});
|
||||
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<string>();
|
||||
@@ -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]
|
||||
|
||||
@@ -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<string, any>; // any other fields
|
||||
pronouns?: any[];
|
||||
timezone?: string;
|
||||
bio?: string;
|
||||
extended?: Record<string, any>;
|
||||
};
|
||||
|
||||
export const useUserProfile = (userId: string): UserProfile => {
|
||||
export const useUserProfile = (userId: string, initialProfile?: Partial<UserProfile>): 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<UserProfile>(() => {
|
||||
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;
|
||||
};
|
||||
};
|
||||
@@ -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<UserProfile>
|
||||
) => 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;
|
||||
};
|
||||
};
|
||||
@@ -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<UserProfile>;
|
||||
};
|
||||
|
||||
export const userRoomProfileAtom = atom<UserRoomProfileState | undefined>(undefined);
|
||||
export const userRoomProfileAtom = atom<UserRoomProfileState | undefined>(undefined);
|
||||
|
||||
export const profilesCacheAtom = atom<Record<string, UserProfile>>({});
|
||||
Reference in new issue
Block a user