Skip to content

Commit

Permalink
refactor: fully control pagination values
Browse files Browse the repository at this point in the history
  • Loading branch information
dohaki committed Nov 8, 2023
1 parent 3f46efa commit bf2450d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 62 deletions.
9 changes: 5 additions & 4 deletions src/components/DepositsTable/DataRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ type Props = {
deposit: Deposit;
headerCells: HeaderCells;
disabledColumns?: ColumnKey[];
onClickSpeedUp?: () => void;
onClickSpeedUp?: (deposit: Deposit) => void;
};

const config = getConfig();

const MAX_PENDING_STATE_TIME_UNTIL_SLOW = 15 * 60; // 15 mins
const MAX_PENDING_STATE_TIME_UNTIL_SLOW = 30 * 60; // 30 mins

function isColumnDisabled(disabledColumns: ColumnKey[], column: ColumnKey) {
return disabledColumns.includes(column);
Expand All @@ -59,8 +59,9 @@ export function DataRow({
);
const isSlowRelay =
deposit.status === "pending" &&
DateTime.fromSeconds(deposit.depositTime).diffNow("seconds").as("seconds") <
MAX_PENDING_STATE_TIME_UNTIL_SLOW;
Math.abs(
DateTime.fromSeconds(deposit.depositTime).diffNow("seconds").as("seconds")
) > MAX_PENDING_STATE_TIME_UNTIL_SLOW;

// Hide unsupported or unknown token deposits
if (!token) {
Expand Down
3 changes: 1 addition & 2 deletions src/components/DepositsTable/DepositsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Deposit } from "hooks/useDeposits";

export type Props = {
disabledColumns?: ColumnKey[];
onClickSpeedUp?: () => void;
onClickSpeedUp?: (deposit: Deposit) => void;
deposits: Deposit[];
};

Expand Down Expand Up @@ -42,7 +42,6 @@ const Wrapper = styled.div`
`;

const StyledTable = styled.table`
width: 1666px;
white-space: nowrap;
table-layout: fixed;
`;
2 changes: 1 addition & 1 deletion src/components/DepositsTable/HeadRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const headerCells = {
},
route: {
label: "Route",
width: 206,
width: 144,
},
address: {
label: "Address",
Expand Down
31 changes: 16 additions & 15 deletions src/components/DepositsTable/PaginatedDepositsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import styled from "@emotion/styled";

import Pagination from "components/Pagination";
import { usePagination } from "hooks/usePagination";
import Pagination, { paginate } from "components/Pagination";
import { DepositsTable, Props as DepositsTableProps } from "./DepositsTable";

type Props = DepositsTableProps & {
onPageChange: (currentPage: number) => void;
onPageSizeChange: (currentPageSize: number) => void;
currentPage: number;
onPageChange: (newPage: number) => void;
currentPageSize: number;
onPageSizeChange: (newPageSize: number) => void;
totalCount: number;
initialPageSize?: number;
pageSizes?: number[];
Expand All @@ -15,29 +16,29 @@ type Props = DepositsTableProps & {
const DEFAULT_PAGE_SIZES = [10, 25, 50];

export function PaginatedDepositsTable({
currentPage,
onPageChange,
currentPageSize,
onPageSizeChange,
totalCount,
initialPageSize = DEFAULT_PAGE_SIZES[0],
pageSizes = DEFAULT_PAGE_SIZES,
...depositsTableProps
}: Props) {
const { pageSize, currentPage, setCurrentPage, setPageSize, paginateValues } =
usePagination(totalCount, { initialPageSize });
const paginateValues = paginate({
elementCount: totalCount,
currentPage,
maxNavigationCount: 5,
elementsPerPage: currentPageSize,
});

return (
<>
<DepositsTable {...depositsTableProps} />
<PaginationWrapper>
<Pagination
onPageChange={(newPage) => {
setCurrentPage(newPage);
onPageChange(newPage);
}}
onPageSizeChange={(newPageSize) => {
setPageSize(newPageSize);
onPageSizeChange(newPageSize);
}}
onPageChange={onPageChange}
onPageSizeChange={onPageSizeChange}
pageList={paginateValues.pageList}
activeIndex={paginateValues.activeIndex}
disableBack={paginateValues.disableBack}
Expand All @@ -46,7 +47,7 @@ export function PaginatedDepositsTable({
hideEnd={paginateValues.hideEnd}
lastPage={paginateValues.lastPage}
currentPage={currentPage}
pageSize={pageSize}
pageSize={currentPageSize}
pageSizes={pageSizes}
/>
</PaginationWrapper>
Expand Down
11 changes: 8 additions & 3 deletions src/components/DepositsTable/cells/ActionsCell.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useCallback } from "react";
import styled from "@emotion/styled";

import { ReactComponent as ZapIcon } from "assets/zap.svg";
Expand All @@ -11,7 +12,7 @@ type Props = {
deposit: Deposit;
isSlowRelay?: boolean;
isProfitable?: boolean;
onClickSpeedUp?: () => void;
onClickSpeedUp?: (deposit: Deposit) => void;
};

export function ActionsCell({
Expand Down Expand Up @@ -41,12 +42,16 @@ export function ActionsCell({
</Tooltip>
) : null;

const handleClickSpeedUp = useCallback(() => {
onClickSpeedUp?.(deposit);
}, [deposit, onClickSpeedUp]);

const speedUp =
deposit.status === "pending" ? (
isProfitable ? (
<ZapIconOnHover id="speed-up-icon" onClick={onClickSpeedUp} />
<ZapIconOnHover id="speed-up-icon" onClick={handleClickSpeedUp} />
) : (
<ZapIconPersistent onClick={onClickSpeedUp} />
<ZapIconPersistent onClick={handleClickSpeedUp} />
)
) : null;

Expand Down
9 changes: 9 additions & 0 deletions src/components/Pagination/Pagination.styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export const PageSizeSelectButton = styled(UnstyledButton)`
svg {
margin-left: 6px;
}
@media ${QUERIESV2.sm.andDown} {
margin-bottom: 1rem;
padding: 10px 20px;
}
`;

const PageSelectDropdownRevealAnimation = keyframes`
Expand Down Expand Up @@ -83,6 +88,10 @@ export const PageSizeOptiontButton = styled(UnstyledButton)`
background-color: var(--color-primary);
color: var(--color-gray);
}
@media ${QUERIESV2.sm.andDown} {
padding: 6px 20px;
}
`;

export const PaginationElements = styled.div`
Expand Down
37 changes: 0 additions & 37 deletions src/hooks/usePagination.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/stories/PaginatedDepositsTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export default meta;
type Story = StoryObj<typeof PaginatedDepositsTable>;

const BasicPagination = () => {
const [currentPage, setCurrentPage] = useState(0);
const [pageSize, setPageSize] = useState(1);
const [deposits, setDeposits] = useState<Deposit[]>(
mockedDeposits.slice(0, 1)
Expand All @@ -171,9 +172,12 @@ const BasicPagination = () => {
return (
<PaginatedDepositsTable
deposits={deposits}
currentPage={currentPage}
onPageChange={(page) => {
setCurrentPage(page);
setDeposits(mockedDeposits.slice(page, page + pageSize));
}}
currentPageSize={pageSize}
onPageSizeChange={(size) => {
setPageSize(size);
setDeposits(mockedDeposits.slice(0, size));
Expand Down

0 comments on commit bf2450d

Please sign in to comment.