refactor: centralize frontend auth query gating

This commit is contained in:
2026-08-25 14:29:41 +03:00
parent fff36f252a
commit 268acdf2e1
24 changed files with 352 additions and 82 deletions
@@ -2,12 +2,20 @@ import { MantineProvider } from "@mantine/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import type { ReactNode } from "react";
import { describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { configureAuthClient } from "@/shared/api";
import { usePortfolio } from "./usePortfolio";
describe("usePortfolio", () => {
afterEach(() => configureAuthClient(null));
it("uses the account endpoint with a 30 second cache and no retry", async () => {
configureAuthClient({
accessToken: () => "token",
refresh: async () => true,
clear: vi.fn(),
});
const client = new QueryClient();
const wrapper = ({ children }: { children: ReactNode }) => (
<MantineProvider>
@@ -15,7 +23,7 @@ describe("usePortfolio", () => {
</MantineProvider>
);
const { result } = renderHook(() => usePortfolio("account-1", "token"), {
const { result } = renderHook(() => usePortfolio("account-1"), {
wrapper,
});
+4 -8
View File
@@ -1,5 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { apiQueryClient } from "@/shared/api";
import { apiQueryClient, useAuthenticatedQuery } from "@/shared/api";
import type { paths } from "@/shared/api/generated/schema";
const portfolioQueryPolicy = {
@@ -10,17 +9,14 @@ const portfolioQueryPolicy = {
export type Portfolio =
paths["/api/v1/brokerage-accounts/{accountId}/portfolio"]["get"]["responses"][200]["content"]["application/json"];
export function usePortfolio(
accountId: string | undefined,
accessToken: string | null,
) {
return useQuery({
export function usePortfolio(accountId: string | undefined) {
return useAuthenticatedQuery({
...apiQueryClient.queryOptions(
"get",
"/api/v1/brokerage-accounts/{accountId}/portfolio",
{ params: { path: { accountId: accountId ?? "" } } },
),
enabled: Boolean(accountId && accessToken),
enabled: Boolean(accountId),
...portfolioQueryPolicy,
});
}