2021-06-19 12:34:53 +01:00
|
|
|
// This code is more or less redundant, but settings has so little state
|
|
|
|
// updates that I can't be asked to pass everything through props each
|
|
|
|
// time when I can just use the Context API.
|
|
|
|
//
|
|
|
|
// Replace references to SettingsContext with connectState in the future
|
|
|
|
// if it does cause problems though.
|
2021-06-24 14:26:18 +01:00
|
|
|
//
|
|
|
|
// This now also supports Audio stuff.
|
2021-06-19 12:34:53 +01:00
|
|
|
|
2021-06-24 14:26:18 +01:00
|
|
|
import { DEFAULT_SOUNDS, Settings, SoundOptions } from "../redux/reducers/settings";
|
|
|
|
import { playSound, Sounds } from "../assets/sounds/Audio";
|
2021-06-19 12:34:53 +01:00
|
|
|
import { connectState } from "../redux/connector";
|
2021-06-24 14:26:18 +01:00
|
|
|
import defaultsDeep from "lodash.defaultsdeep";
|
2021-06-19 12:34:53 +01:00
|
|
|
import { Children } from "../types/Preact";
|
|
|
|
import { createContext } from "preact";
|
2021-06-24 14:26:18 +01:00
|
|
|
import { useMemo } from "preact/hooks";
|
2021-06-19 12:34:53 +01:00
|
|
|
|
2021-07-04 07:09:39 -04:00
|
|
|
export const SettingsContext = createContext<Settings>({});
|
|
|
|
export const SoundContext = createContext<((sound: Sounds) => void)>(null!);
|
2021-06-19 12:34:53 +01:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
children?: Children,
|
|
|
|
settings: Settings
|
|
|
|
}
|
|
|
|
|
2021-06-24 14:26:18 +01:00
|
|
|
function Settings({ settings, children }: Props) {
|
|
|
|
const play = useMemo(() => {
|
2021-06-24 22:06:30 +01:00
|
|
|
const enabled: SoundOptions = defaultsDeep(settings.notification?.sounds ?? {}, DEFAULT_SOUNDS);
|
2021-06-24 14:26:18 +01:00
|
|
|
return (sound: Sounds) => {
|
|
|
|
if (enabled[sound]) {
|
|
|
|
playSound(sound);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [ settings.notification ]);
|
|
|
|
|
2021-06-19 12:34:53 +01:00
|
|
|
return (
|
2021-06-24 14:26:18 +01:00
|
|
|
<SettingsContext.Provider value={settings}>
|
|
|
|
<SoundContext.Provider value={play}>
|
|
|
|
{ children }
|
|
|
|
</SoundContext.Provider>
|
2021-06-19 12:34:53 +01:00
|
|
|
</SettingsContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-24 14:26:18 +01:00
|
|
|
export default connectState<Omit<Props, 'settings'>>(Settings, state => {
|
2021-06-19 12:34:53 +01:00
|
|
|
return {
|
|
|
|
settings: state.settings
|
|
|
|
}
|
|
|
|
});
|