-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
AppConfigurationProvider.tsx
73 lines (59 loc) 路 1.87 KB
/
AppConfigurationProvider.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
import isEmpty from 'lodash/isEmpty';
import isNil from 'lodash/isNil';
import merge from 'lodash/merge';
import React, { FunctionComponent, createContext, useContext, useMemo, useState } from 'react';
import { TemplateUIOptions } from '@verdaccio/types';
import { PRIMARY_COLOR } from '../../Theme/colors';
type ConfigProviderProps = {
configOptions: TemplateUIOptions;
};
const defaultValues: ConfigProviderProps = {
configOptions: {
// note: dark mode set as undefined by design
primaryColor: PRIMARY_COLOR,
pkgManagers: ['yarn', 'pnpm', 'npm'],
scope: '',
base: '',
flags: {},
login: true,
url_prefix: '',
showInfo: true,
showSettings: true,
showThemeSwitch: true,
showFooter: true,
showSearch: true,
showRaw: true,
showDownloadTarball: true,
title: 'Verdaccio',
},
};
function getConfiguration() {
const uiConfiguration = merge(
defaultValues.configOptions,
window?.__VERDACCIO_BASENAME_UI_OPTIONS
);
if (window?.__VERDACCIO_BASENAME_UI_OPTIONS.pkgManagers) {
uiConfiguration.pkgManagers = window?.__VERDACCIO_BASENAME_UI_OPTIONS.pkgManagers;
}
if (isNil(uiConfiguration.primaryColor) || isEmpty(uiConfiguration.primaryColor)) {
uiConfiguration.primaryColor = PRIMARY_COLOR;
}
return uiConfiguration;
}
const AppConfigurationContext = createContext<ConfigProviderProps>(defaultValues);
const AppConfigurationProvider: FunctionComponent<{ children: React.ReactElement<any> }> = ({
children,
}) => {
const [configOptions] = useState<TemplateUIOptions>(getConfiguration());
const value = useMemo(
() => ({
configOptions,
}),
[configOptions]
);
return (
<AppConfigurationContext.Provider value={value}>{children}</AppConfigurationContext.Provider>
);
};
export default AppConfigurationProvider;
export const useConfig = () => useContext(AppConfigurationContext);