Skip to content

Commit fda26db

Browse files
Remove auto url param on portfolio (#1827)
* Remove auto url param on portfolio * Revert "Remove auto url param on portfolio" This reverts commit 665b6c9. * Make changing wallets update the route --------- Co-authored-by: Danny Delott <[email protected]>
1 parent b7fe09b commit fda26db

File tree

1 file changed

+68
-39
lines changed

1 file changed

+68
-39
lines changed
Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Navigate, useNavigate, useSearch } from "@tanstack/react-router";
2-
import { ReactElement } from "react";
2+
import { ReactElement, useEffect, useMemo } from "react";
3+
import { usePrevious } from "react-use";
34
import { Tabs } from "src/ui/base/components/Tabs/Tabs";
45
import { OpenLongsContainer } from "src/ui/portfolio/longs/LongsContainer";
56
import { LpAndWithdrawalSharesContainer } from "src/ui/portfolio/lp/LpAndWithdrawalSharesContainer";
@@ -27,48 +28,54 @@ export function Portfolio(): ReactElement {
2728

2829
const navigate = useNavigate({ from: PORTFOLIO_ROUTE });
2930

30-
const tabs = [
31-
{
32-
id: "longs",
33-
content: <OpenLongsContainer account={account} />,
34-
label: "Long",
35-
onClick: () => {
36-
navigate({
37-
search: (prev) => ({ ...prev, position: "longs" }),
38-
});
31+
// Update the search params if the user switches wallets
32+
useSyncRouteWithAccountChange();
33+
34+
const tabs = useMemo(
35+
() => [
36+
{
37+
id: "longs",
38+
content: <OpenLongsContainer account={account} />,
39+
label: "Long",
40+
onClick: () => {
41+
navigate({
42+
search: (prev) => ({ ...prev, position: "longs" }),
43+
});
44+
},
3945
},
40-
},
41-
{
42-
id: "shorts",
43-
content: <OpenShortsContainer account={account} />,
44-
label: "Short",
45-
onClick: () => {
46-
navigate({
47-
search: (prev) => ({ ...prev, position: "shorts" }),
48-
});
46+
{
47+
id: "shorts",
48+
content: <OpenShortsContainer account={account} />,
49+
label: "Short",
50+
onClick: () => {
51+
navigate({
52+
search: (prev) => ({ ...prev, position: "shorts" }),
53+
});
54+
},
4955
},
50-
},
51-
{
52-
id: "lp",
53-
content: <LpAndWithdrawalSharesContainer account={account} />,
54-
label: "LP",
55-
onClick: () => {
56-
navigate({
57-
search: (prev) => ({ ...prev, position: "lp" }),
58-
});
56+
{
57+
id: "lp",
58+
content: <LpAndWithdrawalSharesContainer account={account} />,
59+
label: "LP",
60+
onClick: () => {
61+
navigate({
62+
search: (prev) => ({ ...prev, position: "lp" }),
63+
});
64+
},
5965
},
60-
},
61-
{
62-
id: "rewards",
63-
content: <RewardsContainer account={account} />,
64-
label: "Rewards",
65-
onClick: () => {
66-
navigate({
67-
search: (prev) => ({ ...prev, position: "rewards" }),
68-
});
66+
{
67+
id: "rewards",
68+
content: <RewardsContainer account={account} />,
69+
label: "Rewards",
70+
onClick: () => {
71+
navigate({
72+
search: (prev) => ({ ...prev, position: "rewards" }),
73+
});
74+
},
6975
},
70-
},
71-
];
76+
],
77+
[account, navigate],
78+
);
7279

7380
return (
7481
<div className="flex w-full flex-col items-center bg-base-100 py-8">
@@ -82,3 +89,25 @@ export function Portfolio(): ReactElement {
8289
</div>
8390
);
8491
}
92+
/**
93+
* Updates the search params if the user switches wallets
94+
*/
95+
function useSyncRouteWithAccountChange() {
96+
const { address: connectedAccount } = useAccount();
97+
const navigate = useNavigate({ from: PORTFOLIO_ROUTE });
98+
const prevConnectedAccount = usePrevious(connectedAccount);
99+
100+
// If the user switches wallets, it should update the search params as it
101+
// takes precedent over whatever account is currently being displayed, be it
102+
// the previously connected account or the one from the route.
103+
const walletChanged =
104+
prevConnectedAccount && connectedAccount !== prevConnectedAccount;
105+
106+
useEffect(() => {
107+
if (walletChanged) {
108+
navigate({
109+
search: (prev) => ({ ...prev, account: connectedAccount }),
110+
});
111+
}
112+
}, [connectedAccount, navigate, prevConnectedAccount, walletChanged]);
113+
}

0 commit comments

Comments
 (0)