Skip to content

Commit

Permalink
refactor: remove snap state and controls from our services
Browse files Browse the repository at this point in the history
this includes removing the toggle and removing from our controller. If we need fine grain control, this will happen in a future PR
  • Loading branch information
Prithpal-Sooriya committed May 23, 2024
1 parent 09d19d0 commit a678bfa
Show file tree
Hide file tree
Showing 22 changed files with 9 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,51 +198,6 @@ describe('metamask-notifications - setFeatureAnnouncementsEnabled()', () => {

expect(controller.state.isFeatureAnnouncementsEnabled).toBe(true);
});

test('does not update if auth is not enabled', async () => {
const { messenger, mockIsSignedIn } = mockNotificationMessenger();
mockIsSignedIn.mockReturnValue(false); // state is off.

const controller = new MetamaskNotificationsController({
messenger,
state: { ...defaultState, isFeatureAnnouncementsEnabled: false },
});

await expect(() =>
controller.setFeatureAnnouncementsEnabled(false),
).rejects.toThrow();
expect(controller.state.isFeatureAnnouncementsEnabled).toBe(false); // this flag was never flipped
});
});

describe('metamask-notifications - setSnapNotificationsEnabled()', () => {
test('flips state when the method is called', async () => {
const { messenger, mockIsSignedIn } = mockNotificationMessenger();
mockIsSignedIn.mockReturnValue(true);

const controller = new MetamaskNotificationsController({
messenger,
state: { ...defaultState, isSnapNotificationsEnabled: false },
});

await controller.setSnapNotificationsEnabled(true);

expect(controller.state.isSnapNotificationsEnabled).toBe(true);
});

test('does not update if auth is not enabled', async () => {
const { messenger, mockIsSignedIn } = mockNotificationMessenger();
mockIsSignedIn.mockReturnValue(false); // state is off.

const controller = new MetamaskNotificationsController({
messenger,
state: { ...defaultState, isSnapNotificationsEnabled: false },
});

await controller.setSnapNotificationsEnabled(false);

expect(controller.state.isSnapNotificationsEnabled).toBe(false); // this flag was never flipped
});
});

describe('metamask-notifications - createOnChainTriggers()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ export type MetamaskNotificationsControllerState = {
*/
isFeatureAnnouncementsEnabled: boolean;

/**
* Flag that indicates if the Snap notifications are enabled
*/
isSnapNotificationsEnabled: boolean;

/**
* List of metamask notifications
*/
Expand Down Expand Up @@ -113,10 +108,6 @@ const metadata: StateMetadata<MetamaskNotificationsControllerState> = {
persist: true,
anonymous: false,
},
isSnapNotificationsEnabled: {
persist: true,
anonymous: false,
},
metamaskNotificationsList: {
persist: true,
anonymous: true,
Expand Down Expand Up @@ -146,7 +137,6 @@ export const defaultState: MetamaskNotificationsControllerState = {
isMetamaskNotificationsFeatureSeen: false,
isMetamaskNotificationsEnabled: false,
isFeatureAnnouncementsEnabled: false,
isSnapNotificationsEnabled: false,
metamaskNotificationsList: [],
metamaskNotificationsReadList: [],
isUpdatingMetamaskNotifications: false,
Expand Down Expand Up @@ -619,44 +609,21 @@ export class MetamaskNotificationsController extends BaseController<
*
* @param featureAnnouncementsEnabled - A boolean value indicating the desired enabled state of the feature announcements.
* @async
* @throws {Error} If the BearerToken token or storage key is missing.
* @throws {Error} If fails to update
*/
public async setFeatureAnnouncementsEnabled(
featureAnnouncementsEnabled: boolean,
) {
try {
this.#assertAuthEnabled();

this.update((state) => {
state.isFeatureAnnouncementsEnabled = featureAnnouncementsEnabled;
this.update((s) => {
s.isFeatureAnnouncementsEnabled = featureAnnouncementsEnabled;
});
} catch (e) {
log.error('Unable to toggle feature announcements', e);
throw new Error('Unable to toggle feature announcements');
}
}

/**
* Sets the enabled state of Snap notifications.
*
* **Action** - used in the notifications settings page to enable/disable snap notifications.
*
* @param snapNotificationsEnabled - A boolean value indicating the desired enabled state of the snap notifications.
* @async
* @throws {Error} If the BearerToken token or storage key is missing.
*/
public async setSnapNotificationsEnabled(snapNotificationsEnabled: boolean) {
try {
this.#assertAuthEnabled();

this.update((state) => {
state.isSnapNotificationsEnabled = snapNotificationsEnabled;
});
} catch (e) {
log.error('Unable to toggle snap notifications', e);
}
}

/**
* This creates/re-creates on-chain triggers defined in User Storage.
*
Expand Down Expand Up @@ -711,7 +678,6 @@ export class MetamaskNotificationsController extends BaseController<
this.update((state) => {
state.isMetamaskNotificationsEnabled = true;
state.isFeatureAnnouncementsEnabled = true;
state.isSnapNotificationsEnabled = true;
state.isMetamaskNotificationsFeatureSeen = true;
});

Expand Down Expand Up @@ -750,7 +716,7 @@ export class MetamaskNotificationsController extends BaseController<
* Disables all MetaMask notifications for the user.
* This method ensures that the user is authenticated, retrieves all linked accounts,
* and disables on-chain triggers for each account. It also sets the global notification
* settings for MetaMask, feature announcements, and Snap notifications to false.
* settings for MetaMask, feature announcements to false.
*
* @throws {Error} If the user is not authenticated or if there is an error during the process.
*/
Expand All @@ -768,7 +734,6 @@ export class MetamaskNotificationsController extends BaseController<
this.update((state) => {
state.isMetamaskNotificationsEnabled = false;
state.isFeatureAnnouncementsEnabled = false;
state.isSnapNotificationsEnabled = false;
state.metamaskNotificationsList = [];
});
} catch (e) {
Expand Down
1 change: 0 additions & 1 deletion app/scripts/lib/setupSentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ export const SENTRY_BACKGROUND_STATE = {
isMetamaskNotificationsFeatureSeen: false,
isMetamaskNotificationsEnabled: false,
isFeatureAnnouncementsEnabled: false,
isSnapNotificationsEnabled: false,
metamaskNotificationsList: false,
metamaskNotificationsReadList: false,
isCheckingAccountsPresence: false,
Expand Down
4 changes: 0 additions & 4 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3678,10 +3678,6 @@ export default class MetamaskController extends EventEmitter {
metamaskNotificationsController.markMetamaskNotificationsAsRead.bind(
metamaskNotificationsController,
),
setSnapNotificationsEnabled:
metamaskNotificationsController.setSnapNotificationsEnabled.bind(
metamaskNotificationsController,
),
setFeatureAnnouncementsEnabled:
metamaskNotificationsController.setFeatureAnnouncementsEnabled.bind(
metamaskNotificationsController,
Expand Down
1 change: 0 additions & 1 deletion test/e2e/fixture-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ function defaultFixture(inputChainId = CHAIN_IDS.LOCALHOST) {
isFeatureAnnouncementsEnabled: false,
isMetamaskNotificationsEnabled: false,
isMetamaskNotificationsFeatureSeen: false,
isSnapNotificationsEnabled: false,
metamaskNotificationsList: [],
metamaskNotificationsReadList: [],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
"isMetamaskNotificationsFeatureSeen": "boolean",
"isMetamaskNotificationsEnabled": "boolean",
"isFeatureAnnouncementsEnabled": "boolean",
"isSnapNotificationsEnabled": "boolean",
"metamaskNotificationsList": "object",
"metamaskNotificationsReadList": "object",
"isUpdatingMetamaskNotifications": "boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@
"isMetamaskNotificationsFeatureSeen": "boolean",
"isMetamaskNotificationsEnabled": "boolean",
"isFeatureAnnouncementsEnabled": "boolean",
"isSnapNotificationsEnabled": "boolean",
"metamaskNotificationsList": "object",
"metamaskNotificationsReadList": "object",
"isUpdatingMetamaskNotifications": "boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"isFeatureAnnouncementsEnabled": "boolean",
"isMetamaskNotificationsEnabled": "boolean",
"isMetamaskNotificationsFeatureSeen": "boolean",
"isSnapNotificationsEnabled": "boolean",
"metamaskNotificationsList": "object",
"metamaskNotificationsReadList": "object"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"isFeatureAnnouncementsEnabled": "boolean",
"isMetamaskNotificationsEnabled": "boolean",
"isMetamaskNotificationsFeatureSeen": "boolean",
"isSnapNotificationsEnabled": "boolean",
"metamaskNotificationsList": "object",
"metamaskNotificationsReadList": "object"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import { useSelector } from 'react-redux';
import {
selectIsMetamaskNotificationsEnabled,
selectIsSnapNotificationsEnabled,
selectIsFeatureAnnouncementsEnabled,
getFeatureAnnouncementsUnreadCount,
getOnChainMetamaskNotificationsUnreadCount,
Expand All @@ -27,12 +26,8 @@ type NotificationsTagCounterProps = {
};

const useSnapNotificationCount = () => {
const isSnapNotificationsEnabled = useSelector(
selectIsSnapNotificationsEnabled,
);
const unreadNotificationsCount = useSelector(getUnreadNotificationsCount);

return isSnapNotificationsEnabled ? unreadNotificationsCount : 0;
return unreadNotificationsCount;
};

const useFeatureAnnouncementCount = () => {
Expand Down
1 change: 0 additions & 1 deletion ui/hooks/metamask-notifications/useNotifications.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jest.mock('../../store/actions', () => ({
createOnChainTriggers: jest.fn(),
deleteOnChainTriggersByAccount: jest.fn(),
fetchAndUpdateMetamaskNotifications: jest.fn(),
setSnapNotificationsEnabled: jest.fn(),
setFeatureAnnouncementsEnabled: jest.fn(),
markMetamaskNotificationsAsRead: jest.fn(),
showLoadingIndication: jest.fn(),
Expand Down
21 changes: 0 additions & 21 deletions ui/hooks/metamask-notifications/useSwitchNotifications.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { Store } from 'redux';
import * as actions from '../../store/actions';
import { MetamaskNotificationsProvider } from '../../contexts/metamask-notifications/metamask-notifications';
import {
useSwitchSnapNotificationsChange,
useSwitchFeatureAnnouncementsChange,
useSwitchAccountNotifications,
useSwitchAccountNotificationsChange,
Expand All @@ -17,7 +16,6 @@ const middlewares = [thunk];
const mockStore = configureStore(middlewares);

jest.mock('../../store/actions', () => ({
setSnapNotificationsEnabled: jest.fn(),
setFeatureAnnouncementsEnabled: jest.fn(),
checkAccountsPresence: jest.fn(),
updateOnChainTriggersByAccount: jest.fn(),
Expand All @@ -33,7 +31,6 @@ describe('useSwitchNotifications', () => {
beforeEach(() => {
store = mockStore({
metamask: {
isSnapNotificationsEnabled: false,
isFeatureAnnouncementsEnabled: false,
internalAccounts: {
accounts: {
Expand All @@ -60,24 +57,6 @@ describe('useSwitchNotifications', () => {
jest.clearAllMocks();
});

it('should toggle snap notifications', async () => {
const { result } = renderHook(() => useSwitchSnapNotificationsChange(), {
wrapper: ({ children }) => (
<Provider store={store}>
<MetamaskNotificationsProvider>
{children}
</MetamaskNotificationsProvider>
</Provider>
),
});

act(() => {
result.current.onChange(true);
});

expect(actions.setSnapNotificationsEnabled).toHaveBeenCalledWith(true);
});

it('should toggle feature announcements', async () => {
const { result } = renderHook(() => useSwitchFeatureAnnouncementsChange(), {
wrapper: ({ children }) => (
Expand Down
32 changes: 0 additions & 32 deletions ui/hooks/metamask-notifications/useSwitchNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,13 @@ import { useState, useCallback } from 'react';
import { useDispatch } from 'react-redux';
import log from 'loglevel';
import {
setSnapNotificationsEnabled,
setFeatureAnnouncementsEnabled,
checkAccountsPresence,
deleteOnChainTriggersByAccount,
updateOnChainTriggersByAccount,
hideLoadingIndication,
} from '../../store/actions';

export function useSwitchSnapNotificationsChange(): {
onChange: (state: boolean) => Promise<void>;
error: null | string;
} {
const dispatch = useDispatch();

const [error, setError] = useState<null | string>(null);

const onChange = useCallback(
async (state: boolean) => {
setError(null);

try {
await dispatch(setSnapNotificationsEnabled(state));
} catch (e) {
const errorMessage =
e instanceof Error ? e.message : JSON.stringify(e ?? '');
setError(errorMessage);
log.error(errorMessage);
throw e;
}
},
[dispatch],
);

return {
onChange,
error,
};
}

export function useSwitchFeatureAnnouncementsChange(): {
onChange: (state: boolean) => Promise<void>;
error: null | string;
Expand Down

0 comments on commit a678bfa

Please sign in to comment.