Skip to content

Commit 26c3024

Browse files
runway-github[bot]n3psNidhiKJha
authored
release(runway): cherry-pick fix: feature flag sidepanel context menu cp-13.10.3 (#38234)
- fix: feature flag sidepanel context menu cp-13.10.3 (#38220) <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> fixes #38222 Wraps the sidepanel context menu configuration with the remote feature flag [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/38220?quickstart=1) ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: fix: feature flag sidepanel context menu ## **Related issues** Fixes: ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Moves sidepanel context menu setup to a new module and enables it only when supported and the remote `extensionUxSidepanel` flag allows it, initializing after background init. > > - **Background**: > - Extracts sidepanel context menu logic into `app/scripts/lib/sidepanel-context-menu.ts`. > - Creates/removes `contextMenus` item `openSidePanel` based on `IS_SIDEPANEL` and remote flag `remoteFeatureFlags.extensionUxSidepanel`. > - Opens `browser.sidePanel` on click for the current window. > - Subscribes to `RemoteFeatureFlagController:stateChange` to toggle menu dynamically. > - Registers initialization via `lazyListener.once('runtime','onInstalled')` in `app/scripts/background.js`, deferring to `handleSidePanelContextMenu()` which awaits `isInitialized` and calls `initSidePanelContextMenu(controller)`. > - Removes previous inline context menu setup from `background.js`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit d320129. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: NidhiKJha <[email protected]> [67d4f18](67d4f18) Co-authored-by: Francis Nepomuceno <[email protected]> Co-authored-by: NidhiKJha <[email protected]>
1 parent b7d90c6 commit 26c3024

File tree

2 files changed

+76
-21
lines changed

2 files changed

+76
-21
lines changed

app/scripts/background.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
CorruptionHandler,
5656
hasVault,
5757
} from './lib/state-corruption/state-corruption-recovery';
58+
import { initSidePanelContextMenu } from './lib/sidepanel-context-menu';
5859
import {
5960
backedUpStateKeys,
6061
PersistenceManager,
@@ -175,7 +176,10 @@ const ONE_SECOND_IN_MILLISECONDS = 1_000;
175176
// Timeout for initializing phishing warning page.
176177
const PHISHING_WARNING_PAGE_TIMEOUT = ONE_SECOND_IN_MILLISECONDS;
177178

178-
lazyListener.once('runtime', 'onInstalled').then(handleOnInstalled);
179+
lazyListener.once('runtime', 'onInstalled').then((details) => {
180+
handleOnInstalled(details);
181+
handleSidePanelContextMenu();
182+
});
179183

180184
/**
181185
* This deferred Promise is used to track whether initialization has finished.
@@ -1672,26 +1676,14 @@ function onInstall() {
16721676
platform.openExtensionInBrowser();
16731677
}
16741678
}
1675-
// Only register sidepanel context menu for browsers that support it (Chrome/Edge/Brave)
1676-
// and when the feature flag is enabled
1677-
if (
1678-
browser.contextMenus &&
1679-
browser.sidePanel &&
1680-
process.env.IS_SIDEPANEL?.toString() === 'true'
1681-
) {
1682-
browser.runtime.onInstalled.addListener(() => {
1683-
browser.contextMenus.create({
1684-
id: 'openSidePanel',
1685-
title: 'MetaMask Sidepanel',
1686-
contexts: ['all'],
1687-
});
1688-
});
1689-
browser.contextMenus.onClicked.addListener((info, tab) => {
1690-
if (info.menuItemId === 'openSidePanel') {
1691-
// This will open the panel in all the pages on the current window.
1692-
browser.sidePanel.open({ windowId: tab.windowId });
1693-
}
1694-
});
1679+
1680+
/**
1681+
* Handles the onInstalled event for sidepanel context menu creation.
1682+
* This is registered via lazyListener to catch the event at module load time.
1683+
*/
1684+
async function handleSidePanelContextMenu() {
1685+
await isInitialized;
1686+
await initSidePanelContextMenu(controller);
16951687
}
16961688

16971689
// // On first install, open a new tab with MetaMask
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import browser from 'webextension-polyfill';
2+
import type { RemoteFeatureFlagControllerState } from '@metamask/remote-feature-flag-controller';
3+
import type MetamaskController from '../metamask-controller';
4+
5+
const MENU_ITEM_ID = 'openSidePanel';
6+
7+
// Type augmentation for sidePanel API (not yet in webextension-polyfill types)
8+
type BrowserWithSidePanel = typeof browser & {
9+
sidePanel?: {
10+
open: (options: { windowId: number }) => Promise<void>;
11+
};
12+
};
13+
14+
export async function initSidePanelContextMenu(
15+
controller: MetamaskController,
16+
): Promise<void> {
17+
const browserWithSidePanel = browser as BrowserWithSidePanel;
18+
19+
if (
20+
!browser.contextMenus ||
21+
!browserWithSidePanel.sidePanel ||
22+
process.env.IS_SIDEPANEL?.toString() !== 'true'
23+
) {
24+
return;
25+
}
26+
27+
const isEnabled = (state?: {
28+
remoteFeatureFlags?: { extensionUxSidepanel?: boolean };
29+
}) => state?.remoteFeatureFlags?.extensionUxSidepanel !== false;
30+
31+
const createMenu = () => {
32+
browser.contextMenus.create({
33+
id: MENU_ITEM_ID,
34+
title: 'MetaMask Sidepanel',
35+
contexts: ['all'],
36+
});
37+
};
38+
39+
const removeMenu = () => {
40+
browser.contextMenus.remove(MENU_ITEM_ID);
41+
};
42+
43+
if (isEnabled(controller?.remoteFeatureFlagController?.state)) {
44+
createMenu();
45+
}
46+
47+
browser.contextMenus.onClicked.addListener((info, tab) => {
48+
if (info.menuItemId === MENU_ITEM_ID && tab?.windowId) {
49+
browserWithSidePanel.sidePanel?.open({ windowId: tab.windowId });
50+
}
51+
});
52+
53+
controller?.controllerMessenger?.subscribe(
54+
'RemoteFeatureFlagController:stateChange',
55+
(state: RemoteFeatureFlagControllerState) => {
56+
if (isEnabled(state)) {
57+
createMenu();
58+
} else {
59+
removeMenu();
60+
}
61+
},
62+
);
63+
}

0 commit comments

Comments
 (0)