@@ -7,6 +7,26 @@ import { useSettings, monoFontFamily } from "@/context/settings"
77import { playSound , SOUND_OPTIONS } from "@/utils/sound"
88import { Link } from "./link"
99
10+
11+ let demoSoundState = {
12+ cleanup : undefined as ( ( ) => void ) | undefined ,
13+ timeout : undefined as NodeJS . Timeout | undefined ,
14+ }
15+
16+ // To prevent audio from overlapping/playing very quickly when navigating the settings menus,
17+ // delay the playback by 100ms during quick selection changes and pause existing sounds.
18+ const playDemoSound = ( src : string ) => {
19+ if ( demoSoundState . cleanup ) {
20+ demoSoundState . cleanup ( ) ;
21+ }
22+
23+ clearTimeout ( demoSoundState . timeout )
24+
25+ demoSoundState . timeout = setTimeout ( ( ) => {
26+ demoSoundState . cleanup = playSound ( src )
27+ } , 100 )
28+ }
29+
1030export const SettingsGeneral : Component = ( ) => {
1131 const theme = useTheme ( )
1232 const language = useLanguage ( )
@@ -211,12 +231,12 @@ export const SettingsGeneral: Component = () => {
211231 label = { ( o ) => language . t ( o . label ) }
212232 onHighlight = { ( option ) => {
213233 if ( ! option ) return
214- playSound ( option . src )
234+ playDemoSound ( option . src )
215235 } }
216236 onSelect = { ( option ) => {
217237 if ( ! option ) return
218238 settings . sounds . setAgent ( option . id )
219- playSound ( option . src )
239+ playDemoSound ( option . src )
220240 } }
221241 variant = "secondary"
222242 size = "small"
@@ -235,12 +255,12 @@ export const SettingsGeneral: Component = () => {
235255 label = { ( o ) => language . t ( o . label ) }
236256 onHighlight = { ( option ) => {
237257 if ( ! option ) return
238- playSound ( option . src )
258+ playDemoSound ( option . src )
239259 } }
240260 onSelect = { ( option ) => {
241261 if ( ! option ) return
242262 settings . sounds . setPermissions ( option . id )
243- playSound ( option . src )
263+ playDemoSound ( option . src )
244264 } }
245265 variant = "secondary"
246266 size = "small"
@@ -259,12 +279,12 @@ export const SettingsGeneral: Component = () => {
259279 label = { ( o ) => language . t ( o . label ) }
260280 onHighlight = { ( option ) => {
261281 if ( ! option ) return
262- playSound ( option . src )
282+ playDemoSound ( option . src )
263283 } }
264284 onSelect = { ( option ) => {
265285 if ( ! option ) return
266286 settings . sounds . setErrors ( option . id )
267- playSound ( option . src )
287+ playDemoSound ( option . src )
268288 } }
269289 variant = "secondary"
270290 size = "small"
0 commit comments