2022-06-21 23:20:53 +00:00
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
import { LocalStorage } from '@vegaprotocol/react-helpers';
|
2022-07-15 11:00:57 +00:00
|
|
|
import { ErrorType } from '../types';
|
|
|
|
import type { Environment, Configuration, Networks } from '../types';
|
2022-06-21 23:20:53 +00:00
|
|
|
import { validateConfiguration } from '../utils/validate-configuration';
|
|
|
|
|
|
|
|
export const LOCAL_STORAGE_NETWORK_KEY = 'vegaNetworkConfig';
|
|
|
|
|
|
|
|
export type EnvironmentWithOptionalUrl = Partial<Environment> &
|
|
|
|
Omit<Environment, 'VEGA_URL'>;
|
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
const compileHosts = (hosts: string[], envUrl?: string) => {
|
|
|
|
if (envUrl && !hosts.includes(envUrl)) {
|
|
|
|
return [...hosts, envUrl];
|
2022-06-21 23:20:53 +00:00
|
|
|
}
|
2022-07-15 11:00:57 +00:00
|
|
|
return hosts;
|
2022-06-21 23:20:53 +00:00
|
|
|
};
|
|
|
|
|
2022-07-12 16:34:54 +00:00
|
|
|
const getCacheKey = (env: Networks) => `${LOCAL_STORAGE_NETWORK_KEY}-${env}`;
|
|
|
|
|
|
|
|
const getCachedConfig = (env: Networks) => {
|
2022-07-15 11:00:57 +00:00
|
|
|
const cacheKey = getCacheKey(env);
|
|
|
|
const value = LocalStorage.getItem(cacheKey);
|
2022-06-21 23:20:53 +00:00
|
|
|
|
|
|
|
if (value) {
|
|
|
|
try {
|
|
|
|
const config = JSON.parse(value) as Configuration;
|
|
|
|
const hasError = validateConfiguration(config);
|
|
|
|
|
|
|
|
if (hasError) {
|
|
|
|
throw new Error('Invalid configuration found in the storage.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
} catch (err) {
|
2022-07-15 11:00:57 +00:00
|
|
|
LocalStorage.removeItem(cacheKey);
|
2022-06-21 23:20:53 +00:00
|
|
|
console.warn(
|
2022-07-15 11:00:57 +00:00
|
|
|
'Malformed data found for network configuration. Removed cached configuration, continuing...'
|
2022-06-21 23:20:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useConfig = (
|
|
|
|
environment: EnvironmentWithOptionalUrl,
|
2022-07-15 11:00:57 +00:00
|
|
|
onError: (errorType: ErrorType) => void
|
2022-06-21 23:20:53 +00:00
|
|
|
) => {
|
2022-07-15 11:00:57 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
2022-06-21 23:20:53 +00:00
|
|
|
const [config, setConfig] = useState<Configuration | undefined>(
|
2022-07-12 16:34:54 +00:00
|
|
|
getCachedConfig(environment.VEGA_ENV)
|
2022-06-21 23:20:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-07-15 11:00:57 +00:00
|
|
|
let isMounted = true;
|
|
|
|
(async () => {
|
|
|
|
if (!config && environment.VEGA_CONFIG_URL) {
|
|
|
|
isMounted && setLoading(true);
|
2022-06-21 23:20:53 +00:00
|
|
|
try {
|
2022-07-15 11:00:57 +00:00
|
|
|
const response = await fetch(environment.VEGA_CONFIG_URL);
|
2022-06-21 23:20:53 +00:00
|
|
|
const configData: Configuration = await response.json();
|
|
|
|
|
|
|
|
if (validateConfiguration(configData)) {
|
2022-07-15 11:00:57 +00:00
|
|
|
onError(ErrorType.CONFIG_VALIDATION_ERROR);
|
|
|
|
isMounted && setLoading(false);
|
2022-06-21 23:20:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-15 11:00:57 +00:00
|
|
|
const hosts = compileHosts(configData.hosts, environment.VEGA_URL);
|
|
|
|
|
|
|
|
isMounted && setConfig({ hosts });
|
2022-06-21 23:20:53 +00:00
|
|
|
LocalStorage.setItem(
|
2022-07-12 16:34:54 +00:00
|
|
|
getCacheKey(environment.VEGA_ENV),
|
2022-07-15 11:00:57 +00:00
|
|
|
JSON.stringify({ hosts })
|
2022-06-21 23:20:53 +00:00
|
|
|
);
|
2022-07-15 11:00:57 +00:00
|
|
|
isMounted && setLoading(false);
|
2022-06-21 23:20:53 +00:00
|
|
|
} catch (err) {
|
2022-07-15 11:00:57 +00:00
|
|
|
if (isMounted) {
|
|
|
|
setLoading(false);
|
|
|
|
setConfig({ hosts: [] });
|
|
|
|
}
|
|
|
|
onError(ErrorType.CONFIG_LOAD_ERROR);
|
2022-06-21 23:20:53 +00:00
|
|
|
}
|
2022-07-15 11:00:57 +00:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
return () => {
|
|
|
|
isMounted = false;
|
|
|
|
};
|
2022-06-21 23:20:53 +00:00
|
|
|
// load config only once per runtime
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-07-15 11:00:57 +00:00
|
|
|
}, [environment.VEGA_CONFIG_URL, !!config, onError, setLoading]);
|
2022-06-21 23:20:53 +00:00
|
|
|
|
|
|
|
return {
|
2022-07-15 11:00:57 +00:00
|
|
|
loading,
|
2022-07-12 16:34:54 +00:00
|
|
|
config,
|
2022-06-21 23:20:53 +00:00
|
|
|
};
|
|
|
|
};
|