Skip to content

Commit d8fd5c3

Browse files
authored
FE: Messages: Fix refresh does pagination instead (#417)
1 parent 22a5053 commit d8fd5c3

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

frontend/src/components/Topics/Topic/Messages/MessagesTable.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { TopicMessage } from 'generated-sources';
55
import React, { useState } from 'react';
66
import { Button } from 'components/common/Button/Button';
77
import * as S from 'components/common/NewTable/Table.styled';
8-
import { useIsLiveMode, useRefreshData } from 'lib/hooks/useMessagesFilters';
8+
import { usePaginateTopics, useIsLiveMode } from 'lib/hooks/useMessagesFilters';
99
import { useMessageFiltersStore } from 'lib/hooks/useMessageFiltersStore';
1010

1111
import PreviewModal from './PreviewModal';
@@ -20,7 +20,7 @@ const MessagesTable: React.FC<MessagesTableProps> = ({
2020
messages,
2121
isFetching,
2222
}) => {
23-
const refreshData = useRefreshData();
23+
const paginate = usePaginateTopics();
2424
const [previewFor, setPreviewFor] = useState<string | null>(null);
2525

2626
const [keyFilters, setKeyFilters] = useState<PreviewFilter[]>([]);
@@ -101,7 +101,7 @@ const MessagesTable: React.FC<MessagesTableProps> = ({
101101
disabled={isLive || isFetching || !nextCursor}
102102
buttonType="secondary"
103103
buttonSize="L"
104-
onClick={refreshData}
104+
onClick={paginate}
105105
>
106106
Next →
107107
</Button>

frontend/src/components/Topics/Topic/Messages/__test__/MessagesTable.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jest.mock('react-router-dom', () => ({
3030

3131
jest.mock('lib/hooks/useMessagesFilters', () => ({
3232
useIsLiveMode: jest.fn(),
33-
useRefreshData: jest.fn(),
33+
usePaginateTopics: jest.fn(),
3434
}));
3535

3636
describe('MessagesTable', () => {

frontend/src/lib/hooks/api/topicMessages.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import { showServerError } from 'lib/errorHandling';
1313
import { useMutation, useQuery } from '@tanstack/react-query';
1414
import { messagesApiClient } from 'lib/api';
1515
import { useSearchParams } from 'react-router-dom';
16-
import { MessagesFilterKeys } from 'lib/hooks/useMessagesFilters';
16+
import {
17+
getCursorValue,
18+
MessagesFilterKeys,
19+
} from 'lib/hooks/useMessagesFilters';
1720
import { convertStrToPollingMode } from 'lib/hooks/filterUtils';
1821
import { useMessageFiltersStore } from 'lib/hooks/useMessageFiltersStore';
1922
import { TopicName } from 'lib/interfaces/topic';
@@ -35,7 +38,7 @@ export const useTopicMessages = ({
3538
React.useState<TopicMessageConsuming>();
3639
const [isFetching, setIsFetching] = React.useState(false);
3740
const abortController = useRef(new AbortController());
38-
const prevReqUrl = useRef<string>('');
41+
const prevCursor = useRef(0);
3942

4043
// get initial properties
4144

@@ -103,14 +106,14 @@ export const useTopicMessages = ({
103106
const tempCompareUrl = new URLSearchParams(requestParams);
104107
tempCompareUrl.delete(MessagesFilterKeys.cursor);
105108

106-
const tempToString = tempCompareUrl.toString();
109+
const currentCursor = getCursorValue(searchParams);
107110

108-
// filters stay the say and we have cursor set cursor
109-
if (nextCursor && tempToString === prevReqUrl.current) {
111+
// filters stay the same and we have cursor set cursor
112+
if (nextCursor && prevCursor.current < currentCursor) {
110113
requestParams.set(MessagesFilterKeys.cursor, nextCursor);
111114
}
112115

113-
prevReqUrl.current = tempToString;
116+
prevCursor.current = currentCursor;
114117
await fetchEventSource(`${url}?${requestParams.toString()}`, {
115118
method: 'GET',
116119
signal: abortController.current.signal,

frontend/src/lib/hooks/useMessagesFilters.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ export function useRefreshData(initSearchParams?: URLSearchParams) {
5252
};
5353
}
5454

55+
export function getCursorValue(urlSearchParam: URLSearchParams) {
56+
const cursor = parseInt(
57+
urlSearchParam.get(MessagesFilterKeys.cursor) || '0',
58+
10
59+
);
60+
61+
if (Number.isNaN(cursor)) {
62+
return 0;
63+
}
64+
65+
return cursor;
66+
}
67+
68+
export function usePaginateTopics(initSearchParams?: URLSearchParams) {
69+
const [, setSearchParams] = useSearchParams(initSearchParams);
70+
return () => {
71+
setSearchParams((params) => {
72+
const cursor = getCursorValue(params) + 1;
73+
74+
if (cursor) {
75+
params.set(MessagesFilterKeys.cursor, cursor.toString());
76+
}
77+
78+
return params;
79+
});
80+
};
81+
}
82+
5583
export function useMessagesFilters() {
5684
const [searchParams, setSearchParams] = useSearchParams();
5785
const refreshData = useRefreshData(searchParams);
@@ -68,6 +96,9 @@ export function useMessagesFilters() {
6896
params.delete(MessagesFilterKeys.activeFilterNPId);
6997
params.delete(MessagesFilterKeys.smartFilterId);
7098
}
99+
100+
params.delete(MessagesFilterKeys.cursor);
101+
71102
return params;
72103
});
73104
}, []);

0 commit comments

Comments
 (0)