From fb848f14847833b02147130a51bfe5d3821713cf Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 20 Jul 2023 16:23:48 -0500 Subject: [PATCH] useAccount: memoize the account --- app/soapbox/api/hooks/accounts/useAccount.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/soapbox/api/hooks/accounts/useAccount.ts b/app/soapbox/api/hooks/accounts/useAccount.ts index 468049441..e7f8b8fea 100644 --- a/app/soapbox/api/hooks/accounts/useAccount.ts +++ b/app/soapbox/api/hooks/accounts/useAccount.ts @@ -1,4 +1,4 @@ -import { useEffect } from 'react'; +import { useEffect, useMemo } from 'react'; import { useHistory } from 'react-router-dom'; import { Entities } from 'soapbox/entity-store/entities'; @@ -20,7 +20,7 @@ function useAccount(accountId?: string, opts: UseAccountOpts = {}) { const { me } = useLoggedIn(); const { withRelationship } = opts; - const { entity: account, isUnauthorized, ...result } = useEntity( + const { entity, isUnauthorized, ...result } = useEntity( [Entities.ACCOUNTS, accountId!], () => api.get(`/api/v1/accounts/${accountId}`), { schema: accountSchema, enabled: !!accountId }, @@ -31,8 +31,13 @@ function useAccount(accountId?: string, opts: UseAccountOpts = {}) { isLoading: isRelationshipLoading, } = useRelationship(accountId, { enabled: withRelationship }); - const isBlocked = account?.relationship?.blocked_by === true; - const isUnavailable = (me === account?.id) ? false : (isBlocked && !features.blockersVisible); + const isBlocked = entity?.relationship?.blocked_by === true; + const isUnavailable = (me === entity?.id) ? false : (isBlocked && !features.blockersVisible); + + const account = useMemo( + () => entity ? { ...entity, relationship } : undefined, + [entity, relationship], + ); useEffect(() => { if (isUnauthorized) { @@ -46,7 +51,7 @@ function useAccount(accountId?: string, opts: UseAccountOpts = {}) { isRelationshipLoading, isUnauthorized, isUnavailable, - account: account ? { ...account, relationship } : undefined, + account, }; }