2022-05-31 10:43:05 -04:00
|
|
|
/*
|
|
|
|
Copyright 2022 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { EventEmitter } from "events";
|
|
|
|
import { useMemo, useState, useEffect, useCallback } from "react";
|
|
|
|
|
|
|
|
// Bus to notify other useSetting consumers when a setting is changed
|
2022-11-04 13:07:14 +01:00
|
|
|
export const settingsBus = new EventEmitter();
|
2022-05-31 10:43:05 -04:00
|
|
|
|
2022-12-19 12:16:59 +01:00
|
|
|
const getSettingKey = (name: string): string => {
|
|
|
|
return `matrix-setting-${name}`;
|
|
|
|
};
|
2022-05-31 10:43:05 -04:00
|
|
|
// Like useState, but reads from and persists the value to localStorage
|
|
|
|
const useSetting = <T>(
|
|
|
|
name: string,
|
|
|
|
defaultValue: T
|
|
|
|
): [T, (value: T) => void] => {
|
2022-12-19 12:16:59 +01:00
|
|
|
const key = useMemo(() => getSettingKey(name), [name]);
|
2022-05-31 10:43:05 -04:00
|
|
|
|
|
|
|
const [value, setValue] = useState<T>(() => {
|
|
|
|
const item = localStorage.getItem(key);
|
|
|
|
return item == null ? defaultValue : JSON.parse(item);
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
settingsBus.on(name, setValue);
|
|
|
|
return () => {
|
|
|
|
settingsBus.off(name, setValue);
|
|
|
|
};
|
|
|
|
}, [name, setValue]);
|
|
|
|
|
|
|
|
return [
|
|
|
|
value,
|
|
|
|
useCallback(
|
|
|
|
(newValue: T) => {
|
|
|
|
setValue(newValue);
|
|
|
|
localStorage.setItem(key, JSON.stringify(newValue));
|
|
|
|
settingsBus.emit(name, newValue);
|
|
|
|
},
|
|
|
|
[name, key, setValue]
|
|
|
|
),
|
|
|
|
];
|
|
|
|
};
|
2022-11-07 18:00:35 +01:00
|
|
|
|
2022-12-19 12:16:59 +01:00
|
|
|
export const getSetting = <T>(name: string, defaultValue: T): T => {
|
|
|
|
const item = localStorage.getItem(getSettingKey(name));
|
2022-11-07 18:00:35 +01:00
|
|
|
return item === null ? defaultValue : JSON.parse(item);
|
|
|
|
};
|
2022-05-31 10:43:05 -04:00
|
|
|
|
2022-12-19 12:16:59 +01:00
|
|
|
export const setSetting = <T>(name: string, newValue: T) => {
|
|
|
|
localStorage.setItem(getSettingKey(name), JSON.stringify(newValue));
|
|
|
|
settingsBus.emit(name, newValue);
|
|
|
|
};
|
|
|
|
|
2022-12-16 17:12:17 +00:00
|
|
|
export const canEnableSpatialAudio = () => {
|
|
|
|
const { userAgent } = navigator;
|
|
|
|
// Spatial audio means routing audio through audio contexts. On Chrome,
|
|
|
|
// this bypasses the AEC processor and so breaks echo cancellation.
|
|
|
|
// We only allow spatial audio to be enabled on Firefox which we know
|
|
|
|
// passes audio context audio through the AEC algorithm.
|
|
|
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=687574 is the
|
|
|
|
// chrome bug for this: once this is fixed and the updated version is deployed
|
|
|
|
// widely enough, we can allow spatial audio everywhere. It's currently in a
|
|
|
|
// chrome flag, so we could enable this in Electron if we enabled the chrome flag
|
|
|
|
// in the Electron wrapper.
|
|
|
|
return userAgent.includes("Firefox");
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useSpatialAudio = (): [boolean, (val: boolean) => void] => {
|
|
|
|
const settingVal = useSetting("spatial-audio", false);
|
|
|
|
if (canEnableSpatialAudio()) return settingVal;
|
|
|
|
|
|
|
|
return [false, (_: boolean) => {}];
|
|
|
|
};
|
2022-12-19 13:18:12 +00:00
|
|
|
|
2022-05-31 10:43:05 -04:00
|
|
|
export const useShowInspector = () => useSetting("show-inspector", false);
|
2023-03-13 18:40:16 -04:00
|
|
|
|
|
|
|
// null = undecided
|
|
|
|
export const useOptInAnalytics = () =>
|
|
|
|
useSetting<boolean | null>("opt-in-analytics", null);
|
|
|
|
|
2022-11-14 10:21:24 +00:00
|
|
|
export const useKeyboardShortcuts = () =>
|
|
|
|
useSetting("keyboard-shortcuts", true);
|
2023-03-13 18:40:16 -04:00
|
|
|
|
2023-02-13 20:36:42 -05:00
|
|
|
export const useNewGrid = () => useSetting("new-grid", false);
|
2023-03-13 18:40:16 -04:00
|
|
|
|
2023-03-01 13:47:36 +01:00
|
|
|
export const useDeveloperSettingsTab = () =>
|
|
|
|
useSetting("developer-settings-tab", false);
|