Skip to content

Commit

Permalink
refactor(background): clear up icons usage (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Jun 21, 2024
1 parent 0741129 commit 886e1bc
Showing 1 changed file with 25 additions and 38 deletions.
63 changes: 25 additions & 38 deletions src/background/services/tabEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ import { StorageService } from './storage'
import { IsTabMonetizedPayload } from '@/shared/messages'

const runtime = browser.runtime
const icon34 = runtime.getURL('assets/icons/icon-34.png')
const icon128 = runtime.getURL('assets/icons/icon-128.png')
const iconActive34 = runtime.getURL('assets/icons/icon-active-34.png')
const iconActive128 = runtime.getURL('assets/icons/icon-active-128.png')
const iconInactive34 = runtime.getURL('assets/icons/icon-inactive-34.png')
const iconInactive128 = runtime.getURL('assets/icons/icon-inactive-128.png')
const iconWarning34 = runtime.getURL('assets/icons/icon-warning-34.png')
const iconWarning128 = runtime.getURL('assets/icons/icon-warning-128.png')
const ICONS = {
default: {
34: runtime.getURL('assets/icons/icon-34.png'),
128: runtime.getURL('assets/icons/icon-128.png')
},
active: {
34: runtime.getURL('assets/icons/icon-active-34.png'),
128: runtime.getURL('assets/icons/icon-active-128.png')
},
inactive: {
34: runtime.getURL('assets/icons/icon-inactive-34.png'),
128: runtime.getURL('assets/icons/icon-inactive-128.png')
},
warning: {
34: runtime.getURL('assets/icons/icon-warning-34.png'),
128: runtime.getURL('assets/icons/icon-warning-128.png')
}
}

export class TabEvents {
constructor(
Expand All @@ -33,17 +43,8 @@ export class TabEvents {

private changeIcon = async () => {
const { enabled } = await this.storage.get(['enabled'])

const iconData = {
'34': enabled ? icon34 : iconWarning34,
'128': enabled ? icon128 : iconWarning128
}

if (this.browser.action) {
await this.browser.action.setIcon({ path: iconData })
} else if (chrome.browserAction) {
chrome.browserAction.setIcon({ path: iconData })
}
const iconData = enabled ? ICONS.default : ICONS.warning
await this.browser.action.setIcon({ path: iconData })
}

onActivatedTab = async () => {
Expand All @@ -57,26 +58,12 @@ export class TabEvents {
onUpdatedTab = async (payload?: IsTabMonetizedPayload) => {
const { enabled } = await this.storage.get(['enabled'])

let iconData = {
'34': enabled ? icon34 : iconWarning34,
'128': enabled ? icon34 : iconWarning128
let iconData = enabled ? ICONS.default : ICONS.warning
if (enabled && payload) {
const { value: isTabMonetized } = payload
iconData = isTabMonetized ? ICONS.active : ICONS.inactive
}

if (enabled) {
if (payload) {
const { value } = payload

iconData = {
'34': value ? iconActive34 : iconInactive34,
'128': value ? iconActive128 : iconInactive128
}
}
}

if (this.browser.action) {
await this.browser.action.setIcon({ path: iconData })
} else if (chrome.browserAction) {
chrome.browserAction.setIcon({ path: iconData })
}
await this.browser.action.setIcon({ path: iconData })
}
}

0 comments on commit 886e1bc

Please sign in to comment.