Skip to content

Commit

Permalink
fix: ensure correct icon is reflected for monetization state (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi authored Jun 21, 2024
1 parent 886e1bc commit e3f0fb7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
7 changes: 3 additions & 4 deletions src/background/services/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ export class Background {

case PopupToBackgroundAction.TOGGLE_WM:
await this.monetizationService.toggleWM()

this.tabEvents.onUpdatedTab()
await this.tabEvents.onUpdatedTab()
return

case PopupToBackgroundAction.PAY_WEBSITE:
Expand All @@ -112,7 +111,7 @@ export class Background {
return

case ContentToBackgroundAction.STOP_MONETIZATION:
this.monetizationService.stopPaymentSession(
await this.monetizationService.stopPaymentSession(
message.payload,
sender
)
Expand All @@ -131,7 +130,7 @@ export class Background {
)

case ContentToBackgroundAction.IS_TAB_MONETIZED:
this.tabEvents.onUpdatedTab(message.payload)
await this.tabEvents.onUpdatedTab(message.payload, sender)
return

case ContentToBackgroundAction.IS_WM_ENABLED:
Expand Down
12 changes: 9 additions & 3 deletions src/background/services/tabEvents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import browser, { type Browser, type Tabs } from 'webextension-polyfill'
import browser from 'webextension-polyfill'
import type { Browser, Runtime, Tabs } from 'webextension-polyfill'
import { MonetizationService } from './monetization'
import { StorageService } from './storage'
import { IsTabMonetizedPayload } from '@/shared/messages'
import { getTabId } from '../utils'

const runtime = browser.runtime
const ICONS = {
Expand Down Expand Up @@ -55,15 +57,19 @@ export class TabEvents {
await this.changeIcon()
}

onUpdatedTab = async (payload?: IsTabMonetizedPayload) => {
onUpdatedTab = async (
payload?: IsTabMonetizedPayload | null,
sender?: Runtime.MessageSender
) => {
const { enabled } = await this.storage.get(['enabled'])

let iconData = enabled ? ICONS.default : ICONS.warning
if (enabled && payload) {
const { value: isTabMonetized } = payload
iconData = isTabMonetized ? ICONS.active : ICONS.inactive
}
const tabId = sender && getTabId(sender)

await this.browser.action.setIcon({ path: iconData })
await this.browser.action.setIcon({ path: iconData, tabId })
}
}
3 changes: 3 additions & 0 deletions src/content/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const checkWalletAddressUrlCall = async (
export const startMonetization = async (
payload: ContentToBackgroundActionPayload[ContentToBackgroundAction.START_MONETIZATION]
) => {
if (!payload.length) return
return await message.send({
action: ContentToBackgroundAction.START_MONETIZATION,
payload
Expand All @@ -30,6 +31,7 @@ export const startMonetization = async (
export const stopMonetization = async (
payload: ContentToBackgroundActionPayload[ContentToBackgroundAction.STOP_MONETIZATION]
) => {
if (!payload.length) return
return await message.send({
action: ContentToBackgroundAction.STOP_MONETIZATION,
payload
Expand All @@ -39,6 +41,7 @@ export const stopMonetization = async (
export const resumeMonetization = async (
payload: ContentToBackgroundActionPayload[ContentToBackgroundAction.RESUME_MONETIZATION]
) => {
if (!payload.length) return
return await message.send({
action: ContentToBackgroundAction.RESUME_MONETIZATION,
payload
Expand Down
8 changes: 1 addition & 7 deletions src/content/services/monetizationTagManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,10 @@ export class MonetizationTagManager extends EventEmitter {
}

private sendStartMonetization(tags: StartMonetizationPayload[]) {
if (!tags.length) return

const isFrameMonetizedMessage = {
message: ContentToContentAction.IS_FRAME_MONETIZED,
id: this.id,
payload: { isMonetized: true }
payload: { isMonetized: tags.length > 0 }
}

if (this.isTopFrame) {
Expand All @@ -443,8 +441,6 @@ export class MonetizationTagManager extends EventEmitter {
}

private async sendStopMonetization(tags: StopMonetizationPayload[]) {
if (!tags.length) return

await stopMonetization(tags)

// Check if tab still monetized
Expand All @@ -467,8 +463,6 @@ export class MonetizationTagManager extends EventEmitter {
}

private sendResumeMonetization(tags: ResumeMonetizationPayload[]) {
if (!tags.length) return

const isFrameMonetizedMessage = {
message: ContentToContentAction.IS_FRAME_MONETIZED,
id: this.id,
Expand Down

0 comments on commit e3f0fb7

Please sign in to comment.