refactor: clarify frontend model ownership
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
export { section } from "./model/section";
|
||||
export { useInstrumentOperations } from "./model/useInstrumentOperations";
|
||||
export { section, type SectionState } from "./model/section";
|
||||
export { InstrumentChart } from "./ui/InstrumentChart/InstrumentChart.view";
|
||||
export { InstrumentEvents } from "./ui/InstrumentEvents.view";
|
||||
export { InstrumentPosition } from "./ui/InstrumentPosition.view";
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import type { paths } from "@/shared/api";
|
||||
|
||||
type Meta
|
||||
= paths["/api/v1/tbank-sync/{id}"]["get"]["responses"][200]["content"]["application/json"]["resources"][number];
|
||||
import type { InvestmentResourceMeta } from "@/shared/api";
|
||||
|
||||
type SectionState = {
|
||||
error: boolean;
|
||||
@@ -20,7 +17,7 @@ function section(
|
||||
data: unknown;
|
||||
isError: boolean;
|
||||
isFetching: boolean;
|
||||
meta?: Meta;
|
||||
meta?: InvestmentResourceMeta;
|
||||
refetch: () => unknown;
|
||||
},
|
||||
retry?: () => unknown,
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { useState } from "react";
|
||||
|
||||
import {
|
||||
brokerageOperationsQueryOptions,
|
||||
type OperationCategory,
|
||||
useBrokerageOperations,
|
||||
} from "@/entities/broker-operation";
|
||||
import { ApiErrorCode, apiErrorCode, isApiErrorCode } from "@/shared/api";
|
||||
|
||||
function useInstrumentOperations(
|
||||
uid: string,
|
||||
filter: string,
|
||||
categories: OperationCategory[],
|
||||
) {
|
||||
const scope = `${uid}:${filter}`;
|
||||
|
||||
const [pagination, setPagination] = useState<{
|
||||
cursors: string[];
|
||||
scope: string;
|
||||
}>({ cursors: [], scope });
|
||||
|
||||
const cursors = pagination.scope === scope ? pagination.cursors : [];
|
||||
const client = useQueryClient();
|
||||
const firstPage = { categories, instrumentUid: uid, limit: 20 };
|
||||
|
||||
const operations = useBrokerageOperations(uid ? "all" : undefined, {
|
||||
...firstPage,
|
||||
cursor: cursors.at(-1),
|
||||
});
|
||||
|
||||
const retry = (retryScreen: () => unknown) => {
|
||||
if (
|
||||
isApiErrorCode(
|
||||
apiErrorCode(operations.error),
|
||||
ApiErrorCode.InvalidOperationsCursor,
|
||||
)
|
||||
) {
|
||||
setPagination({ cursors: [], scope });
|
||||
void client.invalidateQueries({
|
||||
exact: true,
|
||||
queryKey: brokerageOperationsQueryOptions("all", firstPage).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,
|
||||
retry,
|
||||
};
|
||||
}
|
||||
|
||||
export { useInstrumentOperations };
|
||||
@@ -6,18 +6,15 @@ import {
|
||||
apiErrorCode,
|
||||
apiQueryClient,
|
||||
invalidateResourceAccess,
|
||||
type InvestmentResourceMeta,
|
||||
isApiErrorCode,
|
||||
type paths,
|
||||
useAuthenticatedQueries,
|
||||
} from "@/shared/api";
|
||||
|
||||
import { type InvestmentScreen, useTbankSync } from "./sync";
|
||||
|
||||
type ResourceMeta
|
||||
= paths["/api/v1/tbank-sync/{id}"]["get"]["responses"][200]["content"]["application/json"]["resources"][number];
|
||||
|
||||
type ScreenResource = {
|
||||
meta?: ResourceMeta;
|
||||
meta?: InvestmentResourceMeta;
|
||||
observe: (version?: string) => Promise<unknown>;
|
||||
queryKey: QueryKey;
|
||||
transport: {
|
||||
@@ -34,7 +31,7 @@ const STATUS_INTERVAL_MS = 1000;
|
||||
|
||||
function observeResource(
|
||||
resource: ScreenResource,
|
||||
latest: Map<string, ResourceMeta>,
|
||||
latest: Map<string, InvestmentResourceMeta>,
|
||||
completedId: string | undefined,
|
||||
observed: Map<string, string>,
|
||||
) {
|
||||
@@ -143,7 +140,7 @@ function useScreenSync(scope: InvestmentScreen, resources: ScreenResource[]) {
|
||||
invalidateResourceAccess(client, "brokerage", status.error);
|
||||
}
|
||||
|
||||
const latest = new Map<string, ResourceMeta>();
|
||||
const latest = new Map<string, InvestmentResourceMeta>();
|
||||
|
||||
const orderedStatuses = statuses.toSorted(
|
||||
(a, b) => a.dataUpdatedAt - b.dataUpdatedAt,
|
||||
|
||||
Reference in New Issue
Block a user