Skip to content

Commit 522c850

Browse files
committed
Merge branch 'main' of https://github.com/zulip/zulip-desktop into test
2 parents b8c3985 + e32480a commit 522c850

21 files changed

+543
-379
lines changed

app/common/config-util.ts

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,40 @@ import path from "path";
44

55
import {JsonDB} from "node-json-db";
66

7+
import type {DNDSettings} from "./dnd-util";
78
import * as EnterpriseUtil from "./enterprise-util";
89
import Logger from "./logger-util";
910

11+
export interface Config extends DNDSettings {
12+
appLanguage: string | null;
13+
autoHideMenubar: boolean;
14+
autoUpdate: boolean;
15+
badgeOption: boolean;
16+
betaUpdate: boolean;
17+
customCSS: string | false | null;
18+
dnd: boolean;
19+
dndPreviousSettings: Partial<DNDSettings>;
20+
dockBouncing: boolean;
21+
downloadsPath: string;
22+
enableSpellchecker: boolean;
23+
errorReporting: boolean;
24+
lastActiveTab: number;
25+
promptDownload: boolean;
26+
proxyBypass: string;
27+
proxyPAC: string;
28+
proxyRules: string;
29+
quitOnClose: boolean;
30+
showSidebar: boolean;
31+
spellcheckerLanguages: string[] | null;
32+
startAtLogin: boolean;
33+
startMinimized: boolean;
34+
systemProxyRules: string;
35+
trayIcon: boolean;
36+
useManualProxy: boolean;
37+
useProxy: boolean;
38+
useSystemProxy: boolean;
39+
}
40+
1041
/* To make the util runnable in both main and renderer process */
1142
const {app, dialog} = process.type === "renderer" ? electron.remote : electron;
1243

@@ -18,7 +49,10 @@ let db: JsonDB;
1849

1950
reloadDB();
2051

21-
export function getConfigItem(key: string, defaultValue: unknown = null): any {
52+
export function getConfigItem<Key extends keyof Config>(
53+
key: Key,
54+
defaultValue: Config[Key],
55+
): Config[Key] {
2256
try {
2357
db.reload();
2458
} catch (error: unknown) {
@@ -35,16 +69,6 @@ export function getConfigItem(key: string, defaultValue: unknown = null): any {
3569
return value;
3670
}
3771

38-
export function getConfigString(key: string, defaultValue: string): string {
39-
const value = getConfigItem(key, defaultValue);
40-
if (typeof value === "string") {
41-
return value;
42-
}
43-
44-
setConfigItem(key, defaultValue);
45-
return defaultValue;
46-
}
47-
4872
// This function returns whether a key exists in the configuration file (settings.json)
4973
export function isConfigItemExists(key: string): boolean {
5074
try {
@@ -58,9 +82,9 @@ export function isConfigItemExists(key: string): boolean {
5882
return value !== undefined;
5983
}
6084

61-
export function setConfigItem(
62-
key: string,
63-
value: unknown,
85+
export function setConfigItem<Key extends keyof Config>(
86+
key: Key,
87+
value: Config[Key],
6488
override?: boolean,
6589
): void {
6690
if (EnterpriseUtil.configItemExists(key) && !override) {

app/common/dnd-util.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import * as ConfigUtil from "./config-util";
22

3-
type SettingName = "showNotification" | "silent" | "flashTaskbarOnMessage";
3+
type SettingName = keyof DNDSettings;
44

55
export interface DNDSettings {
6-
showNotification?: boolean;
7-
silent?: boolean;
8-
flashTaskbarOnMessage?: boolean;
6+
showNotification: boolean;
7+
silent: boolean;
8+
flashTaskbarOnMessage: boolean;
99
}
1010

1111
interface Toggle {
1212
dnd: boolean;
13-
newSettings: DNDSettings;
13+
newSettings: Partial<DNDSettings>;
1414
}
1515

1616
export function toggle(): Toggle {
@@ -20,27 +20,34 @@ export function toggle(): Toggle {
2020
dndSettingList.push("flashTaskbarOnMessage");
2121
}
2222

23-
let newSettings: DNDSettings;
23+
let newSettings: Partial<DNDSettings>;
2424
if (dnd) {
25-
const oldSettings: DNDSettings = {};
25+
const oldSettings: Partial<DNDSettings> = {};
2626
newSettings = {};
2727

2828
// Iterate through the dndSettingList.
2929
for (const settingName of dndSettingList) {
3030
// Store the current value of setting.
31-
oldSettings[settingName] = ConfigUtil.getConfigItem(settingName);
31+
oldSettings[settingName] = ConfigUtil.getConfigItem(
32+
settingName,
33+
settingName !== "silent",
34+
);
3235
// New value of setting.
3336
newSettings[settingName] = settingName === "silent";
3437
}
3538

3639
// Store old value in oldSettings.
3740
ConfigUtil.setConfigItem("dndPreviousSettings", oldSettings);
3841
} else {
39-
newSettings = ConfigUtil.getConfigItem("dndPreviousSettings");
42+
newSettings = ConfigUtil.getConfigItem("dndPreviousSettings", {
43+
showNotification: true,
44+
silent: false,
45+
...(process.platform === "win32" && {flashTaskbarOnMessage: true}),
46+
});
4047
}
4148

4249
for (const settingName of dndSettingList) {
43-
ConfigUtil.setConfigItem(settingName, newSettings[settingName]);
50+
ConfigUtil.setConfigItem(settingName, newSettings[settingName]!);
4451
}
4552

4653
ConfigUtil.setConfigItem("dnd", dnd);

app/common/enterprise-util.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import fs from "fs";
22
import path from "path";
33

4+
import type {Config} from "./config-util";
45
import Logger from "./logger-util";
56

7+
interface EnterpriseConfig extends Config {
8+
presetOrganizations: string[];
9+
}
10+
611
const logger = new Logger({
712
file: "enterprise-util.log",
813
});
914

10-
// TODO: replace enterpriseSettings type with an interface once settings are final
11-
let enterpriseSettings: Record<string, unknown>;
15+
let enterpriseSettings: Partial<EnterpriseConfig>;
1216
let configFile: boolean;
1317

1418
reloadDB();
@@ -39,20 +43,20 @@ export function hasConfigFile(): boolean {
3943
return configFile;
4044
}
4145

42-
export function getConfigItem(key: string, defaultValue?: unknown): any {
46+
export function getConfigItem<Key extends keyof EnterpriseConfig>(
47+
key: Key,
48+
defaultValue: EnterpriseConfig[Key],
49+
): EnterpriseConfig[Key] {
4350
reloadDB();
4451
if (!configFile) {
4552
return defaultValue;
4653
}
4754

48-
if (defaultValue === undefined) {
49-
defaultValue = null;
50-
}
51-
52-
return configItemExists(key) ? enterpriseSettings[key] : defaultValue;
55+
const value = enterpriseSettings[key];
56+
return value === undefined ? defaultValue : (value as EnterpriseConfig[Key]);
5357
}
5458

55-
export function configItemExists(key: string): boolean {
59+
export function configItemExists(key: keyof EnterpriseConfig): boolean {
5660
reloadDB();
5761
if (!configFile) {
5862
return false;

app/common/translation-util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ i18n.configure({
1010
});
1111

1212
/* Fetches the current appLocale from settings.json */
13-
const appLocale = ConfigUtil.getConfigItem("appLanguage");
13+
const appLocale = ConfigUtil.getConfigItem("appLanguage", "en");
1414

1515
/* If no locale present in the json, en is set default */
1616
export function __(phrase: string): string {

app/common/typed-ipc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface RendererMessage {
7373
autoHideMenubar: boolean,
7474
updateMenu: boolean,
7575
) => void;
76-
"toggle-dnd": (state: boolean, newSettings: DNDSettings) => void;
76+
"toggle-dnd": (state: boolean, newSettings: Partial<DNDSettings>) => void;
7777
"toggle-menubar-setting": (state: boolean) => void;
7878
"toggle-sidebar": (show: boolean) => void;
7979
"toggle-sidebar-setting": (state: boolean) => void;

app/main/autoupdater.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ export async function appUpdater(updateFromMenu = false): Promise<void> {
3434
autoUpdater.logger = log;
3535

3636
// Handle auto updates for beta/pre releases
37-
const isBetaUpdate = ConfigUtil.getConfigItem("betaUpdate");
37+
const isBetaUpdate = ConfigUtil.getConfigItem("betaUpdate", false);
3838

39-
autoUpdater.allowPrerelease = isBetaUpdate || false;
39+
autoUpdater.allowPrerelease = isBetaUpdate;
4040

4141
const eventsListenerRemove = ["update-available", "update-not-available"];
4242
autoUpdater.on("update-available", async (info: UpdateInfo) => {

app/main/badge-settings.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ function updateOverlayIcon(
3939
mainWindow: electron.BrowserWindow,
4040
): void {
4141
if (!mainWindow.isFocused()) {
42-
mainWindow.flashFrame(ConfigUtil.getConfigItem("flashTaskbarOnMessage"));
42+
mainWindow.flashFrame(
43+
ConfigUtil.getConfigItem("flashTaskbarOnMessage", true),
44+
);
4345
}
4446

4547
if (messageCount === 0) {

app/main/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function createMainWindow(): Electron.BrowserWindow {
8484

8585
// Keep the app running in background on close event
8686
win.on("close", (event) => {
87-
if (ConfigUtil.getConfigItem("quitOnClose")) {
87+
if (ConfigUtil.getConfigItem("quitOnClose", false)) {
8888
app.quit();
8989
}
9090

@@ -180,7 +180,7 @@ app.commandLine.appendSwitch("force-color-profile", "srgb");
180180

181181
ipcMain.on("set-spellcheck-langs", () => {
182182
ses.setSpellCheckerLanguages(
183-
ConfigUtil.getConfigItem("spellcheckerLanguages"),
183+
ConfigUtil.getConfigItem("spellcheckerLanguages", null) ?? [],
184184
);
185185
});
186186
AppMenu.setMenu({
@@ -190,18 +190,18 @@ app.commandLine.appendSwitch("force-color-profile", "srgb");
190190

191191
// Auto-hide menu bar on Windows + Linux
192192
if (process.platform !== "darwin") {
193-
const shouldHideMenu = ConfigUtil.getConfigItem("autoHideMenubar") || false;
193+
const shouldHideMenu = ConfigUtil.getConfigItem("autoHideMenubar", false);
194194
mainWindow.autoHideMenuBar = shouldHideMenu;
195195
mainWindow.setMenuBarVisibility(!shouldHideMenu);
196196
}
197197

198198
// Initialize sentry for main process
199-
const errorReporting = ConfigUtil.getConfigItem("errorReporting");
199+
const errorReporting = ConfigUtil.getConfigItem("errorReporting", true);
200200
if (errorReporting) {
201201
sentryInit();
202202
}
203203

204-
const isSystemProxy = ConfigUtil.getConfigItem("useSystemProxy");
204+
const isSystemProxy = ConfigUtil.getConfigItem("useSystemProxy", false);
205205

206206
if (isSystemProxy) {
207207
(async () => ProxyUtil.resolveSystemProxy(mainWindow))();
@@ -210,7 +210,7 @@ app.commandLine.appendSwitch("force-color-profile", "srgb");
210210
const page = mainWindow.webContents;
211211

212212
page.on("dom-ready", () => {
213-
if (ConfigUtil.getConfigItem("startMinimized")) {
213+
if (ConfigUtil.getConfigItem("startMinimized", false)) {
214214
mainWindow.hide();
215215
} else {
216216
mainWindow.show();
@@ -237,7 +237,7 @@ app.commandLine.appendSwitch("force-color-profile", "srgb");
237237

238238
page.once("did-frame-finish-load", async () => {
239239
// Initiate auto-updates on MacOS and Windows
240-
if (ConfigUtil.getConfigItem("autoUpdate")) {
240+
if (ConfigUtil.getConfigItem("autoUpdate", true)) {
241241
await appUpdater();
242242
}
243243
});

app/main/linuxupdater.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function linuxUpdateNotification(
1717
session: Electron.session,
1818
): Promise<void> {
1919
let url = "https://api.github.com/repos/zulip/zulip-desktop/releases";
20-
url = ConfigUtil.getConfigItem("betaUpdate") ? url : url + "/latest";
20+
url = ConfigUtil.getConfigItem("betaUpdate", false) ? url : url + "/latest";
2121

2222
try {
2323
const response = await fetchResponse(net.request({url, session}));
@@ -27,7 +27,7 @@ export async function linuxUpdateNotification(
2727
}
2828

2929
const data = JSON.parse(await getStream(response));
30-
const latestVersion = ConfigUtil.getConfigItem("betaUpdate")
30+
const latestVersion = ConfigUtil.getConfigItem("betaUpdate", false)
3131
? data[0].tag_name
3232
: data.tag_name;
3333
if (typeof latestVersion !== "string") {

app/main/menu.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function getViewSubmenu(): Electron.MenuItemConstructorOptions[] {
227227
accelerator: "CommandOrControl+Shift+S",
228228
click(_item, focusedWindow) {
229229
if (focusedWindow) {
230-
const newValue = !ConfigUtil.getConfigItem("showSidebar");
230+
const newValue = !ConfigUtil.getConfigItem("showSidebar", true);
231231
send(focusedWindow.webContents, "toggle-sidebar", newValue);
232232
ConfigUtil.setConfigItem("showSidebar", newValue);
233233
}
@@ -239,7 +239,7 @@ function getViewSubmenu(): Electron.MenuItemConstructorOptions[] {
239239
visible: process.platform !== "darwin",
240240
click(_item, focusedWindow) {
241241
if (focusedWindow) {
242-
const newValue = !ConfigUtil.getConfigItem("autoHideMenubar");
242+
const newValue = !ConfigUtil.getConfigItem("autoHideMenubar", false);
243243
focusedWindow.autoHideMenuBar = newValue;
244244
focusedWindow.setMenuBarVisibility(!newValue);
245245
send(

0 commit comments

Comments
 (0)