Skip to content
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

Migrate crypto widgets visibility in NTP #11266

Merged
merged 5 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(kNewTabPageShowBraveTalk, false);
registry->RegisterBooleanPref(kNewTabPageShowGemini, true);
registry->RegisterBooleanPref(kNewTabPageHideAllWidgets, false);
registry->RegisterBooleanPref(kNewTabPageWidgetVisibilityMigrated, false);

registry->RegisterIntegerPref(
kNewTabPageShowsOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ base::DictionaryValue GetPreferencesDictionary(PrefService* prefs) {
pref_data.SetBoolean("showBraveTalk",
prefs->GetBoolean(kNewTabPageShowBraveTalk));
pref_data.SetBoolean("showGemini", prefs->GetBoolean(kNewTabPageShowGemini));
pref_data.SetBoolean("widgetVisibilityMigrated",
prefs->GetBoolean(kNewTabPageWidgetVisibilityMigrated));
#if BUILDFLAG(CRYPTO_DOT_COM_ENABLED)
pref_data.SetBoolean(
"showCryptoDotCom",
Expand Down Expand Up @@ -501,6 +503,10 @@ void BraveNewTabMessageHandler::HandleSaveNewTabPagePref(
settingsKey = kNewTabPageShowBraveTalk;
} else if (settingsKeyInput == "showGemini") {
settingsKey = kNewTabPageShowGemini;
} else if (settingsKeyInput == "saveWidgetVisibilityMigrated") {
// This prefs is set to true once.
DCHECK(settingsValueBool);
settingsKey = kNewTabPageWidgetVisibilityMigrated;
#if BUILDFLAG(CRYPTO_DOT_COM_ENABLED)
} else if (settingsKeyInput == "showCryptoDotCom") {
settingsKey = kCryptoDotComNewTabPageShowCryptoDotCom;
Expand Down
2 changes: 2 additions & 0 deletions common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const char kNewTabPageShowGemini[] = "brave.new_tab_page.show_gemini";
const char kNewTabPageShowBraveTalk[] = "brave.new_tab_page.show_together";
const char kNewTabPageHideAllWidgets[] = "brave.new_tab_page.hide_all_widgets";
const char kNewTabPageShowsOptions[] = "brave.new_tab_page.shows_options";
const char kNewTabPageWidgetVisibilityMigrated[] =
"brave.new_tab_page.widget_visibility_migrated";
const char kBraveTodayIntroDismissed[] = "brave.today.intro_dismissed";
const char kBinanceAccessToken[] = "brave.binance.access_token";
const char kBinanceRefreshToken[] = "brave.binance.refresh_token";
Expand Down
1 change: 1 addition & 0 deletions common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extern const char kNewTabPageShowGemini[];
extern const char kNewTabPageShowBraveTalk[];
extern const char kNewTabPageHideAllWidgets[];
extern const char kNewTabPageShowsOptions[];
extern const char kNewTabPageWidgetVisibilityMigrated[];
extern const char kBraveTodayIntroDismissed[];
extern const char kAlwaysShowBookmarkBarOnNTP[];
extern const char kAutocompleteEnabled[];
Expand Down
3 changes: 3 additions & 0 deletions components/brave_new_tab_ui/actions/new_tab_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export const clockWidgetUpdated = (showClockWidget: boolean,
export const setInitialData = (initialData: InitialData) =>
action(types.NEW_TAB_SET_INITIAL_DATA, initialData)

export const setWidgetVisibilityMigrationData = (ftxAuthed: boolean) =>
action(types.NEW_TAB_SET_WIDGET_VISIBILITY_MIGRATION_DATA, ftxAuthed)

export const setMostVisitedSettings = (showTopSites: boolean, customLinksEnabled: boolean) =>
action(types.SET_MOST_VISITED_SITES, { showTopSites, customLinksEnabled })

Expand Down
4 changes: 4 additions & 0 deletions components/brave_new_tab_ui/api/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function sendSavePref (key: string, value: any) {
chrome.send('saveNewTabPagePref', [key, value])
}

export function saveWidgetVisibilityMigrated (): void {
sendSavePref('saveWidgetVisibilityMigrated', true)
}

export function saveShowBackgroundImage (value: boolean): void {
sendSavePref('showBackgroundImage', value)
}
Expand Down
8 changes: 8 additions & 0 deletions components/brave_new_tab_ui/apiEventsToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ export function wireApiEventsToStore () {
rewardsInitData()
}
getActions().setInitialData(initialData)
if (!initialData.preferences.widgetVisibilityMigrated) {
// Other widget's auth state can be fetched from local storage.
// But, need to fetch ftx auth state to migrate.
// Start migration when ftx auth state is ready.
chrome.ftx.getAccountBalances((balances, authInvalid) => {
getActions().setWidgetVisibilityMigrationData(!authInvalid)
})
}
// Listen for API changes and dispatch to store
topSitesAPI.addMostVistedInfoChangedListener(onMostVisitedInfoChanged)
topSitesAPI.updateMostVisitedInfo()
Expand Down
1 change: 1 addition & 0 deletions components/brave_new_tab_ui/constants/new_tab_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const enum types {
NEW_TAB_PREFERENCES_UPDATED = '@@newtab/NEW_TAB_PREFERENCES_UPDATED',
NEW_TAB_DISMISS_BRANDED_WALLPAPER_NOTIFICATION = '@@newtab/NEW_TAB_DISMISS_BRANDED_WALLPAPER_NOTIFICATION',
NEW_TAB_SET_INITIAL_DATA = '@@newtab/NEW_TAB_SET_INITIAL_DATA',
NEW_TAB_SET_WIDGET_VISIBILITY_MIGRATION_DATA = '@@newtab/NEW_TAB_SET_WIDGET_VISIBILITY_MIGRATION_DATA',
SET_MOST_VISITED_SITES = '@@newtab/SET_MOST_VISITED_SITES',
TOP_SITES_STATE_UPDATED = '@@newtab/TOP_SITES_STATE_UPDATED',
CUSTOMIZE_CLICKED = '@@newtab/CUSTOMIZE_CLICKED',
Expand Down
3 changes: 3 additions & 0 deletions components/brave_new_tab_ui/reducers/new_tab_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export const newTabReducer: Reducer<NewTab.State | undefined> = (state: NewTab.S
}
state = storage.addNewStackWidget(state)
state = storage.replaceStackWidgets(state)
break

case types.NEW_TAB_SET_WIDGET_VISIBILITY_MIGRATION_DATA:
state = storage.updateWidgetVisibility(state, payload)
break

case types.NEW_TAB_STATS_UPDATED:
Expand Down
104 changes: 104 additions & 0 deletions components/brave_new_tab_ui/storage/new_tab_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// you can obtain one at http://mozilla.org/MPL/2.0/.

// Utils
import { saveShowBinance, saveShowCryptoDotCom, saveShowFTX, saveShowGemini, saveWidgetVisibilityMigrated } from '../api/preferences'
import { debounce } from '../../common/debounce'
import { loadTimeData } from '../../common/loadTimeData'

Expand All @@ -28,6 +29,7 @@ export const defaultState: NewTab.State = {
showBitcoinDotCom: false,
showCryptoDotCom: false,
showFTX: false,
widgetVisibilityMigrated: false,
hideAllWidgets: false,
brandedWallpaperOptIn: false,
isBrandedWallpaperNotificationDismissed: true,
Expand Down Expand Up @@ -240,6 +242,108 @@ export const replaceStackWidgets = (state: NewTab.State) => {
return state
}

export const updateWidgetVisibility = (state: NewTab.State, ftxUserAuthed: boolean) => {
// Do visibility migration only once.
if (state.widgetVisibilityMigrated) {
return state
}

const {
showRewards,
showBraveTalk,
braveTalkSupported,
showBinance,
binanceSupported,
showCryptoDotCom,
cryptoDotComSupported,
showFTX,
ftxSupported,
showGemini,
geminiSupported,
binanceState,
geminiState
} = state

const widgetLookupTable = {
'braveTalk': {
display: braveTalkSupported && showBraveTalk,
isCrypto: false
},
'rewards': {
display: showRewards,
isCrypto: false
},
'binance': {
display: binanceSupported && showBinance,
isCrypto: true,
userAuthed: binanceState.userAuthed
},
'cryptoDotCom': {
display: cryptoDotComSupported && showCryptoDotCom,
isCrypto: true,
userAuthed: false
},
'ftx': {
display: ftxSupported && showFTX,
isCrypto: true,
userAuthed: ftxUserAuthed
},
'gemini': {
display: geminiSupported && showGemini,
isCrypto: true,
userAuthed: geminiState.userAuthed
}
}

// Find crypto widget that is foremost and visible.
let foremostVisibleCryptoWidget = ''
const lastIndex = state.widgetStackOrder.length - 1
for (let i = lastIndex; i >= 0; --i) {
const widget = widgetLookupTable[state.widgetStackOrder[i]]
if (!widget) {
console.error('Update above lookup table')
continue
}

if (!widget.display) {
continue
}

if (widget.isCrypto) {
foremostVisibleCryptoWidget = state.widgetStackOrder[i]
}
// Found visible foremost widget in the widget stack. Go out.
break
}

const widgetsShowKey = {
'binance': 'showBinance',
'cryptoDotCom': 'showCryptoDotCom',
'ftx': 'showFTX',
'gemini': 'showGemini'
}

for (let key in widgetsShowKey) {
// Show foremost visible crypto widget regardless of auth state
// and show user authed crypto widget.
if (key === foremostVisibleCryptoWidget ||
widgetLookupTable[key].userAuthed) {
state[widgetsShowKey[key]] = true
continue
}

state[widgetsShowKey[key]] = false
}

saveShowBinance(state.showBinance)
saveShowCryptoDotCom(state.showCryptoDotCom)
saveShowFTX(state.showFTX)
saveShowGemini(state.showGemini)
saveWidgetVisibilityMigrated()

return state
}

const cleanData = (state: NewTab.State) => {
const { rewardsState } = state

Expand Down
1 change: 1 addition & 0 deletions components/definitions/newTab.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ declare namespace NewTab {
showBinance: boolean
showGemini: boolean
showCryptoDotCom: boolean
widgetVisibilityMigrated: boolean
hideAllWidgets: boolean
isBraveTodayOptedIn: boolean
isBrandedWallpaperNotificationDismissed: boolean
Expand Down