diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index 223433ae2c05..7cbbe6b7e215 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -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, diff --git a/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc b/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc index e4da38787c7d..eb7d17c945a7 100644 --- a/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc +++ b/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc @@ -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", @@ -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; diff --git a/common/pref_names.cc b/common/pref_names.cc index afee071de500..6bbc55f938f4 100644 --- a/common/pref_names.cc +++ b/common/pref_names.cc @@ -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"; diff --git a/common/pref_names.h b/common/pref_names.h index 4667b3d8bd87..6904492427a7 100644 --- a/common/pref_names.h +++ b/common/pref_names.h @@ -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[]; diff --git a/components/brave_new_tab_ui/api/initialData.ts b/components/brave_new_tab_ui/api/initialData.ts index cad4525a8633..a55c20f85258 100644 --- a/components/brave_new_tab_ui/api/initialData.ts +++ b/components/brave_new_tab_ui/api/initialData.ts @@ -8,6 +8,8 @@ import * as statsAPI from './stats' import * as privateTabDataAPI from './privateTabData' import * as torTabDataAPI from './torTabData' import * as wallpaper from './wallpaper' +import * as storage from '../storage/new_tab_storage' +import { saveShowBinance, saveShowCryptoDotCom, saveShowFTX, saveShowGemini, saveWidgetVisibilityMigrated } from '../api/preferences' export type InitialData = { preferences: NewTab.Preferences @@ -37,6 +39,99 @@ export type InitialRewardsData = { const isIncognito: boolean = chrome.extension.inIncognitoContext +async function migrateInitialDataForWidgetVisibility (initialData: InitialData) { + // 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. + const ftxUserAuthed = await new Promise(resolve => chrome.ftx.getAccountBalances((balances, authInvalid) => { + resolve(!authInvalid) + })) + const peristentState: NewTab.PersistentState = storage.load() + const widgetLookupTable = { + 'braveTalk': { + display: initialData.braveTalkSupported && initialData.preferences.showBraveTalk, + isCrypto: false + }, + 'rewards': { + display: initialData.preferences.showRewards, + isCrypto: false + }, + 'binance': { + display: initialData.binanceSupported && initialData.preferences.showBinance, + isCrypto: true, + userAuthed: peristentState.binanceState.userAuthed + }, + 'cryptoDotCom': { + display: initialData.cryptoDotComSupported && initialData.preferences.showCryptoDotCom, + isCrypto: true, + userAuthed: false + }, + 'ftx': { + display: initialData.ftxSupported && initialData.preferences.showFTX, + isCrypto: true, + userAuthed: ftxUserAuthed + }, + 'gemini': { + display: initialData.geminiSupported && initialData.preferences.showGemini, + isCrypto: true, + userAuthed: peristentState.geminiState.userAuthed + } + } + + // Find crypto widget that is foremost and visible. + let foremostVisibleCryptoWidget = '' + const lastIndex = peristentState.widgetStackOrder.length - 1 + for (let i = lastIndex; i >= 0; --i) { + const widget = widgetLookupTable[peristentState.widgetStackOrder[i]] + if (!widget) { + console.error('Update above lookup table') + continue + } + + if (!widget.display) { + continue + } + + if (widget.isCrypto) { + foremostVisibleCryptoWidget = peristentState.widgetStackOrder[i] + } + // Found visible foremost widget in the widget stack. Go out. + break + } + + const widgetsShowState = { + 'binance': false, + 'cryptoDotCom': false, + 'ftx': false, + 'gemini': false + } + + for (const key in widgetsShowState) { + // Show foremost visible crypto widget regardless of auth state + // and show user authed crypto widget. + if (key === foremostVisibleCryptoWidget || + widgetLookupTable[key].userAuthed) { + widgetsShowState[key] = true + } + } + + // These don't return promise so we can't await and then fetch new preferences, + // instead make manual changes to initialData.preferences after. + saveShowBinance(widgetsShowState.binance) + saveShowCryptoDotCom(widgetsShowState.cryptoDotCom) + saveShowFTX(widgetsShowState.ftx) + saveShowGemini(widgetsShowState.gemini) + saveWidgetVisibilityMigrated() + + initialData.preferences = { + ...initialData.preferences, + showBinance: widgetsShowState.binance, + showCryptoDotCom: widgetsShowState.cryptoDotCom, + showFTX: widgetsShowState.ftx, + showGemini: widgetsShowState.gemini + } +} + // Gets all data required for the first render of the page export async function getInitialData (): Promise { try { @@ -90,7 +185,7 @@ export async function getInitialData (): Promise { }) ]) console.timeStamp('Got all initial data.') - return { + const initialData = { preferences, stats, privateTabData, @@ -102,6 +197,12 @@ export async function getInitialData (): Promise { ftxSupported, binanceSupported } as InitialData + + if (!initialData.preferences.widgetVisibilityMigrated) { + await migrateInitialDataForWidgetVisibility(initialData) + } + + return initialData } catch (e) { console.error(e) throw Error('Error getting initial data') diff --git a/components/brave_new_tab_ui/api/preferences.ts b/components/brave_new_tab_ui/api/preferences.ts index 94183ffad6bf..2fdadec29d06 100644 --- a/components/brave_new_tab_ui/api/preferences.ts +++ b/components/brave_new_tab_ui/api/preferences.ts @@ -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) } diff --git a/components/brave_new_tab_ui/storage/new_tab_storage.ts b/components/brave_new_tab_ui/storage/new_tab_storage.ts index cef58bdd9623..ad121c9e9907 100644 --- a/components/brave_new_tab_ui/storage/new_tab_storage.ts +++ b/components/brave_new_tab_ui/storage/new_tab_storage.ts @@ -28,6 +28,7 @@ export const defaultState: NewTab.State = { showBitcoinDotCom: false, showCryptoDotCom: false, showFTX: false, + widgetVisibilityMigrated: false, hideAllWidgets: false, brandedWallpaperOptIn: false, isBrandedWallpaperNotificationDismissed: true, diff --git a/components/definitions/newTab.d.ts b/components/definitions/newTab.d.ts index 10abbeb5c341..0bffd66bfeb7 100644 --- a/components/definitions/newTab.d.ts +++ b/components/definitions/newTab.d.ts @@ -113,6 +113,8 @@ declare namespace NewTab { showBinance: boolean showGemini: boolean showCryptoDotCom: boolean + showFTX: boolean + widgetVisibilityMigrated: boolean hideAllWidgets: boolean isBraveTodayOptedIn: boolean isBrandedWallpaperNotificationDismissed: boolean @@ -136,7 +138,6 @@ declare namespace NewTab { customLinksEnabled: boolean customLinksNum: number showBitcoinDotCom: boolean - showFTX: boolean, stats: Stats, braveTalkPromptAllowed: boolean brandedWallpaper?: BrandedWallpaper