Skip to content

Commit 8bd66ba

Browse files
committed
feat(profile): automatically switch to selected account
Without this patch, selecting any other account other than your connected EOA using the account selector doesn't do anything on your profile page. However, we want people to be able to cycle through their accounts, so whenever another account is selected via the account selector, we want the profile page to redirect to that account. On the other hand we don't want the profile to automatically switch when the user is on a profile page that is not his connected EOA or one of the multisig they're a signer on. In these cases the selector will do nothing when the account selector is used.
1 parent 720b5ff commit 8bd66ba

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

app/profile/[address]/page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { HypercertsTabContent } from "@/app/profile/[address]/hypercerts-tab-con
88
import { CollectionsTabContent } from "@/app/profile/[address]/collections-tab-content";
99
import { MarketplaceTabContent } from "@/app/profile/[address]/marketplace-tab-content";
1010
import { BlueprintsTabContent } from "@/app/profile/[address]/blueprint-tab-content";
11-
1211
import { ContractAccountBanner } from "@/components/profile/contract-accounts-banner";
12+
import { ProfileAccountSwitcher } from "@/components/profile/account-switcher";
13+
1314
export default function ProfilePage({
1415
params,
1516
searchParams,
@@ -24,6 +25,7 @@ export default function ProfilePage({
2425
return (
2526
<section className="flex flex-col gap-2">
2627
<ContractAccountBanner address={address} />
28+
<ProfileAccountSwitcher address={address} />
2729
<section className="flex flex-wrap gap-2 items-center">
2830
<h1 className="font-serif text-3xl lg:text-5xl tracking-tight">
2931
Profile
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use client";
2+
3+
import { useAccount } from "wagmi";
4+
import { useEffect } from "react";
5+
import { useRouter } from "next/navigation";
6+
7+
import { useAccountStore } from "@/lib/account-store";
8+
import { useSafeAccounts } from "@/hooks/useSafeAccounts";
9+
10+
export function ProfileAccountSwitcher({ address }: { address: string }) {
11+
const { address: connectedAddress } = useAccount();
12+
const { safeAccounts } = useSafeAccounts();
13+
const router = useRouter();
14+
const selectedAccount = useAccountStore((state) => state.selectedAccount);
15+
16+
useEffect(() => {
17+
if (!selectedAccount || !connectedAddress) return;
18+
19+
const currentAddress = address.toLowerCase();
20+
const accounts = [
21+
{ type: "eoa", address: connectedAddress },
22+
...safeAccounts,
23+
];
24+
25+
// Find current account index
26+
const currentIndex = accounts.findIndex(
27+
(account) => account.address.toLowerCase() === currentAddress,
28+
);
29+
30+
// If current address matches the connected address or a safe address the user is a signer on,
31+
// and it's not the selected account, redirect to the selected account
32+
if (
33+
currentIndex !== -1 &&
34+
currentAddress !== selectedAccount.address.toLowerCase()
35+
) {
36+
router.push(`/profile/${selectedAccount.address}`);
37+
}
38+
}, [selectedAccount, address, connectedAddress, safeAccounts, router]);
39+
40+
return null;
41+
}

0 commit comments

Comments
 (0)