import { MIN_STOCK_SEARCH_LENGTH, useStockSearch } from "@/entities/stock"; import { useInstrumentSearch } from "@/features/instrument-search"; import { useScreenSync } from "@/features/tbank-connection"; import { apiErrorCode } from "@/shared/api"; import { instrumentLogoUrl } from "@/shared/lib"; type StocksPageVm = ReturnType; function useStocksPageVm() { const search = useInstrumentSearch(MIN_STOCK_SEARCH_LENGTH); const { canSearch, debounced, isWaiting, query } = search; const result = useStockSearch(debounced); useScreenSync({ query: debounced, screen: "stocks" }, [result]); return { canSearch, code: apiErrorCode(result.error), error: !result.data && (result.isError || result.meta?.lastAttempt.state === "failed"), items: result.data?.map(stock => ({ ...stock, logoSrc: instrumentLogoUrl(stock.logoName), to: `/stocks/${encodeURIComponent(stock.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_STOCK_SEARCH_LENGTH && !isWaiting && Boolean(result.data), }; } export { type StocksPageVm, useStocksPageVm };