diff --git a/browser/ui/webui/brave_tip_ui.cc b/browser/ui/webui/brave_tip_ui.cc index b06f2369d3f5..bbd7c6164782 100644 --- a/browser/ui/webui/brave_tip_ui.cc +++ b/browser/ui/webui/brave_tip_ui.cc @@ -62,6 +62,10 @@ class RewardsTipDOMHandler : public WebUIMessageHandler, void OnGetRecurringTips( std::unique_ptr list); void TweetTip(const base::ListValue *args); + void GetExternalWallet(const base::ListValue* args); + void OnExternalWallet( + const int32_t result, + std::unique_ptr wallet); void OnPublisherBanner( std::unique_ptr banner); @@ -136,6 +140,11 @@ void RewardsTipDOMHandler::RegisterMessages() { "brave_rewards_tip.fetchBalance", base::BindRepeating(&RewardsTipDOMHandler::FetchBalance, base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "brave_rewards_tip.getExternalWallet", + base::BindRepeating( + &RewardsTipDOMHandler::GetExternalWallet, + base::Unretained(this))); } void RewardsTipDOMHandler::GetPublisherTipData( @@ -421,3 +430,42 @@ void RewardsTipDOMHandler::FetchBalance(const base::ListValue* args) { weak_factory_.GetWeakPtr())); } } + +void RewardsTipDOMHandler::GetExternalWallet( + const base::ListValue* args) { + if (!rewards_service_) { + return; + } + + const std::string type = args->GetList()[0].GetString(); + + rewards_service_->GetExternalWallet(type, + base::BindOnce( + &RewardsTipDOMHandler::OnExternalWallet, + weak_factory_.GetWeakPtr())); +} + +void RewardsTipDOMHandler::OnExternalWallet( + const int32_t result, + std::unique_ptr wallet) { + if (!web_ui()->CanCallJavascript()) { + return; + } + + base::DictionaryValue data; + + if (wallet) { + data.SetString("token", wallet->token); + data.SetString("address", wallet->address); + data.SetString("type", wallet->type); + data.SetString("verifyUrl", wallet->verify_url); + data.SetString("addUrl", wallet->add_url); + data.SetString("withdrawUrl", wallet->withdraw_url); + data.SetString("userName", wallet->user_name); + data.SetString("accountUrl", wallet->account_url); + data.SetInteger("status", static_cast(wallet->status)); + } + + web_ui()->CallJavascriptFunctionUnsafe( + "brave_rewards_tip.externalWallet", data); +} diff --git a/components/brave_rewards/resources/tip/actions/tip_actions.ts b/components/brave_rewards/resources/tip/actions/tip_actions.ts index e3a8effb5a8f..2d2c067f79b7 100644 --- a/components/brave_rewards/resources/tip/actions/tip_actions.ts +++ b/components/brave_rewards/resources/tip/actions/tip_actions.ts @@ -57,3 +57,9 @@ export const onBalance = (status: number, balance: RewardsTip.Balance) => action status, balance }) + +export const getExternalWallet = () => action(types.GET_EXTERNAL_WALLET) + +export const onExternalWallet = (wallet: RewardsTip.ExternalWallet) => action(types.ON_EXTERNAL_WALLET, { + wallet +}) diff --git a/components/brave_rewards/resources/tip/brave_rewards_tip.tsx b/components/brave_rewards/resources/tip/brave_rewards_tip.tsx index 5b99aa2a4907..792144ae63f9 100644 --- a/components/brave_rewards/resources/tip/brave_rewards_tip.tsx +++ b/components/brave_rewards/resources/tip/brave_rewards_tip.tsx @@ -86,6 +86,10 @@ window.cr.define('brave_rewards_tip', function () { getActions().onBalance(properties.status, properties.balance) } + function externalWallet (wallet: RewardsTip.ExternalWallet) { + getActions().onExternalWallet(wallet) + } + return { initialize, publisherBanner, @@ -94,7 +98,8 @@ window.cr.define('brave_rewards_tip', function () { reconcileStamp, recurringTipRemoved, recurringTipSaved, - balance + balance, + externalWallet } }) diff --git a/components/brave_rewards/resources/tip/components/siteBanner.tsx b/components/brave_rewards/resources/tip/components/siteBanner.tsx index 2409044d7926..f33746035433 100644 --- a/components/brave_rewards/resources/tip/components/siteBanner.tsx +++ b/components/brave_rewards/resources/tip/components/siteBanner.tsx @@ -37,6 +37,7 @@ class Banner extends React.Component { this.actions.getBalance() this.actions.getRecurringTips() this.actions.getReconcileStamp() + this.actions.getExternalWallet() } get actions () { @@ -182,6 +183,29 @@ class Banner extends React.Component { return new Intl.DateTimeFormat('default', fmtArgs).format(reconcileStamp * 1000) } + shouldShowConnectedMessage = () => { + const publisher = this.props.publisher + const { externalWallet, balance } = this.props.rewardsDonateData + const { wallets } = balance + const notVerified = publisher && utils.isPublisherNotVerified(publisher.status) + const connected = publisher && utils.isPublisherConnected(publisher.status) + const status = utils.getWalletStatus(externalWallet) + + if (notVerified) { + return true + } + + if (connected && (status === 'unverified' || + status === 'disconnected_unverified' || + status === 'disconnected_verified')) { + return false + } + + const hasAnonBalance = wallets['anonymous'] && wallets['anonymous'] > 0 + + return connected && !hasAnonBalance + } + render () { const { balance } = this.props.rewardsDonateData const { total } = balance @@ -189,8 +213,6 @@ class Banner extends React.Component { const mediaMetaData = this.props.mediaMetaData const publisher = this.props.publisher const checkmark = utils.isPublisherConnectedOrVerified(publisher.status) - const verified = utils.isPublisherVerified(publisher.status) - const connected = utils.isPublisherConnected(publisher.status) const bannerType = this.props.monthly ? 'monthly' : 'one-time' let logo = publisher.logo @@ -220,7 +242,7 @@ class Banner extends React.Component { currentAmount={this.state.currentAmount || '0'} onClose={this.onClose} social={this.generateSocialLinks()} - showUnVerifiedNotice={!verified || connected} + showUnVerifiedNotice={this.shouldShowConnectedMessage()} isVerified={checkmark} learnMoreNotice={'https://brave.com/faq/#unclaimed-funds'} addFundsLink={this.addFundsLink} diff --git a/components/brave_rewards/resources/tip/constants/tip_types.ts b/components/brave_rewards/resources/tip/constants/tip_types.ts index c1384d6411c2..e6ab130bf8d5 100644 --- a/components/brave_rewards/resources/tip/constants/tip_types.ts +++ b/components/brave_rewards/resources/tip/constants/tip_types.ts @@ -16,5 +16,7 @@ export const enum types { ON_RECURRING_TIP_REMOVED = '@@rewards/ON_RECURRING_TIP_REMOVED', ON_RECURRING_TIP_SAVED = '@@rewards/ON_RECURRING_TIP_SAVED', GET_BALANCE = '@@rewards/GET_BALANCE', - ON_BALANCE = '@@rewards/ON_BALANCE' + ON_BALANCE = '@@rewards/ON_BALANCE', + GET_EXTERNAL_WALLET = '@@rewards/GET_EXTERNAL_WALLET', + ON_EXTERNAL_WALLET = '@@rewards/ON_EXTERNAL_WALLET' } diff --git a/components/brave_rewards/resources/tip/reducers/tip_reducer.ts b/components/brave_rewards/resources/tip/reducers/tip_reducer.ts index 1440ec641eb1..797c1bda39c2 100644 --- a/components/brave_rewards/resources/tip/reducers/tip_reducer.ts +++ b/components/brave_rewards/resources/tip/reducers/tip_reducer.ts @@ -111,6 +111,15 @@ const publishersReducer: Reducer = (state: RewardsTip.State = } break } + case types.GET_EXTERNAL_WALLET: { + chrome.send('brave_rewards_tip.getExternalWallet', ['uphold']) + break + } + case types.ON_EXTERNAL_WALLET: { + state = { ...state } + state.externalWallet = payload.wallet + break + } } return state diff --git a/components/brave_rewards/resources/tip/utils.ts b/components/brave_rewards/resources/tip/utils.ts index bb00e8ae9cfb..02691db73894 100644 --- a/components/brave_rewards/resources/tip/utils.ts +++ b/components/brave_rewards/resources/tip/utils.ts @@ -52,3 +52,22 @@ export const isPublisherConnectedOrVerified = (status: Rewards.PublisherStatus) export const isPublisherNotVerified = (status: Rewards.PublisherStatus) => { return status === 0 } + +export const getWalletStatus = (externalWallet?: RewardsTip.ExternalWallet) => { + if (!externalWallet) { + return 'unverified' + } + + switch (externalWallet.status) { + case 1: + return 'connected' + case 2: + return 'verified' + case 3: + return 'disconnected_unverified' + case 4: + return 'disconnected_verified' + default: + return 'unverified' + } +} diff --git a/components/definitions/rewardsTip.d.ts b/components/definitions/rewardsTip.d.ts index 38e93a06eae3..8ee63fade674 100644 --- a/components/definitions/rewardsTip.d.ts +++ b/components/definitions/rewardsTip.d.ts @@ -9,6 +9,7 @@ declare namespace RewardsTip { recurringDonations?: RecurringTips[] // TODO(nejczdovc): migrate to tips reconcileStamp: number balance: Balance + externalWallet?: ExternalWallet } interface ApplicationState { @@ -92,4 +93,26 @@ declare namespace RewardsTip { rates: Record wallets: Record } + + export type WalletType = 'anonymous' | 'uphold' + + export enum ExternalWalletStatus { + NOT_CONNECTED = 0, + CONNECTED = 1, + VERIFIED = 2, + DISCONNECTED_NOT_VERIFIED = 3, + DISCONNECTED_VERIFIED = 4 + } + + export interface ExternalWallet { + token: string + address: string + status: ExternalWalletStatus + type: WalletType + verifyUrl: string + addUrl: string + withdrawUrl: string + userName: string + accountUrl: string + } }