Skip to content

Commit 7105fd6

Browse files
committed
feat: add an option to turn off notifications
1 parent 5d84e62 commit 7105fd6

7 files changed

Lines changed: 50 additions & 12 deletions

File tree

‎ext/options.html‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
min="1"
1919
max="65535"
2020
size="4"
21+
required
22+
>
23+
</label>
24+
<label for="enableNotifications"></label>
25+
<label>
26+
<input
27+
id="enableNotifications"
28+
name="enableNotifications"
29+
type="checkbox"
2130
>
2231
</label>
2332
<span></span>

‎src/app-options/ghostbird_options_element.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,32 @@ export class GhostbirdOptionsElement extends HTMLElement {
1010
}
1111

1212
async startSync(optionsStore: IOptionsStore): Promise<never> {
13-
let q = await optionsStore.syncForm(this.optionForm)
14-
13+
let form = this.optionForm
1514
let progress = this.progress
15+
let q = await optionsStore.syncForm(form)
16+
1617
for (;;) {
1718
await q.waitReady()
19+
let msg = q.dequeueReceived()
1820

1921
progress.classList.remove("saved", "error")
2022

21-
// Yield to separate tasks for removing and adding of the class
23+
// Yield to reflect the change to DOM
2224
await Promise.resolve()
2325

24-
let msg = q.dequeueReceived()
2526
switch (msg) {
2627
case "saved":
27-
progress.classList.add("saved")
28-
break
2928
case "error":
30-
progress.classList.add("error")
29+
if (!form.checkValidity()) {
30+
// When the form is invalid, OptionsSync keeps the last valid value
31+
// which results in "saved". We want to show error instead
32+
msg = "error"
33+
}
34+
35+
progress.classList.add(msg)
36+
break
37+
default:
38+
console.warn("unknown message", msg)
3139
break
3240
}
3341
}

‎src/ghosttext-adaptor/api.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ export interface IManifestInfo {
7878
}
7979

8080
/** Options stored in local storage */
81-
export type StoredOptions = { serverPort: number }
81+
export type StoredOptions = {
82+
serverPort: number
83+
enableNotifications: boolean
84+
}
8285

8386
/** * Loads options from storage */
8487
export interface IStoredOptionsLoader {

‎src/ghosttext-adaptor/client_options_loader.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ export class ClientOptionsLoader implements IClientOptionsLoader {
1010
) {}
1111

1212
async load(): Promise<ClientOptions> {
13-
let { serverPort } = await this.storedOptionsLoader.load()
13+
let { serverPort, enableNotifications } = await this.storedOptionsLoader.load()
1414
const extensionId = this.manifestInfo.getId()
1515
let serverUrl = new URL(`http://localhost:${serverPort}/`)
1616
let { host } = new URL(`extension://${extensionId}.localhost`)
1717

1818
return {
1919
serverUrl,
2020
clientHostName: host,
21+
enableNotifications,
2122
}
2223
}
2324
}
@@ -27,13 +28,19 @@ if (import.meta.vitest) {
2728

2829
describe(ClientOptionsLoader, () => {
2930
it("should build a ClientOption from storage and manifest info correctly", async () => {
30-
const storedOptionsLoader = { load: vi.fn().mockResolvedValue({ serverPort: 1234 }) }
31+
const storedOptionsLoader = {
32+
load: vi.fn().mockResolvedValue({
33+
serverPort: 1234,
34+
enableNotifications: true,
35+
}),
36+
}
3137
const manifestInfo = { getId: vi.fn().mockReturnValue("@test.extension") }
3238
const sut = new ClientOptionsLoader(storedOptionsLoader, manifestInfo)
3339

3440
await expect(sut.load()).resolves.deep.equals({
3541
serverUrl: new URL("http://localhost:1234/"),
3642
clientHostName: "test.extension.localhost",
43+
enableNotifications: true,
3744
})
3845
})
3946
})

‎src/ghosttext-adaptor/email_editor_factory.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ export class EmailEditorFactory {
1212

1313
async create(tab: IComposeWindow, port: IGhostServerPort): Promise<EmailEditor> {
1414
const options = await this.clientOptionsLoader.load()
15-
return new EmailEditor(this.notificationTray, tab, port, options)
15+
let tray = options.enableNotifications ? this.notificationTray : new NullNotificationTray()
16+
17+
return new EmailEditor(tray, tab, port, options)
1618
}
1719
}
20+
21+
class NullNotificationTray implements INotificationTray {
22+
async showNotification(): Promise<void> {}
23+
}

‎src/ghosttext-runner/api.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ export interface IGhostTextConnector {
2828

2929
/** Option fields needed for GhostText client */
3030
export type ClientOptions = {
31+
/** Host name to use as `url` field */
3132
readonly clientHostName: string
3233
/** Initial GET request will be sent here */
3334
readonly serverUrl: URL
35+
/** Show notifications on connect/disconnect events if true */
36+
readonly enableNotifications: boolean
3437
}
3538

3639
/**

‎src/thunderbird/options_util/options_store.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class OptionsStore implements IStoredOptionsLoader {
1111
storageType: "local",
1212
defaults: {
1313
serverPort: 4001,
14+
enableNotifications: true,
1415
},
1516
})
1617

@@ -24,10 +25,11 @@ export class OptionsStore implements IStoredOptionsLoader {
2425
try {
2526
e.preventDefault()
2627
await this.options.setAll(this.options.defaults)
27-
q.pushReceived("saved")
2828

2929
// Restart sync to let OptionsStore update the form
3030
await this.options.syncForm(form)
31+
32+
q.pushReceived("saved")
3133
} catch {
3234
q.pushReceived("error")
3335
}

0 commit comments

Comments
 (0)