feat: add operations page
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { apiQueryClient } from "@/shared/api";
|
||||
import type { paths } from "@/shared/api/generated/schema";
|
||||
|
||||
type OperationsEndpoint =
|
||||
paths["/api/v1/brokerage-accounts/{accountId}/operations"]["get"];
|
||||
|
||||
export type BrokerageOperation =
|
||||
OperationsEndpoint["responses"][200]["content"]["application/json"]["items"][number];
|
||||
type BrokerageOperationsQuery = NonNullable<
|
||||
OperationsEndpoint["parameters"]["query"]
|
||||
>;
|
||||
export type OperationCategory = BrokerageOperation["category"];
|
||||
|
||||
export const operationCategories = [
|
||||
"trade",
|
||||
"income",
|
||||
"tax",
|
||||
"fee",
|
||||
"transfer",
|
||||
"other",
|
||||
] as const satisfies readonly OperationCategory[];
|
||||
|
||||
export function brokerageOperationsQueryParams(
|
||||
query: BrokerageOperationsQuery,
|
||||
) {
|
||||
return {
|
||||
...(query.from ? { from: query.from } : {}),
|
||||
...(query.to ? { to: query.to } : {}),
|
||||
...(query.categories?.length
|
||||
? { categories: [...query.categories].sort() }
|
||||
: {}),
|
||||
...(query.cursor ? { cursor: query.cursor } : {}),
|
||||
...(query.limit ? { limit: query.limit } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function useBrokerageOperations(
|
||||
accountId: string | undefined,
|
||||
accessToken: string | null,
|
||||
query: BrokerageOperationsQuery = {},
|
||||
) {
|
||||
const params = brokerageOperationsQueryParams(query);
|
||||
return useQuery({
|
||||
queryKey: ["brokerage-operations", accountId ?? "", params] as const,
|
||||
enabled: Boolean(accountId && accessToken),
|
||||
staleTime: 30_000,
|
||||
retry: false,
|
||||
...apiQueryClient.queryOptions(
|
||||
"get",
|
||||
"/api/v1/brokerage-accounts/{accountId}/operations",
|
||||
{ params: { path: { accountId: accountId ?? "" }, query: params } },
|
||||
),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user