From d8d7957af607a3510c72a30631039001089054e3 Mon Sep 17 00:00:00 2001 From: OMGDuke Date: Fri, 7 Apr 2023 21:09:47 +0100 Subject: [PATCH] Add per game volume, add a default to mute button, fix no music not saving --- README.md | 4 +- package.json | 2 +- src/cache/musicCache.ts | 11 +- src/components/changeTheme/audioPlayer.tsx | 91 +++++++++----- src/components/changeTheme/changePage.tsx | 11 +- src/components/changeTheme/gameSettings.tsx | 130 ++++++++++++++++++++ src/components/changeTheme/index.tsx | 10 ++ src/components/changeTheme/noMusic.tsx | 61 +++++---- src/components/settings/index.tsx | 59 ++++----- src/components/spinner.tsx | 41 ++++++ src/components/themePlayer/index.tsx | 2 +- src/context/settingsContext.tsx | 44 +++---- src/hooks/useThemeMusic.ts | 42 +++---- src/localisation/en.json | 5 + 14 files changed, 382 insertions(+), 131 deletions(-) create mode 100644 src/components/changeTheme/gameSettings.tsx create mode 100644 src/components/spinner.tsx diff --git a/README.md b/README.md index a6ab439..ace538d 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ Localisation is now available via [Crowdin](https://crowdin.com/project/sdh-game [![fr translation](https://img.shields.io/badge/dynamic/json?color=blue&label=French&style=flat&logo=data:image/svg%2bxml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGlkPSJmbGFnLWljb25zLWZyIiB2aWV3Qm94PSIwIDAgNjQwIDQ4MCI+CiAgPGcgZmlsbC1ydWxlPSJldmVub2RkIiBzdHJva2Utd2lkdGg9IjFwdCI+CiAgICA8cGF0aCBmaWxsPSIjZmZmIiBkPSJNMCAwaDY0MHY0ODBIMHoiLz4KICAgIDxwYXRoIGZpbGw9IiMwMDI2NTQiIGQ9Ik0wIDBoMjEzLjN2NDgwSDB6Ii8+CiAgICA8cGF0aCBmaWxsPSIjY2UxMTI2IiBkPSJNNDI2LjcgMEg2NDB2NDgwSDQyNi43eiIvPgogIDwvZz4KPC9zdmc+Cg==&query=%24.progress.8.data.translationProgress&url=https%3A%2F%2Fbadges.awesome-crowdin.com%2Fstats-15707857-578443.json)](https://crowdin.com/project/sdh-gamethememusic/fr) -[![de translation](https://img.shields.io/badge/dynamic/json?color=blue&label=German&style=flat&logo=data:image/svg%2bxml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGlkPSJmbGFnLWljb25zLWRlIiB2aWV3Qm94PSIwIDAgNjQwIDQ4MCI+CiAgPHBhdGggZmlsbD0iI2ZmY2UwMCIgZD0iTTAgMzIwaDY0MHYxNjBIMHoiLz4KICA8cGF0aCBkPSJNMCAwaDY0MHYxNjBIMHoiLz4KICA8cGF0aCBmaWxsPSIjZDAwIiBkPSJNMCAxNjBoNjQwdjE2MEgweiIvPgo8L3N2Zz4K&query=%24.progress.3.data.translationProgress&url=https%3A%2F%2Fbadges.awesome-crowdin.com%2Fstats-15707857-578443.json)](https://crowdin.com/project/sdh-gamethememusic/de) +[![de translation](https://img.shields.io/badge/dynamic/json?color=blue&label=German&style=flat&logo=data:image/svg%2bxml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGlkPSJmbGFnLWljb25zLWRlIiB2aWV3Qm94PSIwIDAgNjQwIDQ4MCI+CiAgPHBhdGggZmlsbD0iI2ZmY2UwMCIgZD0iTTAgMzIwaDY0MHYxNjBIMHoiLz4KICA8cGF0aCBkPSJNMCAwaDY0MHYxNjBIMHoiLz4KICA8cGF0aCBmaWxsPSIjZDAwIiBkPSJNMCAxNjBoNjQwdjE2MEgweiIvPgo8L3N2Zz4K&query=%24.progress.3.data.translationProgress&url=https%3A%2F%2Fbadges.awesome-crowdin.com%2Fstats-15707857-578443.json)](https://crowdin.com/project/sdh-gamethememusic/de) Provided by DarkSide1305 [![el translation](https://img.shields.io/badge/dynamic/json?color=blue&label=Greek&style=flat&logo=data:image/svg%2bxml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGlkPSJmbGFnLWljb25zLWdyIiB2aWV3Qm94PSIwIDAgNjQwIDQ4MCI+CiAgPHBhdGggZmlsbD0iIzBkNWVhZiIgZmlsbC1ydWxlPSJldmVub2RkIiBkPSJNMCAwaDY0MHY1My4zSDB6Ii8+CiAgPHBhdGggZmlsbD0iI2ZmZiIgZmlsbC1ydWxlPSJldmVub2RkIiBkPSJNMCA1My4zaDY0MHY1My40SDB6Ii8+CiAgPHBhdGggZmlsbD0iIzBkNWVhZiIgZmlsbC1ydWxlPSJldmVub2RkIiBkPSJNMCAxMDYuN2g2NDBWMTYwSDB6Ii8+CiAgPHBhdGggZmlsbD0iI2ZmZiIgZmlsbC1ydWxlPSJldmVub2RkIiBkPSJNMCAxNjBoNjQwdjUzLjNIMHoiLz4KICA8cGF0aCBmaWxsPSIjMGQ1ZWFmIiBkPSJNMCAwaDI2Ni43djI2Ni43SDB6Ii8+CiAgPHBhdGggZmlsbD0iIzBkNWVhZiIgZmlsbC1ydWxlPSJldmVub2RkIiBkPSJNMCAyMTMuM2g2NDB2NTMuNEgweiIvPgogIDxwYXRoIGZpbGw9IiNmZmYiIGZpbGwtcnVsZT0iZXZlbm9kZCIgZD0iTTAgMjY2LjdoNjQwVjMyMEgweiIvPgogIDxwYXRoIGZpbGw9IiMwZDVlYWYiIGZpbGwtcnVsZT0iZXZlbm9kZCIgZD0iTTAgMzIwaDY0MHY1My4zSDB6Ii8+CiAgPHBhdGggZmlsbD0iI2ZmZiIgZmlsbC1ydWxlPSJldmVub2RkIiBkPSJNMCAzNzMuM2g2NDB2NTMuNEgweiIvPgogIDxnIGZpbGw9IiNmZmYiIGZpbGwtcnVsZT0iZXZlbm9kZCIgc3Ryb2tlLXdpZHRoPSIxLjMiPgogICAgPHBhdGggZD0iTTEwNi43IDBIMTYwdjI2Ni43aC01My4zeiIvPgogICAgPHBhdGggZD0iTTAgMTA2LjdoMjY2LjdWMTYwSDB6Ii8+CiAgPC9nPgogIDxwYXRoIGZpbGw9IiMwZDVlYWYiIGQ9Ik0wIDQyNi43aDY0MFY0ODBIMHoiLz4KPC9zdmc+Cg==&query=%24.progress.4.data.translationProgress&url=https%3A%2F%2Fbadges.awesome-crowdin.com%2Fstats-15707857-578443.json)](https://crowdin.com/project/sdh-gamethememusic/el) @@ -87,7 +87,7 @@ Localisation is now available via [Crowdin](https://crowdin.com/project/sdh-game [![ru translation](https://img.shields.io/badge/dynamic/json?color=blue&label=Russian&style=flat&logo=data:image/svg%2bxml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGlkPSJmbGFnLWljb25zLXJ1IiB2aWV3Qm94PSIwIDAgNjQwIDQ4MCI+CiAgPGcgZmlsbC1ydWxlPSJldmVub2RkIiBzdHJva2Utd2lkdGg9IjFwdCI+CiAgICA8cGF0aCBmaWxsPSIjZmZmIiBkPSJNMCAwaDY0MHY0ODBIMHoiLz4KICAgIDxwYXRoIGZpbGw9IiMwMDM5YTYiIGQ9Ik0wIDE2MGg2NDB2MzIwSDB6Ii8+CiAgICA8cGF0aCBmaWxsPSIjZDUyYjFlIiBkPSJNMCAzMjBoNjQwdjE2MEgweiIvPgogIDwvZz4KPC9zdmc+Cg==&query=%24.progress.19.data.translationProgress&url=https%3A%2F%2Fbadges.awesome-crowdin.com%2Fstats-15707857-578443.json)](https://crowdin.com/project/sdh-gamethememusic/ru) -[![es-ES translation](https://img.shields.io/badge/dynamic/json?color=blue&label=Spanish&style=flat&logo=data:image/svg%2bxml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI5MDAiIGhlaWdodD0iNjAwIj4KPHJlY3Qgd2lkdGg9IjkwMCIgaGVpZ2h0PSI2MDAiIGZpbGw9IiNjNjBiMWUiLz4KPHJlY3Qgd2lkdGg9IjkwMCIgaGVpZ2h0PSIzMDAiIHk9IjE1MCIgZmlsbD0iI2ZmYzQwMCIvPgo8L3N2Zz4=&query=%24.progress.6.data.translationProgress&url=https%3A%2F%2Fbadges.awesome-crowdin.com%2Fstats-15707857-578443.json)](https://crowdin.com/project/sdh-gamethememusic/es-ES) +[![es-ES translation](https://img.shields.io/badge/dynamic/json?color=blue&label=Spanish&style=flat&logo=data:image/svg%2bxml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI5MDAiIGhlaWdodD0iNjAwIj4KPHJlY3Qgd2lkdGg9IjkwMCIgaGVpZ2h0PSI2MDAiIGZpbGw9IiNjNjBiMWUiLz4KPHJlY3Qgd2lkdGg9IjkwMCIgaGVpZ2h0PSIzMDAiIHk9IjE1MCIgZmlsbD0iI2ZmYzQwMCIvPgo8L3N2Zz4=&query=%24.progress.6.data.translationProgress&url=https%3A%2F%2Fbadges.awesome-crowdin.com%2Fstats-15707857-578443.json)](https://crowdin.com/project/sdh-gamethememusic/es-ES) Provided by OuterWinnie [![es-419 translation](https://img.shields.io/badge/dynamic/json?color=blue&label=Spanish,%20Latin%20America&style=flat&logo=data:image/svg%2bxml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI5MDAiIGhlaWdodD0iNjAwIj4KPHJlY3Qgd2lkdGg9IjkwMCIgaGVpZ2h0PSI2MDAiIGZpbGw9IiNjNjBiMWUiLz4KPHJlY3Qgd2lkdGg9IjkwMCIgaGVpZ2h0PSIzMDAiIHk9IjE1MCIgZmlsbD0iI2ZmYzQwMCIvPgo8L3N2Zz4=&query=%24.progress.5.data.translationProgress&url=https%3A%2F%2Fbadges.awesome-crowdin.com%2Fstats-15707857-578443.json)](https://crowdin.com/project/sdh-gamethememusic/es-419) diff --git a/package.json b/package.json index f8379cd..f17abc3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sdh-gamethememusic", - "version": "1.2.2", + "version": "1.3.0", "description": "Play theme songs on your game pages", "scripts": { "build": "shx rm -rf dist && rollup -c", diff --git a/src/cache/musicCache.ts b/src/cache/musicCache.ts index eab0ac2..9875732 100644 --- a/src/cache/musicCache.ts +++ b/src/cache/musicCache.ts @@ -7,11 +7,18 @@ localforage.config({ }) type GameThemeMusicCache = { - videoId: string | undefined + videoId?: string | undefined + volume?: number } export async function updateCache(appId: number, newData: GameThemeMusicCache) { - const newCache = await localforage.setItem(appId.toString(), newData) + const oldCache = (await localforage.getItem( + appId.toString() + )) as GameThemeMusicCache | null + const newCache = await localforage.setItem(appId.toString(), { + ...(oldCache || {}), + ...newData + }) return newCache } diff --git a/src/components/changeTheme/audioPlayer.tsx b/src/components/changeTheme/audioPlayer.tsx index efb1886..7774bc6 100644 --- a/src/components/changeTheme/audioPlayer.tsx +++ b/src/components/changeTheme/audioPlayer.tsx @@ -3,6 +3,8 @@ import React, { useEffect, useRef, useState } from 'react' import useTranslations from '../../hooks/useTranslations' import { getAudioUrlFromVideoId } from '../../actions/audio' import YouTubeVideo from '../../../types/YouTube' +import { FaCheck } from 'react-icons/fa' +import Spinner from '../spinner' export default function AudioPlayer({ handlePlay, @@ -19,7 +21,7 @@ export default function AudioPlayer({ selected: boolean selectNewAudio: (audio: { title: string - videoId: string | undefined + videoId: string audioUrl: string }) => void }) { @@ -72,9 +74,7 @@ export default function AudioPlayer({

-

- - {video.isPlaying ? t('stop') : t('play')} - - +
+ ) : ( +
- {selected ? t('selected') : t('select')} - -
+ + {video.isPlaying ? t('stop') : t('play')} + +
+ + {selected ? t('selected') : t('select')} + + {selected ? ( +
+ +
+ ) : ( + '' + )} +
+
+ )} ) diff --git a/src/components/changeTheme/changePage.tsx b/src/components/changeTheme/changePage.tsx index 7a81dee..f40debd 100644 --- a/src/components/changeTheme/changePage.tsx +++ b/src/components/changeTheme/changePage.tsx @@ -47,7 +47,7 @@ export default function ChangePage({ function selectNewAudio(audio: { title: string - videoId: string | undefined + videoId: string audioUrl: string }) { setSelected(audio.videoId) @@ -56,15 +56,18 @@ export default function ChangePage({ return (
-

{appName}

- +

{appName}

+
(null) + const { state: settingsState } = useSettings() + const { appid } = useParams<{ appid: string }>() + const appDetails = appStore.GetAppOverviewByGameID(parseInt(appid)) + const appName = appDetails?.display_name + + const [currentAudio, setCurrentAudio] = useState() + const [themeVolume, setThemeVolume] = useState(settingsState.volume) + const [isPlaying, setIsPlaying] = useState(false) + const [loading, setLoading] = useState(true) + + useEffect(() => { + async function getData() { + setLoading(true) + const cache = await getCache(parseInt(appid)) + if (cache?.volume) { + setThemeVolume(cache.volume) + } + if (cache?.videoId?.length) { + const newAudio = await getAudioUrlFromVideoId(serverAPI, { + title: '', + id: cache?.videoId + }) + setCurrentAudio(newAudio) + } else { + const newAudio = await getAudio(serverAPI, appName as string) + setCurrentAudio(newAudio?.audioUrl) + } + setLoading(false) + } + + getData() + }, [appid]) + + useEffect(() => { + if (audioRef.current) { + audioRef.current.volume = themeVolume + } + }, [audioRef, themeVolume]) + + function updateThemeVolume(newVol: number) { + setThemeVolume(newVol) + updateCache(parseInt(appid), { volume: newVol }) + } + + useEffect(() => { + if (audioRef.current) { + audioRef.current.currentTime = 0 + isPlaying ? audioRef.current.play() : audioRef.current.pause() + } + }, [audioRef, isPlaying]) + + return ( +
+

{appName}

+ + +
+ + updateThemeVolume(newVal / 100)} + min={0} + max={100} + step={1} + icon={} + editableValue + /> + +
+ setIsPlaying((v) => !v)} + disabled={loading} + focusable={!loading} + style={{ height: 'max-content' }} + > + {loading ? : isPlaying ? t('stop') : t('play')} + + updateThemeVolume(settingsState.volume)} + style={{ height: 'max-content' }} + > + {t('resetVolume')} + +
+ + +
+ ) +} diff --git a/src/components/changeTheme/index.tsx b/src/components/changeTheme/index.tsx index 3b7281c..db458b8 100644 --- a/src/components/changeTheme/index.tsx +++ b/src/components/changeTheme/index.tsx @@ -7,6 +7,7 @@ import AboutPage from './aboutPage' import { SettingsProvider } from '../../context/settingsContext' import { getYouTubeSearchResults } from '../../actions/audio' import YouTubeVideo from '../../../types/YouTube' +import GameSettings from './gameSettings' export default function ChangeTheme({ serverAPI }: { serverAPI: ServerAPI }) { const [currentTab, setCurrentTab] = useState() @@ -82,6 +83,15 @@ export default function ChangeTheme({ serverAPI }: { serverAPI: ServerAPI }) { ), id: 'change-music-tab' }, + { + title: t('gameSettings'), + content: ( + + + + ), + id: 'game-settings-tab' + }, { title: t('about'), content: , id: 'about-tab' } ]} /> diff --git a/src/components/changeTheme/noMusic.tsx b/src/components/changeTheme/noMusic.tsx index 7f04137..d09a797 100644 --- a/src/components/changeTheme/noMusic.tsx +++ b/src/components/changeTheme/noMusic.tsx @@ -2,7 +2,7 @@ import { DialogButton, Focusable } from 'decky-frontend-lib' import React from 'react' import useTranslations from '../../hooks/useTranslations' -import { FaVolumeMute } from 'react-icons/fa' +import { FaCheck, FaVolumeMute } from 'react-icons/fa' export default function NoMusic({ selected, @@ -11,7 +11,7 @@ export default function NoMusic({ selected: boolean selectNewAudio: (audio: { title: string - videoId: string | undefined + videoId: string audioUrl: string }) => void }) { @@ -20,9 +20,7 @@ export default function NoMusic({

{t('play')} - - selectNewAudio({ - title: '', - videoId: '', - audioUrl: '' - }) - } - > - {selected ? t('selected') : t('select')} - +

+ + selectNewAudio({ + title: '', + videoId: '', + audioUrl: '' + }) + } + > + {selected ? t('selected') : t('select')} + + {selected ? ( +
+ +
+ ) : ( + '' + )} +
diff --git a/src/components/settings/index.tsx b/src/components/settings/index.tsx index a899240..ce112e7 100644 --- a/src/components/settings/index.tsx +++ b/src/components/settings/index.tsx @@ -1,55 +1,58 @@ import { ButtonItem, PanelSection, - PanelSectionProps, PanelSectionRow, - SliderField + SliderField, + ToggleField } from 'decky-frontend-lib' -import React, { FC, ReactNode } from 'react' +import React from 'react' import { useSettings } from '../../context/settingsContext' import useTranslations from '../../hooks/useTranslations' -import { FaVolumeUp } from 'react-icons/fa' +import { FaVolumeMute, FaVolumeUp } from 'react-icons/fa' import { clearCache } from '../../cache/musicCache' -type ExtendedPanelSectionProps = PanelSectionProps & { - children: ReactNode -} - -const DeckPanelSection = PanelSection as FC - -type PanelSectionRowProps = { - children: ReactNode -} - -const DeckPanelSectionRow = PanelSectionRow as FC - export default function Index() { const { state: settingsState, dispatch: settingsDispatch } = useSettings() const t = useTranslations() return (
- - + + { settingsDispatch({ type: 'set-volume', - value: newVal + value: newVal / 100 }) }} min={0} - max={1} - step={0.01} + max={100} + step={1} icon={} + editableValue + /> + + + } + checked={settingsState.defaultMuted} + label={t('defaultMuted')} + description={t('defaultMutedDescription')} + onChange={(newVal: boolean) => { + settingsDispatch({ + type: 'set-default-muted', + value: newVal + }) + }} /> - - - - + + + + {t('deleteOverrides')} - - + +
) } diff --git a/src/components/spinner.tsx b/src/components/spinner.tsx new file mode 100644 index 0000000..664cdd4 --- /dev/null +++ b/src/components/spinner.tsx @@ -0,0 +1,41 @@ +import React from 'react' +import { ImSpinner2 } from 'react-icons/im' + +export default function Spinner() { + return ( +
+ + +
+ ) +} diff --git a/src/components/themePlayer/index.tsx b/src/components/themePlayer/index.tsx index d80d985..1ce46b1 100644 --- a/src/components/themePlayer/index.tsx +++ b/src/components/themePlayer/index.tsx @@ -15,7 +15,7 @@ export default function ThemePlayer({ const { audio } = useThemeMusic(serverAPI, parseInt(appid)) useEffect(() => { - if (audio?.audioUrl && audioRef.current) { + if (audio?.audioUrl?.length && audioRef.current) { audioRef.current.src = audio?.audioUrl audioRef.current.volume = settingsState.volume audioRef.current.play() diff --git a/src/context/settingsContext.tsx b/src/context/settingsContext.tsx index df3b4f9..68bce5b 100644 --- a/src/context/settingsContext.tsx +++ b/src/context/settingsContext.tsx @@ -3,10 +3,12 @@ import * as React from 'react' const LOCAL_STORAGE_KEY = 'game-theme-music-settings' type State = { volume: number + defaultMuted: boolean } type Action = | { type: 'set-volume'; value: State['volume'] } + | { type: 'set-default-muted'; value: State['defaultMuted'] } | { type: 'load-settings'; value: State } type Dispatch = (action: Action) => void @@ -17,7 +19,8 @@ const SettingsStateContext = React.createContext< >(undefined) const defaultSettings = { - volume: 1 + volume: 1, + defaultMuted: false } as const function settingsReducer(state: State, action: Action) { @@ -27,34 +30,31 @@ function settingsReducer(state: State, action: Action) { localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(newState)) return newState } + case 'set-default-muted': { + const newState = { ...state, defaultMuted: action.value } + localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(newState)) + return newState + } case 'load-settings': { return action.value } } } +function getInitialState() { + const settingsString = localStorage.getItem(LOCAL_STORAGE_KEY) + if (!settingsString) { + return defaultSettings + } + const storedSettings = JSON.parse(settingsString) + return { + volume: storedSettings?.volume || defaultSettings.volume, + defaultMuted: storedSettings?.defaultMuted || defaultSettings.defaultMuted + } +} + function SettingsProvider({ children }: SettingsProviderProps) { - const [state, dispatch] = React.useReducer(settingsReducer, defaultSettings) - React.useEffect(() => { - async function getSettings() { - const settingsString = localStorage.getItem(LOCAL_STORAGE_KEY) - if (!settingsString) { - dispatch({ - type: 'load-settings', - value: defaultSettings - }) - return - } - const storedSettings = JSON.parse(settingsString) - dispatch({ - type: 'load-settings', - value: { - volume: storedSettings?.volume || defaultSettings.volume - } - }) - } - getSettings() - }, []) + const [state, dispatch] = React.useReducer(settingsReducer, getInitialState()) const value = { state, dispatch } return ( diff --git a/src/hooks/useThemeMusic.ts b/src/hooks/useThemeMusic.ts index 9f1f096..5a74d52 100644 --- a/src/hooks/useThemeMusic.ts +++ b/src/hooks/useThemeMusic.ts @@ -3,12 +3,16 @@ import { useEffect, useState } from 'react' import { getAudio, getAudioUrlFromVideoId } from '../actions/audio' -import { getCache, updateCache } from '../cache/musicCache' +import { getCache } from '../cache/musicCache' +import { useSettings } from '../context/settingsContext' const useThemeMusic = (serverAPI: ServerAPI, appId: number) => { - const [audio, setAudio] = useState< - { videoId: string; audioUrl: string } | undefined - >() + const { state: settingsState } = useSettings() + console.log('defaultMuted', settingsState.defaultMuted) + const [audio, setAudio] = useState<{ videoId: string; audioUrl: string }>({ + videoId: '', + audioUrl: '' + }) const appDetails = appStore.GetAppOverviewByGameID(appId) const appName = appDetails?.display_name @@ -16,10 +20,9 @@ const useThemeMusic = (serverAPI: ServerAPI, appId: number) => { let ignore = false async function getData() { const cache = await getCache(appId) - if (cache?.videoId && !cache?.videoId?.length) { + if (cache?.videoId?.length == 0) { return setAudio({ videoId: '', audioUrl: '' }) - } - if (cache?.videoId?.length) { + } else if (cache?.videoId?.length) { const newAudio = await getAudioUrlFromVideoId(serverAPI, { title: '', id: cache.videoId @@ -27,15 +30,18 @@ const useThemeMusic = (serverAPI: ServerAPI, appId: number) => { if (newAudio?.length) { return setAudio({ videoId: cache.videoId, audioUrl: newAudio }) } - } - const newAudio = await getAudio(serverAPI, appName as string) - if (ignore) { - return - } - if (!newAudio?.audioUrl?.length) { + } else if (settingsState.defaultMuted) { return setAudio({ videoId: '', audioUrl: '' }) + } else { + const newAudio = await getAudio(serverAPI, appName as string) + if (ignore) { + return + } + if (!newAudio?.audioUrl?.length) { + return setAudio({ videoId: '', audioUrl: '' }) + } + return setAudio(newAudio) } - setAudio(newAudio) } if (appName?.length) { getData() @@ -45,14 +51,6 @@ const useThemeMusic = (serverAPI: ServerAPI, appId: number) => { } }, [appName]) - useEffect(() => { - if (audio?.videoId) { - updateCache(appId, { - videoId: audio.videoId - }) - } - }, [audio, appName]) - return { audio } diff --git a/src/localisation/en.json b/src/localisation/en.json index 76e185c..24c1007 100644 --- a/src/localisation/en.json +++ b/src/localisation/en.json @@ -4,11 +4,16 @@ "aboutLabel": "About Game Theme Music", "changeThemeMusic": "Change Theme Music", "clear": "Clear", + "defaultMuted": "Default Muted", + "defaultMutedDescription": "Don't play music unless manually set", "deleteOverrides": "Delete Overrides", "deleteOverridesLabel": "Delete all overrides", + "gameSettings": "Game Settings", + "gameVolumeDescription": "Adjust music volume for this game", "noMusicLabel": "No Music", "overrides": "Overrides", "play": "Play", + "resetVolume": "Reset", "search": "Search", "select": "Select", "selected": "Selected",