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

@ -38,7 +38,7 @@ const useUpdateCredentials = () => {
const dispatch = useAppDispatch();
return useMutation({
mutationFn: (data: UpdateCredentialsData) => client.accounts.updateCredentials(data),
mutationFn: (data: UpdateCredentialsData) => client.settings.updateCredentials(data),
onMutate(variables) {
const cachedAccount = account;
dispatch(patchMeSuccess({ ...account, ...variables }));

View File

@ -2,18 +2,17 @@ import { InfiniteData, keepPreviousData, useInfiniteQuery, useMutation, useQuery
import sumBy from 'lodash/sumBy';
import { importFetchedAccount, importFetchedAccounts } from 'soapbox/actions/importer';
import { getNextLink } from 'soapbox/api';
import { ChatWidgetScreens, useChatContext } from 'soapbox/contexts/chat-context';
import { useStatContext } from 'soapbox/contexts/stat-context';
import { useAppDispatch, useAppSelector, useClient, useFeatures, useLoggedIn, useOwnAccount } from 'soapbox/hooks';
import { normalizeChatMessage } from 'soapbox/normalizers';
import { ChatMessage } from 'soapbox/types/entities';
import { reOrderChatListItems } from 'soapbox/utils/chats';
import { flattenPages, PaginatedResult, updatePageItem } from 'soapbox/utils/queries';
import { queryClient } from './client';
import { useFetchRelationships } from './relationships';
import type { Chat, ChatMessage, PaginatedResponse } from 'pl-api';
import type { Account } from 'soapbox/schemas';
interface IChat {
@ -40,21 +39,10 @@ const useChatMessages = (chat: IChat) => {
const client = useClient();
const isBlocked = useAppSelector((state) => state.getIn(['relationships', chat.account.id, 'blocked_by']));
const getChatMessages = async (chatId: string, pageParam?: any): Promise<PaginatedResult<ChatMessage>> => {
const nextPageLink = pageParam?.link;
const uri = nextPageLink || `/api/v1/pleroma/chats/${chatId}/messages`;
const response = await client.request<any[]>(uri);
const { json: data } = response;
const getChatMessages = async (chatId: string, pageParam?: Pick<PaginatedResponse<ChatMessage>, 'next'>): Promise<PaginatedResponse<ChatMessage>> => {
const response = await (pageParam?.next ? pageParam.next() : client.chats.getChatMessages(chatId));
const link = getNextLink(response);
const hasMore = !!link;
const result = data.map(normalizeChatMessage);
return {
result,
link,
hasMore,
};
return response;
};
const queryInfo = useInfiniteQuery({
@ -63,14 +51,8 @@ const useChatMessages = (chat: IChat) => {
enabled: !isBlocked,
gcTime: 0,
staleTime: 0,
initialPageParam: { link: undefined as string | undefined },
getNextPageParam: (config) => {
if (config.hasMore) {
return { link: config.link };
}
return undefined;
},
initialPageParam: { next: null as (() => Promise<PaginatedResponse<ChatMessage>>) | null },
getNextPageParam: (config) => config,
});
const data = flattenPages(queryInfo.data)?.reverse();
@ -89,26 +71,17 @@ const useChats = () => {
const fetchRelationships = useFetchRelationships();
const { me } = useLoggedIn();
const getChats = async (pageParam?: any): Promise<PaginatedResult<IChat>> => {
const nextPageLink = pageParam?.link;
const uri = nextPageLink || '/api/v2/pleroma/chats';
const response = await client.request<IChat[]>(uri);
const { json: data } = response;
const getChats = async (pageParam?: Pick<PaginatedResponse<Chat>, 'next'>): Promise<PaginatedResponse<Chat>> => {
const response = await (pageParam?.next || client.chats.getChats)();
const { items } = response;
const link = getNextLink(response);
const hasMore = !!link;
setUnreadChatsCount(Number(response.headers.get('x-unread-messages-count')) || sumBy(data, (chat) => chat.unread));
setUnreadChatsCount(sumBy(data, (chat) => chat.unread));
// Set the relationships to these users in the redux store.
fetchRelationships.mutate({ accountIds: data.map((item) => item.account.id) });
dispatch(importFetchedAccounts(data.map((item) => item.account)));
fetchRelationships.mutate({ accountIds: items.map((item) => item.account.id) });
dispatch(importFetchedAccounts(items.map((item) => item.account)));
return {
result: data,
hasMore,
link,
};
return response;
};
const queryInfo = useInfiniteQuery({
@ -116,14 +89,8 @@ const useChats = () => {
queryFn: ({ pageParam }) => getChats(pageParam),
placeholderData: keepPreviousData,
enabled: features.chats && !!me,
initialPageParam: { link: undefined as string | undefined },
getNextPageParam: (config) => {
if (config.hasMore) {
return { link: config.link };
}
return undefined;
},
initialPageParam: { next: null as (() => Promise<PaginatedResponse<Chat>>) | null },
getNextPageParam: (config) => config,
});
const data = flattenPages(queryInfo.data);
@ -134,7 +101,7 @@ const useChats = () => {
};
const getOrCreateChatByAccountId = (accountId: string) =>
client.request<IChat>(`/api/v1/pleroma/chats/by-account-id/${accountId}`, { method: 'POST' });
client.chats.createChat(accountId);
return { chatsQuery, getOrCreateChatByAccountId };
};
@ -146,7 +113,7 @@ const useChat = (chatId?: string) => {
const getChat = async () => {
if (chatId) {
const { json: data } = await client.request(`/api/v1/pleroma/chats/${chatId}`);
const data = await client.chats.getChat(chatId);
fetchRelationships.mutate({ accountIds: [data.account.id] });
dispatch(importFetchedAccount(data.account));
@ -155,7 +122,7 @@ const useChat = (chatId?: string) => {
}
};
return useQuery<IChat | undefined>({
return useQuery<Chat | undefined>({
queryKey: ChatKeys.chat(chatId),
queryFn: getChat,
gcTime: 0,
@ -172,8 +139,8 @@ const useChatActions = (chatId: string) => {
const { chat, changeScreen } = useChatContext();
const markChatAsRead = async (lastReadId: string) =>
client.request<IChat>(`/api/v1/pleroma/chats/${chatId}/read`, { body: { last_read_id: lastReadId } })
.then(({ json: data }) => {
client.chats.markChatAsRead(chatId, lastReadId)
.then((data) => {
updatePageItem(['chats', 'search'], data, (o, n) => o.id === n.id);
const queryData = queryClient.getQueryData<InfiniteData<PaginatedResult<unknown>>>(['chats', 'search']);
@ -194,10 +161,7 @@ const useChatActions = (chatId: string) => {
const createChatMessage = useMutation({
mutationFn: ({ chatId, content, mediaId }: { chatId: string; content: string; mediaId?: string }) =>
client.request<ChatMessage>(`/api/v1/pleroma/chats/${chatId}/messages`, {
method: 'POST',
body: { content, media_id: mediaId },
}),
client.chats.createChatMessage(chatId, { content, media_id: mediaId }),
retry: false,
onMutate: async (variables) => {
// Cancel any outgoing refetches (so they don't overwrite our optimistic update)
@ -244,21 +208,21 @@ const useChatActions = (chatId: string) => {
queryClient.setQueryData(['chats', 'messages', variables.chatId], context.prevChatMessages);
},
onSuccess: (response: any, variables, context) => {
const nextChat = { ...chat, last_message: response.json };
const nextChat = { ...chat, last_message: response };
updatePageItem(['chats', 'search'], nextChat, (o, n) => o.id === n.id);
updatePageItem(
ChatKeys.chatMessages(variables.chatId),
normalizeChatMessage(response.json),
normalizeChatMessage(response),
(o) => o.id === context.pendingId,
);
reOrderChatListItems();
},
});
const deleteChatMessage = (chatMessageId: string) =>
client.request<IChat>(`/api/v1/pleroma/chats/${chatId}/messages/${chatMessageId}`, { method: 'DELETE' });
client.chats.deleteChatMessage(chatId, chatMessageId);
const deleteChat = useMutation({
mutationFn: () => client.request<IChat>(`/api/v1/pleroma/chats/${chatId}`, { method: 'DELETE' }),
mutationFn: () => client.chats.deleteChat(chatId),
onSuccess() {
changeScreen(ChatWidgetScreens.INBOX);
queryClient.invalidateQueries({ queryKey: ChatKeys.chatMessages(chatId) });

View File

@ -9,12 +9,10 @@ const useFetchRelationships = () => {
return useMutation({
mutationFn: ({ accountIds }: { accountIds: string[]}) => {
const ids = accountIds.map((id) => `id[]=${id}`).join('&');
return client.request(`/api/v1/accounts/relationships?${ids}`);
return client.accounts.getRelationships(accountIds);
},
onSuccess(response) {
dispatch(fetchRelationshipsSuccess(response.json));
dispatch(fetchRelationshipsSuccess(response));
},
onError(error) {
dispatch(fetchRelationshipsFail(error));

View File

@ -15,7 +15,7 @@ const useSuggestions = () => {
const dispatch = useAppDispatch();
const getSuggestions = async () => {
const response = await client.accounts.getSuggestions();
const response = await client.myAccount.getSuggestions();
const accounts = response.map(({ account }) => account);
const accountIds = accounts.map((account) => account.id);
@ -43,7 +43,7 @@ const useDismissSuggestion = () => {
const client = useClient();
return useMutation({
mutationFn: (accountId: string) => client.accounts.dismissSuggestions(accountId),
mutationFn: (accountId: string) => client.myAccount.dismissSuggestions(accountId),
onMutate(accountId: string) {
removePageItem(SuggestionKeys.suggestions, accountId, (o: any, n: any) => o.account === n);
},
@ -55,7 +55,7 @@ const useOnboardingSuggestions = () => {
const dispatch = useAppDispatch();
const getSuggestions = async () => {
const response = await client.accounts.getSuggestions();
const response = await client.myAccount.getSuggestions();
const accounts = response.map(({ account }) => account);
const accountIds = accounts.map((account) => account.id);