From dd508d834b20e3e92ecfa1db2fc65509d15e2395 Mon Sep 17 00:00:00 2001 From: porink0424 Date: Wed, 24 Apr 2024 16:55:59 +0900 Subject: [PATCH] Pass apiClient via context --- optuna_dashboard/ts/apiClientProvider.tsx | 32 ++++ optuna_dashboard/ts/components/App.tsx | 196 +++++++++++----------- 2 files changed, 133 insertions(+), 95 deletions(-) create mode 100644 optuna_dashboard/ts/apiClientProvider.tsx diff --git a/optuna_dashboard/ts/apiClientProvider.tsx b/optuna_dashboard/ts/apiClientProvider.tsx new file mode 100644 index 00000000..2383b1c6 --- /dev/null +++ b/optuna_dashboard/ts/apiClientProvider.tsx @@ -0,0 +1,32 @@ +import React, { createContext, useContext } from "react" +import { APIClient } from "./apiClient" + +type APIClientContextType = { + apiClient: APIClient +} + +export const APIClientContext = createContext( + undefined +) + +export const useAPIClient = (): APIClientContextType => { + const context = useContext(APIClientContext) + if (context === undefined) { + throw new Error("useAPIClient must be used within a APIClientProvider.") + } + return context +} + +export function APIClientProvider({ + apiClient, + children, +}: { + apiClient: APIClient + children: React.ReactNode +}) { + return ( + + {children} + + ) +} diff --git a/optuna_dashboard/ts/components/App.tsx b/optuna_dashboard/ts/components/App.tsx index c915f7c7..1204ae8f 100644 --- a/optuna_dashboard/ts/components/App.tsx +++ b/optuna_dashboard/ts/components/App.tsx @@ -13,10 +13,14 @@ import { BrowserRouter as Router, Route, Routes } from "react-router-dom" import { RecoilRoot } from "recoil" import { QueryClient, QueryClientProvider } from "@tanstack/react-query" +import { APIClientProvider } from "../apiClientProvider" +import { AxiosClient } from "../axiosClient" import { CompareStudies } from "./CompareStudies" import { StudyDetail } from "./StudyDetail" import { StudyList } from "./StudyList" +const axiosAPIClient = new AxiosClient() + const queryClient = new QueryClient({ defaultOptions: { queries: { @@ -50,100 +54,102 @@ export const App: FC = () => { } return ( - - - - - - - - - - } - /> - - } - /> - - } - /> - - } - /> - - } - /> - - } - /> - - } - /> - - } - /> - } - /> - - - - - - - + + + + + + + + + + + } + /> + + } + /> + + } + /> + + } + /> + + } + /> + + } + /> + + } + /> + + } + /> + } + /> + + + + + + + + ) }