2019-03-27 22:39:25 +01:00
|
|
|
import { defaultTheme, themes } from "../types/HyperspaceTheme";
|
2019-09-18 19:52:39 +02:00
|
|
|
import { getNotificationRequestPermission } from "./notifications";
|
|
|
|
import axios from "axios";
|
2019-04-08 00:31:18 +02:00
|
|
|
import { Config } from "../types/Config";
|
2019-04-20 23:23:08 +02:00
|
|
|
import { Visibility } from "../types/Visibility";
|
2019-03-27 22:39:25 +01:00
|
|
|
|
|
|
|
type SettingsTemplate = {
|
2019-10-05 21:08:31 +02:00
|
|
|
[key: string]: any;
|
|
|
|
darkModeEnabled: boolean;
|
|
|
|
systemDecidesDarkMode: boolean;
|
|
|
|
enablePushNotifications: boolean;
|
|
|
|
clearNotificationsOnRead: boolean;
|
|
|
|
displayAllOnNotificationBadge: boolean;
|
|
|
|
defaultVisibility: string;
|
2019-09-18 19:52:39 +02:00
|
|
|
};
|
2019-03-27 22:39:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2019-10-05 21:08:31 +02:00
|
|
|
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";
|
|
|
|
}
|
2019-03-27 22:39:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2019-10-05 21:08:31 +02:00
|
|
|
if (localStorage.getItem(key) === null) {
|
|
|
|
console.warn("This key has not been set before.");
|
|
|
|
}
|
|
|
|
localStorage.setItem(key, value.toString());
|
2019-03-27 22:39:25 +01:00
|
|
|
}
|
|
|
|
|
2019-04-20 23:23:08 +02:00
|
|
|
/**
|
|
|
|
* Gets the user default visibility from localStorage
|
|
|
|
* @returns The Visibility value associated with the key
|
|
|
|
*/
|
|
|
|
export function getUserDefaultVisibility(): Visibility {
|
2019-10-05 21:08:31 +02:00
|
|
|
if (localStorage.getItem("defaultVisibility") === null) {
|
|
|
|
console.warn(
|
|
|
|
"This key has not been set before, so the default value is PUBLIC for now."
|
|
|
|
);
|
|
|
|
return "public";
|
|
|
|
} else {
|
|
|
|
return localStorage.getItem("defaultVisibility") as Visibility;
|
|
|
|
}
|
2019-04-20 23:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a user default visibility to localStorage
|
|
|
|
* @param key The settings key in localStorage to change
|
|
|
|
*/
|
|
|
|
export function setUserDefaultVisibility(key: string) {
|
2019-10-05 21:08:31 +02:00
|
|
|
if (localStorage.getItem("defaultVisibility") === null) {
|
|
|
|
console.warn("This key has not been set before.");
|
|
|
|
}
|
|
|
|
localStorage.setItem("defaultVisibility", key.toString());
|
2019-04-20 23:23:08 +02:00
|
|
|
}
|
|
|
|
|
2019-03-27 22:39:25 +01:00
|
|
|
/**
|
|
|
|
* Gets the user's default theme or the default theme
|
|
|
|
*/
|
|
|
|
export function getUserDefaultTheme() {
|
2019-10-05 21:08:31 +02:00
|
|
|
let returnTheme = defaultTheme;
|
|
|
|
themes.forEach(theme => {
|
|
|
|
if (theme.key === localStorage.getItem("theme")) {
|
|
|
|
returnTheme = theme;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return returnTheme;
|
2019-03-27 22:39:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the user's default theme
|
|
|
|
* @param themeName The name of the theme
|
|
|
|
*/
|
|
|
|
export function setUserDefaultTheme(themeName: string) {
|
2019-10-05 21:08:31 +02:00
|
|
|
localStorage.setItem("theme", themeName);
|
2019-03-27 22:39:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the user defaults if they do not exist already.
|
|
|
|
*/
|
|
|
|
export function createUserDefaults() {
|
2019-10-05 21:08:31 +02:00
|
|
|
let defaults: SettingsTemplate = {
|
|
|
|
darkModeEnabled: false,
|
|
|
|
systemDecidesDarkMode: true,
|
|
|
|
enablePushNotifications: true,
|
|
|
|
clearNotificationsOnRead: false,
|
|
|
|
displayAllOnNotificationBadge: false,
|
|
|
|
defaultVisibility: "public"
|
|
|
|
};
|
2019-03-27 22:39:25 +01:00
|
|
|
|
2019-10-05 21:08:31 +02:00
|
|
|
let settings = [
|
|
|
|
"darkModeEnabled",
|
|
|
|
"systemDecidesDarkMode",
|
|
|
|
"clearNotificationsOnRead",
|
|
|
|
"displayAllOnNotificationBadge",
|
|
|
|
"defaultVisibility"
|
|
|
|
];
|
2019-05-13 15:42:51 +02:00
|
|
|
|
2019-10-05 21:08:31 +02:00
|
|
|
migrateExistingSettings();
|
2019-05-13 15:42:51 +02:00
|
|
|
|
2019-10-05 21:08:31 +02:00
|
|
|
settings.forEach((setting: string) => {
|
|
|
|
if (localStorage.getItem(setting) === null) {
|
|
|
|
if (typeof defaults[setting] === "boolean") {
|
|
|
|
setUserDefaultBool(setting, defaults[setting]);
|
|
|
|
} else {
|
|
|
|
localStorage.setItem(setting, defaults[setting].toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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.
|
|
|
|
*/
|
2019-05-08 15:14:46 +02:00
|
|
|
export async function getConfig(): Promise<Config | undefined> {
|
2019-10-05 21:08:31 +02:00
|
|
|
try {
|
|
|
|
const resp = await axios.get("config.json");
|
|
|
|
return resp.data as Config;
|
|
|
|
} catch (err) {
|
|
|
|
console.error(
|
|
|
|
"Couldn't configure Hyperspace with the config file. Reason: " +
|
|
|
|
err.name
|
|
|
|
);
|
|
|
|
}
|
2019-05-13 15:42:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function migrateExistingSettings() {
|
2019-10-05 21:08:31 +02:00
|
|
|
if (localStorage.getItem("prefers-dark-mode")) {
|
|
|
|
setUserDefaultBool(
|
|
|
|
"darkModeEnabled",
|
|
|
|
localStorage.getItem("prefers-dark-mode") === "true"
|
|
|
|
);
|
|
|
|
}
|
2019-09-18 19:52:39 +02:00
|
|
|
}
|