Skip to content

Commit 9510806

Browse files
committed
fix: defer wallet state until client mount
1 parent 420855c commit 9510806

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

app/auth-layout.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@
22

33
import { UnauthorizedPanel } from '@/components/UnauthorizedPanel';
44
import { useAccount } from 'wagmi';
5+
import { useEffect, useState } from 'react';
56

67
export default function AuthLayout({ children }: { children: React.ReactNode }) {
78
const { address } = useAccount();
9+
const [mounted, setMounted] = useState(false);
10+
11+
useEffect(() => {
12+
setMounted(true);
13+
}, []);
14+
15+
if (!mounted) {
16+
return <UnauthorizedPanel />;
17+
}
18+
819
return <>{address ? children : <UnauthorizedPanel />}</>;
920
}

components/Sidebar/SafeWalletSelect.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ import { SidebarMenu, SidebarMenuButton, SidebarMenuItem } from '@/components/ui
1616
import { getNetworkIcon } from '@/components/BlockChainNetworkIcon';
1717
import { useAccount, useChainId } from 'wagmi';
1818
import { useSafeAccount } from '@/features/safe-sdk/hooks/use-safe-account';
19+
import { useEffect, useState } from 'react';
1920

2021
export function SafeWalletSelect() {
2122
const { safeAccountAddresses, selectedSafeAccountAddress, setSelectedSafeAccountAddress } = useSafeAccount();
2223
const { address } = useAccount();
2324
const chainId = useChainId();
2425
const router = useRouter();
26+
const [mounted, setMounted] = useState(false);
27+
28+
useEffect(() => {
29+
setMounted(true);
30+
}, []);
2531

2632
const handleSafeSelect = (safeAddress: string) => {
2733
setSelectedSafeAccountAddress(safeAddress);
@@ -78,7 +84,7 @@ export function SafeWalletSelect() {
7884
};
7985

8086
// Hardware wallet not connected state
81-
if (!address) {
87+
if (!mounted || !address) {
8288
return (
8389
<SidebarMenu>
8490
<SidebarMenuItem>

features/safe-sdk/providers/safe-sdk-provider.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,30 @@ export function SafeSdkProvider({ children }: { children: ReactNode }) {
2020
const { address } = useAccount();
2121
const { data: walletClient } = useWalletClient();
2222
const chainId = useChainId();
23+
const [mounted, setMounted] = useState(false);
2324

24-
const isConnected = !!address;
25+
useEffect(() => {
26+
setMounted(true);
27+
}, []);
28+
29+
const isConnected = mounted && !!address;
2530

2631
const safeApiService = useMemo(() => {
27-
if (isConnected && chainId) {
32+
if (mounted && isConnected && chainId) {
2833
return new SafeApiService({
2934
chainId: chainId as number,
3035
apiKey: process.env.NEXT_PUBLIC_SAFE_GLOBAL_API_KEY || '',
3136
});
3237
}
3338
return null;
34-
}, [isConnected, chainId]);
39+
}, [mounted, isConnected, chainId]);
3540

3641
const [safeClient, setSafeClient] = useState<SafeClient | null>(null);
3742
const [safeAccountAddresses, setSafeAccountAddresses] = useState<string[]>([]);
3843
const [selectedSafeAccountAddress, setSelectedSafeAccountAddress] = useState<string | null>(null);
3944

4045
useEffect(() => {
46+
if (!mounted) return;
4147
if (safeApiService && address) {
4248
safeApiService.getSafesByOwner(address).then((response) => {
4349
const safes = response.safes || [];
@@ -51,15 +57,16 @@ export function SafeSdkProvider({ children }: { children: ReactNode }) {
5157
} else {
5258
cleanSafeContext();
5359
}
54-
}, [safeApiService, address]);
60+
}, [mounted, safeApiService, address]);
5561

5662
useEffect(() => {
63+
if (!mounted) return;
5764
if (selectedSafeAccountAddress) {
5865
createSafeClientByExistSafeAccount();
5966
} else {
6067
setSafeClient(null);
6168
}
62-
}, [selectedSafeAccountAddress]);
69+
}, [mounted, selectedSafeAccountAddress]);
6370

6471
const createSafeClientByExistSafeAccount = async () => {
6572
try {

0 commit comments

Comments
 (0)