|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import classNames from "classnames"; |
| 18 | +import React, { useCallback } from "react"; |
| 19 | + |
| 20 | +import { Action } from "../../../dispatcher/actions"; |
| 21 | +import defaultDispatcher from "../../../dispatcher/dispatcher"; |
| 22 | +import { useSettingValue } from "../../../hooks/useSettings"; |
| 23 | +import { useUserOnboardingTasks } from "../../../hooks/useUserOnboardingTasks"; |
| 24 | +import { _t } from "../../../languageHandler"; |
| 25 | +import { UseCase } from "../../../settings/enums/UseCase"; |
| 26 | +import { SettingLevel } from "../../../settings/SettingLevel"; |
| 27 | +import SettingsStore from "../../../settings/SettingsStore"; |
| 28 | +import AccessibleButton from "../../views/elements/AccessibleButton"; |
| 29 | +import ProgressBar from "../../views/elements/ProgressBar"; |
| 30 | +import Heading from "../../views/typography/Heading"; |
| 31 | +import { showUserOnboardingPage } from "./UserOnboardingPage"; |
| 32 | + |
| 33 | +function toPercentage(progress: number): string { |
| 34 | + return (progress * 100).toFixed(0); |
| 35 | +} |
| 36 | + |
| 37 | +interface Props { |
| 38 | + selected: boolean; |
| 39 | + minimized: boolean; |
| 40 | +} |
| 41 | + |
| 42 | +export function UserOnboardingButton({ selected, minimized }: Props) { |
| 43 | + const [completedTasks, waitingTasks] = useUserOnboardingTasks(); |
| 44 | + |
| 45 | + const completed = completedTasks.length; |
| 46 | + const waiting = waitingTasks.length; |
| 47 | + const total = completed + waiting; |
| 48 | + |
| 49 | + const progress = waiting ? completed / total : 1; |
| 50 | + |
| 51 | + const onDismiss = useCallback(() => { |
| 52 | + SettingsStore.setValue("FTUE.userOnboardingButton", null, SettingLevel.ACCOUNT, false); |
| 53 | + }, []); |
| 54 | + |
| 55 | + const useCase = useSettingValue<UseCase | null>("FTUE.useCaseSelection"); |
| 56 | + const visible = useSettingValue<boolean>("FTUE.userOnboardingButton"); |
| 57 | + if (!visible || minimized || !showUserOnboardingPage(useCase)) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + return ( |
| 62 | + <AccessibleButton |
| 63 | + className={classNames("mx_UserOnboardingButton", { |
| 64 | + "mx_UserOnboardingButton_selected": selected, |
| 65 | + "mx_UserOnboardingButton_minimized": minimized, |
| 66 | + "mx_UserOnboardingButton_completed": !waiting, |
| 67 | + })} |
| 68 | + onClick={() => defaultDispatcher.fire(Action.ViewHomePage)}> |
| 69 | + { !minimized && ( |
| 70 | + <> |
| 71 | + <div className="mx_UserOnboardingButton_content"> |
| 72 | + <Heading size="h4" className="mx_Heading_h4"> |
| 73 | + { _t("Welcome") } |
| 74 | + </Heading> |
| 75 | + { !completed && ( |
| 76 | + <div className="mx_UserOnboardingButton_percentage"> |
| 77 | + { toPercentage(progress) }% |
| 78 | + </div> |
| 79 | + ) } |
| 80 | + <AccessibleButton |
| 81 | + className="mx_UserOnboardingButton_close" |
| 82 | + onClick={onDismiss} |
| 83 | + /> |
| 84 | + </div> |
| 85 | + <ProgressBar value={completed} max={total} animated /> |
| 86 | + </> |
| 87 | + ) } |
| 88 | + </AccessibleButton> |
| 89 | + ); |
| 90 | +} |
0 commit comments