Skip to content

Commit 16e0ab5

Browse files
committed
Play sound on new notification
1 parent 7d1dc38 commit 16e0ab5

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export enum Page {
99
Landing = 'Landing',
1010
}
1111

12+
export enum InvokeCommand {
13+
PlayNotificationSound = 'play_notification_sound',
14+
SetIconTemplate = 'set_icon_template',
15+
}
16+
1217
export enum NotificationSubject {
1318
CheckSuite = 'CheckSuite',
1419
Discussion = 'Discussion',

src/stores/store.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ import { defineStore } from 'pinia'
33
import { readonly, ref, watch } from 'vue'
44
import type { Thread } from '../api/notifications'
55
import { getNotifications } from '../api/notifications'
6-
import { Page } from '../constants'
6+
import { InvokeCommand, Page } from '../constants'
77
import { AppStorage } from '../storage'
88
import type { NotificationListData, Option } from '../types'
99
import { notificationListFromThreads } from '../utils/notification'
1010

11+
function hasNewNotification(newThreads: Thread[], previousThreads: Thread[]) {
12+
const newThreadsFiltered = newThreads.filter(t => t.unread)
13+
const previousThreadsFiltered = previousThreads.filter(t => t.unread)
14+
return !newThreadsFiltered.every(newT => previousThreadsFiltered.some(prevT => prevT.id === newT.id))
15+
}
16+
1117
export const useStore = defineStore('store', () => {
1218
const notifications = ref<NotificationListData[]>([])
1319
const loadingNotifications = ref(false)
@@ -48,17 +54,23 @@ export const useStore = defineStore('store', () => {
4854
notifications.value = []
4955
failedLoadingNotifications.value = true
5056
}
51-
finally {
52-
loadingNotifications.value = false
53-
skeletonVisible.value = false
54-
}
57+
58+
loadingNotifications.value = false
59+
skeletonVisible.value = false
60+
61+
if (
62+
AppStorage.get('soundsEnabled')
63+
&& hasNewNotification(notificationsRaw, notificationsRawPrevious)
64+
)
65+
invoke(InvokeCommand.PlayNotificationSound)
5566
}
5667

5768
const currentPage = ref(Page.Landing)
5869
const pageFrom = ref<Option<Page>>(null)
5970

6071
function logout() {
6172
AppStorage.set('accessToken', null)
73+
AppStorage.set('user', null)
6274
notifications.value = []
6375
currentPage.value = Page.Landing
6476
}
@@ -73,8 +85,7 @@ export const useStore = defineStore('store', () => {
7385

7486
watch(notifications, () => {
7587
const hasUnread = notificationsRaw.some(n => n.unread)
76-
console.log({ hasUnread })
77-
invoke('set_icon_template', { isTemplate: !hasUnread })
88+
invoke(InvokeCommand.SetIconTemplate, { isTemplate: !hasUnread })
7889
}, { deep: true, immediate: true })
7990

8091
return {

0 commit comments

Comments
 (0)