Skip to content

Commit

Permalink
fix(dorion-notifications): prevent double notifs, better settings inj…
Browse files Browse the repository at this point in the history
…ectiong
  • Loading branch information
SpikeHD committed Aug 14, 2024
1 parent 4b87de1 commit 758a322
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 79 deletions.
1 change: 1 addition & 0 deletions api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface GlobalApi {
cssSanitize: (css: string) => string
fetchImage: (url: string) => Promise<string>
applyNotificationCount: () => void
waitForElm: (selector: string) => Promise<HTMLElement>
}

shouldShowUnreadBadge: boolean
Expand Down
150 changes: 71 additions & 79 deletions plugins/dorion-notifications/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@ const {
solid: {
createSignal,
},
observeDom,
patcher
} = shelter

Notification.requestPermission()

const [settings, setSettings] = createSignal<DorionSettings>(null)
const notifSelector = 'div[class*="contentColumn"] div[class*="container"]'

// Overwrite the default window.Notification function
const unpatchNotif = patcher.instead('Notification', window, () => {
return
})

let unobserve = null
let isOnNotifSection = false
let newSettingInjected = false

Expand All @@ -36,75 +30,75 @@ const settingsHandler = async (payload) => {
else if (isOnNotifSection) return
isOnNotifSection = true

unobserve = observeDom(notifSelector, (node: HTMLDivElement) => {
// Only ever need to get the first
unobserve.now()

node.style.display = 'none'

// The next node after should also be hidden
const next = node.nextElementSibling as HTMLDivElement
if (next) next.style.display = 'none'

if (newSettingInjected) return

const newNotifs = [
(
<SwitchItem
note="Shows a red badge on the app icon when you have unread messages."
value={settings()?.unread_badge}
onChange={async (value) => {
setSettings({
...settings(),
unread_badge: value
})

await invoke('write_config_file', {
contents: JSON.stringify(settings())
})

api.shouldShowUnreadBadge = value

// Also wipe the current badge if it was enabled
if (!value) invoke('notif_count', { amount: 0 })
else api.util.applyNotificationCount()
}}
>
Enable Unread Message Badge
</SwitchItem>
),
(
<SwitchItem
note="If you're looking for per-channel or per-server notifications, right-click the desired server icon and select Notification Settings."
value={settings()?.desktop_notifications}
onChange={async (value) => {
setSettings({
...settings(),
desktop_notifications: value
})

// If enabling, dispatch the flux event as well
FluxDispatcher.dispatch({
type: 'NOTIFICATIONS_SET_PERMISSION_STATE',
enabled: value ? 'ENABLED' : 'DISABLED'
})

await invoke('write_config_file', {
contents: JSON.stringify(settings())
})
}}
>
Enable Desktop Notifications
</SwitchItem>
)
]

for (const newNotif of newNotifs) {
node.parentElement.prepend(newNotif)
}

newSettingInjected = true
})
// Wait for notif tab to load
await window.Dorion.util.waitForElm('#notifications-tab')

const node = document.querySelector(notifSelector) as HTMLElement

node.style.display = 'none'

// The next node after should also be hidden
const next = node.nextElementSibling as HTMLDivElement
if (next) next.style.display = 'none'

if (newSettingInjected) return

const newNotifs = [
(
<SwitchItem
note="Shows a red badge on the app icon when you have unread messages."
value={settings()?.unread_badge}
onChange={async (value) => {
setSettings({
...settings(),
unread_badge: value
})

await invoke('write_config_file', {
contents: JSON.stringify(settings())
})

api.shouldShowUnreadBadge = value

// Also wipe the current badge if it was enabled
if (!value) invoke('notif_count', { amount: 0 })
else api.util.applyNotificationCount()
}}
>
Enable Unread Message Badge
</SwitchItem>
),
(
<SwitchItem
note="If you're looking for per-channel or per-server notifications, right-click the desired server icon and select Notification Settings."
value={settings()?.desktop_notifications}
onChange={async (value) => {
setSettings({
...settings(),
desktop_notifications: value
})

// If enabling, dispatch the flux event as well
FluxDispatcher.dispatch({
type: 'NOTIFICATIONS_SET_PERMISSION_STATE',
enabled: value ? 'ENABLED' : 'DISABLED'
})

await invoke('write_config_file', {
contents: JSON.stringify(settings())
})
}}
>
Enable Desktop Notifications
</SwitchItem>
)
]

for (const newNotif of newNotifs) {
node.parentElement.prepend(newNotif)
}

newSettingInjected = true
}

const notifHandler = (payload) => {
Expand All @@ -127,8 +121,6 @@ export const onLoad = async () => {
}

export const onUnload = () => {
unobserve?.now()
FluxDispatcher.unsubscribe('USER_SETTINGS_MODAL_SET_SECTION', settingsHandler)
FluxDispatcher.unsubscribe('RPC_NOTIFICATION_CREATE', notifHandler)
unpatchNotif()
}

0 comments on commit 758a322

Please sign in to comment.