From ca667fe891446f9f70530397adcbc7368e069a83 Mon Sep 17 00:00:00 2001 From: mkljczk Date: Sat, 15 Mar 2025 11:50:32 +0100 Subject: [PATCH] pl-fe: separate scrobble query from account query Signed-off-by: mkljczk --- .../src/api/hooks/accounts/use-account-lookup.ts | 5 ++--- .../pl-fe/src/api/hooks/accounts/use-account.ts | 14 ++------------ .../pl-fe/src/components/account-hover-card.tsx | 8 ++++---- .../ui/components/panels/profile-info-panel.tsx | 8 +++++--- packages/pl-fe/src/layouts/profile-layout.tsx | 2 +- .../src/queries/accounts/use-account-scrobble.ts | 9 ++------- .../pl-hooks/lib/hooks/accounts/use-account.ts | 1 - 7 files changed, 16 insertions(+), 31 deletions(-) diff --git a/packages/pl-fe/src/api/hooks/accounts/use-account-lookup.ts b/packages/pl-fe/src/api/hooks/accounts/use-account-lookup.ts index 86abdc638..6b6619d74 100644 --- a/packages/pl-fe/src/api/hooks/accounts/use-account-lookup.ts +++ b/packages/pl-fe/src/api/hooks/accounts/use-account-lookup.ts @@ -16,7 +16,6 @@ import type { Account as BaseAccount } from 'pl-api'; interface UseAccountLookupOpts { withRelationship?: boolean; - withScrobble?: boolean; } const useAccountLookup = (acct: string | undefined, opts: UseAccountLookupOpts = {}) => { @@ -24,7 +23,7 @@ const useAccountLookup = (acct: string | undefined, opts: UseAccountLookupOpts = const features = useFeatures(); const history = useHistory(); const { me } = useLoggedIn(); - const { withRelationship, withScrobble } = opts; + const { withRelationship } = opts; const { entity: account, isUnauthorized, ...result } = useEntityLookup( Entities.ACCOUNTS, @@ -41,7 +40,7 @@ const useAccountLookup = (acct: string | undefined, opts: UseAccountLookupOpts = const { scrobble, isLoading: isScrobbleLoading, - } = useAccountScrobble(account?.id, { enabled: withScrobble }); + } = useAccountScrobble(account?.id); const isBlocked = account?.relationship?.blocked_by === true; const isUnavailable = (me === account?.id) ? false : (isBlocked && !features.blockersVisible); diff --git a/packages/pl-fe/src/api/hooks/accounts/use-account.ts b/packages/pl-fe/src/api/hooks/accounts/use-account.ts index 8d88f15b5..b2397dc75 100644 --- a/packages/pl-fe/src/api/hooks/accounts/use-account.ts +++ b/packages/pl-fe/src/api/hooks/accounts/use-account.ts @@ -9,15 +9,12 @@ import { useFeatures } from 'pl-fe/hooks/use-features'; import { useLoggedIn } from 'pl-fe/hooks/use-logged-in'; import { type Account, normalizeAccount } from 'pl-fe/normalizers/account'; -import { useAccountScrobble } from '../../../queries/accounts/use-account-scrobble'; - import { useRelationship } from './use-relationship'; import type { Account as BaseAccount } from 'pl-api'; interface UseAccountOpts { withRelationship?: boolean; - withScrobble?: boolean; } const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => { @@ -25,7 +22,7 @@ const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => { const history = useHistory(); const features = useFeatures(); const { me } = useLoggedIn(); - const { withRelationship, withScrobble } = opts; + const { withRelationship } = opts; const { entity, isUnauthorized, ...result } = useEntity( [Entities.ACCOUNTS, accountId!], @@ -40,11 +37,6 @@ const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => { isLoading: isRelationshipLoading, } = useRelationship(accountId, { enabled: withRelationship }); - const { - scrobble, - isLoading: isScrobbleLoading, - } = useAccountScrobble(accountId, { enabled: withScrobble }); - const isBlocked = entity?.relationship?.blocked_by === true; const isUnavailable = (me === entity?.id) ? false : (isBlocked && !features.blockersVisible); @@ -52,12 +44,11 @@ const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => { () => entity ? { ...entity, relationship, - scrobble, __meta: { meta, ...entity.__meta }, // @ts-ignore is_admin: meta?.role ? (meta.role.permissions & 0x1) === 0x1 : entity.is_admin, } : undefined, - [entity, relationship, scrobble], + [entity, relationship], ); useEffect(() => { @@ -70,7 +61,6 @@ const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => { ...result, isLoading: result.isLoading, isRelationshipLoading, - isScrobbleLoading, isUnauthorized, isUnavailable, account, diff --git a/packages/pl-fe/src/components/account-hover-card.tsx b/packages/pl-fe/src/components/account-hover-card.tsx index 3a2570d13..a9c93f4fd 100644 --- a/packages/pl-fe/src/components/account-hover-card.tsx +++ b/packages/pl-fe/src/components/account-hover-card.tsx @@ -16,6 +16,7 @@ import ActionButton from 'pl-fe/features/ui/components/action-button'; import { UserPanel } from 'pl-fe/features/ui/util/async-components'; import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch'; import { useAppSelector } from 'pl-fe/hooks/use-app-selector'; +import { useAccountScrobble } from 'pl-fe/queries/accounts/use-account-scrobble'; import { useAccountHoverCardStore } from 'pl-fe/stores/account-hover-card'; import { showAccountHoverCard } from './hover-account-wrapper'; @@ -52,7 +53,8 @@ const AccountHoverCard: React.FC = ({ visible = true }) => { const { accountId, ref, updateAccountHoverCard, closeAccountHoverCard } = useAccountHoverCardStore(); const me = useAppSelector(state => state.me); - const { account } = useAccount(accountId || undefined, { withRelationship: true, withScrobble: true }); + const { account } = useAccount(accountId || undefined, { withRelationship: true }); + const { scrobble } = useAccountScrobble(accountId || undefined); const badges = getBadges(account); useEffect(() => { @@ -151,9 +153,7 @@ const AccountHoverCard: React.FC = ({ visible = true }) => { ) : null} - {!!account.scrobble && ( - - )} + {!!scrobble && } {account.note.length > 0 && ( = ({ account, username }) => const me = useAppSelector(state => state.me); const ownAccount = account?.id === me; + const { scrobble } = useAccountScrobble(account?.id); + const getStaffBadge = (): React.ReactNode => { if (account?.is_admin) { return } key='staff' />; @@ -203,7 +205,7 @@ const ProfileInfoPanel: React.FC = ({ account, username }) => {renderBirthday()} - {account.scrobble && } + {scrobble && } {ownAccount ? null : } diff --git a/packages/pl-fe/src/layouts/profile-layout.tsx b/packages/pl-fe/src/layouts/profile-layout.tsx index 3a1715709..807b5c5b5 100644 --- a/packages/pl-fe/src/layouts/profile-layout.tsx +++ b/packages/pl-fe/src/layouts/profile-layout.tsx @@ -35,7 +35,7 @@ const ProfileLayout: React.FC = ({ params, children }) => { const history = useHistory(); const username = params?.username || ''; - const { account } = useAccountLookup(username, { withRelationship: true, withScrobble: true }); + const { account } = useAccountLookup(username, { withRelationship: true }); const me = useAppSelector(state => state.me); const features = useFeatures(); diff --git a/packages/pl-fe/src/queries/accounts/use-account-scrobble.ts b/packages/pl-fe/src/queries/accounts/use-account-scrobble.ts index deacc8964..369d83a54 100644 --- a/packages/pl-fe/src/queries/accounts/use-account-scrobble.ts +++ b/packages/pl-fe/src/queries/accounts/use-account-scrobble.ts @@ -5,20 +5,15 @@ import { useFeatures } from 'pl-fe/hooks/use-features'; import type { Scrobble } from 'pl-api'; -interface UseScrobblesOpts { - enabled?: boolean; -} - -const useAccountScrobble = (accountId?: string, opts: UseScrobblesOpts = {}) => { +const useAccountScrobble = (accountId?: string) => { const client = useClient(); const features = useFeatures(); - const { enabled = false } = opts; const { data: scrobble, ...result } = useQuery({ queryKey: ['scrobbles', accountId!], queryFn: async () => (await client.accounts.getScrobbles(accountId!, { limit: 1 })).items[0] || null, placeholderData: undefined, - enabled: enabled && !!accountId && features.scrobbles, + enabled: !!accountId && features.scrobbles, staleTime: 3 * 60 * 1000, }); diff --git a/packages/pl-hooks/lib/hooks/accounts/use-account.ts b/packages/pl-hooks/lib/hooks/accounts/use-account.ts index 980df85ae..f621c9930 100644 --- a/packages/pl-hooks/lib/hooks/accounts/use-account.ts +++ b/packages/pl-hooks/lib/hooks/accounts/use-account.ts @@ -16,7 +16,6 @@ interface Account extends NormalizedAccount { interface UseAccountOpts { withRelationship?: boolean; - withScrobble?: boolean; withMoveTarget?: boolean; }