This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 830
/
useUserOnboardingTasks.ts
151 lines (140 loc) · 5.41 KB
/
useUserOnboardingTasks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
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 { useMemo } from "react";
import { AppDownloadDialog } from "../components/views/dialogs/AppDownloadDialog";
import { UserTab } from "../components/views/dialogs/UserTab";
import { ButtonEvent } from "../components/views/elements/AccessibleButton";
import { Action } from "../dispatcher/actions";
import defaultDispatcher from "../dispatcher/dispatcher";
import { _t } from "../languageHandler";
import Modal from "../Modal";
import { Notifier } from "../Notifier";
import PosthogTrackers from "../PosthogTrackers";
import { UseCase } from "../settings/enums/UseCase";
import { useSettingValue } from "./useSettings";
import { UserOnboardingContext } from "./useUserOnboardingContext";
export interface UserOnboardingTask {
id: string;
title: string;
description: string;
relevant?: UseCase[];
action?: {
label: string;
onClick?: (ev?: ButtonEvent) => void;
href?: string;
hideOnComplete?: boolean;
};
}
interface InternalUserOnboardingTask extends UserOnboardingTask {
completed: (ctx: UserOnboardingContext) => boolean;
}
const hasOpenDMs = (ctx: UserOnboardingContext) => Boolean(Object.entries(ctx.dmRooms).length);
const onClickStartDm = (ev: ButtonEvent) => {
PosthogTrackers.trackInteraction("WebUserOnboardingTaskSendDm", ev);
defaultDispatcher.dispatch({ action: 'view_create_chat' });
};
const tasks: InternalUserOnboardingTask[] = [
{
id: "create-account",
title: _t("Create account"),
description: _t("You made it!"),
completed: () => true,
},
{
id: "find-friends",
title: _t("Find and invite your friends"),
description: _t("It’s what you’re here for, so lets get to it"),
completed: hasOpenDMs,
relevant: [UseCase.PersonalMessaging, UseCase.Skip],
action: {
label: _t("Find friends"),
onClick: onClickStartDm,
},
},
{
id: "find-coworkers",
title: _t("Find and invite your co-workers"),
description: _t("Get stuff done by finding your teammates"),
completed: hasOpenDMs,
relevant: [UseCase.WorkMessaging],
action: {
label: _t("Find people"),
onClick: onClickStartDm,
},
},
{
id: "find-community-members",
title: _t("Find and invite your community members"),
description: _t("Get stuff done by finding your teammates"),
completed: hasOpenDMs,
relevant: [UseCase.CommunityMessaging],
action: {
label: _t("Find people"),
onClick: onClickStartDm,
},
},
{
id: "download-apps",
title: _t("Download Element"),
description: _t("Don’t miss a thing by taking Element with you"),
completed: (ctx: UserOnboardingContext) => {
return Boolean(ctx.devices.filter(it => it.device_id !== ctx.myDevice).length);
},
action: {
label: _t("Download apps"),
onClick: (ev: ButtonEvent) => {
PosthogTrackers.trackInteraction("WebUserOnboardingTaskDownloadApps", ev);
Modal.createDialog(AppDownloadDialog, {}, "mx_AppDownloadDialog_wrapper", false, true);
},
},
},
{
id: "setup-profile",
title: _t("Set up your profile"),
description: _t("Make sure people know it’s really you"),
completed: (info: UserOnboardingContext) => Boolean(info.avatar),
action: {
label: _t("Your profile"),
onClick: (ev: ButtonEvent) => {
PosthogTrackers.trackInteraction("WebUserOnboardingTaskSetupProfile", ev);
defaultDispatcher.dispatch({
action: Action.ViewUserSettings,
initialTabId: UserTab.General,
});
},
},
},
{
id: "permission-notifications",
title: _t("Turn on notifications"),
description: _t("Don’t miss a reply or important message"),
completed: () => Notifier.isPossible(),
action: {
label: _t("Enable notifications"),
onClick: (ev: ButtonEvent) => {
PosthogTrackers.trackInteraction("WebUserOnboardingTaskEnableNotifications", ev);
Notifier.setEnabled(true);
},
hideOnComplete: true,
},
},
];
export function useUserOnboardingTasks(context: UserOnboardingContext): [UserOnboardingTask[], UserOnboardingTask[]] {
const useCase = useSettingValue<UseCase | null>("FTUE.useCaseSelection") ?? UseCase.Skip;
const relevantTasks = useMemo(
() => tasks.filter(it => !it.relevant || it.relevant.includes(useCase)),
[useCase],
);
const completedTasks = relevantTasks.filter(it => context && it.completed(context));
return [completedTasks, relevantTasks.filter(it => !completedTasks.includes(it))];
}