From bc0ad4ff689ef22ff99ecbb19dbe4cb6b2378037 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Mon, 25 Nov 2024 16:55:16 +0530 Subject: [PATCH 1/3] refactor(bg/storage): rename `enabled` to `continuousPaymentsEnabled` --- src/background/services/monetization.ts | 44 ++++++++++++--------- src/background/services/storage.ts | 15 +++++-- src/background/services/tabEvents.ts | 23 ++++++----- src/popup/components/Settings/RateOfPay.tsx | 12 ++++-- src/popup/lib/context.tsx | 5 ++- src/shared/types.ts | 4 +- 6 files changed, 64 insertions(+), 39 deletions(-) diff --git a/src/background/services/monetization.ts b/src/background/services/monetization.ts index a06e632e..cd68bb6a 100644 --- a/src/background/services/monetization.ts +++ b/src/background/services/monetization.ts @@ -57,13 +57,13 @@ export class MonetizationService { } const { state, - enabled, + continuousPaymentsEnabled, rateOfPay, connected, walletAddress: connectedWallet, } = await this.storage.get([ 'state', - 'enabled', + 'continuousPaymentsEnabled', 'connected', 'rateOfPay', 'walletAddress', @@ -119,7 +119,7 @@ export class MonetizationService { const isAdjusted = await this.adjustSessionsAmount(sessionsArr, rate); if (!isAdjusted) return; - if (enabled && this.canTryPayment(connected, state)) { + if (continuousPaymentsEnabled && this.canTryPayment(connected, state)) { sessionsArr.forEach((session) => { if (!sessions.get(session.id)) return; const source = replacedSessions.has(session.id) @@ -199,12 +199,14 @@ export class MonetizationService { return; } - const { state, connected, enabled } = await this.storage.get([ - 'state', - 'connected', - 'enabled', - ]); - if (!enabled || !this.canTryPayment(connected, state)) return; + const { state, connected, continuousPaymentsEnabled } = + await this.storage.get([ + 'state', + 'connected', + 'continuousPaymentsEnabled', + ]); + if (!continuousPaymentsEnabled || !this.canTryPayment(connected, state)) + return; payload.forEach((p) => { const { requestId } = p; @@ -220,12 +222,14 @@ export class MonetizationService { return; } - const { state, connected, enabled } = await this.storage.get([ - 'state', - 'connected', - 'enabled', - ]); - if (!enabled || !this.canTryPayment(connected, state)) return; + const { state, connected, continuousPaymentsEnabled } = + await this.storage.get([ + 'state', + 'connected', + 'continuousPaymentsEnabled', + ]); + if (!continuousPaymentsEnabled || !this.canTryPayment(connected, state)) + return; for (const session of sessions.values()) { session.resume(); @@ -239,9 +243,11 @@ export class MonetizationService { } async toggleWM() { - const { enabled } = await this.storage.get(['enabled']); - const nowEnabled = !enabled; - await this.storage.set({ enabled: nowEnabled }); + const { continuousPaymentsEnabled } = await this.storage.get([ + 'continuousPaymentsEnabled', + ]); + const nowEnabled = !continuousPaymentsEnabled; + await this.storage.set({ continuousPaymentsEnabled: nowEnabled }); if (nowEnabled) { await this.resumePaymentSessionActiveTab(); } else { @@ -373,7 +379,7 @@ export class MonetizationService { async getPopupData(tab: Pick): Promise { const storedData = await this.storage.get([ - 'enabled', + 'continuousPaymentsEnabled', 'connected', 'state', 'rateOfPay', diff --git a/src/background/services/storage.ts b/src/background/services/storage.ts index d592adfa..d1a6c096 100644 --- a/src/background/services/storage.ts +++ b/src/background/services/storage.ts @@ -19,10 +19,10 @@ const defaultStorage = { * structural changes would need migrations for keeping compatibility with * existing installations. */ - version: 3, + version: 4, state: {}, connected: false, - enabled: true, + continuousPaymentsEnabled: true, exceptionList: {}, walletAddress: null, recurringGrant: null, @@ -116,9 +116,11 @@ export class StorageService { } async getWMState(): Promise { - const { enabled } = await this.get(['enabled']); + const { continuousPaymentsEnabled } = await this.get([ + 'continuousPaymentsEnabled', + ]); - return enabled; + return continuousPaymentsEnabled; } async keyPairExists(): Promise { @@ -284,4 +286,9 @@ const MIGRATIONS: Record = { data.state = newState satisfies Storage['state']; return [data]; }, + 4: (data) => { + data.continuousPaymentsEnabled = data.enabled === true; + const deleteKeys = ['enabled']; + return [data, deleteKeys]; + }, }; diff --git a/src/background/services/tabEvents.ts b/src/background/services/tabEvents.ts index e87b5ce9..e8a8f58d 100644 --- a/src/background/services/tabEvents.ts +++ b/src/background/services/tabEvents.ts @@ -128,13 +128,14 @@ export class TabEvents { updateVisualIndicators = async (tab: Tabs.Tab) => { const tabInfo = this.tabState.getPopupTabData(tab); this.sendToPopup.send('SET_TAB_DATA', tabInfo); - const { enabled, connected, state } = await this.storage.get([ - 'enabled', - 'connected', - 'state', - ]); + const { continuousPaymentsEnabled, connected, state } = + await this.storage.get([ + 'continuousPaymentsEnabled', + 'connected', + 'state', + ]); const { path, title } = this.getIconAndTooltip({ - enabled, + continuousPaymentsEnabled, connected, state, tabInfo, @@ -168,12 +169,12 @@ export class TabEvents { } private getIconAndTooltip({ - enabled, + continuousPaymentsEnabled, connected, state, tabInfo, }: { - enabled: Storage['enabled']; + continuousPaymentsEnabled: Storage['continuousPaymentsEnabled']; connected: Storage['connected']; state: Storage['state']; tabInfo: PopupTabInfo; @@ -183,7 +184,9 @@ export class TabEvents { if (!connected) { // use defaults } else if (!isOkState(state) || tabInfo.status === 'all_sessions_invalid') { - iconData = enabled ? ICONS.enabled_warn : ICONS.disabled_warn; + iconData = continuousPaymentsEnabled + ? ICONS.enabled_warn + : ICONS.disabled_warn; const tabStateText = this.t('icon_state_actionRequired'); title = `${title} - ${tabStateText}`; } else if ( @@ -193,7 +196,7 @@ export class TabEvents { // use defaults } else { const isTabMonetized = tabInfo.status === 'monetized'; - if (enabled) { + if (continuousPaymentsEnabled) { iconData = isTabMonetized ? ICONS.enabled_hasLinks : ICONS.enabled_noLinks; diff --git a/src/popup/components/Settings/RateOfPay.tsx b/src/popup/components/Settings/RateOfPay.tsx index f1a71edf..e839e71a 100644 --- a/src/popup/components/Settings/RateOfPay.tsx +++ b/src/popup/components/Settings/RateOfPay.tsx @@ -18,7 +18,13 @@ export const RateOfPayScreen = () => { const message = useMessage(); const { dispatch, - state: { enabled, rateOfPay, minRateOfPay, maxRateOfPay, walletAddress }, + state: { + continuousPaymentsEnabled, + rateOfPay, + minRateOfPay, + maxRateOfPay, + walletAddress, + }, } = usePopupState(); const updateRateOfPay = React.useRef( @@ -43,7 +49,7 @@ export const RateOfPayScreen = () => { return ( { }; interface Props { - continuousPaymentsEnabled: PopupState['enabled']; + continuousPaymentsEnabled: PopupState['continuousPaymentsEnabled']; rateOfPay: PopupState['rateOfPay']; minRateOfPay: PopupState['minRateOfPay']; maxRateOfPay: PopupState['maxRateOfPay']; diff --git a/src/popup/lib/context.tsx b/src/popup/lib/context.tsx index a487ed83..20d33080 100644 --- a/src/popup/lib/context.tsx +++ b/src/popup/lib/context.tsx @@ -66,7 +66,10 @@ const reducer = (state: PopupState, action: ReducerActions): PopupState => { case 'SET_DATA': return action.data; case 'TOGGLE_WM': - return { ...state, enabled: !state.enabled }; + return { + ...state, + continuousPaymentsEnabled: !state.continuousPaymentsEnabled, + }; case 'SET_CONNECTED': return { ...state, connected: action.data.connected }; case 'UPDATE_RATE_OF_PAY': diff --git a/src/shared/types.ts b/src/shared/types.ts index 5898e09f..f03fee76 100644 --- a/src/shared/types.ts +++ b/src/shared/types.ts @@ -56,10 +56,10 @@ export interface Storage { */ version: number; - /** If web monetization is enabled */ - enabled: boolean; /** If a wallet is connected or not */ connected: boolean; + /** If web monetization is enabled */ + continuousPaymentsEnabled: boolean; /** Extension state */ state: Partial>; From b352a14325bc24b370f43c90dc3577ae961d120a Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Thu, 28 Nov 2024 16:30:35 +0530 Subject: [PATCH 2/3] don't remove `enabled` key yet; but set it to true for future --- src/background/services/storage.ts | 7 ++++--- src/shared/types.ts | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/background/services/storage.ts b/src/background/services/storage.ts index d1a6c096..41178718 100644 --- a/src/background/services/storage.ts +++ b/src/background/services/storage.ts @@ -22,6 +22,7 @@ const defaultStorage = { version: 4, state: {}, connected: false, + enabled: true, continuousPaymentsEnabled: true, exceptionList: {}, walletAddress: null, @@ -287,8 +288,8 @@ const MIGRATIONS: Record = { return [data]; }, 4: (data) => { - data.continuousPaymentsEnabled = data.enabled === true; - const deleteKeys = ['enabled']; - return [data, deleteKeys]; + data.continuousPaymentsEnabled = data.enabled; + data.enabled = true; + return [data]; }, }; diff --git a/src/shared/types.ts b/src/shared/types.ts index f03fee76..09481ff3 100644 --- a/src/shared/types.ts +++ b/src/shared/types.ts @@ -58,6 +58,8 @@ export interface Storage { /** If a wallet is connected or not */ connected: boolean; + /** Whether the extension (actually any sort of payment) is enabled */ + enabled: boolean; /** If web monetization is enabled */ continuousPaymentsEnabled: boolean; /** Extension state */ From 3abdf573b68d22d8e358dc96609a8c22abefc721 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Thu, 28 Nov 2024 16:35:40 +0530 Subject: [PATCH 3/3] fix build failure --- src/background/services/monetization.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/background/services/monetization.ts b/src/background/services/monetization.ts index dddb2f40..a5597b0e 100644 --- a/src/background/services/monetization.ts +++ b/src/background/services/monetization.ts @@ -459,6 +459,7 @@ export class MonetizationService { async getPopupData(tab: Pick): Promise { const storedData = await this.storage.get([ + 'enabled', 'continuousPaymentsEnabled', 'connected', 'state',