Skip to content

Commit

Permalink
chore: simplify toolbar button configuration
Browse files Browse the repository at this point in the history
fix #943, removes options for hiding toolbar button badge completely, and to open Pontoon home page regardless of the locale team selected
  • Loading branch information
MikkCZ committed Oct 8, 2024
1 parent 11a3275 commit e7bf214
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 29 deletions.
19 changes: 4 additions & 15 deletions src/background/toolbarButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ async function registerBadgeChanges() {
}
},
);
listenToOptionChange('display_toolbar_button_badge', () => {
updateBadge();
});
await updateBadge();
}

Expand All @@ -88,9 +85,6 @@ async function buttonClickHandler() {
switch (action) {
case 'popup':
break;
case 'home-page':
await openNewPontoonTab(await getOneOption('pontoon_base_url'));
break;
case 'team-page':
await openNewPontoonTab(pontoonTeam(pontoonBaseUrl, { code: teamCode }));
break;
Expand All @@ -105,7 +99,6 @@ function registerButtonPopup(action: OptionValue<'toolbar_button_action'>) {
case 'popup':
popup = getResourceUrl('frontend/toolbar-button.html');
break;
case 'home-page':
case 'team-page':
popup = '';
break;
Expand All @@ -131,14 +124,10 @@ async function updateBadge(
notificationsDataLoadingState === 'loaded') &&
typeof notificationsData === 'object'
) {
if (await getOneOption('display_toolbar_button_badge')) {
const unreadNotificationsCount = Object.values(notificationsData).filter(
(n) => n.unread,
).length;
await loadedBadge(unreadNotificationsCount);
} else {
await hideBadge();
}
const unreadNotificationsCount = Object.values(notificationsData).filter(
(n) => n.unread,
).length;
await loadedBadge(unreadNotificationsCount);
} else if (notificationsDataLoadingState === 'loading') {
await loadingBadge();
} else {
Expand Down
15 changes: 12 additions & 3 deletions src/commons/data/defaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { browser, BrowserFamily } from '../webExtensionsApi';
export interface OptionsContent {
locale_team: string;
data_update_interval: number;
display_toolbar_button_badge: boolean;
toolbar_button_action: 'popup' | 'team-page' | 'home-page';
toolbar_button_action: 'popup' | 'team-page';
toolbar_button_popup_always_hide_read_notifications: boolean;
show_notifications: boolean;
contextual_identity: string;
Expand All @@ -16,12 +15,22 @@ const generalDefaultOptions: Omit<OptionsContent, 'contextual_identity'> = {
locale_team: browser.i18n.getUILanguage(),
pontoon_base_url: DEFAULT_PONTOON_BASE_URL,
data_update_interval: 15,
display_toolbar_button_badge: true,
toolbar_button_action: 'popup',
toolbar_button_popup_always_hide_read_notifications: false,
show_notifications: true,
};

export function coalesceLegacyValues<K extends keyof OptionsContent>(
id: K,
value: OptionsContent[K],
): OptionsContent[K] {
if (id === 'toolbar_button_action' && value === 'home-page') {
return 'team-page' as OptionsContent[K];
} else {
return value;
}
}

export function defaultOptionsFor(
browserFamily: BrowserFamily,
): OptionsContent {
Expand Down
1 change: 1 addition & 0 deletions src/commons/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest.mock('./data/defaultOptions', () => ({
defaultOptionsFor: () => ({
locale_team: 'en',
}),
coalesceLegacyValues: (_: unknown, value: unknown) => value,
}));

beforeEach(() => {
Expand Down
9 changes: 6 additions & 3 deletions src/commons/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Storage } from 'webextension-polyfill';

import { browser, browserFamily } from './webExtensionsApi';
import type { OptionsContent } from './data/defaultOptions';
import { defaultOptionsFor } from './data/defaultOptions';
import { coalesceLegacyValues, defaultOptionsFor } from './data/defaultOptions';

export type OptionId = keyof OptionsContent;

Expand All @@ -11,7 +11,7 @@ type OptionValues<ID extends OptionId> = Pick<OptionsContent, ID>;
export type OptionValue<ID extends OptionId> = OptionValues<ID>[ID];

interface OptionChange<ID extends OptionId> extends Storage.StorageChange {
oldValue?: OptionValue<ID>;
oldValue?: never;
newValue: OptionValue<ID>;
}

Expand All @@ -37,7 +37,10 @@ export async function getOptions<ID extends OptionId>(
for (const optionId of optionIds) {
const storageKey = storageKeyFor(optionId);
if (typeof storageItems[storageKey] !== 'undefined') {
optionsWithDefaultValues[optionId] = storageItems[storageKey];
optionsWithDefaultValues[optionId] = coalesceLegacyValues(
optionId,
storageItems[storageKey],
);
} else {
optionsWithDefaultValues[optionId] = defaultValues[optionId];
}
Expand Down
5 changes: 0 additions & 5 deletions src/frontend/options/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ export const App: React.FC = () => {
</Section>
<Section>
<Heading3>Toolbar button</Heading3>
<div>
<Checkbox optionKey="display_toolbar_button_badge">
Show the number of unread notifications in the toolbar button
</Checkbox>
</div>
<ToolbarButtonActionSelection />
<div>
<Checkbox optionKey="toolbar_button_popup_always_hide_read_notifications">
Expand Down
3 changes: 0 additions & 3 deletions src/frontend/options/ToolbarButtonActionSelection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ const options: {
'team-page': {
label: 'Open your locale team page',
},
'home-page': {
label: 'Open your Pontoon homepage',
},
};

export const ToolbarButtonActionSelection: React.FC = () => {
Expand Down

0 comments on commit e7bf214

Please sign in to comment.