Improve infinite scroll

chats-router
Justin 2022-09-15 10:49:09 -04:00
rodzic 6240ea5a23
commit 8e35d1dd92
1 zmienionych plików z 19 dodań i 17 usunięć

Wyświetl plik

@ -3,6 +3,7 @@ import { useEffect, useState } from 'react';
import { fetchRelationships } from 'soapbox/actions/accounts'; import { fetchRelationships } from 'soapbox/actions/accounts';
import snackbar from 'soapbox/actions/snackbar'; import snackbar from 'soapbox/actions/snackbar';
import { getNextLink } from 'soapbox/api';
import compareId from 'soapbox/compare_id'; import compareId from 'soapbox/compare_id';
import { useChatContext } from 'soapbox/contexts/chat-context'; import { useChatContext } from 'soapbox/contexts/chat-context';
import { useApi, useAppDispatch } from 'soapbox/hooks'; import { useApi, useAppDispatch } from 'soapbox/hooks';
@ -47,20 +48,19 @@ const reverseOrder = (a: IChat, b: IChat): number => compareId(a.id, b.id);
const useChatMessages = (chatId: string) => { const useChatMessages = (chatId: string) => {
const api = useApi(); const api = useApi();
const getChatMessages = async(chatId: string, pageParam?: any): Promise<{ result: IChatMessage[], maxId: string, hasMore: boolean }> => { const getChatMessages = async(chatId: string, pageParam?: any): Promise<{ result: IChatMessage[], hasMore: boolean, link?: string }> => {
const { data, headers } = await api.get(`/api/v1/pleroma/chats/${chatId}/messages`, { const nextPageLink = pageParam?.link;
params: { const uri = nextPageLink || `/api/v1/pleroma/chats/${chatId}/messages`;
max_id: pageParam?.maxId, const response = await api.get(uri);
}, const { data } = response;
});
const hasMore = !!headers.link; const link = getNextLink(response);
const hasMore = !!link;
const result = data.sort(reverseOrder).map(normalizeChatMessage); const result = data.sort(reverseOrder).map(normalizeChatMessage);
const nextMaxId = result[0]?.id;
return { return {
result, result,
maxId: nextMaxId, link,
hasMore, hasMore,
}; };
}; };
@ -69,7 +69,7 @@ const useChatMessages = (chatId: string) => {
keepPreviousData: true, keepPreviousData: true,
getNextPageParam: (config) => { getNextPageParam: (config) => {
if (config.hasMore) { if (config.hasMore) {
return { maxId: config.maxId }; return { link: config.link };
} }
return undefined; return undefined;
@ -91,24 +91,26 @@ const useChats = (search?: string) => {
const api = useApi(); const api = useApi();
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const getChats = async(pageParam?: any): Promise<{ result: IChat[], maxId: string, hasMore: boolean }> => { const getChats = async(pageParam?: any): Promise<{ result: IChat[], hasMore: boolean, link?: string }> => {
const { data, headers } = await api.get<IChat[]>('/api/v1/pleroma/chats', { const nextPageLink = pageParam?.link;
const uri = nextPageLink || '/api/v1/pleroma/chats';
const response = await api.get<IChat[]>(uri, {
params: { params: {
max_id: pageParam?.maxId,
search, search,
}, },
}); });
const { data } = response;
const hasMore = !!headers.link; const link = getNextLink(response);
const nextMaxId = data[data.length - 1]?.id; const hasMore = !!link;
// Set the relationships to these users in the redux store. // Set the relationships to these users in the redux store.
dispatch(fetchRelationships(data.map((item) => item.account.id))); dispatch(fetchRelationships(data.map((item) => item.account.id)));
return { return {
result: data, result: data,
maxId: nextMaxId,
hasMore, hasMore,
link,
}; };
}; };
@ -116,7 +118,7 @@ const useChats = (search?: string) => {
keepPreviousData: true, keepPreviousData: true,
getNextPageParam: (config) => { getNextPageParam: (config) => {
if (config.hasMore) { if (config.hasMore) {
return { maxId: config.maxId }; return { link: config.link };
} }
return undefined; return undefined;