diff --git a/app/soapbox/api/hooks/accounts/useAccountList.ts b/app/soapbox/api/hooks/accounts/useAccountList.ts new file mode 100644 index 0000000000..cb82153e03 --- /dev/null +++ b/app/soapbox/api/hooks/accounts/useAccountList.ts @@ -0,0 +1,70 @@ +import { Entities } from 'soapbox/entity-store/entities'; +import { useEntities } from 'soapbox/entity-store/hooks'; +import { useApi } from 'soapbox/hooks'; +import { Account, accountSchema } from 'soapbox/schemas'; + +import { useRelationships } from './useRelationships'; + +import type { EntityFn } from 'soapbox/entity-store/hooks/types'; + +interface useAccountListOpts { + enabled?: boolean +} + +function useAccountList(listKey: string[], entityFn: EntityFn, opts: useAccountListOpts = {}) { + const { entities, ...rest } = useEntities( + [Entities.ACCOUNTS, ...listKey], + entityFn, + { schema: accountSchema, enabled: opts.enabled }, + ); + + const { relationships } = useRelationships( + listKey, + entities.map(({ id }) => id), + ); + + const accounts: Account[] = entities.map((account) => ({ + ...account, + relationship: relationships[account.id], + })); + + return { accounts, ...rest }; +} + +function useBlocks() { + const api = useApi(); + return useAccountList(['blocks'], () => api.get('/api/v1/blocks')); +} + +function useMutes() { + const api = useApi(); + return useAccountList(['mutes'], () => api.get('/api/v1/mutes')); +} + +function useFollowing(accountId: string | undefined) { + const api = useApi(); + + return useAccountList( + [accountId!, 'following'], + () => api.get(`/api/v1/accounts/${accountId}/following`), + { enabled: !!accountId }, + ); +} + +function useFollowers(accountId: string | undefined) { + const api = useApi(); + + return useAccountList( + [accountId!, 'followers'], + () => api.get(`/api/v1/accounts/${accountId}/followers`), + { enabled: !!accountId }, + ); +} + +export { + useAccountList, + useBlocks, + useMutes, + useFollowing, + useFollowers, +}; \ No newline at end of file diff --git a/app/soapbox/api/hooks/accounts/useBlocks.ts b/app/soapbox/api/hooks/accounts/useBlocks.ts deleted file mode 100644 index d9867e50c8..0000000000 --- a/app/soapbox/api/hooks/accounts/useBlocks.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Entities } from 'soapbox/entity-store/entities'; -import { useEntities } from 'soapbox/entity-store/hooks'; -import { useApi, useLoggedIn } from 'soapbox/hooks'; -import { Account, accountSchema } from 'soapbox/schemas'; - -import { useRelationships } from './useRelationships'; - -function useBlocks(type: 'blocks' | 'mutes' = 'blocks') { - const api = useApi(); - const { isLoggedIn } = useLoggedIn(); - - const { entities, ...rest } = useEntities( - [Entities.ACCOUNTS, type], - () => api.get(`/api/v1/${type}`), - { schema: accountSchema, enabled: isLoggedIn }, - ); - - const { relationships } = useRelationships( - [type], - entities.map(({ id }) => id), - ); - - const accounts: Account[] = entities.map((account) => ({ - ...account, - relationship: relationships[account.id], - })); - - return { accounts, ...rest }; -} - -export { useBlocks }; \ No newline at end of file diff --git a/app/soapbox/api/hooks/accounts/useFollowing.ts b/app/soapbox/api/hooks/accounts/useFollowing.ts deleted file mode 100644 index 8b5e11851e..0000000000 --- a/app/soapbox/api/hooks/accounts/useFollowing.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Entities } from 'soapbox/entity-store/entities'; -import { useEntities } from 'soapbox/entity-store/hooks'; -import { useApi } from 'soapbox/hooks'; -import { Account, accountSchema } from 'soapbox/schemas'; - -import { useRelationships } from './useRelationships'; - -function useFollowing(accountId: string | undefined, type: 'followers' | 'following') { - const api = useApi(); - - const { entities, ...rest } = useEntities( - [Entities.ACCOUNTS, accountId!, type], - () => api.get(`/api/v1/accounts/${accountId}/${type}`), - { schema: accountSchema, enabled: !!accountId }, - ); - - const { relationships } = useRelationships( - [accountId!, type], - entities.map(({ id }) => id), - ); - - const accounts: Account[] = entities.map((account) => ({ - ...account, - relationship: relationships[account.id], - })); - - return { accounts, ...rest }; -} - -export { useFollowing }; \ No newline at end of file diff --git a/app/soapbox/api/hooks/index.ts b/app/soapbox/api/hooks/index.ts index da7a77ef65..cdfe0c16f9 100644 --- a/app/soapbox/api/hooks/index.ts +++ b/app/soapbox/api/hooks/index.ts @@ -2,9 +2,13 @@ // Accounts export { useAccount } from './accounts/useAccount'; export { useAccountLookup } from './accounts/useAccountLookup'; -export { useBlocks } from './accounts/useBlocks'; +export { + useBlocks, + useMutes, + useFollowers, + useFollowing, +} from './accounts/useAccountList'; export { useFollow } from './accounts/useFollow'; -export { useFollowing } from './accounts/useFollowing'; export { useRelationships } from './accounts/useRelationships'; export { usePatronUser } from './accounts/usePatronUser'; diff --git a/app/soapbox/features/followers/index.tsx b/app/soapbox/features/followers/index.tsx index a7e4143c40..cadac497e8 100644 --- a/app/soapbox/features/followers/index.tsx +++ b/app/soapbox/features/followers/index.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; -import { useAccountLookup, useFollowing } from 'soapbox/api/hooks'; +import { useAccountLookup, useFollowers } from 'soapbox/api/hooks'; import Account from 'soapbox/components/account'; import MissingIndicator from 'soapbox/components/missing-indicator'; import ScrollableList from 'soapbox/components/scrollable-list'; @@ -28,7 +28,7 @@ const Followers: React.FC = ({ params }) => { hasNextPage, fetchNextPage, isLoading, - } = useFollowing(account?.id, 'followers'); + } = useFollowers(account?.id); if (isLoading) { return ( diff --git a/app/soapbox/features/following/index.tsx b/app/soapbox/features/following/index.tsx index 65b395ad29..938af829be 100644 --- a/app/soapbox/features/following/index.tsx +++ b/app/soapbox/features/following/index.tsx @@ -28,7 +28,7 @@ const Following: React.FC = ({ params }) => { hasNextPage, fetchNextPage, isLoading, - } = useFollowing(account?.id, 'following'); + } = useFollowing(account?.id); if (isLoading) { return ( diff --git a/app/soapbox/features/mutes/index.tsx b/app/soapbox/features/mutes/index.tsx index 618a219d4f..1bb83d0b31 100644 --- a/app/soapbox/features/mutes/index.tsx +++ b/app/soapbox/features/mutes/index.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { defineMessages, useIntl, FormattedMessage } from 'react-intl'; -import { useBlocks } from 'soapbox/api/hooks'; +import { useMutes } from 'soapbox/api/hooks'; import Account from 'soapbox/components/account'; import ScrollableList from 'soapbox/components/scrollable-list'; import { Column, Spinner } from 'soapbox/components/ui'; @@ -18,7 +18,7 @@ const Mutes: React.FC = () => { hasNextPage, fetchNextPage, isLoading, - } = useBlocks('mutes'); + } = useMutes(); if (isLoading) { return (