From 60e9d13d78f56e38dbce9f220b9b29b29e4fe133 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Wed, 23 Aug 2023 19:32:15 +0300 Subject: [PATCH] further reduce profile layout shift --- src/js/components/user/ProfileCard.tsx | 2 +- src/js/components/user/Stats.tsx | 67 ++++++++++++++------------ 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/js/components/user/ProfileCard.tsx b/src/js/components/user/ProfileCard.tsx index 00d5669b8..98ae6f9e3 100644 --- a/src/js/components/user/ProfileCard.tsx +++ b/src/js/components/user/ProfileCard.tsx @@ -162,7 +162,7 @@ const ProfileCard = (props: { hexPub: string; npub: string }) => {
-
{profilePicture}
+
{profilePicture}
diff --git a/src/js/components/user/Stats.tsx b/src/js/components/user/Stats.tsx index 388d24a18..99e56e6bf 100644 --- a/src/js/components/user/Stats.tsx +++ b/src/js/components/user/Stats.tsx @@ -1,7 +1,9 @@ -import { memo, useEffect, useState } from 'react'; +import { memo, useCallback, useEffect, useState } from 'react'; import throttle from 'lodash/throttle'; import { Link } from 'preact-router'; +import { ID, STR } from '@/utils/UniqueIds.ts'; + import Key from '../../nostr/Key'; import SocialNetwork from '../../nostr/SocialNetwork'; import { translate as t } from '../../translations/Translation.mjs'; @@ -14,50 +16,51 @@ const ProfileStats = ({ address }) => { const [followerCount, setFollowerCount] = useState(0); const [followerCountFromApi, setFollowerCountFromApi] = useState(0); const [followedUserCountFromApi, setFollowedUserCountFromApi] = useState(0); - const [knownFollowers, setKnownFollowers] = useState([]); const isMyProfile = Key.isMine(address); - useEffect(() => { - const subscriptions = [] as any[]; + const getKnownFollowers = useCallback(() => { + const followerSet = SocialNetwork.followersByUser.get(ID(address)); + const followers = Array.from(followerSet || new Set()); + return followers + ?.filter((id) => typeof id === 'number' && SocialNetwork.followDistanceByUser.get(id) === 1) + .map((id) => STR(id)); + }, []); + + const [knownFollowers, setKnownFollowers] = useState(getKnownFollowers()); - fetch(`https://eu.rbr.bio/${address}/info.json`).then((res) => { + useEffect(() => { + const fetchData = async () => { + const res = await fetch(`https://eu.rbr.bio/${address}/info.json`); if (!res.ok) { return; } - res.json().then((json) => { - if (json) { - setFollowedUserCountFromApi(json.following?.length); - setFollowerCountFromApi(json.followerCount); - } - }); - }); - - const throttledSetKnownFollowers = throttle((followers) => { - const knownFollowers = new Set(); - for (const follower of followers) { - if (SocialNetwork.getFollowDistance(follower) === 1) { - knownFollowers.add(follower); - } + const json = await res.json(); + if (json) { + setFollowedUserCountFromApi(json.following?.length); + setFollowerCountFromApi(json.followerCount); } - setKnownFollowers(Array.from(knownFollowers)); + }; + + fetchData(); + + const throttledSetKnownFollowers = throttle(() => { + setKnownFollowers(getKnownFollowers()); }, 1000); - setTimeout(() => { - subscriptions.push( - SocialNetwork.getFollowersByUser(address, (followers) => { - setFollowerCount(followers.size); - throttledSetKnownFollowers(followers); - }), - ); - subscriptions.push( - SocialNetwork.getFollowedByUser(address, (followed) => setFollowedUserCount(followed.size)), - ); - }, 1000); // this causes social graph recursive loading, so let some other stuff like feed load first + const subscriptions = [ + SocialNetwork.getFollowersByUser(address, (followers: Set) => { + setFollowerCount(followers.size); + throttledSetKnownFollowers(); + }), + SocialNetwork.getFollowedByUser(address, (followed) => setFollowedUserCount(followed.size)), + ]; + + setTimeout(() => subscriptions.forEach((sub) => sub()), 1000); return () => { subscriptions.forEach((unsub) => unsub()); }; - }, [address]); + }, [address, getKnownFollowers]); return (