Skip to content

Commit 7c177a6

Browse files
make pagination work without rank numbers
1 parent a64bfb1 commit 7c177a6

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

packages/app-builder/src/components/PaginationButtons.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type ItemWithId = {
2222

2323
type CursorPaginationsButtonsProps = PaginatedResponse<ItemWithId> & {
2424
onPaginationChange: (paginationParams: PaginationParams) => void;
25+
hasPreviousPage: boolean;
26+
hideBoundaries?: boolean;
2527
};
2628

2729
function getStartAndEndFormatted(
@@ -65,15 +67,13 @@ function getStartAndEndFormatted(
6567

6668
export function CursorPaginationButtons({
6769
items,
68-
startIndex,
69-
endIndex,
7070
hasNextPage,
71+
hasPreviousPage,
7172
onPaginationChange,
73+
hideBoundaries,
7274
}: CursorPaginationsButtonsProps) {
7375
const { t } = useTranslation(['common']);
7476
const language = useFormatLanguage();
75-
const start = Math.min(startIndex, endIndex);
76-
const end = Math.max(startIndex, endIndex);
7777

7878
const startTs = items[0]?.createdAt;
7979
const endTs = items[items.length - 1]?.createdAt;
@@ -96,11 +96,11 @@ export function CursorPaginationButtons({
9696

9797
const { startFormatted, endFormatted } = getStartAndEndFormatted(startTs, endTs, language);
9898

99-
const previousDisabled = start <= 1;
99+
const previousDisabled = !hasPreviousPage;
100100
const nextDisabled = !hasNextPage;
101101
return (
102102
<div className="flex items-center justify-end gap-2">
103-
{startFormatted !== '' && endFormatted !== '' ? (
103+
{!hideBoundaries && startFormatted !== '' && endFormatted !== '' ? (
104104
<Trans
105105
t={t}
106106
i18nKey="common:items_displayed"

packages/app-builder/src/hooks/useCursorPaginatedFetcher.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@ export const useCursorPaginatedFetcher = <T, D = T>({
6464
next,
6565
previous,
6666
reset,
67+
hasPreviousPage: paginationState.hasPreviousPage,
6768
};
6869
};

packages/app-builder/src/hooks/useCursorPagination.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ export type CursorPaginationState = {
77
previousCursors: string[];
88
lastAction: PaginationAction;
99
isPristine: boolean;
10+
hasPreviousPage: boolean;
1011
};
1112

1213
const INITIAL_STATE: CursorPaginationState = {
1314
cursor: null,
1415
previousCursors: [],
1516
lastAction: 'next',
1617
isPristine: true,
18+
hasPreviousPage: false,
1719
};
1820

1921
export const useCursorPagination = () => {
@@ -28,17 +30,22 @@ export const useCursorPagination = () => {
2830
: [],
2931
lastAction: 'next',
3032
isPristine: false,
33+
hasPreviousPage: true,
3134
}));
3235
}, []);
3336

3437
const previous = useCallback(() => {
35-
setState((currentState) => ({
36-
...currentState,
37-
cursor: currentState.previousCursors.pop() ?? null,
38-
previousCursors: [...currentState.previousCursors],
39-
lastAction: 'previous',
40-
isPristine: false,
41-
}));
38+
setState((currentState) => {
39+
const hasPreviousPage = currentState.previousCursors.length > 0;
40+
return {
41+
...currentState,
42+
cursor: currentState.previousCursors.pop() ?? null,
43+
previousCursors: [...currentState.previousCursors],
44+
lastAction: 'previous',
45+
isPristine: false,
46+
hasPreviousPage,
47+
};
48+
});
4249
}, []);
4350

4451
const reset = useCallback(() => {

packages/app-builder/src/routes/_builder+/cases+/inboxes.$inboxId.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export default function Cases() {
150150
pagination: initialPagination,
151151
} = useLoaderData<typeof loader>();
152152

153-
const { data, next, previous, reset } = useCursorPaginatedFetcher<
153+
const { data, next, previous, reset, hasPreviousPage } = useCursorPaginatedFetcher<
154154
typeof loader,
155155
PaginatedResponse<Case>
156156
>({
@@ -259,6 +259,8 @@ export default function Cases() {
259259
onPaginationChange={(paginationParams: PaginationParams) =>
260260
navigateCasesList(filters, paginationParams)
261261
}
262+
hideBoundaries
263+
hasPreviousPage={hasPreviousPage}
262264
{...pagination}
263265
/>
264266
</CasesFiltersProvider>

packages/app-builder/src/routes/_builder+/decisions+/_index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export default function Decisions() {
113113
inboxes,
114114
} = useLoaderData<typeof loader>();
115115

116-
const { data, next, previous, reset } = useCursorPaginatedFetcher<
116+
const { data, next, previous, reset, hasPreviousPage } = useCursorPaginatedFetcher<
117117
typeof loader,
118118
PaginatedResponse<Decision>
119119
>({
@@ -198,6 +198,7 @@ export default function Decisions() {
198198
onPaginationChange={(paginationParams: PaginationParams) =>
199199
navigateDecisionList(filters, paginationParams)
200200
}
201+
hasPreviousPage={hasPreviousPage}
201202
{...pagination}
202203
/>
203204
</DecisionFiltersProvider>

0 commit comments

Comments
 (0)