lbry-desktop/ui/js/actions/settings.js

88 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
2017-08-20 01:59:48 +02:00
import * as settings from "constants/settings";
2017-06-06 23:19:12 +02:00
import lbry from "lbry";
2017-08-24 18:11:39 +02:00
const { remote } = require("electron");
const { extname } = require("path");
const { download } = remote.require("electron-dl");
const { readdirSync } = remote.require("fs");
export function doFetchDaemonSettings() {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
lbry.settings_get().then(settings => {
dispatch({
type: types.DAEMON_SETTINGS_RECEIVED,
data: {
2017-06-06 23:19:12 +02:00
settings,
},
});
});
};
}
export function doSetDaemonSetting(key, value) {
return function(dispatch, getState) {
let settings = {};
settings[key] = value;
2017-06-06 23:19:12 +02:00
lbry.settings_set(settings).then(settings);
lbry.settings_get().then(settings => {
dispatch({
type: types.DAEMON_SETTINGS_RECEIVED,
data: {
2017-06-06 23:19:12 +02:00
settings,
},
});
});
};
2017-06-06 06:21:55 +02:00
}
2017-06-28 09:12:01 +02:00
export function doSetClientSetting(key, value) {
lbry.setClientSetting(key, value);
return {
type: types.CLIENT_SETTING_CHANGED,
data: {
key,
value,
},
};
}
2017-08-07 02:55:31 +02:00
2017-08-24 18:11:39 +02:00
export function doGetThemes() {
const dir = `${remote.app.getAppPath()}/dist/themes`;
// Get all .css files
const files = readdirSync(dir).filter(file => extname(file) === ".css");
return function(dispatch, getState) {
// Find themes
const themes = files.map(file => ({
name: file.replace(".css", ""),
path: `./themes/${file}`,
}));
dispatch(doSetClientSetting("themes", themes));
};
}
2017-08-20 01:59:48 +02:00
export function doSetTheme(name) {
return function(dispatch, getState) {
2017-08-20 01:59:48 +02:00
const last = lbry.getClientSetting(settings.THEME);
const find = name => themes.find(theme => theme.name === name);
2017-08-20 01:59:48 +02:00
// Get themes
const themes = lbry.getClientSetting(settings.THEMES);
2017-08-20 01:59:48 +02:00
// Find theme
const theme = find(name) || find(last) || find("light");
2017-08-07 02:55:31 +02:00
2017-08-20 01:59:48 +02:00
if (theme.path) {
// update theme
const link = document.getElementById("theme");
link.href = theme.path;
2017-08-20 00:03:40 +02:00
2017-08-20 01:59:48 +02:00
dispatch(doSetClientSetting(settings.THEME, theme.name));
}
2017-08-07 02:55:31 +02:00
};
}