Skip to content

Commit

Permalink
fix(extensions): ensure the extensions refresh badge and context menu…
Browse files Browse the repository at this point in the history
… after decrypting the config
  • Loading branch information
edmundhung committed Aug 13, 2021
1 parent 7502c1d commit 39a8f56
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions packages/extensions/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ async function savePassphase(

throw error;
}

await refreshData(context);
}

async function reset(context): Promise<void> {
Expand All @@ -145,13 +147,24 @@ async function updateActiveTab(context: Context, tab: Tabs.Tab): Promise<void> {

context.activeTabHost = url.host;

await refreshData(context);
}

async function refreshData(context: Context): Promise<void> {
const domains = Object.keys(context.config.domains);
const emails = lookupEmails(context.config, context.activeTabHost);
const configByDomain = deriveConfigByDomain(context.config);
const emailsByDomain = getEmailsByDomain(domains, emails);

await browser.browserAction.setBadgeText({
text: emails.length > 0 ? `${emails.length}` : '',
});

await updateContextMenu(emailsByDomain);
}

async function updateContextMenu(
emailsByDomain: Record<string, string[]>,
): Promise<void> {
await browser.contextMenus.removeAll();

browser.contextMenus.create({
Expand All @@ -160,16 +173,14 @@ async function updateActiveTab(context: Context, tab: Tabs.Tab): Promise<void> {
contexts: ['all'],
});

for (const domain of Object.keys(configByDomain)) {
for (const [domain, emails] of Object.entries(emailsByDomain)) {
browser.contextMenus.create({
id: `maildog-${domain}`,
parentId: 'maildog',
title: domain,
});

for (const email of emails.filter((email) =>
email.endsWith(`@${domain}`),
)) {
for (const email of emails) {
browser.contextMenus.create({
id: `maildog-${domain}-${email}`,
parentId: `maildog-${domain}`,
Expand Down Expand Up @@ -197,6 +208,18 @@ function lookupEmails(config: any, host: string | null): string[] {
);
}

function getEmailsByDomain(domains: string[], emails: string[]) {
return emails.reduce((result, email) => {
const domain = email.slice(email.indexOf('@') + 1);
const emails = result[domain] ?? [];

emails.push(email);
result[domain] = emails;

return result;
}, Object.fromEntries(domains.map<[string, string[]]>((domain) => [domain, []])));
}

function deriveConfigByDomain(config: any): Record<string, Config> | null {
if (config === null) {
return null;
Expand Down

0 comments on commit 39a8f56

Please sign in to comment.