Files
ThreadNet-Web/src/components/views/dialogs/UserSettingsDialog.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

258 lines
11 KiB
TypeScript
Raw Normal View History

2019-01-18 13:09:17 -07:00
/*
Copyright 2019 New Vector Ltd
Copyright 2019, 2024 The Matrix.org Foundation C.I.C.
2019-01-18 13:09:17 -07:00
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 { Toast } from "@vector-im/compound-web";
2024-06-06 09:57:28 +01:00
import React, { useState } from "react";
2024-08-06 17:08:45 +01:00
import UserProfileIcon from "@vector-im/compound-design-tokens/assets/web/icons/user-profile";
import DevicesIcon from "@vector-im/compound-design-tokens/assets/web/icons/devices";
import VisibilityOnIcon from "@vector-im/compound-design-tokens/assets/web/icons/visibility-on";
import NotificationsIcon from "@vector-im/compound-design-tokens/assets/web/icons/notifications";
import PreferencesIcon from "@vector-im/compound-design-tokens/assets/web/icons/preferences";
import KeyboardIcon from "@vector-im/compound-design-tokens/assets/web/icons/keyboard";
import SidebarIcon from "@vector-im/compound-design-tokens/assets/web/icons/sidebar";
import MicOnIcon from "@vector-im/compound-design-tokens/assets/web/icons/mic-on";
import LockIcon from "@vector-im/compound-design-tokens/assets/web/icons/lock";
import LabsIcon from "@vector-im/compound-design-tokens/assets/web/icons/labs";
import BlockIcon from "@vector-im/compound-design-tokens/assets/web/icons/block";
import HelpIcon from "@vector-im/compound-design-tokens/assets/web/icons/help";
2021-10-22 17:23:32 -05:00
import TabbedView, { Tab, useActiveTabWithDefault } from "../../structures/TabbedView";
2021-06-29 13:11:58 +01:00
import { _t, _td } from "../../../languageHandler";
import AccountUserSettingsTab from "../settings/tabs/user/AccountUserSettingsTab";
import SettingsStore from "../../../settings/SettingsStore";
import LabsUserSettingsTab, { showLabsFlags } from "../settings/tabs/user/LabsUserSettingsTab";
2020-04-16 20:26:04 +01:00
import AppearanceUserSettingsTab from "../settings/tabs/user/AppearanceUserSettingsTab";
2019-02-22 10:47:18 -07:00
import SecurityUserSettingsTab from "../settings/tabs/user/SecurityUserSettingsTab";
import NotificationUserSettingsTab from "../settings/tabs/user/NotificationUserSettingsTab";
import PreferencesUserSettingsTab from "../settings/tabs/user/PreferencesUserSettingsTab";
import VoiceUserSettingsTab from "../settings/tabs/user/VoiceUserSettingsTab";
import HelpUserSettingsTab from "../settings/tabs/user/HelpUserSettingsTab";
2019-10-31 13:19:54 -06:00
import MjolnirUserSettingsTab from "../settings/tabs/user/MjolnirUserSettingsTab";
2021-06-29 13:11:58 +01:00
import { UIFeature } from "../../../settings/UIFeature";
2021-07-03 10:44:03 +02:00
import BaseDialog from "./BaseDialog";
2021-11-11 13:07:41 +00:00
import SidebarUserSettingsTab from "../settings/tabs/user/SidebarUserSettingsTab";
import KeyboardUserSettingsTab from "../settings/tabs/user/KeyboardUserSettingsTab";
import SessionManagerTab from "../settings/tabs/user/SessionManagerTab";
2022-03-24 16:42:53 -06:00
import { UserTab } from "./UserTab";
import { NonEmptyArray } from "../../../@types/common";
import { SDKContext, SdkContextClass } from "../../../contexts/SDKContext";
import { useSettingValue } from "../../../hooks/useSettings";
import { ToastContext, useActiveToast } from "../../../contexts/ToastContext";
2021-06-14 23:42:36 +01:00
interface IProps {
initialTabId?: UserTab;
2024-06-06 09:57:28 +01:00
showMsc4108QrCode?: boolean;
sdkContext: SdkContextClass;
onFinished(): void;
2021-06-14 23:42:36 +01:00
}
function titleForTabID(tabId: UserTab): React.ReactNode {
const subs = {
strong: (sub: string) => <span className="mx_UserSettingsDialog_title_strong">{sub}</span>,
};
switch (tabId) {
case UserTab.Account:
return _t("settings|account|dialog_title", undefined, subs);
case UserTab.SessionManager:
return _t("settings|sessions|dialog_title", undefined, subs);
case UserTab.Appearance:
return _t("settings|appearance|dialog_title", undefined, subs);
case UserTab.Notifications:
return _t("settings|notifications|dialog_title", undefined, subs);
case UserTab.Preferences:
return _t("settings|preferences|dialog_title", undefined, subs);
case UserTab.Keyboard:
return _t("settings|keyboard|dialog_title", undefined, subs);
case UserTab.Sidebar:
return _t("settings|sidebar|dialog_title", undefined, subs);
case UserTab.Voice:
return _t("settings|voip|dialog_title", undefined, subs);
case UserTab.Security:
return _t("settings|security|dialog_title", undefined, subs);
case UserTab.Labs:
return _t("settings|labs|dialog_title", undefined, subs);
case UserTab.Mjolnir:
return _t("settings|labs_mjolnir|dialog_title", undefined, subs);
case UserTab.Help:
return _t("setting|help_about|dialog_title", undefined, subs);
}
}
export default function UserSettingsDialog(props: IProps): JSX.Element {
const voipEnabled = useSettingValue<boolean>(UIFeature.Voip);
const mjolnirEnabled = useSettingValue<boolean>("feature_mjolnir");
2024-06-06 09:57:28 +01:00
// store this prop in state as changing tabs back and forth should clear it
const [showMsc4108QrCode, setShowMsc4108QrCode] = useState(props.showMsc4108QrCode);
const getTabs = (): NonEmptyArray<Tab<UserTab>> => {
const tabs: Tab<UserTab>[] = [];
2019-01-23 17:32:27 -07:00
tabs.push(
new Tab(
UserTab.Account,
_td("settings|account|title"),
2024-08-06 17:08:45 +01:00
<UserProfileIcon />,
<AccountUserSettingsTab closeSettingsFn={props.onFinished} />,
"UserSettingsGeneral",
2022-12-12 12:24:14 +01:00
),
2019-01-23 17:32:27 -07:00
);
tabs.push(
new Tab(
UserTab.SessionManager,
_td("settings|sessions|title"),
2024-08-06 17:08:45 +01:00
<DevicesIcon />,
2024-06-06 09:57:28 +01:00
<SessionManagerTab showMsc4108QrCode={showMsc4108QrCode} />,
undefined,
),
);
2020-04-14 16:20:41 +01:00
tabs.push(
new Tab(
UserTab.Appearance,
_td("common|appearance"),
2024-08-06 17:08:45 +01:00
<VisibilityOnIcon />,
2020-04-16 20:26:04 +01:00
<AppearanceUserSettingsTab />,
"UserSettingsAppearance",
2022-12-12 12:24:14 +01:00
),
2020-04-14 16:20:41 +01:00
);
2019-01-23 17:32:27 -07:00
tabs.push(
new Tab(
UserTab.Notifications,
_td("notifications|enable_prompt_toast_title"),
2024-08-06 17:08:45 +01:00
<NotificationsIcon />,
2019-02-22 10:47:18 -07:00
<NotificationUserSettingsTab />,
"UserSettingsNotifications",
2022-12-12 12:24:14 +01:00
),
2019-01-23 17:32:27 -07:00
);
tabs.push(
new Tab(
UserTab.Preferences,
_td("common|preferences"),
2024-08-06 17:08:45 +01:00
<PreferencesIcon />,
<PreferencesUserSettingsTab closeSettingsFn={props.onFinished} />,
"UserSettingsPreferences",
2022-12-12 12:24:14 +01:00
),
2019-01-23 17:32:27 -07:00
);
tabs.push(
new Tab(
UserTab.Keyboard,
_td("settings|keyboard|title"),
2024-08-06 17:08:45 +01:00
<KeyboardIcon />,
<KeyboardUserSettingsTab />,
"UserSettingsKeyboard",
2022-12-12 12:24:14 +01:00
),
);
2022-01-25 12:33:17 +00:00
tabs.push(
new Tab(
UserTab.Sidebar,
_td("settings|sidebar|title"),
2024-08-06 17:08:45 +01:00
<SidebarIcon />,
2022-01-25 12:33:17 +00:00
<SidebarUserSettingsTab />,
"UserSettingsSidebar",
2022-12-12 12:24:14 +01:00
),
2022-01-25 12:33:17 +00:00
);
2021-11-11 13:07:41 +00:00
if (voipEnabled) {
2020-09-16 13:40:27 +01:00
tabs.push(
new Tab(
UserTab.Voice,
_td("settings|voip|title"),
2024-08-06 17:08:45 +01:00
<MicOnIcon />,
2020-09-16 13:40:27 +01:00
<VoiceUserSettingsTab />,
"UserSettingsVoiceVideo",
2022-12-12 12:24:14 +01:00
),
2020-09-16 13:40:27 +01:00
);
}
2019-01-23 17:32:27 -07:00
tabs.push(
new Tab(
UserTab.Security,
_td("room_settings|security|title"),
2024-08-06 17:08:45 +01:00
<LockIcon />,
<SecurityUserSettingsTab closeSettingsFn={props.onFinished} />,
"UserSettingsSecurityPrivacy",
2022-12-12 12:24:14 +01:00
),
);
if (showLabsFlags() || SettingsStore.getFeatureSettingNames().some((k) => SettingsStore.getBetaInfo(k))) {
2019-01-23 17:32:27 -07:00
tabs.push(
2024-08-06 17:08:45 +01:00
new Tab(UserTab.Labs, _td("common|labs"), <LabsIcon />, <LabsUserSettingsTab />, "UserSettingsLabs"),
2019-01-23 17:32:27 -07:00
);
}
if (mjolnirEnabled) {
2019-10-31 13:19:54 -06:00
tabs.push(
new Tab(
UserTab.Mjolnir,
_td("labs_mjolnir|title"),
2024-08-06 17:08:45 +01:00
<BlockIcon />,
2019-10-31 13:19:54 -06:00
<MjolnirUserSettingsTab />,
"UserSettingMjolnir",
2022-12-12 12:24:14 +01:00
),
2019-10-31 13:19:54 -06:00
);
}
2019-01-23 17:32:27 -07:00
tabs.push(
new Tab(
UserTab.Help,
_td("setting|help_about|title"),
2024-08-06 17:08:45 +01:00
<HelpIcon />,
<HelpUserSettingsTab />,
"UserSettingsHelpAbout",
2022-12-12 12:24:14 +01:00
),
2019-01-23 17:32:27 -07:00
);
return tabs as NonEmptyArray<Tab<UserTab>>;
};
2019-01-18 13:09:17 -07:00
const [activeTabId, _setActiveTabId] = useActiveTabWithDefault(getTabs(), UserTab.Account, props.initialTabId);
2024-06-06 09:57:28 +01:00
const setActiveTabId = (tabId: UserTab): void => {
_setActiveTabId(tabId);
// Clear this so switching away from the tab and back to it will not show the QR code again
setShowMsc4108QrCode(false);
};
const [activeToast, toastRack] = useActiveToast();
return (
// XXX: SDKContext is provided within the LoggedInView subtree.
// Modals function outside the MatrixChat React tree, so sdkContext is reprovided here to simulate that.
// The longer term solution is to move our ModalManager into the React tree to inherit contexts properly.
<SDKContext.Provider value={props.sdkContext}>
<ToastContext.Provider value={toastRack}>
<BaseDialog
className="mx_UserSettingsDialog"
hasCancel={true}
onFinished={props.onFinished}
title={titleForTabID(activeTabId)}
titleClass="mx_UserSettingsDialog_title"
>
<div className="mx_SettingsDialog_content">
<TabbedView
tabs={getTabs()}
activeTabId={activeTabId}
screenName="UserSettings"
onChange={setActiveTabId}
responsive={true}
/>
</div>
<div className="mx_SettingsDialog_toastContainer">
{activeToast && <Toast>{activeToast}</Toast>}
</div>
</BaseDialog>
</ToastContext.Provider>
</SDKContext.Provider>
);
}