44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import { MIN_BOND_SEARCH_LENGTH, useBondSearch } from "@/entities/bond";
|
|
import { useInstrumentSearch } from "@/features/instrument-search";
|
|
import { useScreenSync } from "@/features/tbank-connection";
|
|
import { apiErrorCode } from "@/shared/api";
|
|
import { instrumentLogoUrl } from "@/shared/lib";
|
|
|
|
type BondsPageVm = ReturnType<typeof useBondsPageVm>;
|
|
|
|
function useBondsPageVm() {
|
|
const search = useInstrumentSearch(MIN_BOND_SEARCH_LENGTH);
|
|
const { canSearch, debounced, isWaiting, query } = search;
|
|
const result = useBondSearch(debounced);
|
|
useScreenSync({ query: debounced, screen: "bonds" }, [result]);
|
|
|
|
return {
|
|
canSearch,
|
|
code: apiErrorCode(result.error),
|
|
error:
|
|
!result.data
|
|
&& (result.isError || result.meta?.lastAttempt.state === "failed"),
|
|
items:
|
|
result.data?.map(bond => ({
|
|
...bond,
|
|
logoSrc: instrumentLogoUrl(bond.logoName),
|
|
to: `/bonds/${encodeURIComponent(bond.slug)}`,
|
|
})) ?? [],
|
|
loading:
|
|
canSearch
|
|
&& (isWaiting
|
|
|| (!result.data
|
|
&& !result.isError
|
|
&& result.meta?.lastAttempt.state !== "failed")),
|
|
onQueryChange: search.onQueryChange,
|
|
partial: result.meta?.completeness === "incomplete",
|
|
query,
|
|
ready:
|
|
debounced.length >= MIN_BOND_SEARCH_LENGTH
|
|
&& !isWaiting
|
|
&& Boolean(result.data),
|
|
};
|
|
}
|
|
|
|
export { type BondsPageVm, useBondsPageVm };
|