db4e552594cb | atagen | 2026-10-26 04:43 UTC | 1 | <script lang="ts"> |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 2 | import { disableNotifications, enableNotifications, notificationPermission, notificationsEnabled } from './notifications'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 3 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 4 | import type { MessageSoundMode } from './message-sound-model'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 5 | import { messageSound, playMessageSound, saveMessageSound, uploadMessageSound } from './message-sound'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 6 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 7 | let busy = false; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 8 | let soundBusy = false; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 9 | let soundError = ''; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 10 | let fileInput: HTMLInputElement; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 11 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 12 | const SOUND_MODES: { id: MessageSoundMode; label: string }[] = [ |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 13 | { id: 'all', label: 'All messages' }, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 14 | { id: 'mentions_dms', label: 'Mentions and DMs' }, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 15 | { id: 'off', label: 'Off' } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 16 | ]; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 17 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 18 | async function guarded(task: () => Promise<void>): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 19 | if (soundBusy) return; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 20 | soundBusy = true; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 21 | soundError = ''; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 22 | try { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 23 | await task(); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 24 | } catch (cause) { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 25 | soundError = cause instanceof Error ? cause.message : 'Could not save the sound settings.'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 26 | } finally { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 27 | soundBusy = false; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 28 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 29 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 30 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 31 | async function upload(event: Event): Promise<void> { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 32 | const input = event.currentTarget as HTMLInputElement; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 33 | const file = input.files?.[0]; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 34 | input.value = ''; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 35 | if (file) await guarded(() => uploadMessageSound(file)); |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 36 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 37 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 38 | $: denied = $notificationPermission === 'denied'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 39 | $: unsupported = $notificationPermission === 'unsupported'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 40 | $: on = $notificationsEnabled && $notificationPermission === 'granted'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 41 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 42 | async function toggle(): Promise<void> { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 43 | if (busy) return; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 44 | busy = true; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 45 | try { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 46 | if (on) disableNotifications(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 47 | else await enableNotifications(); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 48 | } finally { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 49 | busy = false; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 50 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 51 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 52 | </script> |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 53 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 54 | <section aria-label="Notifications"> |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 55 | <div class="sec-head"> |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 56 | <h3 class="sec-h">Notifications</h3> |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 57 | <span class="state" class:ok={on}>{unsupported ? 'Unavailable' : denied ? 'Blocked' : on ? 'On' : 'Off'}</span> |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 58 | </div> |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 59 | <button type="button" class="ghost-btn" disabled={busy || denied || unsupported} aria-pressed={on} on:click={() => void toggle()}> |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 60 | {on ? 'Turn off' : 'Turn on'} |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 61 | </button> |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 62 | <div class="sound-modes" role="radiogroup" aria-label="Message sound"> |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 63 | {#each SOUND_MODES as option (option.id)} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 64 | <button |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 65 | type="button" |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 66 | role="radio" |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 67 | aria-checked={$messageSound.mode === option.id} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 68 | class:active={$messageSound.mode === option.id} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 69 | disabled={soundBusy} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 70 | on:click={() => void guarded(() => saveMessageSound({ ...$messageSound, mode: option.id }))} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 71 | > |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 72 | {option.label} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 73 | </button> |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 74 | {/each} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 75 | </div> |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 76 | <div class="sound-actions"> |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 77 | <button type="button" class="ghost-btn" on:click={() => void playMessageSound()}>Play</button> |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 78 | <button type="button" class="ghost-btn" disabled={soundBusy} on:click={() => fileInput.click()}>Upload sound</button> |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 79 | <button |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 80 | type="button" |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 81 | class="ghost-btn" |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 82 | disabled={soundBusy || $messageSound.sound === null} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 83 | on:click={() => void guarded(() => saveMessageSound({ ...$messageSound, sound: null }))} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 84 | > |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 85 | Reset |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 86 | </button> |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 87 | <input bind:this={fileInput} type="file" accept="audio/*" hidden on:change={(e) => void upload(e)} /> |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 88 | </div> |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 89 | {#if soundError} |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 90 | <p class="sound-error" role="alert">{soundError}</p> |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 91 | {/if} |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 92 | </section> |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 93 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 94 | <style> |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 95 | section { margin-top: 18px; } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 96 | .sec-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 97 | .sec-h { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 0; } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 98 | .state { font-size: 12px; color: var(--muted); } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 99 | .state.ok { color: var(--accent); } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 100 | .sound-modes { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 101 | .sound-modes button { border: 1px solid var(--border); background: transparent; color: var(--muted); border-radius: var(--r-sm); padding: 3px 10px; font: inherit; font-size: 12.5px; cursor: pointer; transition: color var(--pop), border-color var(--pop), background var(--pop); } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 102 | .sound-modes button:hover:not(:disabled) { color: var(--text); } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 103 | .sound-modes button.active { color: var(--text); border-color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, transparent); } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 104 | .sound-modes button:disabled { cursor: wait; } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 105 | .sound-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 106 | .sound-error { margin: 8px 0 0; color: var(--danger); font-size: 13px; } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 107 | </style> |