NotificationToast.svelte 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <script lang="ts">
  2. import { settings, playingNotificationSound, isLastActiveTab } from '$lib/stores';
  3. import DOMPurify from 'dompurify';
  4. import { marked } from 'marked';
  5. import { createEventDispatcher, onMount } from 'svelte';
  6. const dispatch = createEventDispatcher();
  7. export let onClick: Function = () => {};
  8. export let title: string = 'HI';
  9. export let content: string;
  10. onMount(() => {
  11. if (!navigator.userActivation.hasBeenActive) {
  12. return;
  13. }
  14. if ($settings?.notificationSound ?? true) {
  15. if (!$playingNotificationSound && $isLastActiveTab) {
  16. playingNotificationSound.set(true);
  17. const audio = new Audio(`/audio/notification.mp3`);
  18. audio.play().finally(() => {
  19. // Ensure the global state is reset after the sound finishes
  20. playingNotificationSound.set(false);
  21. });
  22. }
  23. }
  24. });
  25. </script>
  26. <button
  27. class="flex gap-2.5 text-left min-w-[var(--width)] w-full dark:bg-gray-850 dark:text-white bg-white text-black border border-gray-100 dark:border-gray-850 rounded-xl px-3.5 py-3.5"
  28. on:click={() => {
  29. onClick();
  30. dispatch('closeToast');
  31. }}
  32. >
  33. <div class="shrink-0 self-top -translate-y-0.5">
  34. <img src={'/static/favicon.png'} alt="favicon" class="size-7 rounded-full" />
  35. </div>
  36. <div>
  37. {#if title}
  38. <div class=" text-[13px] font-medium mb-0.5 line-clamp-1 capitalize">{title}</div>
  39. {/if}
  40. <div class=" line-clamp-2 text-xs self-center dark:text-gray-300 font-normal">
  41. {@html DOMPurify.sanitize(marked(content))}
  42. </div>
  43. </div>
  44. </button>