Skip to content

Commit

Permalink
add badge
Browse files Browse the repository at this point in the history
  • Loading branch information
ong-ar committed Apr 10, 2022
1 parent 20553b1 commit e9b40b3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
27 changes: 17 additions & 10 deletions src/Popup/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { PATH } from '~/constants/route';
import { TENDERMINT_METHOD_TYPE, TENDERMINT_NO_POPUP_METHOD_TYPE, TENDERMINT_POPUP_METHOD_TYPE } from '~/constants/tendermint';
import { getStorage, setStorage } from '~/Popup/utils/chromeStorage';
import { openTab } from '~/Popup/utils/chromeTabs';
import { openWindow } from '~/Popup/utils/chromeWindows';
import { closeWindow, openWindow } from '~/Popup/utils/chromeWindows';
import { getAddress, getKeyPair } from '~/Popup/utils/common';
import { EthereumRPCError, TendermintRPCError } from '~/Popup/utils/error';
import { responseToWeb } from '~/Popup/utils/message';
import type { TendermintChain } from '~/types/chain';
import type { CurrencyType, LanguageType } from '~/types/chromeStorage';
import type { CurrencyType, LanguageType, Queue } from '~/types/chromeStorage';
import type { ContentScriptToBackgroundEventMessage, RequestMessage } from '~/types/message';
import type { TenAddChainParams, TenRequestAccountResponse, TenSignAminoParams, TenSignDirectParams } from '~/types/tendermint/message';
import type { ThemeType } from '~/types/theme';
Expand Down Expand Up @@ -340,14 +340,16 @@ function background() {
}
});

// chrome.storage.onChanged.addListener((changes) => {
// // eslint-disable-next-line no-restricted-syntax
// for (const [key, { newValue }] of Object.entries(changes)) {
// if (key === 'queues') {
// void chrome.browserAction.setBadgeText({ text: '1' });
// }
// }
// });
chrome.storage.onChanged.addListener((changes) => {
// eslint-disable-next-line no-restricted-syntax
for (const [key, { newValue }] of Object.entries(changes)) {
if (key === 'queues') {
const newQueues = newValue as Queue[] | undefined;
const text = newQueues ? `${newQueues.length > 0 ? newQueues.length : ''}` : '';
void chrome.action.setBadgeText({ text });
}
}
});

chrome.windows.onRemoved.addListener((windowId) => {
void (async function asyncHandler() {
Expand Down Expand Up @@ -378,6 +380,8 @@ function background() {
messageId: queue.messageId,
origin: queue.origin,
});

void closeWindow(queue.windowId);
});

await setStorage('queues', []);
Expand Down Expand Up @@ -424,6 +428,9 @@ function background() {
}
})();
});

void chrome.action.setBadgeBackgroundColor({ color: '#7C4FFC' });
void chrome.action.setBadgeText({ text: '' });
}

background();
23 changes: 19 additions & 4 deletions src/Popup/utils/chromeWindows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ import { getStorage, setStorage } from './chromeStorage';
export async function openWindow(): Promise<chrome.windows.Window | undefined> {
const url = chrome.runtime.getURL('popup.html');

const windowId = await getStorage('windowId');
const queues = await getStorage('queues');

const currentWindow = windowId ? await getWindow(windowId) : undefined;
const currentWindowIds = queues.filter((item) => typeof item.windowId === 'number').map((item) => item.windowId) as number[];

const currentWindowId = await getStorage('windowId');

if (typeof currentWindowId === 'number') {
currentWindowIds.push(currentWindowId);
}

const windowIds = Array.from(new Set(currentWindowIds));

const currentWindows = windowIds
.map(async (item) => {
const window = await getWindow(item);
return window;
})
.filter((item) => item !== undefined) as Promise<chrome.windows.Window>[];

return new Promise((res, rej) => {
if (currentWindow) {
res(currentWindow);
if (currentWindows.length > 0) {
res(currentWindows[0]);
return;
}

Expand Down

0 comments on commit e9b40b3

Please sign in to comment.