Disable the opt in analytics setting if Posthog isn't configured

This commit is contained in:
Robin Townsend 2023-03-23 13:07:34 -04:00
parent 971eca59ff
commit 5f41f9476b
2 changed files with 19 additions and 15 deletions

View file

@ -32,7 +32,6 @@ import {
useSpatialAudio, useSpatialAudio,
useShowInspector, useShowInspector,
useOptInAnalytics, useOptInAnalytics,
canEnableSpatialAudio,
useNewGrid, useNewGrid,
useDeveloperSettingsTab, useDeveloperSettingsTab,
} from "./useSetting"; } from "./useSetting";
@ -133,16 +132,16 @@ export const SettingsModal = (props: Props) => {
label={t("Spatial audio")} label={t("Spatial audio")}
type="checkbox" type="checkbox"
checked={spatialAudio} checked={spatialAudio}
disabled={!canEnableSpatialAudio()} disabled={setSpatialAudio === null}
description={ description={
canEnableSpatialAudio() setSpatialAudio === null
? t( ? t("This feature is only supported on Firefox.")
: t(
"This will make a speaker's audio seem as if it is coming from where their tile is positioned on screen. (Experimental feature: this may impact the stability of audio.)" "This will make a speaker's audio seem as if it is coming from where their tile is positioned on screen. (Experimental feature: this may impact the stability of audio.)"
) )
: t("This feature is only supported on Firefox.")
} }
onChange={(event: React.ChangeEvent<HTMLInputElement>) => onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
setSpatialAudio(event.target.checked) setSpatialAudio!(event.target.checked)
} }
/> />
</FieldRow> </FieldRow>

View file

@ -16,6 +16,10 @@ limitations under the License.
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import { useMemo, useState, useEffect, useCallback } from "react"; import { useMemo, useState, useEffect, useCallback } from "react";
import { PosthogAnalytics } from "../analytics/PosthogAnalytics";
type Setting<T> = [T, (value: T) => void];
type DisableableSetting<T> = [T, ((value: T) => void) | null];
// Bus to notify other useSetting consumers when a setting is changed // Bus to notify other useSetting consumers when a setting is changed
export const settingsBus = new EventEmitter(); export const settingsBus = new EventEmitter();
@ -24,10 +28,7 @@ const getSettingKey = (name: string): string => {
return `matrix-setting-${name}`; return `matrix-setting-${name}`;
}; };
// Like useState, but reads from and persists the value to localStorage // Like useState, but reads from and persists the value to localStorage
const useSetting = <T>( const useSetting = <T>(name: string, defaultValue: T): Setting<T> => {
name: string,
defaultValue: T
): [T, (value: T) => void] => {
const key = useMemo(() => getSettingKey(name), [name]); const key = useMemo(() => getSettingKey(name), [name]);
const [value, setValue] = useState<T>(() => { const [value, setValue] = useState<T>(() => {
@ -65,7 +66,7 @@ export const setSetting = <T>(name: string, newValue: T) => {
settingsBus.emit(name, newValue); settingsBus.emit(name, newValue);
}; };
export const canEnableSpatialAudio = () => { const canEnableSpatialAudio = () => {
const { userAgent } = navigator; const { userAgent } = navigator;
// Spatial audio means routing audio through audio contexts. On Chrome, // Spatial audio means routing audio through audio contexts. On Chrome,
// this bypasses the AEC processor and so breaks echo cancellation. // this bypasses the AEC processor and so breaks echo cancellation.
@ -79,18 +80,22 @@ export const canEnableSpatialAudio = () => {
return userAgent.includes("Firefox"); return userAgent.includes("Firefox");
}; };
export const useSpatialAudio = (): [boolean, (val: boolean) => void] => { export const useSpatialAudio = (): DisableableSetting<boolean> => {
const settingVal = useSetting("spatial-audio", false); const settingVal = useSetting("spatial-audio", false);
if (canEnableSpatialAudio()) return settingVal; if (canEnableSpatialAudio()) return settingVal;
return [false, (_: boolean) => {}]; return [false, null];
}; };
export const useShowInspector = () => useSetting("show-inspector", false); export const useShowInspector = () => useSetting("show-inspector", false);
// null = undecided // null = undecided
export const useOptInAnalytics = () => export const useOptInAnalytics = (): DisableableSetting<boolean | null> => {
useSetting<boolean | null>("opt-in-analytics", null); const settingVal = useSetting<boolean | null>("opt-in-analytics", null);
if (PosthogAnalytics.instance.isEnabled()) return settingVal;
return [false, null];
};
export const useKeyboardShortcuts = () => export const useKeyboardShortcuts = () =>
useSetting("keyboard-shortcuts", true); useSetting("keyboard-shortcuts", true);