Frontend security / security (push) Successful in 9s
Frontend verify / verify (push) Failing after 37m50s
Frontend security / security (pull_request) Successful in 9s
Frontend verify / verify (pull_request) Failing after 42m10s
Conventional commits / commits (pull_request) Successful in 29s
Enable every plugin recommended preset and replace Biome with ESLint formatting. Preserve API contracts and JSX whitespace; verify source, tests, builds and tooling.
108 lines
2.7 KiB
TypeScript
108 lines
2.7 KiB
TypeScript
import { apiQueryClient, type paths, useResourceQuery } from "@/shared/api";
|
|
|
|
const MIN_BOND_SEARCH_LENGTH = 2;
|
|
|
|
type BondPeriod = NonNullable<
|
|
NonNullable<
|
|
paths["/api/v1/bonds/{uid}/candles"]["get"]["parameters"]["query"]
|
|
>["period"]
|
|
>;
|
|
|
|
const BOND_STALE_TIME_MS = 30_000;
|
|
|
|
function bondQueryOptions(uid: string) {
|
|
return {
|
|
...apiQueryClient.queryOptions("get", "/api/v1/bonds/{uid}", {
|
|
params: { path: { uid } },
|
|
}),
|
|
staleTime: BOND_STALE_TIME_MS,
|
|
};
|
|
}
|
|
function useBond(identifier: string) {
|
|
const isUid
|
|
= /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
|
|
identifier,
|
|
);
|
|
|
|
const byUid = useResourceQuery({
|
|
...bondQueryOptions(identifier),
|
|
enabled: Boolean(identifier) && isUid,
|
|
});
|
|
const byTicker = useResourceQuery({
|
|
...apiQueryClient.queryOptions("get", "/api/v1/bonds/by-ticker/{ticker}", {
|
|
params: { path: { ticker: identifier } },
|
|
}),
|
|
enabled: Boolean(identifier) && !isUid,
|
|
staleTime: BOND_STALE_TIME_MS,
|
|
});
|
|
return isUid ? byUid : byTicker;
|
|
}
|
|
|
|
function useBondCandles(uid: string, period: BondPeriod) {
|
|
return useResourceQuery({
|
|
...apiQueryClient.queryOptions("get", "/api/v1/bonds/{uid}/candles", {
|
|
params: { path: { uid }, query: { period } },
|
|
}),
|
|
enabled: Boolean(uid),
|
|
staleTime: BOND_STALE_TIME_MS,
|
|
});
|
|
}
|
|
function useBondCoupons(uid: string) {
|
|
return useResourceQuery({
|
|
meta: { accessGroup: "brokerage" },
|
|
...apiQueryClient.queryOptions("get", "/api/v1/bonds/{uid}/coupons", {
|
|
params: { path: { uid } },
|
|
}),
|
|
enabled: Boolean(uid),
|
|
staleTime: BOND_STALE_TIME_MS,
|
|
});
|
|
}
|
|
function useBondMetrics(uid: string) {
|
|
return useResourceQuery({
|
|
...apiQueryClient.queryOptions("get", "/api/v1/bonds/{uid}/metrics", {
|
|
params: { path: { uid } },
|
|
}),
|
|
enabled: Boolean(uid),
|
|
staleTime: BOND_STALE_TIME_MS,
|
|
});
|
|
}
|
|
function useBondPosition(uid: string) {
|
|
return useResourceQuery({
|
|
meta: { accessGroup: "brokerage" },
|
|
...apiQueryClient.queryOptions("get", "/api/v1/bonds/{uid}/position", {
|
|
params: { path: { uid } },
|
|
}),
|
|
enabled: Boolean(uid),
|
|
staleTime: BOND_STALE_TIME_MS,
|
|
});
|
|
}
|
|
function useBondQuote(uid: string) {
|
|
return useResourceQuery({
|
|
...apiQueryClient.queryOptions("get", "/api/v1/bonds/{uid}/quote", {
|
|
params: { path: { uid } },
|
|
}),
|
|
enabled: Boolean(uid),
|
|
staleTime: BOND_STALE_TIME_MS,
|
|
});
|
|
}
|
|
function useBondSearch(query: string) {
|
|
return useResourceQuery({
|
|
...apiQueryClient.queryOptions("get", "/api/v1/bonds", {
|
|
params: { query: { query } },
|
|
}),
|
|
enabled: query.length >= MIN_BOND_SEARCH_LENGTH,
|
|
staleTime: BOND_STALE_TIME_MS,
|
|
});
|
|
}
|
|
|
|
export { type BondPeriod,
|
|
MIN_BOND_SEARCH_LENGTH,
|
|
useBond,
|
|
useBondCandles,
|
|
useBondCoupons,
|
|
useBondMetrics,
|
|
useBondPosition,
|
|
useBondQuote,
|
|
useBondSearch,
|
|
};
|