feat: show persisted investment data and sync state

Consume the provided resource-envelope API snapshot. Preserve financial snapshots during
refresh, observe synchronization without importing, and fence late responses across
account and connection changes.
This commit is contained in:
2026-09-26 21:22:49 +03:00
parent b987fd8e78
commit 4e5f564fa1
81 changed files with 4714 additions and 1420 deletions
@@ -6,6 +6,7 @@ import { MemoryRouter } from "react-router";
import { describe, expect, it, vi } from "vitest";
import { configureAuthClient } from "@/shared/api";
import { createTestQueryClient } from "@/test/query";
import { resource } from "@/test/resource";
import { server } from "@/test/server";
import { StocksPage } from "./StocksPage";
@@ -15,22 +16,24 @@ describe("stock search", () => {
server.use(
http.get("*/api/v1/stocks", ({ request }) => {
query(new URL(request.url).searchParams.get("query"));
return HttpResponse.json([
{
name: "Сбербанк",
ticker: "SBER",
instrumentUid: "unique-stock",
slug: "SBER",
logoName: "sber.png",
classCode: "TQBR",
isin: "RU0009029540",
realExchange: "REAL_EXCHANGE_MOEX",
shareType: "SHARE_TYPE_COMMON",
apiTradeAvailable: true,
price: { currency: "RUB", value: "306.12" },
priceAt: "2026-09-10T10:00:00Z",
},
]);
return HttpResponse.json(
resource([
{
name: "Сбербанк",
ticker: "SBER",
instrumentUid: "unique-stock",
slug: "SBER",
logoName: "sber.png",
classCode: "TQBR",
isin: "RU0009029540",
realExchange: "REAL_EXCHANGE_MOEX",
shareType: "SHARE_TYPE_COMMON",
apiTradeAvailable: true,
price: { currency: "RUB", value: "306.12" },
priceAt: "2026-09-10T10:00:00Z",
},
]),
);
}),
);
configureAuthClient({
@@ -32,6 +32,16 @@ const StocksPageView: FC<StocksPageViewProps> = (props) => {
{t("stock.searchSubtitle")}
</Text>
</div>
<Button
onClick={vm.retry}
disabled={vm.refreshing || vm.query.trim().length < 2}
w="fit-content"
>
{t("investment.refresh")}
</Button>
{vm.partial ? (
<Alert role="status">{t("investment.partial")}</Alert>
) : null}
<TextInput
label={t("stock.searchLabel")}
placeholder={t("stock.searchPlaceholder")}
@@ -44,7 +54,7 @@ const StocksPageView: FC<StocksPageViewProps> = (props) => {
<Text c="dimmed">{t("stock.searchHint")}</Text>
) : null}
{vm.error ? (
<Alert role="alert" color="orange">
<Alert role="status" aria-live="polite" color="orange">
<Stack gap="xs">
<Text>
{t(
@@ -142,7 +152,9 @@ const StocksPageView: FC<StocksPageViewProps> = (props) => {
))}
</Stack>
) : (
<Text c="dimmed">{t("stock.noSearchResults")}</Text>
<Text c="dimmed">
{t(vm.partial ? "investment.partial" : "stock.noSearchResults")}
</Text>
)
) : null}
</Stack>
@@ -1,6 +1,7 @@
import { useDebouncedValue } from "@mantine/hooks";
import { useSearchParams } from "react-router";
import { useStockSearch } from "@/entities/stock";
import { useScreenSync } from "@/features/tbank-connection";
import { apiErrorCode } from "@/shared/api";
import { instrumentLogoUrl } from "@/shared/lib";
@@ -11,15 +12,23 @@ function useStocksPageVm() {
const query = params.get("q") ?? "";
const [debounced] = useDebouncedValue(query.trim(), SEARCH_DEBOUNCE_MS);
const result = useStockSearch(debounced);
const sync = useScreenSync({ screen: "stocks", query: debounced }, [result]);
const waiting = query.trim() !== debounced;
return {
query,
setQuery: (q: string) => setParams(q ? { q } : {}, { replace: true }),
loading: query.trim().length >= 2 && (waiting || result.isLoading),
error: result.isError,
loading:
query.trim().length >= 2 &&
(waiting ||
(!result.data &&
!result.isError &&
result.meta?.sync.state !== "failed")),
error: result.isError || sync.sourceError || sync.transportError,
refreshing: sync.refreshing,
partial: result.meta?.availability === "partial",
code: apiErrorCode(result.error),
retry: () => {
void result.refetch();
void sync.refresh();
},
ready: debounced.length >= 2 && !waiting && Boolean(result.data),
items: