hyperspace-desktop-client-w.../src/utilities/settings.tsx

100 lines
2.9 KiB
TypeScript
Raw Normal View History

import { defaultTheme, themes } from "../types/HyperspaceTheme";
import { getNotificationRequestPermission } from './notifications';
2019-04-08 00:31:18 +02:00
import axios from 'axios';
import { Config } from "../types/Config";
type SettingsTemplate = {
[key:string]: any;
darkModeEnabled: boolean;
enablePushNotifications: boolean;
clearNotificationsOnRead: boolean;
displayAllOnNotificationBadge: boolean;
}
/**
* Gets the user default from localStorage
* @param key The settings key to retrieve from localStorage
* @returns The boolean value associated with the key
*/
export function getUserDefaultBool(key: string): boolean {
if (localStorage.getItem(key) === null) {
console.warn('This key has not been set before, so the default value is FALSE for now.');
return false;
} else {
return localStorage.getItem(key) === "true";
}
}
/**
* Set a user default to localStorage
* @param key The settings key in localStorage to change
* @param value The boolean value for the key
*/
export function setUserDefaultBool(key: string, value: boolean) {
if (localStorage.getItem(key) === null) {
console.warn('This key has not been set before.');
}
localStorage.setItem(key, value.toString());
}
/**
* Gets the user's default theme or the default theme
*/
export function getUserDefaultTheme() {
let returnTheme = defaultTheme;
themes.forEach((theme) => {
if(theme.key === localStorage.getItem('theme')) {
returnTheme = theme;
}
});
return returnTheme;
}
/**
* Sets the user's default theme
* @param themeName The name of the theme
*/
export function setUserDefaultTheme(themeName: string) {
localStorage.setItem('theme', themeName);
}
/**
* Creates the user defaults if they do not exist already.
*/
export function createUserDefaults() {
let defaults: SettingsTemplate = {
darkModeEnabled: false,
enablePushNotifications: true,
clearNotificationsOnRead: false,
2019-04-20 23:14:47 +02:00
displayAllOnNotificationBadge: false,
defaultVisibility: "public"
}
2019-04-20 23:14:47 +02:00
let settings = ["darkModeEnabled", "clearNotificationsOnRead", "displayAllOnNotificationBadge", "defaultVisibility"];
settings.forEach((setting: string) => {
if (localStorage.getItem(setting) === null) {
2019-04-20 23:14:47 +02:00
if (typeof defaults[setting] === "boolean") {
setUserDefaultBool(setting, defaults[setting]);
} else {
localStorage.setItem(setting, defaults[setting]);
}
}
})
getNotificationRequestPermission();
2019-04-08 00:31:18 +02:00
}
2019-04-12 19:39:24 +02:00
/**
* Gets the configuration data from `config.json`
* @returns The Promise data from getting the config.
*/
export async function getConfig() {
try {
const resp = await axios.get('config.json');
2019-04-08 00:31:18 +02:00
let config: Config = resp.data;
return config;
2019-04-12 19:39:24 +02:00
}
catch (err) {
console.error("Couldn't configure Hyperspace with the config file. Reason: " + err.name);
}
}