Skip to content

Commit

Permalink
fix(background): use default icon on non-http/https tabs (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi authored Sep 4, 2024
1 parent e42ffbe commit a6da5ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/background/services/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export class Background {
private async updateVisualIndicatorsForCurrentTab() {
const activeTab = await getCurrentActiveTab(this.browser);
if (activeTab?.id) {
void this.tabEvents.updateVisualIndicators(activeTab.id);
void this.tabEvents.updateVisualIndicators(activeTab.id, activeTab.url);
}
}

Expand All @@ -251,8 +251,9 @@ export class Background {
await this.updateVisualIndicatorsForCurrentTab();
});

this.events.on('monetization.state_update', (tabId) => {
void this.tabEvents.updateVisualIndicators(tabId);
this.events.on('monetization.state_update', async (tabId) => {
const tab = await this.browser.tabs.get(tabId);
void this.tabEvents.updateVisualIndicators(tabId, tab?.url);
});

this.events.on('storage.balance_update', (balance) =>
Expand Down
17 changes: 13 additions & 4 deletions src/background/services/tabEvents.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import browser from 'webextension-polyfill';
import type { Browser } from 'webextension-polyfill';
import { isOkState, removeQueryParams } from '@/shared/helpers';
import { ALLOWED_PROTOCOLS } from '@/shared/defines';
import type { Storage, TabId } from '@/shared/types';
import type { Cradle } from '@/background/container';

Expand Down Expand Up @@ -81,7 +82,7 @@ export class TabEvents {
if (clearOverpaying) {
this.tabState.clearOverpayingByTabId(tabId);
}
void this.updateVisualIndicators(tabId);
void this.updateVisualIndicators(tabId, url);
}
};

Expand All @@ -91,23 +92,28 @@ export class TabEvents {
};

onActivatedTab: CallbackTab<'onActivated'> = async (info) => {
await this.updateVisualIndicators(info.tabId);
const tab = await this.browser.tabs.get(info.tabId);
await this.updateVisualIndicators(info.tabId, tab?.url);
};

onCreatedTab: CallbackTab<'onCreated'> = async (tab) => {
if (!tab.id) return;
await this.updateVisualIndicators(tab.id);
await this.updateVisualIndicators(tab.id, tab.url);
};

updateVisualIndicators = async (
tabId: TabId,
tabUrl?: string,
isTabMonetized: boolean = tabId
? this.tabState.isTabMonetized(tabId)
: false,
hasTabAllSessionsInvalid: boolean = tabId
? this.tabState.tabHasAllSessionsInvalid(tabId)
: false,
) => {
const canMonetizeTab = ALLOWED_PROTOCOLS.some((scheme) =>
tabUrl?.startsWith(scheme),
);
const { enabled, connected, state } = await this.storage.get([
'enabled',
'connected',
Expand All @@ -117,6 +123,7 @@ export class TabEvents {
enabled,
connected,
state,
canMonetizeTab,
isTabMonetized,
hasTabAllSessionsInvalid,
});
Expand All @@ -140,18 +147,20 @@ export class TabEvents {
enabled,
connected,
state,
canMonetizeTab,
isTabMonetized,
hasTabAllSessionsInvalid,
}: {
enabled: Storage['enabled'];
connected: Storage['connected'];
state: Storage['state'];
canMonetizeTab: boolean;
isTabMonetized: boolean;
hasTabAllSessionsInvalid: boolean;
}) {
let title = this.t('appName');
let iconData = ICONS.default;
if (!connected) {
if (!connected || !canMonetizeTab) {
// use defaults
} else if (!isOkState(state) || hasTabAllSessionsInvalid) {
iconData = enabled ? ICONS.enabled_warn : ICONS.disabled_warn;
Expand Down

0 comments on commit a6da5ac

Please sign in to comment.