Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Rework how the onboarding notifications task works #12839

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/hooks/useUserOnboardingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Notifier } from "../Notifier";
import DMRoomMap from "../utils/DMRoomMap";
import { useMatrixClientContext } from "../contexts/MatrixClientContext";
import { useSettingValue } from "./useSettings";

export interface UserOnboardingContext {
hasAvatar: boolean;
hasDevices: boolean;
hasDmRooms: boolean;
hasNotificationsEnabled: boolean;
showNotificationsPrompt: boolean;
}

const USER_ONBOARDING_CONTEXT_INTERVAL = 5000;
Expand Down Expand Up @@ -82,6 +83,22 @@ function useUserOnboardingContextValue<T>(defaultValue: T, callback: (cli: Matri
return value;
}

function useShowNotificationsPrompt(): boolean {
const [value, setValue] = useState<boolean>(Notifier.shouldShowPrompt());
useEffect(() => {
window.addEventListener("storage", (event) => {
if (event.storageArea === window.localStorage && event.key === "notifications_hidden") {
setValue(Notifier.shouldShowPrompt());
}
});
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
}, []);
const setting = useSettingValue("notificationsEnabled");
useEffect(() => {
setValue(Notifier.shouldShowPrompt());
}, [setting]);
return value;
}

export function useUserOnboardingContext(): UserOnboardingContext {
const hasAvatar = useUserOnboardingContextValue(false, async (cli) => {
const profile = await cli.getProfileInfo(cli.getUserId()!);
Expand All @@ -96,12 +113,10 @@ export function useUserOnboardingContext(): UserOnboardingContext {
const dmRooms = DMRoomMap.shared().getUniqueRoomsWithIndividuals() ?? {};
return Boolean(Object.keys(dmRooms).length);
});
const hasNotificationsEnabled = useUserOnboardingContextValue(false, async () => {
return Notifier.isPossible();
});
const showNotificationsPrompt = useShowNotificationsPrompt();

return useMemo(
() => ({ hasAvatar, hasDevices, hasDmRooms, hasNotificationsEnabled }),
[hasAvatar, hasDevices, hasDmRooms, hasNotificationsEnabled],
() => ({ hasAvatar, hasDevices, hasDmRooms, showNotificationsPrompt }),
[hasAvatar, hasDevices, hasDmRooms, showNotificationsPrompt],
);
}
10 changes: 7 additions & 3 deletions src/hooks/useUserOnboardingTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,18 @@ const tasks: UserOnboardingTask[] = [
id: "permission-notifications",
title: _t("onboarding|enable_notifications"),
description: _t("onboarding|enable_notifications_description"),
completed: (ctx: UserOnboardingContext) => ctx.hasNotificationsEnabled,
completed: (ctx: UserOnboardingContext) => !ctx.showNotificationsPrompt,
action: {
label: _t("onboarding|enable_notifications_action"),
onClick: (ev: ButtonEvent) => {
PosthogTrackers.trackInteraction("WebUserOnboardingTaskEnableNotifications", ev);
Notifier.setEnabled(true);
defaultDispatcher.dispatch({
action: Action.ViewUserSettings,
initialTabId: UserTab.Notifications,
});
Notifier.setPromptHidden(true);
},
hideOnComplete: true,
hideOnComplete: !Notifier.isPossible(),
},
},
];
Expand Down
4 changes: 2 additions & 2 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1652,8 +1652,8 @@
"download_brand_desktop": "Download %(brand)s Desktop",
"download_f_droid": "Get it on F-Droid",
"download_google_play": "Get it on Google Play",
"enable_notifications": "Turn on notifications",
"enable_notifications_action": "Enable notifications",
"enable_notifications": "Turn on desktop notifications",
"enable_notifications_action": "Open settings",
"enable_notifications_description": "Don’t miss a reply or important message",
"explore_rooms": "Explore Public Rooms",
"find_community_members": "Find and invite your community members",
Expand Down
6 changes: 4 additions & 2 deletions src/toasts/DesktopNotificationsToast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import GenericToast from "../components/views/toasts/GenericToast";
import ToastStore from "../stores/ToastStore";
import { MatrixClientPeg } from "../MatrixClientPeg";
import { getLocalNotificationAccountDataEventType } from "../utils/notifications";
import SettingsStore from "../settings/SettingsStore";
import { SettingLevel } from "../settings/SettingLevel";

const onAccept = (): void => {
Notifier.setEnabled(true);
const onAccept = async (): Promise<void> => {
await SettingsStore.setValue("notificationsEnabled", null, SettingLevel.DEVICE, true);
const cli = MatrixClientPeg.safeGet();
const eventType = getLocalNotificationAccountDataEventType(cli.deviceId!);
cli.setAccountData(eventType, {
Expand Down
42 changes: 40 additions & 2 deletions test/hooks/useUserOnboardingTasks-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React from "react";
import { renderHook } from "@testing-library/react-hooks";
import { waitFor } from "@testing-library/react";

import { useUserOnboardingTasks } from "../../src/hooks/useUserOnboardingTasks";
import { useUserOnboardingContext } from "../../src/hooks/useUserOnboardingContext";
import { stubClient } from "../test-utils";
import MatrixClientContext from "../../src/contexts/MatrixClientContext";
import DMRoomMap from "../../src/utils/DMRoomMap";
import PlatformPeg from "../../src/PlatformPeg";

describe("useUserOnboardingTasks", () => {
it.each([
Expand All @@ -25,15 +32,15 @@ describe("useUserOnboardingTasks", () => {
hasAvatar: false,
hasDevices: false,
hasDmRooms: false,
hasNotificationsEnabled: false,
showNotificationsPrompt: false,
},
},
{
context: {
hasAvatar: true,
hasDevices: false,
hasDmRooms: false,
hasNotificationsEnabled: true,
showNotificationsPrompt: true,
},
},
])("sequence should stay static", async ({ context }) => {
Expand All @@ -46,4 +53,35 @@ describe("useUserOnboardingTasks", () => {
expect(result.current[3].id).toBe("setup-profile");
expect(result.current[4].id).toBe("permission-notifications");
});

it("should mark desktop notifications task completed on click", async () => {
jest.spyOn(PlatformPeg, "get").mockReturnValue({
supportsNotifications: jest.fn().mockReturnValue(true),
maySendNotifications: jest.fn().mockReturnValue(true),
} as any);

const cli = stubClient();
cli.pushRules = {
global: {
override: [
{
rule_id: ".m.rule.master",
enabled: false,
actions: [],
default: true,
},
],
},
};
DMRoomMap.makeShared(cli);
const context = renderHook(() => useUserOnboardingContext(), {
wrapper: (props) => {
return <MatrixClientContext.Provider value={cli}>{props.children}</MatrixClientContext.Provider>;
},
});
const { result } = renderHook(() => useUserOnboardingTasks(context.result.current));
expect(result.current[4].id).toBe("permission-notifications");
result.current[4].action!.onClick!({ type: "click" } as any);
await waitFor(() => expect(result.current[4].completed).toBe(true));
});
});
Loading