Skip to content

Commit b175612

Browse files
authored
Merge pull request #286 from argonism/ku/notification-settings
Add notify setting
2 parents 60ab054 + 8dde91f commit b175612

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

src/lib/Setting.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type SettingType = {
1616
readonly bdashServer: BdashServerSettingType;
1717
readonly experimentalFeature: ExperimentalFeatureSettingType;
1818
readonly defaultDataSourceId: number | null;
19+
readonly notification: NotificationSettingType;
1920
};
2021

2122
export type FormatterSettingType = {
@@ -39,6 +40,14 @@ export type ExperimentalFeatureSettingType = {
3940
autoCompleteEnabled: boolean;
4041
};
4142

43+
export const notificationWhenOptions = ["focusing", "not_focusing", "always"] as const;
44+
export type NotificationWhenType = typeof notificationWhenOptions[number];
45+
46+
export type NotificationSettingType = {
47+
enabled: boolean;
48+
when: NotificationWhenType;
49+
};
50+
4251
// type for partial updating parameter.
4352
export type PartialSettingType = { [P in keyof SettingType]?: Partial<SettingType[P]> };
4453

@@ -66,6 +75,10 @@ export default class Setting {
6675
autoCompleteEnabled: false,
6776
},
6877
defaultDataSourceId: null,
78+
notification: {
79+
enabled: true,
80+
when: "not_focusing",
81+
},
6982
};
7083
}
7184

src/main/window.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@ import electron, { BrowserWindow, dialog, ipcMain, shell, Notification } from "e
22
import path from "path";
33
import logger from "./logger";
44
import Config from "./Config";
5+
import { SettingType } from "src/lib/Setting";
56

67
const windows: BrowserWindow[] = [];
78

9+
const shouldNotify = (setting: SettingType, isFocused: boolean) => {
10+
return (
11+
setting.notification.enabled &&
12+
(setting.notification.when === "always" ||
13+
(setting.notification.when === "focusing" && isFocused) ||
14+
(setting.notification.when === "not_focusing" && !isFocused))
15+
);
16+
};
17+
818
export async function createWindow(): Promise<void> {
919
const win = new electron.BrowserWindow({
1020
width: 1280,
@@ -21,9 +31,9 @@ export async function createWindow(): Promise<void> {
2131
ipcMain.handle("getConfig", async () => Config);
2232

2333
ipcMain.on("queryCompleted", (_event, data) => {
24-
const { success, title, runtime, rowCount, errorMessage } = data;
34+
const { success, title, runtime, rowCount, errorMessage, _setting } = data;
2535

26-
if (!win.isFocused()) {
36+
if (shouldNotify(_setting, win.isFocused())) {
2737
const notificationTitle = success ? "✅️ Query completed" : "❌️ Query failed";
2838
let notificationBody;
2939

src/renderer/pages/Query/QueryAction.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const QueryAction = {
8686
const { query: queryBody, startLine } = await Util.findQueryByLine(query.body, dataSource.mimeType, line);
8787
const executor = DataSource.create(dataSource);
8888
const id = query.id;
89+
const _setting = setting.load();
8990
dispatch("updateQuery", { id, params: { status: "working", executor } });
9091

9192
const start = Date.now();
@@ -110,6 +111,7 @@ const QueryAction = {
110111
success: false,
111112
title: query.title,
112113
errorMessage: err.message,
114+
_setting,
113115
});
114116

115117
return;
@@ -141,6 +143,7 @@ const QueryAction = {
141143
title: query.title,
142144
runtime: params.runtime,
143145
rowCount: result.rows?.length || 0,
146+
_setting,
144147
});
145148
},
146149

src/renderer/pages/Setting/Setting.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Action from "./SettingAction";
66
import Button from "../../components/Button";
77
import ProgressIcon from "../../components/ProgressIcon";
88
import { selectStyles } from "../../components/Select";
9-
import { indentValues } from "../../../lib/Setting";
9+
import { indentValues, notificationWhenOptions } from "../../../lib/Setting";
1010

1111
class Setting extends React.Component<unknown, SettingState> {
1212
override componentDidMount(): void {
@@ -174,6 +174,28 @@ class Setting extends React.Component<unknown, SettingState> {
174174
/>
175175
</div>
176176
</div>
177+
<div className="page-Setting-section1">
178+
<h1>Notification</h1>
179+
<div className="page-Setting-section2">
180+
<h2>Enable</h2>
181+
<input
182+
type="checkbox"
183+
onChange={(e): void => Action.update({ notification: { enabled: e.target.checked } })}
184+
checked={setting.notification.enabled}
185+
/>
186+
</div>
187+
<div className="page-Setting-section2">
188+
<h2>Notify when</h2>
189+
<Select
190+
value={{ value: setting.notification.when, label: setting.notification.when }}
191+
options={notificationWhenOptions.map((v) => ({ value: v, label: v }))}
192+
onChange={(e): void => Action.update({ notification: { when: (e as OptionTypeBase).value } })}
193+
isClearable={false}
194+
isSearchable={false}
195+
styles={selectStyles}
196+
/>
197+
</div>
198+
</div>
177199
<div className="page-Setting-section1">
178200
<h1>Experimental Features</h1>
179201
<div className="page-Setting-section2">

0 commit comments

Comments
 (0)