Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | 1x 24x 24x 24x 24x 24x 24x 24x 39x 24x 39x 24x 39x 24x 24x 24x 24x 24x 14x 8x 24x 1x 1x 1x 1x 1x 24x 8x 8x 8x 24x 24x 8x 8x 24x 24x | import { useMemo, useRef, useState } from "react";
import { useQueryClient } from "react-query";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import { selectQueryString } from "@redux/slices/search";
import { selectSelectedMerchant } from "@redux/slices/merchantFilters";
import { useGetCurrentMerchantId } from "@hooks/common";
import { CursorValue, PaginationMethodType } from "@components/VirtualList/api";
import { useGetInfiniteTransactionsV2 } from "@components/VirtualList/hooks/queries";
import {
TRANSFER_MERCHANTS_FILTER_KEY,
TransferTableRow,
} from "@components/Tranfers/utils";
import { resetFilterByKey } from "@redux/slices/dynamicFilterSlice";
import { ROWS_PER_PAGE } from "@hooks/common/usePagination";
import { TRANSFERS_QUERY_KEY } from "@components/Tranfers/hooks/useTransfersTable";
import { sortingKey } from "@redux/slices/fundraisers";
import { useFormatManageMoneyFilter } from "@features/MerchantPortal/ManageMoney/hooks/useFormatManageMoneyFilters";
import { useStateEffect } from "@hooks/customReactCore";
export const useListTransfersTransactions = ({
paginationMethod = "cursor",
rowsPerPage = ROWS_PER_PAGE,
cursorPropertyName = "id",
}: {
paginationMethod?: PaginationMethodType;
rowsPerPage?: number;
cursorPropertyName?: string;
} = {}) => {
const dispatch = useAppDispatch();
const queryClient = useQueryClient();
const [page, setPageDispatcher] = useState(1);
const nextCursorValue = useRef<null | CursorValue>(null);
const pageRef = useRef<number | null>(1);
const { merchantId } = useGetCurrentMerchantId();
const selectedMerchant = useAppSelector((state) =>
selectSelectedMerchant(state, TRANSFER_MERCHANTS_FILTER_KEY),
);
const searchQuery = useAppSelector((state) =>
selectQueryString(state, TRANSFERS_QUERY_KEY),
);
const sorting = useAppSelector((state) =>
sortingKey(state, TRANSFERS_QUERY_KEY),
);
const selectedMerchantID = selectedMerchant?.accID || merchantId;
const { formattedFilterString } =
useFormatManageMoneyFilter(TRANSFERS_QUERY_KEY);
const { data, fetchNextPage, isLoading, isFetching, isFetchingNextPage } =
useGetInfiniteTransactionsV2(
{
queryString: formattedFilterString,
path: "transfers",
rowsPerPage,
sorting,
searchQuery,
merchantId: selectedMerchantID,
cursorPropertyName,
sortPropertyName: sorting.replace("-", ""),
nextCursorValue,
pageRef,
paginationMethod,
},
{
queryKey: [
TRANSFERS_QUERY_KEY,
selectedMerchantID,
searchQuery,
sorting,
formattedFilterString,
],
enabled: true,
},
);
const hasNextPage = Boolean(
paginationMethod === "cursor" ? nextCursorValue.current : pageRef.current,
);
// Flatten all pages into one array
const allRows = useMemo<TransferTableRow[]>(
() =>
data?.pages?.[0]?.data
? data.pages.flatMap((page: any) => page.data ?? [])
: [],
[data],
);
// Pagination behavior
const handlePageChange = () => {
Eif (!isFetchingNextPage && hasNextPage) {
const nextPage =
paginationMethod === "offset"
? (pageRef.current ?? 0) + 1
: nextCursorValue.current;
fetchNextPage({ pageParam: nextPage });
Iif (paginationMethod === "offset") pageRef.current = nextPage as number;
setPageDispatcher((prev) => prev + 1);
}
};
const resetPagination = () => {
Eif (paginationMethod === "cursor") {
nextCursorValue.current = null;
return;
}
pageRef.current = 1;
setPageDispatcher(1);
};
// Filter reset
const handleResetFilters = () => {
dispatch(resetFilterByKey({ filterKey: TRANSFERS_QUERY_KEY }));
};
// Reset pagination when filters/sorting/search change
useStateEffect(() => {
queryClient.invalidateQueries([TRANSFERS_QUERY_KEY]);
resetPagination();
}, [sorting, searchQuery, formattedFilterString]);
const hasFilters = Boolean(formattedFilterString);
return {
// Data state
allRows,
totalRows:
paginationMethod === "offset"
? data?.pages?.[0]?.total ?? 0
: allRows.length,
rowsPerPage,
// Pagination
page,
setPage: handlePageChange,
setPageDispatcher,
resetPagination,
// Filters
hasFilters,
handleResetFilters,
// Loading
isLoading,
isFetching,
isFetchingNextPage,
hasNextPage,
// Cursor
nextCursor: nextCursorValue.current,
};
};
|