-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathAnalyticsToast.tsx
121 lines (108 loc) · 4.2 KB
/
AnalyticsToast.tsx
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
/*
Copyright 2024 New Vector Ltd.
Copyright 2020 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import React, { type JSX } from "react";
import { _t } from "../languageHandler";
import SdkConfig from "../SdkConfig";
import dis from "../dispatcher/dispatcher";
import AccessibleButton from "../components/views/elements/AccessibleButton";
import GenericToast from "../components/views/toasts/GenericToast";
import ToastStore from "../stores/ToastStore";
import {
ButtonClicked,
showDialog as showAnalyticsLearnMoreDialog,
} from "../components/views/dialogs/AnalyticsLearnMoreDialog";
import { Action } from "../dispatcher/actions";
import SettingsStore from "../settings/SettingsStore";
const onAccept = (): void => {
dis.dispatch({
action: Action.PseudonymousAnalyticsAccept,
});
};
const onReject = (): void => {
dis.dispatch({
action: Action.PseudonymousAnalyticsReject,
});
};
const onLearnMoreNoOptIn = (): void => {
showAnalyticsLearnMoreDialog({
onFinished: (buttonClicked?: ButtonClicked) => {
if (buttonClicked === ButtonClicked.Primary) {
// user clicked "Enable"
onAccept();
}
// otherwise, the user either clicked "Cancel", or closed the dialog without making a choice,
// leave the toast open
},
primaryButton: _t("action|enable"),
});
};
const onLearnMorePreviouslyOptedIn = (): void => {
showAnalyticsLearnMoreDialog({
onFinished: (buttonClicked?: ButtonClicked) => {
if (buttonClicked === ButtonClicked.Primary) {
// user clicked "That's fine"
onAccept();
} else if (buttonClicked === ButtonClicked.Cancel) {
// user clicked "Stop"
onReject();
}
// otherwise, the user closed the dialog without making a choice, leave the toast open
},
primaryButton: _t("analytics|accept_button"),
cancelButton: _t("action|stop"),
});
};
const TOAST_KEY = "analytics";
export function getPolicyUrl(): string | undefined {
return SdkConfig.get("privacy_policy_url");
}
export const showToast = (): void => {
const legacyAnalyticsOptIn = SettingsStore.getValue("analyticsOptIn", null, true);
let props: Omit<React.ComponentProps<typeof GenericToast>, "toastKey">;
if (legacyAnalyticsOptIn) {
// The user previously opted into our old analytics system - let them know things have changed and ask
// them to opt in again.
props = {
description: _t("analytics|consent_migration"),
primaryLabel: _t("analytics|accept_button"),
onPrimaryClick: onAccept,
secondaryLabel: _t("action|learn_more"),
onSecondaryClick: onLearnMorePreviouslyOptedIn,
};
} else if (legacyAnalyticsOptIn === null || legacyAnalyticsOptIn === undefined) {
// The user had no analytics setting previously set, so we just need to prompt to opt-in, rather than
// explaining any change.
const learnMoreLink = (sub: string): JSX.Element => (
<AccessibleButton kind="link_inline" onClick={onLearnMoreNoOptIn}>
{sub}
</AccessibleButton>
);
props = {
description: _t("analytics|learn_more", {}, { LearnMoreLink: learnMoreLink }),
primaryLabel: _t("action|yes"),
onPrimaryClick: onAccept,
secondaryLabel: _t("action|no"),
onSecondaryClick: onReject,
};
} else {
// false
// The user previously opted out of analytics, don't ask again
return;
}
const analyticsOwner = SdkConfig.get("analytics_owner") ?? SdkConfig.get().brand;
ToastStore.sharedInstance().addOrReplaceToast({
key: TOAST_KEY,
title: _t("analytics|enable_prompt", { analyticsOwner }),
props,
component: GenericToast,
className: "mx_AnalyticsToast",
priority: 10,
});
};
export const hideToast = (): void => {
ToastStore.sharedInstance().dismissToast(TOAST_KEY);
};