Skip to content

Commit

Permalink
fix: deviceConnect for safe (RabbyHub#2608)
Browse files Browse the repository at this point in the history
  • Loading branch information
heisenberg-2077 authored and christianholman committed Nov 20, 2024
1 parent d6c2de1 commit a731925
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/ui/utils/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const useApproval = () => {
approvalId?: string
) => {
// handle connect
if (!(await deviceConnect(data?.type))) {
if (!(await deviceConnect(data))) {
return;
}

Expand Down
29 changes: 17 additions & 12 deletions src/ui/utils/useDeviceConnect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { KEYRING_CLASS } from '@/constant';
import React from 'react';
import { useLedgerStatus } from '../component/ConnectStatus/useLedgerStatus';
import { useCommonPopupView } from './WalletContext';
import { useSessionStatus } from '../component/WalletConnect/useSessionStatus';
import { useCommonPopupView, useWallet } from './WalletContext';
import { useCurrentAccount } from '../hooks/backgroundState/useAccount';
import { useImKeyStatus } from '../component/ConnectStatus/useImKeyStatus';

Expand All @@ -12,29 +11,35 @@ import { useImKeyStatus } from '../component/ConnectStatus/useImKeyStatus';
*/
export const useDeviceConnect = () => {
const ledgerStatus = useLedgerStatus();
const account = useCurrentAccount();
const walletConnectStatus = useSessionStatus(account!);
const imKeyStatus = useImKeyStatus();
const { activePopup, setAccount } = useCommonPopupView();
const wallet = useWallet();
const currentAccount = useCurrentAccount();

/**
* @returns {boolean} true if connected, false if not connected and popup is shown
*/
const connect = React.useCallback(
async (type: string) => {
async (data: any) => {
if (!data) return;
const { type, account, isGnosis } = data;

if (type === KEYRING_CLASS.HARDWARE.LEDGER) {
if (ledgerStatus.status === 'DISCONNECTED') {
activePopup('Ledger');
return false;
}
} else if (type === KEYRING_CLASS.WALLETCONNECT) {
if (
!walletConnectStatus.status ||
walletConnectStatus.status === 'DISCONNECTED'
) {
if (account) {
const acc = isGnosis ? account : currentAccount;
const status = await wallet.getWalletConnectSessionStatus(
acc.address,
acc.brandName
);

if (!status || status === 'DISCONNECTED') {
if (acc) {
setAccount({
...account,
...acc,
type,
});
}
Expand All @@ -50,7 +55,7 @@ export const useDeviceConnect = () => {

return true;
},
[ledgerStatus, walletConnectStatus, imKeyStatus, account]
[ledgerStatus, imKeyStatus]
);

return connect;
Expand Down

0 comments on commit a731925

Please sign in to comment.