general-assistant
Version:
General Assistant components
63 lines (62 loc) • 2.21 kB
JavaScript
import set from 'lodash/set';
import get from 'lodash/get';
import defaults from 'lodash/defaults';
import defaultsDeep from 'lodash/defaultsDeep';
import SimpleTheme from '../SimpleTheme';
const userAgent = navigator.userAgent;
const screen = window.screen;
const _set = set, _get = get, _defaultsDeep = defaultsDeep;
export default (defaultConfig, customConfig, set, get) => {
const appOption = _defaultsDeep(customConfig, defaultConfig);
setThemeColor();
function setThemeColor(_name) {
const _themeName = _name || _get(appOption, 'theme');
const _token = _get(appOption, 'token');
const _themeItem = Object.assign(_get(SimpleTheme, _themeName, {}), _token);
console.log(0, _themeItem);
if (_themeItem !== null && typeof _themeItem === 'object') {
for (const key in _themeItem) {
const _colorValue = _themeItem[key];
_colorValue && document.documentElement.style.setProperty(key, _colorValue);
}
}
}
function getRequestConfig() {
return _get(appOption, 'requests');
}
function getConfig() {
return get().config;
}
function setConfig(_config, isMerge = true) {
set((state) => ({ config: (isMerge ? defaultsDeep : defaults)(_config, state.config) }));
}
function getAiName() {
return _get(getConfig(), 'robot.name');
}
function getAiAvatar() {
return _get(getConfig(), 'robot.avatar', '');
}
function getUserName() {
return _get(getConfig(), 'user.name');
}
function getUserAvatar() {
return _get(getConfig(), 'user.avatar', '');
}
return {
config: _get(appOption, 'config', {}),
getAiName,
getAiAvatar,
getUserName,
getUserAvatar,
getConfig,
setConfig,
getRequestConfig,
getDeviceInfo: () => ({
isMobile: /Mobile|iPhone|iPad|iPod|Android/i.test(userAgent),
screenX: screen.width,
screenY: screen.height,
}),
setThemeColor: (_name) => setThemeColor(_name),
getToolbarlist: () => _get(appOption, ['config', 'toolbarlist'], []),
};
};