useOnlineNotificationSound.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { storeToRefs } from 'pinia'
  3. import { computed } from 'vue'
  4. import { useSessionStore } from '#shared/stores/session.ts'
  5. const defaultSoundFile = 'Xylo.mp3'
  6. export const useOnlineNotificationSound = () => {
  7. const { user } = storeToRefs(useSessionStore())
  8. const isEnabled = computed(
  9. () => Boolean(user.value?.preferences?.notification_sound?.enabled ?? true), // it is enabled by default for new users
  10. )
  11. const audioPath = computed(() => {
  12. const fileName = user.value?.preferences?.notification_sound?.file
  13. return `/assets/sounds/${fileName ?? defaultSoundFile}`
  14. })
  15. const getPreloadedAudio = (path: string) => {
  16. const sound = new Audio(path)
  17. sound.preload = 'auto'
  18. return sound
  19. }
  20. const audio = computed(() => getPreloadedAudio(audioPath.value))
  21. const play = () =>
  22. audio.value?.play().catch(() => {
  23. console.error('Failed to play notification sound')
  24. })
  25. return {
  26. isEnabled,
  27. play,
  28. }
  29. }