Fix pagination of chat messages
This commit is contained in:
parent
f7bd5c5951
commit
30646ec8c3
2 changed files with 5 additions and 12 deletions
|
@ -8,7 +8,6 @@ import { ChatWidgetScreens, useChatContext } from 'soapbox/contexts/chat-context
|
||||||
import { useStatContext } from 'soapbox/contexts/stat-context';
|
import { useStatContext } from 'soapbox/contexts/stat-context';
|
||||||
import { useApi, useAppDispatch, useAppSelector, useFeatures, useOwnAccount } from 'soapbox/hooks';
|
import { useApi, useAppDispatch, useAppSelector, useFeatures, useOwnAccount } from 'soapbox/hooks';
|
||||||
import { normalizeChatMessage } from 'soapbox/normalizers';
|
import { normalizeChatMessage } from 'soapbox/normalizers';
|
||||||
import { compareId } from 'soapbox/utils/comparators';
|
|
||||||
import { flattenPages, PaginatedResult, updatePageItem } from 'soapbox/utils/queries';
|
import { flattenPages, PaginatedResult, updatePageItem } from 'soapbox/utils/queries';
|
||||||
|
|
||||||
import { queryClient } from './client';
|
import { queryClient } from './client';
|
||||||
|
@ -80,8 +79,6 @@ const isLastMessage = (chatMessageId: string): boolean => {
|
||||||
return !!chat;
|
return !!chat;
|
||||||
};
|
};
|
||||||
|
|
||||||
const reverseOrder = (a: IChat, b: IChat): number => compareId(a.id, b.id);
|
|
||||||
|
|
||||||
const useChatMessages = (chat: IChat) => {
|
const useChatMessages = (chat: IChat) => {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
const isBlocked = useAppSelector((state) => state.getIn(['relationships', chat.account.id, 'blocked_by']));
|
const isBlocked = useAppSelector((state) => state.getIn(['relationships', chat.account.id, 'blocked_by']));
|
||||||
|
@ -89,12 +86,12 @@ const useChatMessages = (chat: IChat) => {
|
||||||
const getChatMessages = async (chatId: string, pageParam?: any): Promise<PaginatedResult<IChatMessage>> => {
|
const getChatMessages = async (chatId: string, pageParam?: any): Promise<PaginatedResult<IChatMessage>> => {
|
||||||
const nextPageLink = pageParam?.link;
|
const nextPageLink = pageParam?.link;
|
||||||
const uri = nextPageLink || `/api/v1/pleroma/chats/${chatId}/messages`;
|
const uri = nextPageLink || `/api/v1/pleroma/chats/${chatId}/messages`;
|
||||||
const response = await api.get(uri);
|
const response = await api.get<any[]>(uri);
|
||||||
const { data } = response;
|
const { data } = response;
|
||||||
|
|
||||||
const link = getNextLink(response);
|
const link = getNextLink(response);
|
||||||
const hasMore = !!link;
|
const hasMore = !!link;
|
||||||
const result = data.sort(reverseOrder).map(normalizeChatMessage);
|
const result = data.map(normalizeChatMessage);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
result,
|
result,
|
||||||
|
@ -116,7 +113,7 @@ const useChatMessages = (chat: IChat) => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = flattenPages(queryInfo.data);
|
const data = flattenPages(queryInfo.data)?.reverse();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...queryInfo,
|
...queryInfo,
|
||||||
|
@ -169,10 +166,7 @@ const useChats = (search?: string) => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = queryInfo.data?.pages.reduce<IChat[]>(
|
const data = flattenPages(queryInfo.data);
|
||||||
(prev: IChat[], curr) => [...prev, ...curr.result],
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
|
|
||||||
const chatsQuery = {
|
const chatsQuery = {
|
||||||
...queryInfo,
|
...queryInfo,
|
||||||
|
|
|
@ -28,8 +28,7 @@ const deduplicateById = <T extends Entity>(entities: T[]): T[] => {
|
||||||
/** Flatten paginated results into a single array. */
|
/** Flatten paginated results into a single array. */
|
||||||
const flattenPages = <T>(queryData: InfiniteData<PaginatedResult<T>> | undefined) => {
|
const flattenPages = <T>(queryData: InfiniteData<PaginatedResult<T>> | undefined) => {
|
||||||
const data = queryData?.pages.reduce<T[]>(
|
const data = queryData?.pages.reduce<T[]>(
|
||||||
// FIXME: Pleroma wants these to be reversed for Chats.
|
(prev: T[], curr) => [...prev, ...curr.result],
|
||||||
(prev: T[], curr) => [...curr.result, ...prev],
|
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue