Migrate to external library for interacting with API

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak
2024-08-06 19:52:36 +02:00
parent 3b87810f85
commit 32c68a9221
89 changed files with 364 additions and 710 deletions

View File

@ -21,7 +21,7 @@ const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => {
const { entity, isUnauthorized, ...result } = useEntity<Account>(
[Entities.ACCOUNTS, accountId!],
() => client.request(`/api/v1/accounts/${accountId}`),
() => client.accounts.getAccount(accountId!),
{ schema: accountSchema, enabled: !!accountId },
);

View File

@ -33,12 +33,12 @@ const useAccountList = (listKey: string[], entityFn: EntityFn<void>, opts: useAc
const useBlocks = () => {
const client = useClient();
return useAccountList(['blocks'], () => client.request('/api/v1/blocks'));
return useAccountList(['blocks'], () => client.filtering.getBlocks());
};
const useMutes = () => {
const client = useClient();
return useAccountList(['mutes'], () => client.request('/api/v1/mutes'));
return useAccountList(['mutes'], () => client.filtering.getMutes());
};
const useFollowing = (accountId: string | undefined) => {
@ -46,7 +46,7 @@ const useFollowing = (accountId: string | undefined) => {
return useAccountList(
[accountId!, 'following'],
() => client.request(`/api/v1/accounts/${accountId}/following`),
() => client.accounts.getAccountFollowing(accountId),
{ enabled: !!accountId },
);
};
@ -56,7 +56,7 @@ const useFollowers = (accountId: string | undefined) => {
return useAccountList(
[accountId!, 'followers'],
() => client.request(`/api/v1/accounts/${accountId}/followers`),
() => client.accounts.getAccountFollowers(accountId),
{ enabled: !!accountId },
);
};

View File

@ -22,7 +22,7 @@ const useAccountLookup = (acct: string | undefined, opts: UseAccountLookupOpts =
const { entity: account, isUnauthorized, ...result } = useEntityLookup<Account>(
Entities.ACCOUNTS,
(account) => account.acct.toLowerCase() === acct?.toLowerCase(),
() => client.request(`/api/v1/accounts/lookup?acct=${acct}`),
() => client.accounts.lookupAccount(acct),
{ schema: accountSchema, enabled: !!acct },
);

View File

@ -2,8 +2,8 @@ import { z } from 'zod';
import { Entities } from 'soapbox/entity-store/entities';
import { useEntity } from 'soapbox/entity-store/hooks';
import { type Relationship, relationshipSchema } from 'soapbox/schemas';
import { useClient } from 'soapbox/hooks';
import { type Relationship, relationshipSchema } from 'soapbox/schemas';
interface UseRelationshipOpts {
enabled?: boolean;
@ -15,7 +15,7 @@ const useRelationship = (accountId: string | undefined, opts: UseRelationshipOpt
const { entity: relationship, ...result } = useEntity<Relationship>(
[Entities.RELATIONSHIPS, accountId!],
() => client.request(`/api/v1/accounts/relationships?id[]=${accountId}`),
() => client.accounts.getRelationships([accountId]),
{
enabled: enabled && !!accountId,
schema: z.array(relationshipSchema).nonempty().transform(arr => arr[0]),

View File

@ -7,8 +7,7 @@ const useRelationships = (listKey: string[], ids: string[]) => {
const client = useClient();
const { isLoggedIn } = useLoggedIn();
const fetchRelationships = (ids: string[]) =>
client.request('/api/v1/accounts/relationships', { params: { ids } });
const fetchRelationships = (ids: string[]) => client.accounts.getRelationships(ids);
const { entityMap: relationships, ...result } = useBatchedEntities<Relationship>(
[Entities.RELATIONSHIPS, ...listKey],

View File

@ -1,22 +0,0 @@
import { Entities } from 'soapbox/entity-store/entities';
import { useCreateEntity } from 'soapbox/entity-store/hooks';
import { useClient, useOwnAccount } from 'soapbox/hooks';
import type { Group } from 'soapbox/schemas';
const useCancelMembershipRequest = (group: Group) => {
const client = useClient();
const { account: me } = useOwnAccount();
const { createEntity, isSubmitting } = useCreateEntity(
[Entities.GROUP_RELATIONSHIPS],
() => client.request(`/client.request/v1/groups/${group.id}/membership_requests/${me?.id}/reject`, { method: 'POST' }),
);
return {
mutate: createEntity,
isSubmitting,
};
};
export { useCancelMembershipRequest };

View File

@ -1,8 +1,8 @@
import { serialize } from 'object-to-formdata';
import { useClient } from 'soapbox/hooks';
import { Entities } from 'soapbox/entity-store/entities';
import { useCreateEntity } from 'soapbox/entity-store/hooks';
import { useClient } from 'soapbox/hooks';
import { groupSchema } from 'soapbox/schemas';
interface CreateGroupParams {
@ -18,12 +18,11 @@ interface CreateGroupParams {
const useCreateGroup = () => {
const client = useClient();
const { createEntity, ...rest } = useCreateEntity([Entities.GROUPS, 'search', ''], (params: CreateGroupParams) =>
client.request('/api/v1/groups', {
method: 'POST',
contentType: '',
body: params,
}), { schema: groupSchema });
const { createEntity, ...rest } = useCreateEntity(
[Entities.GROUPS, 'search', ''],
(params: CreateGroupParams) => client.experimental.groups.createGroup(params),
{ schema: groupSchema },
);
return {
createGroup: createEntity,

View File

@ -1,6 +1,6 @@
import { useClient } from 'soapbox/hooks';
import { Entities } from 'soapbox/entity-store/entities';
import { useDeleteEntity } from 'soapbox/entity-store/hooks';
import { useClient } from 'soapbox/hooks';
import type { Group } from 'soapbox/schemas';
@ -8,7 +8,7 @@ const useDeleteGroupStatus = (group: Group, statusId: string) => {
const client = useClient();
const { deleteEntity, isSubmitting } = useDeleteEntity(
Entities.STATUSES,
() => client.request(`/api/v1/groups/${group.id}/statuses/${statusId}`, { method: 'DELETE' }),
() => client.experimental.groups.deleteGroupStatus(group.id, statusId),
);
return {

View File

@ -1,9 +1,9 @@
import { useEffect } from 'react';
import { useHistory } from 'react-router-dom';
import { useClient } from 'soapbox/hooks';
import { Entities } from 'soapbox/entity-store/entities';
import { useEntity } from 'soapbox/entity-store/hooks';
import { useClient } from 'soapbox/hooks';
import { type Group, groupSchema } from 'soapbox/schemas';
import { useGroupRelationship } from './useGroupRelationship';
@ -14,7 +14,7 @@ const useGroup = (groupId: string, refetch = true) => {
const { entity: group, isUnauthorized, ...result } = useEntity<Group>(
[Entities.GROUPS, groupId],
() => client.request(`/api/v1/groups/${groupId}`),
() => client.experimental.groups.getGroup(groupId),
{
schema: groupSchema,
refetch,

View File

@ -11,7 +11,7 @@ const useGroupMedia = (groupId: string) => {
return useEntities(
[Entities.STATUSES, 'groupMedia', groupId],
() => client.request(`/api/v1/timelines/group/${groupId}?only_media=true`),
() => client.timelines.groupTimeline(groupId, { only_media: true }),
{ schema: statusSchema })
;
};

View File

@ -9,7 +9,7 @@ const useGroupMembers = (groupId: string, role: GroupRoles) => {
const { entities, ...result } = useEntities<GroupMember>(
[Entities.GROUP_MEMBERSHIPS, groupId, role],
() => client.request(`/api/v1/groups/${groupId}/memberships?role=${role}`),
() => client.experimental.groups.getGroupMemberships(groupId, role),
{ schema: groupMemberSchema },
);

View File

@ -16,7 +16,7 @@ const useGroupMembershipRequests = (groupId: string) => {
const { entities, invalidate, fetchEntities, ...rest } = useEntities(
path,
() => client.request(`/api/v1/groups/${groupId}/membership_requests`),
() => client.experimental.groups.getGroupMembershipRequests(groupId),
{
schema: accountSchema,
enabled: relationship?.role === GroupRoles.OWNER || relationship?.role === GroupRoles.ADMIN,
@ -24,13 +24,13 @@ const useGroupMembershipRequests = (groupId: string) => {
);
const { dismissEntity: authorize } = useDismissEntity(path, async (accountId: string) => {
const response = await client.request(`/api/v1/groups/${groupId}/membership_requests/${accountId}/authorize`, { method: 'POST' });
const response = await client.experimental.groups.acceptGroupMembershipRequest(groupId, accountId);
invalidate();
return response;
});
const { dismissEntity: reject } = useDismissEntity(path, async (accountId: string) => {
const response = await client.request(`/api/v1/groups/${groupId}/membership_requests/${accountId}/reject`, { method: 'POST' });
const response = await client.experimental.groups.rejectGroupMembershipRequest(groupId, accountId);
invalidate();
return response;
});

View File

@ -10,7 +10,7 @@ const useGroupRelationship = (groupId: string | undefined) => {
const { entity: groupRelationship, ...result } = useEntity<GroupRelationship>(
[Entities.GROUP_RELATIONSHIPS, groupId!],
() => client.request(`/api/v1/groups/relationships?id[]=${groupId}`),
() => client.experimental.groups.getGroupRelationships([groupId]),
{
enabled: !!groupId,
schema: z.array(groupRelationshipSchema).nonempty().transform(arr => arr[0]),

View File

@ -8,7 +8,7 @@ const useGroupRelationships = (listKey: string[], ids: string[]) => {
const { isLoggedIn } = useLoggedIn();
const fetchGroupRelationships = (ids: string[]) =>
client.request('/api/v1/groups/relationships', { params: { id: ids } });
client.experimental.groups.getGroupRelationships(ids);
const { entityMap: relationships, ...result } = useBatchedEntities<GroupRelationship>(
[Entities.RELATIONSHIPS, ...listKey],

View File

@ -12,7 +12,7 @@ const useGroups = () => {
const { entities, ...result } = useEntities<Group>(
[Entities.GROUPS, 'search', ''],
() => client.request('/api/v1/groups'),
() => client.experimental.groups.getGroups(),
{ enabled: features.groups, schema: groupSchema },
);
const { relationships } = useGroupRelationships(

View File

@ -15,12 +15,11 @@ interface UpdateGroupParams {
const useUpdateGroup = (groupId: string) => {
const client = useClient();
const { createEntity, ...rest } = useCreateEntity([Entities.GROUPS], (params: UpdateGroupParams) =>
client.request(`/api/v1/groups/${groupId}`, {
method: 'PUT',
contentType: '',
body: params,
}), { schema: groupSchema });
const { createEntity, ...rest } = useCreateEntity(
[Entities.GROUPS],
(params: UpdateGroupParams) => client.experimental.groups.updateGroup(groupId, params),
{ schema: groupSchema },
);
return {
updateGroup: createEntity,

View File

@ -13,7 +13,6 @@ export { useRelationships } from './accounts/useRelationships';
// Groups
export { useBlockGroupMember } from './groups/useBlockGroupMember';
export { useCancelMembershipRequest } from './groups/useCancelMembershipRequest';
export { useCreateGroup, type CreateGroupParams } from './groups/useCreateGroup';
export { useDeleteGroup } from './groups/useDeleteGroup';
export { useDemoteGroupMember } from './groups/useDemoteGroupMember';

View File

@ -10,7 +10,7 @@ const useBookmarkFolders = () => {
const { entities, ...result } = useEntities<BookmarkFolder>(
[Entities.BOOKMARK_FOLDERS],
() => client.request('/api/v1/pleroma/bookmark_folders'),
() => client.myAccount.getBookmarkFolders,
{ enabled: features.bookmarkFolders, schema: bookmarkFolderSchema },
);

View File

@ -14,10 +14,7 @@ const useCreateBookmarkFolder = () => {
const { createEntity, ...rest } = useCreateEntity(
[Entities.BOOKMARK_FOLDERS],
(params: CreateBookmarkFolderParams) =>
client.request('/api/v1/pleroma/bookmark_folders', {
method: 'POST',
body: JSON.stringify(params),
}),
client.myAccount.createBookmarkFolder(params),
{ schema: bookmarkFolderSchema },
);

View File

@ -1,10 +1,13 @@
import { Entities } from 'soapbox/entity-store/entities';
import { useEntityActions } from 'soapbox/entity-store/hooks';
import { useDeleteEntity } from 'soapbox/entity-store/hooks';
import { useClient } from 'soapbox/hooks';
const useDeleteBookmarkFolder = () => {
const { deleteEntity, isSubmitting } = useEntityActions(
[Entities.BOOKMARK_FOLDERS],
{ delete: '/api/v1/pleroma/bookmark_folders/:id' },
const useDeleteBookmarkFolder = (folderId: string) => {
const client = useClient();
const { deleteEntity, isSubmitting } = useDeleteEntity(
Entities.BOOKMARK_FOLDERS,
() => client.myAccount.deleteBookmarkFolder(folderId),
);
return {

View File

@ -14,10 +14,7 @@ const useUpdateBookmarkFolder = (folderId: string) => {
const { createEntity, ...rest } = useCreateEntity(
[Entities.BOOKMARK_FOLDERS],
(params: UpdateBookmarkFolderParams) =>
client.request(`/api/v1/pleroma/bookmark_folders/${folderId}`, {
method: 'PATCH',
body: JSON.stringify(params),
}),
client.myAccount.updateBookmarkFolder(folderId, params),
{ schema: bookmarkFolderSchema },
);