-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInjector.tsx
114 lines (107 loc) · 3.46 KB
/
Injector.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React, { useContext } from "react";
import { useLocation } from "react-router-dom";
import { isJsonParserId, JsonParserId, SchedulerImpl } from "@/Core";
import {
PrimaryFeatureManager,
GetServerStatusStateHelper,
BaseApiHelper,
FileFetcherImpl,
CommandResolverImpl,
QueryResolverImpl,
CommandManagerResolverImpl,
QueryManagerResolverImpl,
Store,
PrimaryArchiveHelper,
PrimaryFileManager,
PrimaryLogger,
} from "@/Data";
import {
PrimaryBaseUrlManager,
PrimaryRouteManager,
DependencyProvider,
EnvironmentHandlerImpl,
EnvironmentModifierImpl,
UrlManagerImpl,
} from "@/UI";
import { AuthContext } from "./Data/Auth/";
import { UpdateBanner } from "./UI/Components/UpdateBanner";
import { ModalProvider } from "./UI/Root/Components/ModalProvider";
interface Props {
store: Store;
}
/**
* This component creates instances of managers, helpers, and resolvers, and provides them through a `DependencyProvider`.
* It also contains `ModalProvider` and an `UpdateBanner`.
*
* @props {Props} props - The properties passed to the component.
* @prop {Store} store - The store to be used by the managers and resolvers.
* @prop {React.ReactNode} children - The children to be rendered within the Injector.
* @returns {React.FC<React.PropsWithChildren<Props>>} A `DependencyProvider` that wraps a `ModalProvider`, an `UpdateBanner`, and the children.
*/
export const Injector: React.FC<React.PropsWithChildren<Props>> = ({
store,
children,
}) => {
const authHelper = useContext(AuthContext);
const featureManager = new PrimaryFeatureManager(
GetServerStatusStateHelper(store),
new PrimaryLogger(),
getJsonParserId(globalThis),
COMMITHASH,
APP_VERSION,
);
const baseUrlManager = new PrimaryBaseUrlManager(
globalThis.location.origin,
globalThis.location.pathname,
);
const basePathname = baseUrlManager.getBasePathname();
const baseUrl = baseUrlManager.getBaseUrl(process.env.API_BASEURL);
const routeManager = PrimaryRouteManager(basePathname);
const apiHelper = BaseApiHelper(baseUrl, authHelper);
const queryResolver = new QueryResolverImpl(
new QueryManagerResolverImpl(
store,
apiHelper,
new SchedulerImpl(5000),
new SchedulerImpl(10000),
),
);
const commandResolver = new CommandResolverImpl(
new CommandManagerResolverImpl(store, apiHelper, authHelper),
);
const urlManager = new UrlManagerImpl(featureManager, baseUrl);
const fileFetcher = new FileFetcherImpl(apiHelper);
const environmentModifier = EnvironmentModifierImpl();
const environmentHandler = EnvironmentHandlerImpl(useLocation, routeManager);
const fileManager = new PrimaryFileManager();
const archiveHelper = new PrimaryArchiveHelper(fileManager);
return (
<DependencyProvider
dependencies={{
queryResolver,
commandResolver,
urlManager,
fileFetcher,
environmentModifier,
featureManager,
routeManager,
environmentHandler,
archiveHelper,
authHelper,
}}
>
<ModalProvider>
<UpdateBanner apiHelper={apiHelper} />
{children}
</ModalProvider>
</DependencyProvider>
);
};
const getJsonParserId = (container: unknown): JsonParserId | undefined => {
if (typeof container !== "object") return undefined;
if (container === null) return undefined;
const id = container["jsonParserId"];
if (typeof id !== "string") return undefined;
if (!isJsonParserId(id)) return undefined;
return id;
};