Files
ThreadNet-Web/src/toasts/AnalyticsToast.tsx
T

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

122 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-05-22 22:04:21 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2020-05-22 22:04:21 +01:00
Copyright 2020 The Matrix.org Foundation C.I.C.
2024-09-09 14:57:16 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
2020-05-22 22:04:21 +01:00
*/
2022-06-14 17:51:51 +01:00
import React from "react";
2020-05-22 22:04:21 +01:00
import { _t } from "../languageHandler";
2020-07-10 19:07:11 +01:00
import SdkConfig from "../SdkConfig";
2020-05-22 22:04:21 +01:00
import dis from "../dispatcher/dispatcher";
import AccessibleButton from "../components/views/elements/AccessibleButton";
import GenericToast from "../components/views/toasts/GenericToast";
import ToastStore from "../stores/ToastStore";
2021-12-06 09:39:33 +11:00
import {
ButtonClicked,
showDialog as showAnalyticsLearnMoreDialog,
} from "../components/views/dialogs/AnalyticsLearnMoreDialog";
import { Action } from "../dispatcher/actions";
2022-06-14 17:51:51 +01:00
import SettingsStore from "../settings/SettingsStore";
2020-05-22 22:04:21 +01:00
const onAccept = (): void => {
2020-05-22 22:04:21 +01:00
dis.dispatch({
2021-12-06 09:39:33 +11:00
action: Action.PseudonymousAnalyticsAccept,
2020-05-22 22:04:21 +01:00
});
};
const onReject = (): void => {
2020-05-22 22:04:21 +01:00
dis.dispatch({
2021-12-06 09:39:33 +11:00
action: Action.PseudonymousAnalyticsReject,
});
};
const onLearnMoreNoOptIn = (): void => {
2021-12-06 09:39:33 +11:00
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"),
2021-12-06 09:39:33 +11:00
});
};
const onLearnMorePreviouslyOptedIn = (): void => {
2021-12-06 09:39:33 +11:00
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"),
2020-05-22 22:04:21 +01:00
});
};
const TOAST_KEY = "analytics";
export function getPolicyUrl(): string | undefined {
return SdkConfig.get("privacy_policy_url");
2022-06-14 17:51:51 +01:00
}
2021-12-06 09:39:33 +11:00
2022-06-14 17:51:51 +01:00
export const showToast = (): void => {
const legacyAnalyticsOptIn = SettingsStore.getValue("analyticsOptIn", null, true);
2020-05-22 22:04:21 +01:00
2022-06-14 17:51:51 +01:00
let props: Omit<React.ComponentProps<typeof GenericToast>, "toastKey">;
if (legacyAnalyticsOptIn) {
2021-12-06 09:39:33 +11:00
// 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"),
2024-07-30 13:57:15 +01:00
primaryLabel: _t("analytics|accept_button"),
onPrimaryClick: onAccept,
secondaryLabel: _t("action|learn_more"),
onSecondaryClick: onLearnMorePreviouslyOptedIn,
2021-12-06 09:39:33 +11:00
};
2022-06-14 17:51:51 +01:00
} else if (legacyAnalyticsOptIn === null || legacyAnalyticsOptIn === undefined) {
2021-12-06 09:39:33 +11:00
// 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>
2021-12-06 09:39:33 +11:00
);
props = {
description: _t("analytics|learn_more", {}, { LearnMoreLink: learnMoreLink }),
2024-07-30 13:57:15 +01:00
primaryLabel: _t("action|yes"),
onPrimaryClick: onAccept,
secondaryLabel: _t("action|no"),
onSecondaryClick: onReject,
2021-12-06 09:39:33 +11:00
};
} else {
// false
// The user previously opted out of analytics, don't ask again
return;
}
2022-06-14 17:51:51 +01:00
const analyticsOwner = SdkConfig.get("analytics_owner") ?? SdkConfig.get().brand;
ToastStore.sharedInstance().addOrReplaceToast({
key: TOAST_KEY,
title: _t("analytics|enable_prompt", { analyticsOwner }),
2022-06-14 17:51:51 +01:00
props,
component: GenericToast,
className: "mx_AnalyticsToast",
priority: 10,
});
2021-12-06 09:39:33 +11:00
};
export const hideToast = (): void => {
2020-05-22 22:04:21 +01:00
ToastStore.sharedInstance().dismissToast(TOAST_KEY);
};