Skip to content

Commit

Permalink
refactor: move settings management into helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslang02 committed Sep 27, 2021
1 parent 7b92c2a commit 3f0f723
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/js/client/http/Http.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Settings } from "store/Settings";

type HTTPRequest = {
method: string
path: string
Expand All @@ -12,7 +14,7 @@ export const Http = {
) {
const METHOD = method.toUpperCase();

http.request(METHOD, path, window.store.get("settings").token, body ?? "");
http.request(METHOD, path, Settings.get("token"), body ?? "");

function listener(response: string) {
callback(null, JSON.parse(response));
Expand Down
5 changes: 2 additions & 3 deletions src/js/client/socket/SocketManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { defaultSettings } from "store/Settings";
import { Settings } from "store/Settings";
import { Payload } from "../../structures/dto/Payload";
import { Client } from "../Client";
import { handlers } from "./handlers/index";
Expand All @@ -10,8 +10,7 @@ export class SocketManager {
constructor(private client: Client) { }

connect() {
const settings = window.store.get("settings");
const [host, port] = (settings.proxyUrl || defaultSettings.proxyUrl).split(":");
const [host, port = "80"] = Settings.get("proxyUrl").split(":");

socket.connectToServer(host, +port);
}
Expand Down
12 changes: 12 additions & 0 deletions src/js/store/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@ export type Settings = {
autoConnect: boolean
cdnProxyUrl: string
proxyUrl: string
};

export const Settings = {
get<K extends keyof Settings>(key: K) {
return window.store.get("settings")[key] ?? defaultSettings[key];
},
set<K extends keyof Settings>(key: K, value: Settings[K]) {
window.store.fetch("settings", settings => {
settings[key] = value;
window.store.set("settings", settings);
});
},
};
4 changes: 2 additions & 2 deletions src/js/ui/DMPage/DMPage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultSettings } from "store/Settings";
import { Settings } from "store/Settings";
import { PrivateChannel } from "structures/PrivateChannel";

type DmListItem = {
Expand All @@ -14,7 +14,7 @@ declare const dmPage: Qml.Page & Qml.Component;

function loadChannels() {
dmListModel.clear();
const cdnProxyUrl = window.store.get("settings").cdnProxyUrl || defaultSettings.cdnProxyUrl;
const cdnProxyUrl = Settings.get("cdnProxyUrl");
const channels = Object.keys(window.client.privateChannels)
.filter(a => window.client.privateChannels[a].lastMessageId)
.sort((a, b) => {
Expand Down
4 changes: 2 additions & 2 deletions src/js/ui/MessagesPage/MessagesPage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Http } from "client/http/Http";
import { defaultSettings } from "store/Settings";
import { Settings } from "store/Settings";
import { MessageDto } from "structures/dto/Message";
import { markdown } from "utils/drawdown";

Expand All @@ -22,7 +22,7 @@ function sendMessage(content: string) {
}

function appendMessage(msg: MessageDto) {
const cdnProxyUrl = window.store.get("settings").cdnProxyUrl || defaultSettings.cdnProxyUrl;
const cdnProxyUrl = Settings.get("cdnProxyUrl");
const splitTimestamp = msg.timestamp.split("T");
const [year, month, day] = splitTimestamp[0].split("-");
const [hour, minute, second] = splitTimestamp[1].split(".")[0].split(":");
Expand Down
25 changes: 8 additions & 17 deletions src/js/ui/SettingsPage/SettingsPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,46 @@ declare const dialogField: Qml.TextField;
declare const debugModeItem: Qml.SelectionListItem;

function loadSettings() {
const settings = window.store.get("settings");

debugModeItem.subTitle = settings.debug ? "Enabled" : "Disabled";
debugModeItem.subTitle = Settings.get("debug") ? "Enabled" : "Disabled";
}

function handleReady() {
let property: keyof Settings = "token";

dialog.buttonClicked.connect(bi => {
if (bi === 0) {
window.store.fetch("settings", settings => {
// @ts-ignore
settings[property] = dialogField.text;
window.store.set("settings", settings);
loadSettings();
});
Settings.set(property, dialogField.text);
loadSettings();
}
});

tokenItem.clicked.connect(() => {
dialog.titleText = "Token";
dialogField.text = window.store.get("settings").token ?? "";
dialogField.text = Settings.get("token") ?? "";
dialogField.placeholderText = "";
property = "token";
dialog.open();
});

cdnProxyUrlItem.clicked.connect(() => {
dialog.titleText = "CDN proxy URL";
dialogField.text = window.store.get("settings").cdnProxyUrl ?? "";
dialogField.text = Settings.get("cdnProxyUrl") ?? "";
dialogField.placeholderText = "hostname:port";
property = "cdnProxyUrl";
dialog.open();
});

proxyUrlItem.clicked.connect(() => {
dialog.titleText = "Gateway proxy URL";
dialogField.text = window.store.get("settings").proxyUrl ?? "";
dialogField.text = Settings.get("proxyUrl") ?? "";
dialogField.placeholderText = "hostname:port";
property = "proxyUrl";
dialog.open();
});

debugModeItem.clicked.connect(() => {
window.store.fetch("settings", settings => {
settings.debug = !settings.debug;
window.store.set("settings", settings);
loadSettings();
});
Settings.set("debug", !Settings.get("debug"));
loadSettings();
});

loadSettings();
Expand Down
9 changes: 5 additions & 4 deletions src/js/ui/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Client } from "client/Client";
import { DatabaseStore } from "store/DatabaseStore";
import { Settings } from "store/Settings";

declare const backButton: Qml.ToolButton;
declare const loginButton: Qml.ToolButton;
Expand Down Expand Up @@ -34,10 +35,10 @@ function handleReady() {
});

loginButton.clicked.connect(() => {
const settings = window.store.get("settings");
const token = Settings.get("token");

if (settings.token) {
window.client.login(settings.token);
if (token) {
window.client.login(token);
} else {
banner.text = "You need to provide a token in order to sign in.";
banner.open();
Expand All @@ -63,7 +64,7 @@ function handleReady() {
});

window.client.on("debug", msg => {
if (window.store.get("settings").debug) {
if (Settings.get("debug")) {
console.log("[Socket DEBUG]", msg);
}
});
Expand Down

0 comments on commit 3f0f723

Please sign in to comment.