refactor: clarify frontend model ownership
This commit is contained in:
@@ -68,6 +68,7 @@ function useBrokerageOperations(
|
||||
|
||||
export {
|
||||
type BrokerageOperation,
|
||||
type BrokerageOperationsQuery,
|
||||
brokerageOperationsQueryOptions,
|
||||
brokerageOperationsQueryParameters as brokerageOperationsQueryParams,
|
||||
operationCategories,
|
||||
|
||||
@@ -5,4 +5,5 @@ export {
|
||||
type OperationCategory,
|
||||
useBrokerageOperations,
|
||||
} from "./api/useBrokerageOperations";
|
||||
export { useBrokerageOperationHistory } from "./model/useBrokerageOperationHistory";
|
||||
export { LedgerEntryRow } from "./ui/LedgerEntryRow";
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import { hashKey, useQueryClient } from "@tanstack/react-query";
|
||||
import { useState } from "react";
|
||||
|
||||
import { ApiErrorCode, apiErrorCode, isApiErrorCode } from "@/shared/api";
|
||||
|
||||
import {
|
||||
type BrokerageOperationsQuery,
|
||||
brokerageOperationsQueryOptions,
|
||||
useBrokerageOperations,
|
||||
} from "../api/useBrokerageOperations";
|
||||
|
||||
function useBrokerageOperationHistory(
|
||||
accountId: string | undefined,
|
||||
firstPage: Omit<BrokerageOperationsQuery, "cursor">,
|
||||
) {
|
||||
const { queryKey } = brokerageOperationsQueryOptions(accountId, firstPage);
|
||||
const scope = hashKey(queryKey);
|
||||
|
||||
const [pagination, setPagination] = useState<{
|
||||
cursors: string[];
|
||||
scope: string;
|
||||
}>({ cursors: [], scope });
|
||||
|
||||
const cursors = pagination.scope === scope ? pagination.cursors : [];
|
||||
const client = useQueryClient();
|
||||
|
||||
const operations = useBrokerageOperations(accountId, {
|
||||
...firstPage,
|
||||
cursor: cursors.at(-1),
|
||||
});
|
||||
|
||||
const reset = () => setPagination({ cursors: [], scope });
|
||||
|
||||
const retry = (retryScreen: () => unknown) => {
|
||||
if (
|
||||
isApiErrorCode(
|
||||
apiErrorCode(operations.error),
|
||||
ApiErrorCode.InvalidOperationsCursor,
|
||||
)
|
||||
) {
|
||||
reset();
|
||||
void client.invalidateQueries({
|
||||
exact: true,
|
||||
queryKey,
|
||||
});
|
||||
}
|
||||
else void retryScreen();
|
||||
};
|
||||
|
||||
return {
|
||||
hasPrevious: cursors.length > 0,
|
||||
onNext: () => {
|
||||
if (operations.data?.nextCursor)
|
||||
setPagination({
|
||||
cursors: [...cursors, operations.data.nextCursor],
|
||||
scope,
|
||||
});
|
||||
},
|
||||
onPrevious: () => setPagination({ cursors: cursors.slice(0, -1), scope }),
|
||||
operations,
|
||||
reset,
|
||||
retry,
|
||||
};
|
||||
}
|
||||
|
||||
export { useBrokerageOperationHistory };
|
||||
Reference in New Issue
Block a user