Add useRelationship hook, disable fetching relationship for account by default
This commit is contained in:
parent
ec8177d578
commit
072014e39e
5 changed files with 54 additions and 13 deletions
|
@ -3,10 +3,15 @@ import { useEntity } from 'soapbox/entity-store/hooks';
|
||||||
import { useApi } from 'soapbox/hooks/useApi';
|
import { useApi } from 'soapbox/hooks/useApi';
|
||||||
import { type Account, accountSchema } from 'soapbox/schemas';
|
import { type Account, accountSchema } from 'soapbox/schemas';
|
||||||
|
|
||||||
import { useRelationships } from './useRelationships';
|
import { useRelationship } from './useRelationship';
|
||||||
|
|
||||||
function useAccount(accountId?: string) {
|
interface UseAccountOpts {
|
||||||
|
withRelationship?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
function useAccount(accountId?: string, opts: UseAccountOpts = {}) {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
|
const { withRelationship } = opts;
|
||||||
|
|
||||||
const { entity: account, ...result } = useEntity<Account>(
|
const { entity: account, ...result } = useEntity<Account>(
|
||||||
[Entities.ACCOUNTS, accountId!],
|
[Entities.ACCOUNTS, accountId!],
|
||||||
|
@ -15,15 +20,15 @@ function useAccount(accountId?: string) {
|
||||||
);
|
);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
relationships,
|
relationship,
|
||||||
isLoading: isRelationshipLoading,
|
isLoading: isRelationshipLoading,
|
||||||
} = useRelationships(accountId ? [accountId] : []);
|
} = useRelationship(accountId, { enabled: withRelationship });
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...result,
|
...result,
|
||||||
isLoading: result.isLoading,
|
isLoading: result.isLoading,
|
||||||
isRelationshipLoading,
|
isRelationshipLoading,
|
||||||
account: account ? { ...account, relationship: relationships[0] || null } : undefined,
|
account: account ? { ...account, relationship } : undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,15 @@ import { useEntityLookup } from 'soapbox/entity-store/hooks';
|
||||||
import { useApi } from 'soapbox/hooks/useApi';
|
import { useApi } from 'soapbox/hooks/useApi';
|
||||||
import { type Account, accountSchema } from 'soapbox/schemas';
|
import { type Account, accountSchema } from 'soapbox/schemas';
|
||||||
|
|
||||||
import { useRelationships } from './useRelationships';
|
import { useRelationship } from './useRelationship';
|
||||||
|
|
||||||
function useAccountLookup(acct?: string) {
|
interface UseAccountLookupOpts {
|
||||||
|
withRelationship?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
function useAccountLookup(acct: string | undefined, opts: UseAccountLookupOpts = {}) {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
|
const { withRelationship } = opts;
|
||||||
|
|
||||||
const { entity: account, ...result } = useEntityLookup<Account>(
|
const { entity: account, ...result } = useEntityLookup<Account>(
|
||||||
Entities.ACCOUNTS,
|
Entities.ACCOUNTS,
|
||||||
|
@ -16,15 +21,15 @@ function useAccountLookup(acct?: string) {
|
||||||
);
|
);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
relationships,
|
relationship,
|
||||||
isLoading: isRelationshipLoading,
|
isLoading: isRelationshipLoading,
|
||||||
} = useRelationships(account ? [account.id] : []);
|
} = useRelationship(account?.id, { enabled: withRelationship });
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...result,
|
...result,
|
||||||
isLoading: result.isLoading,
|
isLoading: result.isLoading,
|
||||||
isRelationshipLoading,
|
isRelationshipLoading,
|
||||||
account: account ? { ...account, relationship: relationships[0] || null } : undefined,
|
account: account ? { ...account, relationship } : undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
28
app/soapbox/api/hooks/accounts/useRelationship.ts
Normal file
28
app/soapbox/api/hooks/accounts/useRelationship.ts
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import { Entities } from 'soapbox/entity-store/entities';
|
||||||
|
import { useEntity } from 'soapbox/entity-store/hooks';
|
||||||
|
import { useApi } from 'soapbox/hooks';
|
||||||
|
import { type Relationship, relationshipSchema } from 'soapbox/schemas';
|
||||||
|
|
||||||
|
interface UseRelationshipOpts {
|
||||||
|
enabled?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
function useRelationship(accountId: string | undefined, opts: UseRelationshipOpts = {}) {
|
||||||
|
const api = useApi();
|
||||||
|
const { enabled = false } = opts;
|
||||||
|
|
||||||
|
const { entity: relationship, ...result } = useEntity<Relationship>(
|
||||||
|
[Entities.RELATIONSHIPS, accountId!],
|
||||||
|
() => api.get(`/api/v1/accounts/relationships?id[]=${accountId}`),
|
||||||
|
{
|
||||||
|
enabled: enabled && !!accountId,
|
||||||
|
schema: z.array(relationshipSchema).nonempty().transform(arr => arr[0]),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return { relationship, ...result };
|
||||||
|
}
|
||||||
|
|
||||||
|
export { useRelationship };
|
|
@ -16,7 +16,7 @@ function useGroupRelationship(groupId: string | undefined) {
|
||||||
() => api.get(`/api/v1/groups/relationships?id[]=${groupId}`),
|
() => api.get(`/api/v1/groups/relationships?id[]=${groupId}`),
|
||||||
{
|
{
|
||||||
enabled: !!groupId,
|
enabled: !!groupId,
|
||||||
schema: z.array(groupRelationshipSchema).transform(arr => arr[0]),
|
schema: z.array(groupRelationshipSchema).nonempty().transform(arr => arr[0]),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,10 @@ import { Card, CardBody, HStack, Icon, Stack, Text } from './ui';
|
||||||
import type { Account, PatronUser } from 'soapbox/schemas';
|
import type { Account, PatronUser } from 'soapbox/schemas';
|
||||||
import type { AppDispatch } from 'soapbox/store';
|
import type { AppDispatch } from 'soapbox/store';
|
||||||
|
|
||||||
const getBadges = (account?: Account, patronUser?: PatronUser): JSX.Element[] => {
|
const getBadges = (
|
||||||
|
account?: Pick<Account, 'admin' | 'moderator'>,
|
||||||
|
patronUser?: Pick<PatronUser, 'is_patron'>,
|
||||||
|
): JSX.Element[] => {
|
||||||
const badges = [];
|
const badges = [];
|
||||||
|
|
||||||
if (account?.admin) {
|
if (account?.admin) {
|
||||||
|
@ -65,7 +68,7 @@ export const ProfileHoverCard: React.FC<IProfileHoverCard> = ({ visible = true }
|
||||||
|
|
||||||
const me = useAppSelector(state => state.me);
|
const me = useAppSelector(state => state.me);
|
||||||
const accountId: string | undefined = useAppSelector(state => state.profile_hover_card.accountId || undefined);
|
const accountId: string | undefined = useAppSelector(state => state.profile_hover_card.accountId || undefined);
|
||||||
const { account } = useAccount(accountId);
|
const { account } = useAccount(accountId, { withRelationship: true });
|
||||||
const { patronUser } = usePatronUser(account?.url);
|
const { patronUser } = usePatronUser(account?.url);
|
||||||
const targetRef = useAppSelector(state => state.profile_hover_card.ref?.current);
|
const targetRef = useAppSelector(state => state.profile_hover_card.ref?.current);
|
||||||
const badges = getBadges(account, patronUser);
|
const badges = getBadges(account, patronUser);
|
||||||
|
|
Loading…
Reference in a new issue