Skip to content

Commit

Permalink
Improve performance simplifying code
Browse files Browse the repository at this point in the history
  • Loading branch information
sembrestels committed Jun 23, 2024
1 parent a26c6c4 commit 24b9744
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
1 change: 0 additions & 1 deletion packages/evmcrispr-terminal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"@react-spring/web": "^9.7.3",
"@safe-global/safe-apps-provider": "^0.18.3",
"@safe-global/safe-apps-sdk": "^9.1.0",
"@safe-global/safe-apps-react-sdk": "^4.7.2",
"@tanstack/react-query": "^5.45.0",
"@types/lodash.debounce": "^4.0.9",
"@udecode/zustood": "^2.0.0",
Expand Down
9 changes: 3 additions & 6 deletions packages/evmcrispr-terminal/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
createHashRouter,
createRoutesFromElements,
} from 'react-router-dom';
import { SafeProvider } from '@safe-global/safe-apps-react-sdk';
import { ChakraProvider, DarkMode, extendTheme } from '@chakra-ui/react';

import theme from './theme';
Expand All @@ -33,11 +32,9 @@ const App = () => {
<ChakraProvider theme={extendTheme(theme)}>
<Fonts />
<DarkMode>
<SafeProvider>
<Wagmi>
<RouterProvider router={router} />
</Wagmi>
</SafeProvider>
<Wagmi>
<RouterProvider router={router} />
</Wagmi>
</DarkMode>
</ChakraProvider>
</div>
Expand Down
23 changes: 9 additions & 14 deletions packages/evmcrispr-terminal/src/hooks/useSafeConnection.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { useEffect } from 'react';
import { useAccount, useConnect } from 'wagmi';
import { useSafeAppsSDK } from '@safe-global/safe-apps-react-sdk';
import type { Connector } from 'wagmi';

export function useSafeConnection() {
const { address, connector: activeConnector } = useAccount();
const { safe, connected: isSafe } = useSafeAppsSDK();
const { connector: activeConnector } = useAccount();
const { connectors, connectAsync } = useConnect();

const isSafe = activeConnector?.id === 'safe';
useEffect(() => {
if (
isSafe &&
address !== safe.safeAddress &&
activeConnector?.id !== 'safe'
) {
const safeConnector = connectors.find((c) => c.id === 'safe');
if (safeConnector) {
connectAsync({ connector: safeConnector });
}
const safeConnector = connectors.find((c: Connector) => c.id === 'safe');
if (safeConnector && !isSafe && window.parent !== window) {
connectAsync({ connector: safeConnector }).catch(() => {
console.log('error connecting to safe');
});
}
}, [isSafe, connectors, address]);
}, [connectors, connectAsync, activeConnector, isSafe]);

return { isSafe };
}

0 comments on commit 24b9744

Please sign in to comment.