Skip to content

Commit

Permalink
Update electron-store and make the config strongly-typed
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 9, 2019
1 parent a39b236 commit 7965b36
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 44 deletions.
52 changes: 33 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"main": "dist-js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"postinstall": "electron-builder install-app-deps",
Expand All @@ -30,7 +30,7 @@
"electron-dl": "^1.14.0",
"electron-localshortcut": "^3.1.0",
"electron-log": "^3.0.7",
"electron-store": "4.0.0",
"electron-store": "^5.0.0",
"electron-updater": "^4.0.14",
"electron-util": "^0.11.0",
"element-ready": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion source/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ ipc.on('zoom-reset', () => {
});

ipc.on('zoom-in', () => {
const zoomFactor = (config.get('zoomFactor') as number) + 0.1;
const zoomFactor = config.get('zoomFactor') + 0.1;

if (zoomFactor < 1.6) {
setZoom(zoomFactor);
Expand Down
62 changes: 50 additions & 12 deletions source/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
import Store = require('electron-store');
import {is} from 'electron-util';
import {JSONSchema} from 'json-schema-typed';

const schema: {[key: string]: JSONSchema} = {
type StoreType = {
followSystemAppearance: boolean;
darkMode: boolean;
privateMode: boolean;
vibrancy: 'none' | 'sidebar' | 'full';
zoomFactor: number;
lastWindowState: {
x: number;
y: number;
width: number;
height: number;
};
menuBarMode: boolean;
showDockIcon: boolean;
showTrayIcon: boolean;
alwaysOnTop?: boolean;
bounceDockOnMessage: boolean;
showUnreadBadge: boolean;
showMessageButtons: boolean;
launchMinimized: boolean;
flashWindowOnMessage: boolean;
notificationMessagePreview: boolean;
block: {
chatSeen: boolean;
typingIndicator: boolean;
deliveryReceipt: boolean;
};
emojiStyle: 'native' | 'facebook-3-0' | 'messenger-1-0' | 'facebook-2-2';
confirmImagePaste: boolean;
useWorkChat: boolean;
sidebarHidden: boolean;
autoHideMenuBar: boolean;
notificationsMuted: boolean;
hardwareAcceleration: boolean;
quitOnWindowClose: boolean;
keepMeSignedIn: boolean;
autoplayVideos: boolean;
};

const schema: {[Key in keyof StoreType]: Store.Schema} = {
followSystemAppearance: {
type: 'boolean',
default: true
Expand All @@ -29,24 +67,24 @@ const schema: {[key: string]: JSONSchema} = {
lastWindowState: {
type: 'object',
properties: {
width: {
x: {
type: 'number'
},
height: {
y: {
type: 'number'
},
x: {
width: {
type: 'number'
},
y: {
height: {
type: 'number'
}
},
default: {
width: 800,
height: 600,
x: undefined,
y: undefined
y: undefined,
width: 800,
height: 600
}
},
menuBarMode: {
Expand Down Expand Up @@ -150,7 +188,7 @@ const schema: {[key: string]: JSONSchema} = {
}
};

function updateVibrancySetting(store: Store<any>): void {
function updateVibrancySetting(store: Store): void {
const vibrancy = store.get('vibrancy');

if (!is.macos) {
Expand All @@ -162,11 +200,11 @@ function updateVibrancySetting(store: Store<any>): void {
}
}

function migrate(store: Store<any>): void {
function migrate(store: Store): void {
updateVibrancySetting(store);
}

const store: Store<any> = new Store({schema});
const store = new Store<StoreType>({schema});
migrate(store);

export default store;
3 changes: 2 additions & 1 deletion source/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ with this: https://static.xx.fbcdn.net/images/emoji.php/v9/z27/2/32/1f600.png
(see here) ^
*/
export async function process(url: string): Promise<Response> {
const emojiStyle = config.get('emojiStyle');
// TODO: Remove the `any` here.
const emojiStyle = config.get('emojiStyle') as any;
const emojiSetCode = codeForEmojiStyle(emojiStyle);

// The character code is the filename without the extension.
Expand Down
6 changes: 3 additions & 3 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ function initRequestsFiltering(): void {
if (url.includes('emoji.php')) {
callback(await processEmojiUrl(url));
} else if (url.includes('typ.php')) {
callback({cancel: config.get('block.typingIndicator')});
callback({cancel: config.get('block.typingIndicator' as any)});
} else if (url.includes('change_read_status.php')) {
callback({cancel: config.get('block.chatSeen')});
callback({cancel: config.get('block.chatSeen' as any)});
} else if (url.includes('delivery_receipts') || url.includes('unread_threads')) {
callback({cancel: config.get('block.deliveryReceipt')});
callback({cancel: config.get('block.deliveryReceipt' as any)});
}
});
}
Expand Down
12 changes: 6 additions & 6 deletions source/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,25 @@ export default async function updateMenu(): Promise<Menu> {
{
label: 'Block Seen Indicator',
type: 'checkbox',
checked: config.get('block.chatSeen'),
checked: config.get('block.chatSeen' as any),
click(menuItem) {
config.set('block.chatSeen', menuItem.checked);
config.set('block.chatSeen' as any, menuItem.checked);
}
},
{
label: 'Block Typing Indicator',
type: 'checkbox',
checked: config.get('block.typingIndicator'),
checked: config.get('block.typingIndicator' as any),
click(menuItem) {
config.set('block.typingIndicator', menuItem.checked);
config.set('block.typingIndicator' as any, menuItem.checked);
}
},
{
label: 'Block Delivery Receipts',
type: 'checkbox',
checked: config.get('block.deliveryReceipt'),
checked: config.get('block.deliveryReceipt' as any),
click(menuItem) {
config.set('block.deliveryReceipt', menuItem.checked);
config.set('block.deliveryReceipt' as any, menuItem.checked);
}
}
];
Expand Down

0 comments on commit 7965b36

Please sign in to comment.