2019-11-16 17:24:03 +00:00
|
|
|
import storage from "./localStorage";
|
|
|
|
import socket from "./socket";
|
2019-11-05 20:51:55 +00:00
|
|
|
import {config, createState} from "./settings";
|
2022-06-19 00:25:21 +00:00
|
|
|
import {Store} from "vuex";
|
|
|
|
import {State} from "./store";
|
2019-11-05 20:51:55 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export function createSettingsStore(store: Store<State>) {
|
2019-11-05 20:51:55 +00:00
|
|
|
return {
|
|
|
|
namespaced: true,
|
|
|
|
state: assignStoredSettings(createState(), loadFromLocalStorage()),
|
|
|
|
mutations: {
|
|
|
|
set(state, {name, value}) {
|
|
|
|
state[name] = value;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
syncAll({state}, force = false) {
|
2019-11-20 17:43:27 +00:00
|
|
|
if (state.syncSettings === false && force === false) {
|
2019-11-05 20:51:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-20 17:43:27 +00:00
|
|
|
store.commit("serverHasSettings", true);
|
|
|
|
|
2019-11-05 20:51:55 +00:00
|
|
|
for (const name in state) {
|
|
|
|
if (config[name].sync !== "never" || config[name].sync === "always") {
|
|
|
|
socket.emit("setting:set", {name, value: state[name]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
applyAll({state}) {
|
|
|
|
for (const settingName in config) {
|
2019-12-15 16:03:13 +00:00
|
|
|
config[settingName].apply(store, state[settingName], true);
|
2019-11-05 20:51:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
update({state, commit}, {name, value, sync = false}) {
|
|
|
|
if (state[name] === value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const settingConfig = config[name];
|
|
|
|
|
2019-11-26 14:15:03 +00:00
|
|
|
// Trying to update a non existing setting (e.g. server has an old key)
|
|
|
|
if (!settingConfig) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-05 20:51:55 +00:00
|
|
|
if (
|
|
|
|
sync === false &&
|
|
|
|
(state.syncSettings === false || settingConfig.sync === "never")
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
commit("set", {name, value});
|
|
|
|
storage.set("settings", JSON.stringify(state));
|
|
|
|
settingConfig.apply(store, value);
|
|
|
|
|
|
|
|
if (!sync) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
(state.syncSettings && settingConfig.sync !== "never") ||
|
|
|
|
settingConfig.sync === "always"
|
|
|
|
) {
|
|
|
|
socket.emit("setting:set", {name, value});
|
|
|
|
}
|
|
|
|
},
|
2019-11-03 18:05:19 +00:00
|
|
|
},
|
2019-11-05 20:51:55 +00:00
|
|
|
};
|
2019-11-03 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 20:51:55 +00:00
|
|
|
function loadFromLocalStorage() {
|
2022-06-19 00:25:21 +00:00
|
|
|
let storedSettings: Record<string, any> = {};
|
2019-11-26 14:15:03 +00:00
|
|
|
|
|
|
|
try {
|
2022-06-19 00:25:21 +00:00
|
|
|
storedSettings = JSON.parse(storage.get("settings") || "{}");
|
2019-11-26 14:15:03 +00:00
|
|
|
} catch (e) {
|
|
|
|
storage.remove("settings");
|
|
|
|
}
|
2019-11-05 20:51:55 +00:00
|
|
|
|
|
|
|
if (!storedSettings) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-10-09 10:28:14 +00:00
|
|
|
// Older Hard Lounge versions converted highlights to an array, turn it back into a string
|
2020-08-07 16:52:50 +00:00
|
|
|
if (storedSettings.highlights !== null && typeof storedSettings.highlights === "object") {
|
2019-11-05 20:51:55 +00:00
|
|
|
storedSettings.highlights = storedSettings.highlights.join(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
return storedSettings;
|
2019-11-03 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 20:51:55 +00:00
|
|
|
/**
|
|
|
|
* Essentially Object.assign but does not overwrite and only assigns
|
|
|
|
* if key exists in both supplied objects and types match
|
|
|
|
*
|
|
|
|
* @param {object} defaultSettings
|
|
|
|
* @param {object} storedSettings
|
|
|
|
*/
|
2022-06-19 00:25:21 +00:00
|
|
|
function assignStoredSettings(
|
|
|
|
defaultSettings: Record<string, any>,
|
|
|
|
storedSettings: Record<string, any>
|
|
|
|
) {
|
2019-11-05 20:51:55 +00:00
|
|
|
const newSettings = {...defaultSettings};
|
|
|
|
|
|
|
|
for (const key in defaultSettings) {
|
|
|
|
// Make sure the setting in local storage has the same type that the code expects
|
|
|
|
if (
|
|
|
|
typeof storedSettings[key] !== "undefined" &&
|
|
|
|
typeof defaultSettings[key] === typeof storedSettings[key]
|
|
|
|
) {
|
|
|
|
newSettings[key] = storedSettings[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newSettings;
|
|
|
|
}
|