Skip to content

Commit

Permalink
FE: Add Prevoius button to topic message page - frontend side (kafbat…
Browse files Browse the repository at this point in the history
  • Loading branch information
hadisfr committed Sep 17, 2024
1 parent 55fc250 commit 7d68c19
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/src/main/java/io/kafbat/ui/emitter/ConsumingStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ void sendFinishEvent(FluxSink<TopicMessageEventDTO> sink, @Nullable Cursor.Track
sink.next(
new TopicMessageEventDTO()
.type(TopicMessageEventDTO.TypeEnum.DONE)
.prevCursor( // FIXME
null
)
.nextCursor(
cursor != null
? new TopicMessagePageCursorDTO().id(cursor.registerCursor())
Expand Down
2 changes: 2 additions & 0 deletions contract/src/main/resources/swagger/kafbat-ui-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2954,6 +2954,8 @@ components:
$ref: "#/components/schemas/TopicMessagePhase"
consuming:
$ref: "#/components/schemas/TopicMessageConsuming"
prevCursor:
$ref: "#/components/schemas/TopicMessagePageCursor"
nextCursor:
$ref: "#/components/schemas/TopicMessagePageCursor"

Expand Down
12 changes: 11 additions & 1 deletion frontend/src/components/Topics/Topic/Messages/MessagesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TopicMessage } from 'generated-sources';
import React, { useState } from 'react';
import { Button } from 'components/common/Button/Button';
import * as S from 'components/common/NewTable/Table.styled';
import { useGoToNextPage, useIsLiveMode } from 'lib/hooks/useMessagesFilters';
import { useGoToPrevPage, useGoToNextPage, useIsLiveMode } from 'lib/hooks/useMessagesFilters';
import { useMessageFiltersStore } from 'lib/hooks/useMessageFiltersStore';

import PreviewModal from './PreviewModal';
Expand All @@ -21,11 +21,13 @@ const MessagesTable: React.FC<MessagesTableProps> = ({
isFetching,
}) => {
const goToNextPage = useGoToNextPage();
const goToPrevPage = useGoToPrevPage();
const [previewFor, setPreviewFor] = useState<string | null>(null);

const [keyFilters, setKeyFilters] = useState<PreviewFilter[]>([]);
const [contentFilters, setContentFilters] = useState<PreviewFilter[]>([]);
const nextCursor = useMessageFiltersStore((state) => state.nextCursor);
const prevCursor = useMessageFiltersStore((state) => state.prevCursor);
const isLive = useIsLiveMode();

return (
Expand Down Expand Up @@ -97,6 +99,14 @@ const MessagesTable: React.FC<MessagesTableProps> = ({
</Table>
<S.Pagination>
<S.Pages>
<Button
disabled={isLive || isFetching || !prevCursor}
buttonType="secondary"
buttonSize="L"
onClick={goToPrevPage}
>
← Previous
</Button>
<Button
disabled={isLive || isFetching || !nextCursor}
buttonType="secondary"
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/lib/hooks/api/topicMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,13 @@ export const useTopicMessages = ({
requestParams.append(MessagesFilterKeys.partitions, partitions);
}
const { nextCursor, setNextCursor } = useMessageFiltersStore.getState();
const { prevCursor, setPrevCursor } = useMessageFiltersStore.getState();

const searchParamPage = getPageValue(searchParams);
if (currentPage.current < searchParamPage && nextCursor) {
requestParams.set(MessagesFilterKeys.cursor, nextCursor);
} else if (currentPage.current > searchParamPage && prevCursor) {
requestParams.set(MessagesFilterKeys.cursor, prevCursor);
}
currentPage.current = searchParamPage;

Expand Down Expand Up @@ -147,6 +150,9 @@ export const useTopicMessages = ({
if (nextCursor !== parsedData.nextCursor?.id) {
setNextCursor(parsedData.nextCursor?.id || undefined);
}
if (prevCursor !== parsedData.prevCursor?.id) {
setPrevCursor(parsedData.prevCursor?.id || undefined);
}
break;
default:
}
Expand All @@ -157,6 +163,7 @@ export const useTopicMessages = ({
},
onerror(err) {
setNextCursor(undefined);
setPrevCursor(undefined);
setIsFetching(false);
abortController.current = new AbortController();
showServerError(err);
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/lib/hooks/useMessageFiltersStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ interface MessageFiltersState {
notPersistedFilter: AdvancedFilter | undefined;
save: (filter: AdvancedFilter) => void;
nextCursor: string | undefined;
prevCursor: string | undefined;
setNextCursor: (str: string | undefined) => void;
setPrevCursor: (str: string | undefined) => void;
replace: (filterId: string, filter: AdvancedFilter) => void;
commit: (filter: AdvancedFilter | undefined) => void;
remove: (id: string) => void;
Expand All @@ -39,6 +41,7 @@ export const useMessageFiltersStore = create<MessageFiltersState>()(
(set) => ({
filters: {},
nextCursor: undefined,
prevCursor: undefined,
notPersistedFilter: undefined,
save: (filter) =>
set((state) => ({
Expand Down Expand Up @@ -73,6 +76,7 @@ export const useMessageFiltersStore = create<MessageFiltersState>()(
}),
removeAll: () => set(() => ({ filters: {} })),
setNextCursor: (cursor) => set(() => ({ nextCursor: cursor })),
setPrevCursor: (cursor) => set(() => ({ prevCursor: cursor })),
}),
{
name: `${LOCAL_STORAGE_KEY_PREFIX}-message-filters`,
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/lib/hooks/useMessagesFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ export function getPageValue(urlSearchParam: URLSearchParams) {
return page;
}

export function useGoToPrevPage(initSearchParams?: URLSearchParams) {
const [, setSearchParams] = useSearchParams(initSearchParams);
return () => {
setSearchParams((params) => {
const prevPage = getPageValue(params) - 1;
params.set(MessagesFilterKeys.page, prevPage.toString());

return params;
});
};
}

export function useGoToNextPage(initSearchParams?: URLSearchParams) {
const [, setSearchParams] = useSearchParams(initSearchParams);
return () => {
Expand Down

0 comments on commit 7d68c19

Please sign in to comment.