Rework alert settings and UI
This commit is contained in:
@@ -73,7 +73,7 @@ import {
|
||||
getThunkError,
|
||||
} from "@welshman/app"
|
||||
import {compressFile} from "@lib/html"
|
||||
import type {SettingsValues} from "@app/core/state"
|
||||
import type {SettingsValues, SpaceNotificationSettings} from "@app/core/state"
|
||||
import {
|
||||
SETTINGS,
|
||||
PROTECTED,
|
||||
@@ -82,6 +82,7 @@ import {
|
||||
userSpaceUrls,
|
||||
userSettingsValues,
|
||||
getSetting,
|
||||
getSettings,
|
||||
userGroupList,
|
||||
shouldIgnoreError,
|
||||
stripPrefix,
|
||||
@@ -356,6 +357,48 @@ export const addTrustedRelay = async (url: string) =>
|
||||
export const removeTrustedRelay = async (url: string) =>
|
||||
publishSettings({trusted_relays: remove(url, getSetting<string[]>("trusted_relays"))})
|
||||
|
||||
// Space and room notification settings
|
||||
|
||||
export const setSpaceNotifications = async (url: string, notify: boolean) => {
|
||||
const {alerts} = getSettings()
|
||||
const existing = alerts.find((s: SpaceNotificationSettings) => s.url === url)
|
||||
|
||||
let updated: typeof alerts
|
||||
|
||||
if (existing) {
|
||||
// Clear exceptions when changing the space notification setting
|
||||
updated = alerts.map((s: SpaceNotificationSettings) =>
|
||||
s.url === url ? {...s, notify, exceptions: []} : s,
|
||||
)
|
||||
} else {
|
||||
updated = [...alerts, {url, notify, exceptions: []}]
|
||||
}
|
||||
|
||||
return publishSettings({alerts: updated})
|
||||
}
|
||||
|
||||
export const toggleRoomNotifications = async (url: string, h: string) => {
|
||||
const {alerts} = getSettings()
|
||||
const existing = alerts.find((s: SpaceNotificationSettings) => s.url === url)
|
||||
|
||||
let updated: typeof alerts
|
||||
|
||||
if (!existing) {
|
||||
// No space settings yet, create one with this room as an exception (default is notify: true)
|
||||
updated = [...alerts, {url, notify: true, exceptions: [h]}]
|
||||
} else {
|
||||
// Toggle exception status
|
||||
const hasException = existing.exceptions.includes(h)
|
||||
const exceptions = hasException ? remove(h, existing.exceptions) : append(h, existing.exceptions)
|
||||
|
||||
updated = alerts.map((s: SpaceNotificationSettings) =>
|
||||
s.url === url ? {...s, exceptions} : s,
|
||||
)
|
||||
}
|
||||
|
||||
return publishSettings({alerts: updated})
|
||||
}
|
||||
|
||||
// Join request
|
||||
|
||||
export type JoinRequestParams = {
|
||||
|
||||
+23
-2
@@ -271,6 +271,12 @@ export enum RelayAuthMode {
|
||||
Conservative = "conservative",
|
||||
}
|
||||
|
||||
export type SpaceNotificationSettings = {
|
||||
url: string
|
||||
notify: boolean
|
||||
exceptions: string[]
|
||||
}
|
||||
|
||||
export type SettingsValues = {
|
||||
show_media: boolean
|
||||
hide_sensitive: boolean
|
||||
@@ -280,7 +286,7 @@ export type SettingsValues = {
|
||||
relay_auth: RelayAuthMode
|
||||
send_delay: number
|
||||
font_size: number
|
||||
muted_rooms: string[]
|
||||
alerts: SpaceNotificationSettings[]
|
||||
}
|
||||
|
||||
export type Settings = {
|
||||
@@ -297,7 +303,7 @@ export const defaultSettings: SettingsValues = {
|
||||
relay_auth: RelayAuthMode.Conservative,
|
||||
send_delay: 0,
|
||||
font_size: 1.1,
|
||||
muted_rooms: [],
|
||||
alerts: [],
|
||||
}
|
||||
|
||||
export const settingsByPubkey = deriveItemsByKey({
|
||||
@@ -993,3 +999,18 @@ export const parseInviteLink = (invite: string): InviteData | undefined => {
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
// Hierarchical notification helpers
|
||||
|
||||
export const isMuted = (url: string, h?: string) => {
|
||||
const {alerts} = getSettings()
|
||||
const pref = alerts.find(spec({url}))
|
||||
|
||||
if (!pref) return true
|
||||
if (!h) return pref.notify
|
||||
if (pref.notify) return !pref.exceptions.includes(h)
|
||||
if (!pref.notify) return pref.exceptions.includes(h)
|
||||
}
|
||||
|
||||
export const deriveIsMuted = (url: string, h?: string) =>
|
||||
derived(userSettingsValues, () => isMuted(url, h))
|
||||
|
||||
Reference in New Issue
Block a user