From fd20483d05a21c48b3f924b003ae6704e453ad4a Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Thu, 12 Dec 2024 16:41:41 +0530 Subject: [PATCH 01/12] feat: add global payment switch --- src/_locales/en/messages.json | 3 ++ src/background/services/background.ts | 6 +++ src/background/services/monetization.ts | 47 +++++++++++++++++--- src/background/services/storage.ts | 8 ---- src/background/services/tabEvents.ts | 8 +++- src/pages/popup/components/layout/Header.tsx | 30 ++++++++++++- src/pages/popup/lib/store.ts | 5 +++ src/pages/popup/pages/Home.tsx | 6 ++- src/pages/shared/components/ui/Switch.tsx | 12 ++++- src/shared/messages.ts | 4 ++ 10 files changed, 108 insertions(+), 21 deletions(-) diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index 6fac4329..109e6ce1 100755 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -30,6 +30,9 @@ "notMonetized_text_unsupportedScheme": { "message": "Web Monetization only works with websites using https://." }, + "app_text_disabled": { + "message": "It looks like payments are disabled. Use the toggle to continue making payments." + }, "keyRevoked_error_title": { "message": "Unauthorized access to the wallet." }, diff --git a/src/background/services/background.ts b/src/background/services/background.ts index 5082a95f..7e0e4e39 100644 --- a/src/background/services/background.ts +++ b/src/background/services/background.ts @@ -234,6 +234,12 @@ export class Background { return; } + case 'TOGGLE_PAYMENTS': { + await this.monetizationService.togglePayments(); + await this.updateVisualIndicatorsForCurrentTab(); + return; + } + case 'UPDATE_RATE_OF_PAY': return success( await this.storage.updateRate(message.payload.rateOfPay), diff --git a/src/background/services/monetization.ts b/src/background/services/monetization.ts index 19cc562b..956e056b 100644 --- a/src/background/services/monetization.ts +++ b/src/background/services/monetization.ts @@ -72,12 +72,14 @@ export class MonetizationService { const { state, continuousPaymentsEnabled, + enabled, rateOfPay, connected, walletAddress: connectedWallet, } = await this.storage.get([ 'state', 'continuousPaymentsEnabled', + 'enabled', 'connected', 'rateOfPay', 'walletAddress', @@ -133,7 +135,11 @@ export class MonetizationService { const isAdjusted = await this.adjustSessionsAmount(sessionsArr, rate); if (!isAdjusted) return; - if (continuousPaymentsEnabled && this.canTryPayment(connected, state)) { + if ( + enabled && + continuousPaymentsEnabled && + this.canTryPayment(connected, state) + ) { sessionsArr.forEach((session) => { if (!sessions.get(session.id)) return; const source = replacedSessions.has(session.id) @@ -213,14 +219,20 @@ export class MonetizationService { return; } - const { state, connected, continuousPaymentsEnabled } = + const { state, connected, continuousPaymentsEnabled, enabled } = await this.storage.get([ 'state', 'connected', 'continuousPaymentsEnabled', + 'enabled', ]); - if (!continuousPaymentsEnabled || !this.canTryPayment(connected, state)) + if ( + !enabled || + !continuousPaymentsEnabled || + !this.canTryPayment(connected, state) + ) { return; + } payload.forEach((p) => { const { requestId } = p; @@ -236,14 +248,20 @@ export class MonetizationService { return; } - const { state, connected, continuousPaymentsEnabled } = + const { state, connected, continuousPaymentsEnabled, enabled } = await this.storage.get([ 'state', 'connected', 'continuousPaymentsEnabled', + 'enabled', ]); - if (!continuousPaymentsEnabled || !this.canTryPayment(connected, state)) + if ( + !enabled || + !continuousPaymentsEnabled || + !this.canTryPayment(connected, state) + ) { return; + } for (const session of sessions.values()) { session.resume(); @@ -257,12 +275,27 @@ export class MonetizationService { } async toggleWM() { - const { continuousPaymentsEnabled } = await this.storage.get([ + const { continuousPaymentsEnabled, enabled } = await this.storage.get([ 'continuousPaymentsEnabled', + 'enabled', ]); const nowEnabled = !continuousPaymentsEnabled; await this.storage.set({ continuousPaymentsEnabled: nowEnabled }); - if (nowEnabled) { + if (nowEnabled && enabled) { + await this.resumePaymentSessionActiveTab(); + } else { + this.stopAllSessions(); + } + } + + async togglePayments() { + const { continuousPaymentsEnabled, enabled } = await this.storage.get([ + 'continuousPaymentsEnabled', + 'enabled', + ]); + const nowEnabled = !enabled; + await this.storage.set({ enabled: nowEnabled }); + if (nowEnabled && continuousPaymentsEnabled) { await this.resumePaymentSessionActiveTab(); } else { this.stopAllSessions(); diff --git a/src/background/services/storage.ts b/src/background/services/storage.ts index 41178718..4a91b53e 100644 --- a/src/background/services/storage.ts +++ b/src/background/services/storage.ts @@ -116,14 +116,6 @@ export class StorageService { return data as unknown as Storage; } - async getWMState(): Promise { - const { continuousPaymentsEnabled } = await this.get([ - 'continuousPaymentsEnabled', - ]); - - return continuousPaymentsEnabled; - } - async keyPairExists(): Promise { const keys = await this.get(['privateKey', 'publicKey', 'keyId']); if ( diff --git a/src/background/services/tabEvents.ts b/src/background/services/tabEvents.ts index df60d447..b3b80da3 100644 --- a/src/background/services/tabEvents.ts +++ b/src/background/services/tabEvents.ts @@ -131,14 +131,16 @@ export class TabEvents { updateVisualIndicators = async (tab: Tabs.Tab) => { const tabInfo = this.tabState.getPopupTabData(tab); this.sendToPopup.send('SET_TAB_DATA', tabInfo); - const { continuousPaymentsEnabled, connected, state } = + const { continuousPaymentsEnabled, enabled, connected, state } = await this.storage.get([ 'continuousPaymentsEnabled', + 'enabled', 'connected', 'state', ]); const { path, title } = this.getIconAndTooltip({ continuousPaymentsEnabled, + enabled, connected, state, tabInfo, @@ -173,11 +175,13 @@ export class TabEvents { private getIconAndTooltip({ continuousPaymentsEnabled, + enabled, connected, state, tabInfo, }: { continuousPaymentsEnabled: Storage['continuousPaymentsEnabled']; + enabled: Storage['enabled']; connected: Storage['connected']; state: Storage['state']; tabInfo: PopupTabInfo; @@ -186,6 +190,8 @@ export class TabEvents { let iconData = ICONS.default; if (!connected) { // use defaults + } else if (!enabled) { + iconData = ICONS.default_gray; } else if (!isOkState(state) || tabInfo.status === 'all_sessions_invalid') { iconData = continuousPaymentsEnabled ? ICONS.enabled_warn diff --git a/src/pages/popup/components/layout/Header.tsx b/src/pages/popup/components/layout/Header.tsx index 4c785769..f61e3506 100644 --- a/src/pages/popup/components/layout/Header.tsx +++ b/src/pages/popup/components/layout/Header.tsx @@ -1,10 +1,12 @@ import React from 'react'; import { Link, useLocation } from 'react-router-dom'; import { ArrowBack, Settings } from '@/pages/shared/components/Icons'; +import { Switch } from '@/pages/shared/components/ui/Switch'; import { HeaderEmpty } from './HeaderEmpty'; +import { isOkState } from '@/shared/helpers'; import { ROUTES_PATH } from '@/popup/Popup'; -import { useBrowser } from '@/popup/lib/context'; -import { usePopupState } from '@/popup/lib/store'; +import { useBrowser, useMessage } from '@/popup/lib/context'; +import { usePopupState, dispatch } from '@/popup/lib/store'; const NavigationButton = () => { const location = useLocation(); @@ -33,12 +35,36 @@ const NavigationButton = () => { }, [location, connected]); }; +const TogglePaymentsButton = ({ toggle }: { toggle: () => void }) => { + const { enabled, connected, state } = usePopupState(); + + if (!connected) return null; + if (!isOkState(state)) return null; + + return ( + + ); +}; + export const Header = () => { const browser = useBrowser(); + const message = useMessage(); const Logo = browser.runtime.getURL('assets/images/logo.svg'); return ( + { + message.send('TOGGLE_PAYMENTS'); + dispatch({ type: 'TOGGLE_PAYMENTS' }); + }} + /> ); diff --git a/src/pages/popup/lib/store.ts b/src/pages/popup/lib/store.ts index ceef243d..5f49603d 100644 --- a/src/pages/popup/lib/store.ts +++ b/src/pages/popup/lib/store.ts @@ -26,6 +26,10 @@ export const dispatch = ({ type, data }: Actions) => { store.continuousPaymentsEnabled = !store.continuousPaymentsEnabled; return; } + case 'TOGGLE_PAYMENTS': { + store.enabled = !store.enabled; + return; + } case 'SET_CONNECTED': store.connected = data.connected; break; @@ -52,6 +56,7 @@ export const dispatch = ({ type, data }: Actions) => { type Actions = | { type: 'SET_DATA'; data: PopupState } | { type: 'TOGGLE_WM'; data?: never } + | { type: 'TOGGLE_PAYMENTS'; data?: never } | { type: 'SET_CONNECTED'; data: { connected: boolean } } | { type: 'UPDATE_RATE_OF_PAY'; data: { rateOfPay: string } } | BackgroundToPopupMessage; diff --git a/src/pages/popup/pages/Home.tsx b/src/pages/popup/pages/Home.tsx index be2694d0..3236da6b 100644 --- a/src/pages/popup/pages/Home.tsx +++ b/src/pages/popup/pages/Home.tsx @@ -9,7 +9,11 @@ import { usePopupState } from '@/popup/lib/store'; export const Component = () => { const t = useTranslation(); - const { tab } = usePopupState(); + const { tab, enabled } = usePopupState(); + + if (!enabled) { + return ; + } if (tab.status !== 'monetized') { switch (tab.status) { diff --git a/src/pages/shared/components/ui/Switch.tsx b/src/pages/shared/components/ui/Switch.tsx index 054d86cd..ad2d1779 100644 --- a/src/pages/shared/components/ui/Switch.tsx +++ b/src/pages/shared/components/ui/Switch.tsx @@ -42,11 +42,19 @@ export interface SwitchProps } export const Switch = forwardRef(function Switch( - { size, label, className, disabled = false, onChange = () => {}, ...props }, + { + size, + label, + className, + title, + disabled = false, + onChange = () => {}, + ...props + }, ref, ) { return ( -