-
Notifications
You must be signed in to change notification settings - Fork 188
/
provider.tsx
207 lines (186 loc) · 5.79 KB
/
provider.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {
dataServiceLocator,
useConnectionInfo,
} from '@mongodb-js/compass-connections/provider';
import {
createServiceLocator,
createServiceProvider,
} from 'hadron-app-registry';
import type { MongoDBInstanceProps } from 'mongodb-instance-model';
import { MongoDBInstance } from 'mongodb-instance-model';
import React, {
createContext,
useContext,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { MongoDBInstancesManager } from './instances-manager';
import toNS from 'mongodb-ns';
import type Collection from 'mongodb-collection-model';
import type Database from 'mongodb-database-model';
export {
MongoDBInstancesManagerEvents,
type MongoDBInstancesManager,
} from './instances-manager';
/**
* Exported only for testing purposes. Allows to set up a MongoDBInstanceManager
* with preconfigured MongoDBInstance
*
* @internal
*/
export class TestMongoDBInstanceManager extends MongoDBInstancesManager {
private _instance: MongoDBInstance;
constructor(instanceProps = {} as Partial<MongoDBInstanceProps>) {
super();
this._instance = new MongoDBInstance(instanceProps as MongoDBInstanceProps);
}
getMongoDBInstanceForConnection() {
return this._instance;
}
createMongoDBInstanceForConnection() {
return this._instance;
}
}
export const MongoDBInstancesManagerContext =
createContext<MongoDBInstancesManager | null>(
process.env.NODE_ENV === 'test'
? (new TestMongoDBInstanceManager() as unknown as MongoDBInstancesManager)
: null
);
export const MongoDBInstancesManagerProvider =
MongoDBInstancesManagerContext.Provider;
export const mongoDBInstancesManagerLocator = createServiceLocator(
function useMongoDBInstanceManager(): MongoDBInstancesManager {
const instancesManager = useContext(MongoDBInstancesManagerContext);
if (!instancesManager) {
throw new Error(
'No MongoDBInstancesManager available in this context, provider was not setup correctly'
);
}
return instancesManager;
},
'mongoDBInstancesManagerLocator'
);
export const mongoDBInstanceLocator = createServiceLocator(
function useMongoDBInstance(): MongoDBInstance {
const connectionInfo = useConnectionInfo();
const instancesManager = mongoDBInstancesManagerLocator();
return instancesManager.getMongoDBInstanceForConnection(connectionInfo.id);
},
'mongoDBInstanceLocator'
);
const NamespaceModelContext = React.createContext<Database | Collection | null>(
null
);
export const databaseModelLocator = createServiceLocator(
function useDatabaseModel(this: void) {
const model = useContext(NamespaceModelContext);
if (!model || model.modelType !== 'Database') {
const got = model ? 'Collection model' : 'undefined';
throw new Error(
`Tried to locate Database model, but got ${got} instead. Are you trying to use databaseModelLocator outside of database namespace scope?`
);
}
return model;
},
'databaseModelLocator'
);
export const collectionModelLocator = createServiceLocator(
function useCollectionModel(this: void) {
const model = useContext(NamespaceModelContext);
if (!model || model.modelType !== 'Collection') {
const got = model ? 'Database model' : 'undefined';
throw new Error(
`Tried to locate Collection model, but got ${got} instead. Are you trying to use collectionModelLocator outside of collection namespace scope?`
);
}
return model;
},
'collectionModelLocator'
);
/**
*
*/
export const NamespaceProvider = createServiceProvider(
function NamespaceProvider({
children,
namespace,
onNamespaceFallbackSelect,
}: {
children?: React.ReactNode;
namespace: string;
onNamespaceFallbackSelect?(namespace: string | null): void;
}) {
const onNamespaceFallbackSelectRef = useRef(onNamespaceFallbackSelect);
onNamespaceFallbackSelectRef.current = onNamespaceFallbackSelect;
const dataService = dataServiceLocator();
const mongoDBInstance = mongoDBInstanceLocator();
const ns = useMemo(() => {
return toNS(namespace);
}, [namespace]);
const [namespaceModel, setNamespaceModel] = useState<
Database | Collection | null
>(() => {
const model = ns.collection
? mongoDBInstance.databases.get(ns.database)?.collections.get(ns.ns)
: mongoDBInstance.databases.get(ns.database);
return model ?? null;
});
useEffect(() => {
let cancelled = false;
void (async () => {
if (namespaceModel && namespaceModel._id === ns.ns) {
return;
}
setNamespaceModel(null);
await mongoDBInstance.fetchDatabases({ dataService }).catch(() => {
// We don't care if it failed here, for us it's similar to not finding
// the namespace
});
const db = mongoDBInstance.databases.get(ns.database);
if (!db) {
onNamespaceFallbackSelectRef.current?.(null);
return;
}
if (!ns.collection) {
if (!cancelled) {
setNamespaceModel(db);
}
return;
}
await db.fetchCollections({ dataService }).catch(() => {
// See above
});
const coll = db.collections.get(ns.ns);
if (!coll) {
onNamespaceFallbackSelectRef.current?.(db._id);
return;
}
if (!cancelled) {
setNamespaceModel(coll);
}
})();
return () => {
cancelled = true;
};
}, [
dataService,
mongoDBInstance,
namespaceModel,
ns.collection,
ns.database,
ns.ns,
]);
if (!namespaceModel) {
return null;
}
return (
<NamespaceModelContext.Provider value={namespaceModel}>
{children}
</NamespaceModelContext.Provider>
);
}
);
export type { MongoDBInstance, Collection, Database };