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 (uplift to 1.33.x) #11288

Merged
merged 1 commit into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
103 changes: 102 additions & 1 deletion components/brave_new_tab_ui/api/initialData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<InitialData> {
try {
Expand Down Expand Up @@ -90,7 +185,7 @@ export async function getInitialData (): Promise<InitialData> {
})
])
console.timeStamp('Got all initial data.')
return {
const initialData = {
preferences,
stats,
privateTabData,
Expand All @@ -102,6 +197,12 @@ 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: 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
1 change: 1 addition & 0 deletions components/brave_new_tab_ui/storage/new_tab_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const defaultState: NewTab.State = {
showBitcoinDotCom: false,
showCryptoDotCom: false,
showFTX: false,
widgetVisibilityMigrated: false,
hideAllWidgets: false,
brandedWallpaperOptIn: false,
isBrandedWallpaperNotificationDismissed: true,
Expand Down
3 changes: 2 additions & 1 deletion components/definitions/newTab.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ declare namespace NewTab {
showBinance: boolean
showGemini: boolean
showCryptoDotCom: boolean
showFTX: boolean
widgetVisibilityMigrated: boolean
hideAllWidgets: boolean
isBraveTodayOptedIn: boolean
isBrandedWallpaperNotificationDismissed: boolean
Expand All @@ -136,7 +138,6 @@ declare namespace NewTab {
customLinksEnabled: boolean
customLinksNum: number
showBitcoinDotCom: boolean
showFTX: boolean,
stats: Stats,
braveTalkPromptAllowed: boolean
brandedWallpaper?: BrandedWallpaper
Expand Down