47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import type { QueryClient } from "@tanstack/react-query";
|
|
|
|
import {
|
|
apiQueryClient,
|
|
type paths,
|
|
} from "@/shared/api";
|
|
|
|
type TbankConnectionStatus
|
|
= paths["/api/v1/tbank-connection"]["get"]["responses"][200]["content"]["application/json"];
|
|
|
|
function isInvestmentPath(path: string) {
|
|
return [
|
|
"/api/v1/brokerage-accounts",
|
|
"/api/v1/stocks",
|
|
"/api/v1/bonds",
|
|
].some(root => path === root || path.startsWith(`${root}/`));
|
|
}
|
|
|
|
const tbankConnectionQueryOptions = () =>
|
|
apiQueryClient.queryOptions("get", "/api/v1/tbank-connection");
|
|
|
|
const tbankConnectionQueryKey = tbankConnectionQueryOptions().queryKey;
|
|
|
|
function commitTbankConnectionBoundary(
|
|
queryClient: QueryClient,
|
|
status: TbankConnectionStatus,
|
|
) {
|
|
queryClient.setQueryData(tbankConnectionQueryKey, status);
|
|
void queryClient.resetQueries({
|
|
predicate: ({ queryKey }) => {
|
|
const [method, path] = queryKey;
|
|
|
|
return (
|
|
method === "get"
|
|
&& typeof path === "string"
|
|
&& isInvestmentPath(path)
|
|
);
|
|
},
|
|
});
|
|
}
|
|
|
|
export {
|
|
commitTbankConnectionBoundary,
|
|
tbankConnectionQueryOptions,
|
|
type TbankConnectionStatus,
|
|
};
|