Merge remote-tracking branch 'origin/main' into dbkr/spatial_audio_ff_only
This commit is contained in:
commit
4ad5ea49c2
7 changed files with 144 additions and 51 deletions
|
|
@ -20,12 +20,15 @@ import { useMemo, useState, useEffect, useCallback } from "react";
|
|||
// Bus to notify other useSetting consumers when a setting is changed
|
||||
export const settingsBus = new EventEmitter();
|
||||
|
||||
const getSettingKey = (name: string): string => {
|
||||
return `matrix-setting-${name}`;
|
||||
};
|
||||
// Like useState, but reads from and persists the value to localStorage
|
||||
const useSetting = <T>(
|
||||
name: string,
|
||||
defaultValue: T
|
||||
): [T, (value: T) => void] => {
|
||||
const key = useMemo(() => `matrix-setting-${name}`, [name]);
|
||||
const key = useMemo(() => getSettingKey(name), [name]);
|
||||
|
||||
const [value, setValue] = useState<T>(() => {
|
||||
const item = localStorage.getItem(key);
|
||||
|
|
@ -51,13 +54,17 @@ const useSetting = <T>(
|
|||
),
|
||||
];
|
||||
};
|
||||
export const getSetting = <T>(name: string, defaultValue: T): T => {
|
||||
const key = `matrix-setting-${name}`;
|
||||
|
||||
const item = localStorage.getItem(key);
|
||||
export const getSetting = <T>(name: string, defaultValue: T): T => {
|
||||
const item = localStorage.getItem(getSettingKey(name));
|
||||
return item === null ? defaultValue : JSON.parse(item);
|
||||
};
|
||||
|
||||
export const setSetting = <T>(name: string, newValue: T) => {
|
||||
localStorage.setItem(getSettingKey(name), JSON.stringify(newValue));
|
||||
settingsBus.emit(name, newValue);
|
||||
};
|
||||
|
||||
export const canEnableSpatialAudio = () => {
|
||||
const { userAgent } = navigator;
|
||||
// Spatial audio means routing audio through audio contexts. On Chrome,
|
||||
|
|
@ -78,6 +85,7 @@ export const useSpatialAudio = (): [boolean, (val: boolean) => void] => {
|
|||
|
||||
return [false, (_: boolean) => {}];
|
||||
};
|
||||
|
||||
export const useShowInspector = () => useSetting("show-inspector", false);
|
||||
export const useOptInAnalytics = () => useSetting("opt-in-analytics", false);
|
||||
export const useKeyboardShortcuts = () =>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue