-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix getting currently connected dapp info (#3671)
Resolves #3661 ### What - Add a reusable hook that will handle finding a browser tab currently connected to the wallet to make it easier to display info about connected dapp. - Don't use `lastFocusedWindow` argument so window popup (token spend approval) can use the same hook for finding connected dapp. ![image](https://github.com/tahowallet/extension/assets/20949277/ea628b35-a7f0-40e0-a649-87457c853a75) ### Testing steps - [x] make sure wallet displays info about connected dapp in the top header when your active tab is the dapp's tab ![image](https://github.com/tahowallet/extension/assets/20949277/317fae15-9f7f-484d-9f3c-2c9890d6d322) - [x] make sure you can see the dapp avatar on the token approval screen Latest build: [extension-builds-3671](https://github.com/tahowallet/extension/suites/18184789698/artifacts/1049527106) (as of Tue, 14 Nov 2023 17:55:43 GMT).
- Loading branch information
Showing
3 changed files
with
64 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { PermissionRequest } from "@tallyho/provider-bridge-shared" | ||
import { useCallback, useEffect, useState } from "react" | ||
import { browser } from "@tallyho/tally-background" | ||
import { selectAllowedPages } from "@tallyho/tally-background/redux-slices/selectors" | ||
import { useBackgroundSelector } from "./redux-hooks" | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export function useDappPermission(): { | ||
isConnected: boolean | ||
currentPermission: PermissionRequest | undefined | ||
allowedPages: PermissionRequest[] | ||
} { | ||
const [isConnected, setisConnected] = useState(false) | ||
const [currentPermission, setCurrentPermission] = useState< | ||
PermissionRequest | undefined | ||
>(undefined) | ||
|
||
const allowedPages = useBackgroundSelector(selectAllowedPages) | ||
|
||
const initPermissionAndOrigin = useCallback(async () => { | ||
const { url } = await browser.tabs | ||
.query({ | ||
active: true, | ||
}) | ||
.then((tabs) => | ||
tabs[0] ? tabs[0] : { url: "", favIconUrl: "", title: "" }, | ||
) | ||
|
||
if (!url) return | ||
|
||
const { origin } = new URL(url) | ||
|
||
const allowPermission = allowedPages.find( | ||
(permission) => permission.origin === origin, | ||
) | ||
|
||
if (allowPermission) { | ||
setCurrentPermission(allowPermission) | ||
setisConnected(true) | ||
} else { | ||
setisConnected(false) | ||
} | ||
}, [allowedPages, setCurrentPermission]) | ||
|
||
useEffect(() => { | ||
initPermissionAndOrigin() | ||
}, [initPermissionAndOrigin]) | ||
|
||
return { | ||
isConnected, | ||
currentPermission, | ||
allowedPages, | ||
} | ||
} |