-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #495 from secretkeylabs/vic/guards
feat: add guard for import ledger page
- Loading branch information
Showing
7 changed files
with
106 additions
and
36 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
30 changes: 0 additions & 30 deletions
30
src/app/components/guards/onboarding/useOnboardingSingleton.ts
This file was deleted.
Oops, something went wrong.
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,66 @@ | ||
import { useEffect } from 'react'; | ||
|
||
type GuardType = 'onboarding' | 'importLedger' | 'closeWallet'; | ||
|
||
const getChannelAndPingNames = (guardName: GuardType) => { | ||
const channelName = `${guardName}Channel`; | ||
const pingName = `${guardName}Ping`; | ||
return { channelName, pingName }; | ||
}; | ||
|
||
// NOTE: This is for calling the guard from a non-component context | ||
// The window that is sending it will also receive it if the channel it is broadcasting to is open | ||
// This is predominantly made for the reset wallet guard to close all tabs | ||
// Use with care. | ||
export const PostGuardPing = (guardName: GuardType): void => { | ||
const { channelName, pingName } = getChannelAndPingNames(guardName); | ||
const broadcastChannel = new BroadcastChannel(channelName); | ||
broadcastChannel.postMessage(pingName); | ||
broadcastChannel.close(); | ||
}; | ||
|
||
/** | ||
* This hook is used to ensure that only one window with the guard name is open at a time. | ||
* It fires off an event only once on its first render and will close the window if it receives | ||
* the event. | ||
*/ | ||
export const useSingleTabGuard = (guardName: GuardType, broadcastOnLoad = true): void => { | ||
const { channelName, pingName } = getChannelAndPingNames(guardName); | ||
|
||
useEffect(() => { | ||
const broadcastChannel = new BroadcastChannel(channelName); | ||
|
||
broadcastChannel.onmessage = (message) => { | ||
if (message.data !== pingName) { | ||
return; | ||
} | ||
|
||
broadcastChannel.close(); | ||
window.close(); | ||
}; | ||
|
||
if (broadcastOnLoad) { | ||
broadcastChannel.postMessage(pingName); | ||
} | ||
|
||
return () => { | ||
broadcastChannel.close(); | ||
}; | ||
}, []); | ||
}; | ||
|
||
type SingleTabProps = { | ||
guardName: GuardType; | ||
children?: React.ReactElement | React.ReactElement[]; | ||
}; | ||
|
||
/** | ||
* This guard is used to ensure that only one window with the guard name is open at a time. | ||
* It fires off an event only once on its first render and will close the window if it receives | ||
* the event. | ||
*/ | ||
export function SingleTabGuard({ guardName, children }: SingleTabProps): React.ReactNode { | ||
useSingleTabGuard(guardName); | ||
|
||
return children; | ||
} |
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,17 @@ | ||
import { useSingleTabGuard } from '@components/guards/singleTab'; | ||
|
||
type WalletCloseGuardProps = { | ||
children?: React.ReactElement | React.ReactElement[]; | ||
}; | ||
|
||
/** | ||
* This guard is used to close any open tabs when the wallet is locked or reset. | ||
* It should only be rendered at the root of the options page. | ||
*/ | ||
function WalletCloseGuard({ children }: WalletCloseGuardProps): React.ReactNode { | ||
useSingleTabGuard('closeWallet', false); | ||
|
||
return children; | ||
} | ||
|
||
export default WalletCloseGuard; |
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