refactor: simplify auth and instrument pages
This commit is contained in:
@@ -14,35 +14,28 @@ type BondPeriod = NonNullable<
|
||||
|
||||
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,
|
||||
});
|
||||
// eslint-disable-next-line unicorn/prefer-minimal-ternary -- Hoisting params breaks the generated union of endpoint request shapes.
|
||||
const init = isUid
|
||||
? { params: { path: { uid: identifier } } }
|
||||
: { params: { path: { ticker: identifier } } };
|
||||
|
||||
const byTicker = useResourceQuery({
|
||||
...apiQueryClient.queryOptions("get", "/api/v1/bonds/by-ticker/{ticker}", {
|
||||
params: { path: { ticker: identifier } },
|
||||
}),
|
||||
enabled: Boolean(identifier) && !isUid,
|
||||
const options = apiQueryClient.queryOptions(
|
||||
"get",
|
||||
isUid ? "/api/v1/bonds/{uid}" : "/api/v1/bonds/by-ticker/{ticker}",
|
||||
init,
|
||||
);
|
||||
|
||||
return useResourceQuery({
|
||||
...options,
|
||||
enabled: Boolean(identifier),
|
||||
staleTime: BOND_STALE_TIME_MS,
|
||||
});
|
||||
|
||||
return isUid ? byUid : byTicker;
|
||||
}
|
||||
|
||||
function useBondCandles(uid: string, period: BondPeriod) {
|
||||
@@ -110,7 +103,6 @@ function useBondSearch(query: string) {
|
||||
export {
|
||||
type BondCoupons,
|
||||
type BondMetrics,
|
||||
type BondPeriod,
|
||||
MIN_BOND_SEARCH_LENGTH,
|
||||
useBond,
|
||||
useBondCandles,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
export {
|
||||
type BondCoupons,
|
||||
type BondMetrics,
|
||||
type BondPeriod,
|
||||
MIN_BOND_SEARCH_LENGTH,
|
||||
useBond,
|
||||
useBondCandles,
|
||||
|
||||
Reference in New Issue
Block a user