Line data Source code
1 : import * as React from 'react';
2 :
3 : import { isDesktopHost } from '../../platform/native_globals.js';
4 : import { HyperDatabaseClient } from './hyperdb_grpc_client.js';
5 : import { NativeHyperDatabaseClient } from './platform/native_hyperdb_grpc_client.js';
6 : import { WebHyperDatabaseClient } from './platform/web_hyperdb_http_client.js';
7 : import { useHttpClient } from '../../platform/http/http_client_provider.js';
8 : import { useLogger } from '../../platform/logger/logger_provider.js';
9 :
10 : type Props = {
11 : children: React.ReactElement;
12 : };
13 :
14 1 : const GRPC_CLIENT_CTX = React.createContext<HyperDatabaseClient | null>(null);
15 1 : const HTTP_CLIENT_CTX = React.createContext<HyperDatabaseClient | null>(null);
16 :
17 1 : export const useHyperGrpcClient = () => React.useContext(GRPC_CLIENT_CTX);
18 1 : export const useHyperHttpClient = () => React.useContext(HTTP_CLIENT_CTX);
19 :
20 1 : export const HyperDatabaseClientProvider: React.FC<Props> = (props: Props) => {
21 1 : const logger = useLogger();
22 1 : const httpClient = useHttpClient();
23 1 : const [grpcClient, setGrpcClient] = React.useState<HyperDatabaseClient | null>(null);
24 1 : const [httpHyperClient, setHttpHyperClient] = React.useState<HyperDatabaseClient | null>(null);
25 1 : React.useEffect(() => {
26 1 : if (isDesktopHost()) {
27 1 : setGrpcClient(new NativeHyperDatabaseClient({ proxyEndpoint: new URL("dashql-native://localhost") }, logger));
28 : }
29 1 : if (httpClient) {
30 1 : setHttpHyperClient(new WebHyperDatabaseClient(httpClient, logger));
31 : }
32 1 : }, [httpClient]);
33 1 : return (
34 1 : <GRPC_CLIENT_CTX.Provider value={grpcClient}>
35 1 : <HTTP_CLIENT_CTX.Provider value={httpHyperClient}>
36 1 : {props.children}
37 1 : </HTTP_CLIENT_CTX.Provider>
38 1 : </GRPC_CLIENT_CTX.Provider>
39 : );
40 1 : };
|