Settings: Link notification sounds to playSound.

Fix: Restore hooks.ts patch, additionally use numbers.
This commit is contained in:
Paul
2021-06-24 14:26:18 +01:00
parent 352c0e880c
commit 8f62625506
10 changed files with 104 additions and 62 deletions

View File

@@ -4,28 +4,47 @@
//
// Replace references to SettingsContext with connectState in the future
// if it does cause problems though.
//
// This now also supports Audio stuff.
import { Settings } from "../redux/reducers/settings";
import { DEFAULT_SOUNDS, Settings, SoundOptions } from "../redux/reducers/settings";
import { playSound, Sounds } from "../assets/sounds/Audio";
import { connectState } from "../redux/connector";
import defaultsDeep from "lodash.defaultsdeep";
import { Children } from "../types/Preact";
import { createContext } from "preact";
import { useMemo } from "preact/hooks";
export const SettingsContext = createContext<Settings>({} as any);
export const SoundContext = createContext<(sound: Sounds) => void>({} as any);
interface Props {
children?: Children,
settings: Settings
}
function Settings(props: Props) {
function Settings({ settings, children }: Props) {
console.info(settings.notification);
const play = useMemo(() => {
const enabled: SoundOptions = defaultsDeep(settings.notification ?? {}, DEFAULT_SOUNDS);
return (sound: Sounds) => {
console.info('check if we can play sound', enabled[sound]);
if (enabled[sound]) {
playSound(sound);
}
};
}, [ settings.notification ]);
return (
<SettingsContext.Provider value={props.settings}>
{ props.children }
<SettingsContext.Provider value={settings}>
<SoundContext.Provider value={play}>
{ children }
</SoundContext.Provider>
</SettingsContext.Provider>
)
}
export default connectState(Settings, state => {
export default connectState<Omit<Props, 'settings'>>(Settings, state => {
return {
settings: state.settings
}