-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from SFTtech/milo/remove-recoil
refactor(frontend): remove recoil from web
- Loading branch information
Showing
7 changed files
with
105 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import * as React from "react"; | ||
import { z } from "zod"; | ||
import { environment } from "@/environments/environment"; | ||
import { AlertColor } from "@mui/material/Alert/Alert"; | ||
import Loading from "@/components/style/Loading"; | ||
import { Alert, AlertTitle } from "@mui/material"; | ||
|
||
const configSchema = z.object({ | ||
imprintURL: z.string().optional().nullable(), | ||
sourceCodeURL: z.string().optional(), | ||
issueTrackerURL: z.string().optional().nullable(), | ||
messages: z | ||
.array( | ||
z.object({ | ||
type: z.union([z.literal("info"), z.literal("error"), z.literal("warning"), z.literal("success")]), | ||
title: z.string().default(null).nullable(), | ||
body: z.string(), | ||
}) | ||
) | ||
.optional(), | ||
error: z.string().optional(), | ||
}); | ||
|
||
export interface StatusMessage { | ||
type: AlertColor; | ||
title: string | null; | ||
body: string; | ||
} | ||
|
||
export type Config = z.infer<typeof configSchema>; | ||
|
||
const ConfigContext = React.createContext<Config>(null as Config); | ||
|
||
export type ConfigProviderProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
type ConfigState = | ||
| { | ||
state: "loaded"; | ||
config: Config; | ||
} | ||
| { | ||
state: "loading"; | ||
} | ||
| { | ||
state: "error"; | ||
error: string; | ||
}; | ||
|
||
const invalidConfigurationMessage = "This frontend is configured incorrectly, please contact your server administrator"; | ||
|
||
export const ConfigProvider: React.FC<ConfigProviderProps> = ({ children }) => { | ||
const [state, setState] = React.useState<ConfigState>({ state: "loading" }); | ||
|
||
React.useEffect(() => { | ||
const fetcher = async () => { | ||
try { | ||
const path = environment.production ? "/config.json" : "/assets/config.json"; | ||
const resp = await fetch(path, { headers: { "Content-Type": "application/json" } }); | ||
const data = await resp.json(); | ||
const parsed = configSchema.safeParse(data); | ||
if (parsed.success) { | ||
setState({ state: "loaded", config: parsed.data }); | ||
} else { | ||
setState({ state: "error", error: invalidConfigurationMessage }); | ||
} | ||
} catch (e) { | ||
setState({ state: "error", error: invalidConfigurationMessage }); | ||
} | ||
}; | ||
fetcher(); | ||
}, []); | ||
|
||
if (state.state === "loading") { | ||
return <Loading />; | ||
} | ||
|
||
if (state.state === "error") { | ||
return ( | ||
<Alert severity="error"> | ||
<AlertTitle>Error loading config: {state.error}</AlertTitle> | ||
</Alert> | ||
); | ||
} | ||
|
||
return <ConfigContext.Provider value={state.config}>{children}</ConfigContext.Provider>; | ||
}; | ||
|
||
export const useConfig = () => { | ||
return React.useContext(ConfigContext); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import React from "react"; | ||
import * as ReactDOM from "react-dom/client"; | ||
import { Provider } from "react-redux"; | ||
import { RecoilRoot } from "recoil"; | ||
import { PersistGate } from "redux-persist/integration/react"; | ||
import { App } from "./app/app"; | ||
import { Loading } from "./components/style/Loading"; | ||
import "./i18n"; | ||
import { persistor, store } from "./store"; | ||
import { ConfigProvider } from "./core/config"; | ||
|
||
const root = ReactDOM.createRoot(document.getElementById("root") as HTMLElement); | ||
root.render( | ||
<React.StrictMode> | ||
<RecoilRoot> | ||
<Provider store={store}> | ||
<PersistGate loading={<Loading />} persistor={persistor}> | ||
<React.Suspense fallback={<Loading />}> | ||
<Provider store={store}> | ||
<PersistGate loading={<Loading />} persistor={persistor}> | ||
<React.Suspense fallback={<Loading />}> | ||
<ConfigProvider> | ||
<App /> | ||
</React.Suspense> | ||
</PersistGate> | ||
</Provider> | ||
</RecoilRoot> | ||
</ConfigProvider> | ||
</React.Suspense> | ||
</PersistGate> | ||
</Provider> | ||
</React.StrictMode> | ||
); |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters