44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
![]() |
import { useState } from "react";
|
||
|
import useSWRInfinite from "swr/infinite";
|
||
|
import { enqueueSnackbar } from "notistack";
|
||
|
|
||
|
import { historyApi } from "./requests";
|
||
|
|
||
|
export function useHistory(accountId: string) {
|
||
|
const [currentPage, setCurrentPage] = useState<number>(1);
|
||
|
|
||
|
const swrResponse = useSWRInfinite(
|
||
|
() => `history-${currentPage}`,
|
||
|
async () => {
|
||
|
const [historyResponse, error] = await historyApi.getUserHistory(
|
||
|
accountId,
|
||
|
currentPage
|
||
|
);
|
||
|
|
||
|
if (error) {
|
||
|
throw new Error(error);
|
||
|
}
|
||
|
|
||
|
if (!historyResponse) {
|
||
|
throw new Error("Empty history data");
|
||
|
}
|
||
|
|
||
|
if (currentPage < historyResponse.totalPages) {
|
||
|
setCurrentPage((page) => page + 1);
|
||
|
}
|
||
|
|
||
|
return historyResponse;
|
||
|
},
|
||
|
{
|
||
|
onError(err) {
|
||
|
console.log("Error fetching users", err);
|
||
|
enqueueSnackbar(err.message, { variant: "error" });
|
||
|
},
|
||
|
focusThrottleInterval: 60e3,
|
||
|
keepPreviousData: true,
|
||
|
}
|
||
|
);
|
||
|
|
||
|
return swrResponse;
|
||
|
}
|