diff --git a/webapp/src/apiService/index.ts b/webapp/src/apiService/index.ts index 5355d2a..2f6ca02 100644 --- a/webapp/src/apiService/index.ts +++ b/webapp/src/apiService/index.ts @@ -3,8 +3,9 @@ import { makeAutomationAPI } from "./automations"; import { makeRemoteAPI } from "./base"; import { useAPIService } from "./core"; import { useAutoMockAPI, useProfileMockAPI } from "./mock"; -import { useRef } from "react"; +import { createContext, useContext, useRef } from "react"; import { makeProfileAPI } from "./profile"; +import { ApiState } from "./types"; const locationPrefixWeb = window.location.pathname.match(/(\/.+\/)web/i); const baseURL = new URL( @@ -25,3 +26,10 @@ export const useMockApiService = ( makeAutomationAPI(useAutoMockAPI(initialAutos, useRef, returnErrors)), makeProfileAPI(useProfileMockAPI()) ); + +const ApiStateContext = createContext({ + automations: { ready: false }, + userProfile: { ready: false }, +}); +export const ApiStateProvider = ApiStateContext.Provider; +export const useApiState = () => useContext(ApiStateContext); diff --git a/webapp/src/components/BottomBar/index.tsx b/webapp/src/components/BottomBar/index.tsx index 2b92796..925da50 100644 --- a/webapp/src/components/BottomBar/index.tsx +++ b/webapp/src/components/BottomBar/index.tsx @@ -16,7 +16,7 @@ export const BottomBar: FC = ({ api }) => { return (
- + diff --git a/webapp/src/components/Modal/index.tsx b/webapp/src/components/Modal/index.tsx index 57bef5b..ce32db3 100644 --- a/webapp/src/components/Modal/index.tsx +++ b/webapp/src/components/Modal/index.tsx @@ -9,14 +9,15 @@ export interface ModalStyleProps { } export interface Props extends ModalStyleProps { children: ReactNode; + className?: string; } -export const Modal: FC = ({ children, open }) => { +export const Modal: FC = ({ children, open, className }) => { const { isMobile } = useWindowSize(); return (
- {children} + {children}
diff --git a/webapp/src/components/Page/index.tsx b/webapp/src/components/Page/index.tsx index af13640..4783fe9 100644 --- a/webapp/src/components/Page/index.tsx +++ b/webapp/src/components/Page/index.tsx @@ -15,7 +15,7 @@ import { ConfirmProvider } from "material-ui-confirm"; import { Color } from "@mui/material"; import useWindowSize from "utils/useWindowSize"; import { ApiService } from "apiService/core"; -import { useMockApiService } from "apiService"; +import { ApiStateProvider, useMockApiService } from "apiService"; export type PageProps = PropsWithChildren<{ api: ApiService; @@ -100,7 +100,9 @@ export const Page: FC = (props) => { dense preventDuplicate > - + + + diff --git a/webapp/src/lang/index.ts b/webapp/src/lang/index.ts index e05ec9e..77e3709 100644 --- a/webapp/src/lang/index.ts +++ b/webapp/src/lang/index.ts @@ -1,3 +1,4 @@ +import { useApiState } from "apiService"; import { useEffect, useState } from "react"; const MISSING_TEXT = "<>"; @@ -5,6 +6,7 @@ const LOADING_TEXT = "..."; export type LangStore = ReturnType; export const useLang = () => { + const apiState = useApiState(); const [langStore, setLangStore] = useState | null>(null); const [langEngStore, setLangEngStore] = @@ -12,13 +14,16 @@ export const useLang = () => { useEffect(() => { import("./eng.json").then((data) => { - // setLangStore(data.default); setLangEngStore(data.default); }); - import("./eng.json").then((data) => { - setLangStore(data.default); - }); }, []); + useEffect(() => { + if (apiState.userProfile.ready && apiState.userProfile.ok) { + import(`./${apiState.userProfile.data.lang}.json`).then((data) => { + setLangStore(data.default); + }); + } + }, [apiState.userProfile]); const updateParams = ( textTemplate: string, @@ -33,15 +38,20 @@ export const useLang = () => { }; return { - get: (langId: string, replacements: Record = {}) => - updateParams( - langStore === null || langEngStore === null - ? LOADING_TEXT - : langStore[langId.toLocaleUpperCase()] ?? - langEngStore[langId.toLocaleUpperCase()] ?? - `<<${langStore.MISSING_TEXT}>>` ?? - MISSING_TEXT, - { ...replacements, langId } - ), + get: (langId: string, replacements: Record = {}) => { + let out = LOADING_TEXT; + if (langStore !== null) { + out = + langStore[langId.toLocaleUpperCase()] ?? + `<<${langStore.MISSING_TEXT}>>` ?? + MISSING_TEXT; + } else if (langEngStore !== null) { + out = + langEngStore[langId.toLocaleUpperCase()] ?? + `<<${langEngStore.MISSING_TEXT}>>` ?? + MISSING_TEXT; + } + return updateParams(out, { ...replacements, langId }); + }, }; };