Files
frontend/src/entities/broker-operation/api/useBrokerageOperations.ts
T
ksv741 4e5f564fa1 feat: show persisted investment data and sync state
Consume the provided resource-envelope API snapshot. Preserve financial snapshots during
refresh, observe synchronization without importing, and fence late responses across
account and connection changes.
2026-09-26 21:22:49 +03:00

73 lines
1.8 KiB
TypeScript

import { apiQueryClient, type paths, useResourceQuery } from "@/shared/api";
type OperationsEndpoint =
paths["/api/v1/brokerage-accounts/{accountId}/operations"]["get"];
type BrokerageOperation = NonNullable<
OperationsEndpoint["responses"][200]["content"]["application/json"]["data"]
>["items"][number];
type BrokerageOperationsQuery = Omit<
NonNullable<OperationsEndpoint["parameters"]["query"]>,
"sync"
>;
type OperationCategory = BrokerageOperation["category"];
const operationCategories = [
"trade",
"income",
"tax",
"fee",
"transfer",
"other",
] as const satisfies readonly OperationCategory[];
function brokerageOperationsQueryParams(query: BrokerageOperationsQuery) {
return {
...(query.instrumentUid ? { instrumentUid: query.instrumentUid } : {}),
...(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 } : {}),
};
}
function brokerageOperationsQueryOptions(
accountId: string | undefined,
query: BrokerageOperationsQuery = {},
) {
return apiQueryClient.queryOptions(
"get",
"/api/v1/brokerage-accounts/{accountId}/operations",
{
params: {
path: { accountId: accountId ?? "" },
query: brokerageOperationsQueryParams(query),
},
},
);
}
function useBrokerageOperations(
accountId: string | undefined,
query: BrokerageOperationsQuery = {},
) {
return useResourceQuery({
meta: { accessGroup: "brokerage" },
...brokerageOperationsQueryOptions(accountId, query),
enabled: Boolean(accountId),
staleTime: 30_000,
});
}
export {
type BrokerageOperation,
brokerageOperationsQueryOptions,
brokerageOperationsQueryParams,
type OperationCategory,
operationCategories,
useBrokerageOperations,
};