partey_workadventure/desktop/electron/src/settings.ts

73 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-02-17 18:09:57 +01:00
import ElectronLog from "electron-log";
import Settings from "electron-settings";
2022-02-19 01:08:33 +01:00
import type { Server } from "./preload-local-app/types";
2022-02-18 01:51:35 +01:00
2022-02-22 10:05:21 +01:00
export type SettingsData = {
2022-02-18 23:03:05 +01:00
log_level: ElectronLog.LogLevel;
auto_launch_enabled: boolean;
servers: Server[];
2022-02-22 19:01:26 +01:00
shortcuts: Record<"mute_toggle" | "camera_toggle", string>;
2022-02-17 18:09:57 +01:00
};
let settings: SettingsData;
2022-02-22 19:01:26 +01:00
const defaultSettings: SettingsData = {
log_level: "info",
auto_launch_enabled: true,
servers: [
{
_id: `${Date.now()}-1`,
name: "WA Demo",
url: "https://play.staging.workadventu.re/@/tcm/workadventure/wa-village",
},
],
2022-02-22 19:01:26 +01:00
shortcuts: {
mute_toggle: "",
camera_toggle: "",
},
};
2022-02-17 18:09:57 +01:00
async function init() {
2022-02-22 19:01:26 +01:00
let _settings = await Settings.get();
if (_settings !== undefined) {
_settings = defaultSettings;
}
settings = _settings as SettingsData;
2022-02-17 18:09:57 +01:00
}
2022-02-22 12:02:56 +01:00
function get(): SettingsData;
function get<T extends keyof SettingsData>(key: T): SettingsData[T] | undefined;
function get<T extends keyof SettingsData>(key?: T): SettingsData | SettingsData[T] | undefined {
2022-02-19 01:48:56 +01:00
if (settings === undefined) {
2022-02-18 23:03:05 +01:00
throw new Error("Settings not initialized");
}
2022-02-17 18:09:57 +01:00
2022-02-22 12:02:56 +01:00
if (key === undefined) {
return settings;
}
2022-02-18 23:03:05 +01:00
return settings?.[key];
2022-02-17 18:09:57 +01:00
}
2022-02-22 12:02:56 +01:00
function set(key: SettingsData): void;
function set<T extends keyof SettingsData>(key: T, value: SettingsData[T]): void;
function set<T extends keyof SettingsData>(key: T | SettingsData, value?: SettingsData[T]) {
2022-02-19 01:48:56 +01:00
if (settings === undefined) {
2022-02-18 23:03:05 +01:00
throw new Error("Settings not initialized");
}
2022-02-17 18:09:57 +01:00
2022-02-22 14:09:36 +01:00
if (typeof key === "string" && value !== undefined) {
2022-02-22 12:02:56 +01:00
settings[key] = value;
2022-02-22 14:09:36 +01:00
} else if (typeof key !== "string") {
2022-02-22 12:02:56 +01:00
Object.assign(settings, key);
}
2022-02-18 23:03:05 +01:00
void Settings.set(settings);
2022-02-17 18:09:57 +01:00
}
export default {
2022-02-18 23:03:05 +01:00
init,
get,
set,
2022-02-17 18:09:57 +01:00
};