Files
GS13NG/tgui/packages/tgui-panel/settings/reducer.js
2020-08-22 13:11:13 +08:00

74 lines
1.4 KiB
JavaScript

/**
* @file
* @copyright 2020 Aleksej Komarov
* @license MIT
*/
import { changeSettingsTab, loadSettings, openChatSettings, toggleSettings, updateSettings } from './actions';
import { SETTINGS_TABS } from './constants';
const initialState = {
version: 1,
fontSize: 13,
lineHeight: 1.2,
theme: 'default',
adminMusicVolume: 0.5,
highlightText: '',
highlightColor: '#ffdd44',
view: {
visible: false,
activeTab: SETTINGS_TABS[0].id,
},
};
export const settingsReducer = (state = initialState, action) => {
const { type, payload } = action;
if (type === updateSettings.type) {
return {
...state,
...payload,
};
}
if (type === loadSettings.type) {
// Validate version and/or migrate state
if (!payload?.version) {
return state;
}
delete payload.view;
return {
...state,
...payload,
};
}
if (type === toggleSettings.type) {
return {
...state,
view: {
...state.view,
visible: !state.view.visible,
},
};
}
if (type === openChatSettings.type) {
return {
...state,
view: {
...state.view,
visible: true,
activeTab: 'chatPage',
},
};
}
if (type === changeSettingsTab.type) {
const { tabId } = payload;
return {
...state,
view: {
...state.view,
activeTab: tabId,
},
};
}
return state;
};