Skip to content

Commit

Permalink
Merge pull request #11847 from brave/ntp-widget-done-visibility-migra…
Browse files Browse the repository at this point in the history
…tion

New Tab Page WebUI - remove widget visibility migration
  • Loading branch information
petemill committed Jan 15, 2022
1 parent 5533d6b commit 34fbabc
Show file tree
Hide file tree
Showing 10 changed files with 5 additions and 122 deletions.
5 changes: 2 additions & 3 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,10 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterStringPref(kNewTabPageClockFormat, "");
registry->RegisterBooleanPref(kNewTabPageShowStats, true);
registry->RegisterBooleanPref(kNewTabPageShowRewards, true);
registry->RegisterBooleanPref(kNewTabPageShowBinance, true);
registry->RegisterBooleanPref(kNewTabPageShowBinance, false);
registry->RegisterBooleanPref(kNewTabPageShowBraveTalk, false);
registry->RegisterBooleanPref(kNewTabPageShowGemini, true);
registry->RegisterBooleanPref(kNewTabPageShowGemini, false);
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 @@ -108,8 +108,6 @@ 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 @@ -500,10 +498,6 @@ 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: 0 additions & 2 deletions common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ 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: 0 additions & 1 deletion common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ 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
103 changes: 1 addition & 102 deletions components/brave_new_tab_ui/api/initialData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ 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
Expand Down Expand Up @@ -39,99 +37,6 @@ 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<InitialData> {
try {
Expand Down Expand Up @@ -185,7 +90,7 @@ export async function getInitialData (): Promise<InitialData> {
})
])
console.timeStamp('Got all initial data.')
const initialData = {
return {
preferences,
stats,
privateTabData,
Expand All @@ -197,12 +102,6 @@ export async function getInitialData (): Promise<InitialData> {
ftxSupported,
binanceSupported
} as InitialData

if (!initialData.preferences.widgetVisibilityMigrated) {
await migrateInitialDataForWidgetVisibility(initialData)
}

return initialData
} catch (e) {
console.error(e)
throw Error('Error getting initial data')
Expand Down
4 changes: 0 additions & 4 deletions components/brave_new_tab_ui/api/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ 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
1 change: 0 additions & 1 deletion components/brave_new_tab_ui/storage/new_tab_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const defaultState: NewTab.State = {
showBitcoinDotCom: false,
showCryptoDotCom: false,
showFTX: false,
widgetVisibilityMigrated: false,
hideAllWidgets: false,
brandedWallpaperOptIn: false,
isBrandedWallpaperNotificationDismissed: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace crypto_dot_com {

void RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(kCryptoDotComNewTabPageShowCryptoDotCom, true);
registry->RegisterBooleanPref(kCryptoDotComNewTabPageShowCryptoDotCom, false);
registry->RegisterBooleanPref(kCryptoDotComHasBoughtCrypto, false);
registry->RegisterBooleanPref(kCryptoDotComHasInteracted, false);
}
Expand Down
1 change: 0 additions & 1 deletion components/definitions/newTab.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ declare namespace NewTab {
showGemini: boolean
showCryptoDotCom: boolean
showFTX: boolean
widgetVisibilityMigrated: boolean
hideAllWidgets: boolean
isBraveTodayOptedIn: boolean
isBrandedWallpaperNotificationDismissed: boolean
Expand Down
2 changes: 1 addition & 1 deletion components/ftx/browser/ftx_pref_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace ftx {

void RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(kFTXNewTabPageShowFTX, true);
registry->RegisterBooleanPref(kFTXNewTabPageShowFTX, false);
registry->RegisterStringPref(kFTXAccessToken, "");
const std::string country_code = brave_l10n::GetCountryCode(
brave_l10n::LocaleHelper::GetInstance()->GetLocale());
Expand Down

0 comments on commit 34fbabc

Please sign in to comment.