-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: social leaderboard opt out #32982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
19ed4d5
feat: social leaderboard opt out
Bigshmow 31b1161
Merge branch 'main' into feat/social-leaderboard-opt-out
Bigshmow 8841798
feat: opt out in privacy settings
Bigshmow 4022e9c
Merge branch 'main' into feat/social-leaderboard-opt-out
Bigshmow f362f5b
Merge branch 'main' into feat/social-leaderboard-opt-out
Bigshmow 0885941
Merge branch 'main' into feat/social-leaderboard-opt-out
Bigshmow dd20104
fix: delegate SocialService opt-out/in actions to SocialController me…
Bigshmow 1042f4f
chore: feature flag
Bigshmow 5ae3560
Merge branch 'main' into feat/social-leaderboard-opt-out
Bigshmow b68e7e3
chore: register aiSocialLeaderboardOptFlowEnabled feature flag
Bigshmow 992ae54
chore: track leaderboard visbility toggled event
Bigshmow ac2d297
Merge branch 'main' into feat/social-leaderboard-opt-out
Bigshmow 1326704
fix: write opt ins to state only on success
Bigshmow f2929c3
fix: react compiler
Bigshmow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
app/components/Views/Settings/SecuritySettings/Sections/TopTradersSection.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { fireEvent, screen, waitFor } from '@testing-library/react-native'; | ||
| import React from 'react'; | ||
|
|
||
| import { strings } from '../../../../../../locales/i18n'; | ||
| import renderWithProvider from '../../../../../util/test/renderWithProvider'; | ||
| import { SecurityPrivacyViewSelectorsIDs } from '../SecurityPrivacyView.testIds'; | ||
| import TopTradersSection from './TopTradersSection'; | ||
|
|
||
| const mockMessengerCall = jest.fn().mockResolvedValue(undefined); | ||
| const mockLoggerError = jest.fn(); | ||
| let mockLeaderboardEnabled = true; | ||
| let mockOptFlowEnabled = true; | ||
|
|
||
| jest.mock('../../../../../core/Engine', () => ({ | ||
| controllerMessenger: { | ||
| call: (...args: unknown[]) => mockMessengerCall(...args), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('../../../../../util/Logger', () => ({ | ||
| error: (...args: unknown[]) => mockLoggerError(...args), | ||
| })); | ||
|
|
||
| jest.mock( | ||
| '../../../../../selectors/featureFlagController/socialLeaderboard', | ||
| () => ({ | ||
| selectSocialLeaderboardEnabled: () => mockLeaderboardEnabled, | ||
| selectSocialLeaderboardOptFlowEnabled: () => mockOptFlowEnabled, | ||
| }), | ||
| ); | ||
|
|
||
| const renderWith = (showAccountOnLeaderboard = true) => | ||
| renderWithProvider(<TopTradersSection />, { | ||
| state: { settings: { showAccountOnLeaderboard } }, | ||
| }); | ||
|
|
||
| describe('TopTradersSection', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockLeaderboardEnabled = true; | ||
| mockOptFlowEnabled = true; | ||
| mockMessengerCall.mockResolvedValue(undefined); | ||
| }); | ||
|
|
||
| it('renders the section and toggle when the leaderboard is enabled', () => { | ||
| renderWith(true); | ||
|
|
||
| expect( | ||
| screen.getByTestId(SecurityPrivacyViewSelectorsIDs.TOP_TRADERS_SECTION), | ||
| ).toBeOnTheScreen(); | ||
| expect( | ||
| screen.getByText( | ||
| strings('social_leaderboard.settings.show_account_on_leaderboard'), | ||
| ), | ||
| ).toBeOnTheScreen(); | ||
| expect( | ||
| screen.getByTestId( | ||
| SecurityPrivacyViewSelectorsIDs.SHOW_ACCOUNT_ON_LEADERBOARD_TOGGLE, | ||
| ).props.value, | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('renders nothing when the leaderboard feature is disabled', () => { | ||
| mockLeaderboardEnabled = false; | ||
| renderWith(true); | ||
|
|
||
| expect( | ||
| screen.queryByTestId(SecurityPrivacyViewSelectorsIDs.TOP_TRADERS_SECTION), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it('renders nothing when the leaderboard opt-flow flag is disabled', () => { | ||
| mockOptFlowEnabled = false; | ||
| renderWith(true); | ||
|
|
||
| expect( | ||
| screen.queryByTestId(SecurityPrivacyViewSelectorsIDs.TOP_TRADERS_SECTION), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it('opts out (and flips the toggle off) when turned off', async () => { | ||
| renderWith(true); | ||
| const toggle = screen.getByTestId( | ||
| SecurityPrivacyViewSelectorsIDs.SHOW_ACCOUNT_ON_LEADERBOARD_TOGGLE, | ||
| ); | ||
|
|
||
| fireEvent(toggle, 'valueChange', false); | ||
|
|
||
| expect(toggle.props.value).toBe(false); | ||
| await waitFor(() => | ||
| expect(mockMessengerCall).toHaveBeenCalledWith( | ||
| 'SocialController:optOutOfLeaderboard', | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it('opts in when turned on', async () => { | ||
| renderWith(false); | ||
| const toggle = screen.getByTestId( | ||
| SecurityPrivacyViewSelectorsIDs.SHOW_ACCOUNT_ON_LEADERBOARD_TOGGLE, | ||
| ); | ||
|
|
||
| fireEvent(toggle, 'valueChange', true); | ||
|
|
||
| await waitFor(() => | ||
| expect(mockMessengerCall).toHaveBeenCalledWith( | ||
| 'SocialController:optInToLeaderboard', | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it('reverts and logs when the request fails', async () => { | ||
| mockMessengerCall.mockRejectedValueOnce(new Error('network down')); | ||
| renderWith(true); | ||
| const toggle = screen.getByTestId( | ||
| SecurityPrivacyViewSelectorsIDs.SHOW_ACCOUNT_ON_LEADERBOARD_TOGGLE, | ||
| ); | ||
|
|
||
| fireEvent(toggle, 'valueChange', false); | ||
|
|
||
| await waitFor(() => expect(mockLoggerError).toHaveBeenCalled()); | ||
| expect(toggle.props.value).toBe(true); | ||
| }); | ||
| }); |
100 changes: 100 additions & 0 deletions
100
app/components/Views/Settings/SecuritySettings/Sections/TopTradersSection.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { Text, TextVariant } from '@metamask/design-system-react-native'; | ||
| import React, { useCallback, useState } from 'react'; | ||
| import { View } from 'react-native'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
|
|
||
| import { strings } from '../../../../../../locales/i18n'; | ||
| import { setShowAccountOnLeaderboard } from '../../../../../actions/settings'; | ||
| import Engine from '../../../../../core/Engine'; | ||
| import { RootState } from '../../../../../reducers'; | ||
| import { | ||
| selectSocialLeaderboardEnabled, | ||
| selectSocialLeaderboardOptFlowEnabled, | ||
| } from '../../../../../selectors/featureFlagController/socialLeaderboard'; | ||
| import Logger from '../../../../../util/Logger'; | ||
| import { SecurityOptionToggle } from '../../../../UI/SecurityOptionToggle'; | ||
| import { useStyles } from '../../../../hooks/useStyles'; | ||
| import createStyles from '../SecuritySettings.styles'; | ||
| import { SecurityPrivacyViewSelectorsIDs } from '../SecurityPrivacyView.testIds'; | ||
|
|
||
| /** | ||
| * "Top Traders" section for the Security & privacy screen. Lets the user toggle | ||
| * whether their account is shown on the Top Traders leaderboard, calling | ||
| * `SocialController:optInToLeaderboard` / `optOutOfLeaderboard`. The displayed | ||
| * value is a local (non-AUS) mirror kept in the `settings` reducer, defaulting | ||
| * to shown; it is updated optimistically and reverted if the request fails. | ||
| */ | ||
| const TopTradersSection = () => { | ||
| const { styles } = useStyles(createStyles, {}); | ||
| const dispatch = useDispatch(); | ||
| const isSocialLeaderboardEnabled = useSelector( | ||
| selectSocialLeaderboardEnabled, | ||
| ); | ||
| const isLeaderboardOptFlowEnabled = useSelector( | ||
| selectSocialLeaderboardOptFlowEnabled, | ||
| ); | ||
| const showAccountOnLeaderboard = useSelector( | ||
| (state: RootState) => state.settings.showAccountOnLeaderboard ?? true, | ||
| ); | ||
| const [isUpdating, setIsUpdating] = useState(false); | ||
|
|
||
| const handleToggle = useCallback( | ||
| async (enabled: boolean) => { | ||
| if (isUpdating) { | ||
| return; | ||
| } | ||
| setIsUpdating(true); | ||
| // Optimistic: the reducer value is the displayed state. | ||
| dispatch(setShowAccountOnLeaderboard(enabled)); | ||
| try { | ||
| await (Engine.controllerMessenger.call as CallableFunction)( | ||
| enabled | ||
| ? 'SocialController:optInToLeaderboard' | ||
| : 'SocialController:optOutOfLeaderboard', | ||
| ); | ||
| } catch (error) { | ||
| Logger.error( | ||
| error as Error, | ||
| 'TopTradersSection: failed to update leaderboard visibility', | ||
| ); | ||
| // Revert on failure so the toggle reflects the unchanged backend state. | ||
| dispatch(setShowAccountOnLeaderboard(!enabled)); | ||
| } finally { | ||
| setIsUpdating(false); | ||
| } | ||
|
Bigshmow marked this conversation as resolved.
|
||
| }, | ||
| [dispatch, isUpdating], | ||
| ); | ||
|
|
||
| if (!isSocialLeaderboardEnabled || !isLeaderboardOptFlowEnabled) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <View testID={SecurityPrivacyViewSelectorsIDs.TOP_TRADERS_SECTION}> | ||
| <Text variant={TextVariant.HeadingMd} style={styles.subHeading}> | ||
| {strings('social_leaderboard.settings.section_title')} | ||
| </Text> | ||
| {/* Wrap the toggle in `styles.setting` (marginTop) so the gap below the | ||
| section heading matches the other sections (e.g. Analytics). */} | ||
| <View style={styles.setting}> | ||
| <SecurityOptionToggle | ||
| title={strings( | ||
| 'social_leaderboard.settings.show_account_on_leaderboard', | ||
| )} | ||
| description={strings( | ||
| 'social_leaderboard.settings.show_account_on_leaderboard_description', | ||
| )} | ||
| value={showAccountOnLeaderboard} | ||
| onOptionUpdated={handleToggle} | ||
| testId={ | ||
| SecurityPrivacyViewSelectorsIDs.SHOW_ACCOUNT_ON_LEADERBOARD_TOGGLE | ||
| } | ||
| disabled={isUpdating} | ||
| /> | ||
| </View> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| export default TopTradersSection; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.