flotilla/src/app/components/NewNotificationSound.svelte

40 lines
911 B
Svelte
Raw Normal View History

<script lang="ts">
import {onMount} from "svelte"
import {Capacitor} from "@capacitor/core"
import {notificationSettings} from "@app/settings"
2026-06-08 21:14:40 +00:00
import {onNotification} from "@app/push"
let audioElement: HTMLAudioElement
let enabled = $state(false)
2026-01-28 22:08:15 +00:00
const onVisibilityChange = () => {
if (document.hidden) {
enabled = true
} else {
enabled = false
}
2026-01-28 22:08:15 +00:00
}
2025-10-06 18:23:19 +00:00
onMount(() => {
if (Capacitor.isNativePlatform()) return
audioElement.load()
2026-01-28 22:08:15 +00:00
document.addEventListener("visibilitychange", onVisibilityChange)
const unsubscribe = onNotification(() => {
2026-01-28 22:08:15 +00:00
if (enabled && $notificationSettings.sound) {
audioElement?.play()
}
})
2026-01-28 22:08:15 +00:00
return () => {
unsubscribe()
document.removeEventListener("visibilitychange", onVisibilityChange)
}
})
</script>
<audio bind:this={audioElement} src="/new-notification-3-398649.mp3"></audio>