mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-11 00:08:30 +01:00
feat: add paging to event log (#7793)
Adds sticky pagination to the event log: ![image](https://github.com/user-attachments/assets/c426f30d-bb64-44a5-b3b4-8c295207b249) This PR uses the sticky pagination bar that we use on other tables to navigate the event search results. ## Decisions / discussion points The trickiest issue here is how we calculate the next and previous page offsets. This is tricky because we don't expose the page number to the API, but the raw offset itself. This abstraction makes it possible to set an offset that isn't a multiple of the page size. Say the page size is 25. If you manually set an offset of 30 (through changing the URL), what do you expect should happen when you: - load the page? Should you see results 31 to 55? 26 to 50? - go to the next page? Should your next offset be 55 or 50? - previous page: should your previous page offset be 5? 25? 0? The current implementation has taken what I thought would be the easiest way out: If your offset is between two multiples of the page size, we'll consider it to be the lower of the two. - The next page's offset is the next multiple of the page size that is higher than the current offset (50 in the example above). - The previous page's offset will be not the nearest lower page size, but the one below. So if you set offset 35 and page size 25, your next page will take you back to 0 (as if the offset was 25). We could instead update the API to accept `page` instead of offset, but that wouldn't align with how other tables do it. Comparing to the global flags table, if you set an offset that isn't a multiple of the page size, we force the offset to 0. We can look at handling it like that in a follow-up, though I'd argue that forcing it to be the next lower multiple of the page size would make more sense. One issue that appears when you can set custom offsets is that the little "showing x-y items out of z" gets out of whack (because it only operates on multiples of the page size (seemingly)) ![image](https://github.com/user-attachments/assets/ec9df89c-2717-45d9-97dd-5c4e8ebc24cc) ## The Event Log as a table While we haven't used the HTML `table` element to render the event log, I would argue that it _is_ actually a table. It displays tabular data. Each card (row) has an id, a project, etc. The current implementation forces the event log search to act as a table state manager, but we could transform the event list into an events table to better align the pagination handling. The best part? We can keep the exact same design too. A table doesn't have to _look_ like a table to be a table.
This commit is contained in:
parent
4fac38ce01
commit
ff9b7298b6
@ -16,6 +16,7 @@ import { useUiFlag } from 'hooks/useUiFlag';
|
||||
import { EventLogFilters } from './EventLogFilters';
|
||||
import type { EventSchema } from 'openapi';
|
||||
import { useEventLogSearch } from './useEventLogSearch';
|
||||
import { StickyPaginationBar } from 'component/common/Table/StickyPaginationBar/StickyPaginationBar';
|
||||
|
||||
interface IEventLogProps {
|
||||
title: string;
|
||||
@ -43,14 +44,21 @@ const EventResultWrapper = styled('div')(({ theme }) => ({
|
||||
}));
|
||||
|
||||
const NewEventLog = ({ title, project, feature }: IEventLogProps) => {
|
||||
const { events, total, loading, tableState, setTableState, filterState } =
|
||||
useEventLogSearch(
|
||||
project
|
||||
? { type: 'project', projectId: project }
|
||||
: feature
|
||||
? { type: 'flag', flagName: feature }
|
||||
: { type: 'global' },
|
||||
);
|
||||
const {
|
||||
events,
|
||||
total,
|
||||
loading,
|
||||
tableState,
|
||||
setTableState,
|
||||
filterState,
|
||||
pagination,
|
||||
} = useEventLogSearch(
|
||||
project
|
||||
? { type: 'project', projectId: project }
|
||||
: feature
|
||||
? { type: 'flag', flagName: feature }
|
||||
: { type: 'global' },
|
||||
);
|
||||
|
||||
const setSearchValue = (query = '') => {
|
||||
setTableState({ query });
|
||||
@ -129,6 +137,19 @@ const NewEventLog = ({ title, project, feature }: IEventLogProps) => {
|
||||
/>
|
||||
{resultComponent()}
|
||||
</EventResultWrapper>
|
||||
<ConditionallyRender
|
||||
condition={total > 25}
|
||||
show={
|
||||
<StickyPaginationBar
|
||||
totalItems={total}
|
||||
pageSize={pagination.pageSize}
|
||||
pageIndex={pagination.currentPage}
|
||||
fetchPrevPage={pagination.prevPage}
|
||||
fetchNextPage={pagination.nextPage}
|
||||
setPageLimit={pagination.setPageLimit}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</PageContent>
|
||||
);
|
||||
};
|
||||
|
@ -0,0 +1,35 @@
|
||||
import { calculatePaginationInfo } from './useEventLogSearch';
|
||||
|
||||
test.each([
|
||||
[{ offset: 5, pageSize: 2 }, { currentPage: 2 }],
|
||||
[{ offset: 6, pageSize: 2 }, { currentPage: 3 }],
|
||||
])('it calculates currentPage correctly', (input, output) => {
|
||||
const result = calculatePaginationInfo(input);
|
||||
expect(result).toMatchObject(output);
|
||||
});
|
||||
|
||||
test("it doesn't try to divide by zero", () => {
|
||||
const result = calculatePaginationInfo({ offset: 0, pageSize: 0 });
|
||||
|
||||
expect(result.currentPage).not.toBeNaN();
|
||||
});
|
||||
|
||||
test('it calculates the correct offsets', () => {
|
||||
const result = calculatePaginationInfo({ offset: 50, pageSize: 25 });
|
||||
|
||||
expect(result).toMatchObject({
|
||||
currentPage: 2,
|
||||
nextPageOffset: 75,
|
||||
previousPageOffset: 25,
|
||||
});
|
||||
});
|
||||
|
||||
test(`it "fixes" offsets if you've set a weird offset`, () => {
|
||||
const result = calculatePaginationInfo({ offset: 35, pageSize: 25 });
|
||||
|
||||
expect(result).toMatchObject({
|
||||
currentPage: 1,
|
||||
nextPageOffset: 50,
|
||||
previousPageOffset: 0,
|
||||
});
|
||||
});
|
@ -4,7 +4,6 @@ import {
|
||||
StringParam,
|
||||
withDefault,
|
||||
} from 'use-query-params';
|
||||
import { DEFAULT_PAGE_LIMIT } from 'hooks/api/getters/useFeatureSearch/useFeatureSearch';
|
||||
import { FilterItemParam } from 'utils/serializeQueryParams';
|
||||
import { usePersistentTableState } from 'hooks/usePersistentTableState';
|
||||
import mapValues from 'lodash.mapvalues';
|
||||
@ -34,6 +33,24 @@ const extraParameters = (logType: Log) => {
|
||||
}
|
||||
};
|
||||
|
||||
const DEFAULT_PAGE_SIZE = 25;
|
||||
|
||||
export const calculatePaginationInfo = ({
|
||||
offset,
|
||||
pageSize,
|
||||
}: {
|
||||
offset: number;
|
||||
pageSize: number;
|
||||
}) => {
|
||||
const currentPage = Math.floor(offset / Math.max(pageSize, 1));
|
||||
|
||||
return {
|
||||
currentPage,
|
||||
nextPageOffset: pageSize * (currentPage + 1),
|
||||
previousPageOffset: pageSize * Math.max(currentPage - 1, 0),
|
||||
};
|
||||
};
|
||||
|
||||
export const useEventLogSearch = (
|
||||
logType: Log,
|
||||
storageKey = 'event-log',
|
||||
@ -41,7 +58,7 @@ export const useEventLogSearch = (
|
||||
) => {
|
||||
const stateConfig = {
|
||||
offset: withDefault(NumberParam, 0),
|
||||
limit: withDefault(NumberParam, DEFAULT_PAGE_LIMIT),
|
||||
limit: withDefault(NumberParam, DEFAULT_PAGE_SIZE),
|
||||
query: StringParam,
|
||||
from: FilterItemParam,
|
||||
to: FilterItemParam,
|
||||
@ -91,6 +108,12 @@ export const useEventLogSearch = (
|
||||
},
|
||||
);
|
||||
|
||||
const { currentPage, nextPageOffset, previousPageOffset } =
|
||||
calculatePaginationInfo({
|
||||
offset: tableState.offset ?? 0,
|
||||
pageSize: tableState.limit ?? 1,
|
||||
});
|
||||
|
||||
return {
|
||||
events,
|
||||
total,
|
||||
@ -100,5 +123,18 @@ export const useEventLogSearch = (
|
||||
tableState,
|
||||
setTableState,
|
||||
filterState,
|
||||
pagination: {
|
||||
pageSize: tableState.limit ?? 0,
|
||||
currentPage,
|
||||
nextPage: () => {
|
||||
setTableState({ offset: nextPageOffset });
|
||||
},
|
||||
prevPage: () => {
|
||||
setTableState({ offset: previousPageOffset });
|
||||
},
|
||||
setPageLimit: (limit: number) => {
|
||||
setTableState({ limit, offset: 0 });
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user