Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wallet connection status with the site when user updated the site url in the existing tab #658

Merged
merged 1 commit into from
May 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ const updateOrigin = async (windowId: number) => {
}

const store = await getExistingMainStoreSingletonOrInit();
const state = store.getState();
const activeOrigin = state.activeOrigin;

const activeTabs = await browser.tabs.query({ active: true, windowId });
const tab0 = activeTabs[0];
Expand All @@ -166,42 +168,50 @@ const updateOrigin = async (windowId: number) => {
newActiveOrigin = getUrlOrigin(tab0.url) || null;
}

store.dispatch(activeOriginChanged(newActiveOrigin));

const state = store.getState();
const activeAccount = selectVaultActiveAccount(state);

if (newActiveOrigin && activeAccount) {
const isLocked = selectVaultIsLocked(state);
const isActiveAccountConnected = selectIsAccountConnected(
state,
newActiveOrigin,
activeAccount.name
);

emitSdkEventToActiveTabsWithOrigin(
newActiveOrigin,
sdkEvent.changedTab({
isLocked: isLocked,
isConnected: isLocked ? undefined : isActiveAccountConnected,
activeKey:
!isLocked && isActiveAccountConnected
? activeAccount.publicKey
: undefined
})
);
if (activeOrigin !== newActiveOrigin) {
store.dispatch(activeOriginChanged(newActiveOrigin));

const activeAccount = selectVaultActiveAccount(state);

if (newActiveOrigin && activeAccount) {
const isLocked = selectVaultIsLocked(state);
const isActiveAccountConnected = selectIsAccountConnected(
state,
newActiveOrigin,
activeAccount.name
);

emitSdkEventToActiveTabsWithOrigin(
newActiveOrigin,
sdkEvent.changedTab({
isLocked: isLocked,
isConnected: isLocked ? undefined : isActiveAccountConnected,
activeKey:
!isLocked && isActiveAccountConnected
? activeAccount.publicKey
: undefined
})
);
}
}
};

browser.windows.onFocusChanged.addListener(async (windowId: number) => {
updateOrigin(windowId);
});

browser.tabs.onActivated.addListener(
async ({ windowId, tabId }: browser.Tabs.OnActivatedActiveInfoType) => {
updateOrigin(windowId);
}
);

browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo && changeInfo.url && tab.windowId) {
updateOrigin(tab.windowId);
}
});

// NOTE: if two events are send at the same time (same function) it must reuse the same store instance
browser.runtime.onMessage.addListener(
async (action: RootAction | SdkMethod | ServiceMessage, sender) => {
Expand Down