Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass account to asset page #7882

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-hornets-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ledger-live-desktop": patch
---

pass from and default accounts to all instances of redirect to /swap
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import React, { useCallback, useRef } from "react";
import { useTranslation } from "react-i18next";
import { AccountLike, ValueChange } from "@ledgerhq/types-live";
import { flattenAccounts } from "@ledgerhq/live-common/account/index";
import { getAvailableAccountsById } from "@ledgerhq/live-common/exchange/swap/utils/index";
import { useFeature } from "@ledgerhq/live-common/featureFlags/index";
import { Text } from "@ledgerhq/react-ui";
import { Unit } from "@ledgerhq/types-cryptoassets";
import { AccountLike, ValueChange } from "@ledgerhq/types-live";
import React, { useCallback, useMemo, useRef } from "react";
import { useTranslation } from "react-i18next";
import { useSelector } from "react-redux";
import { useHistory } from "react-router-dom";
import { setTrackingSource } from "~/renderer/analytics/TrackPage";
import Box from "~/renderer/components/Box";
import Button from "~/renderer/components/ButtonV3";
import FormattedVal from "~/renderer/components/FormattedVal";
import PillsDaysCount from "~/renderer/components/PillsDaysCount";
import TransactionsPendingConfirmationWarning from "~/renderer/components/TransactionsPendingConfirmationWarning";
import { useResize } from "~/renderer/hooks/useResize";
import { accountsSelector } from "~/renderer/reducers/accounts";
import { useGetSwapTrackingProperties } from "~/renderer/screens/exchange/Swap2/utils/index";
import { PlaceholderLine } from "./Placeholder";
import Button from "~/renderer/components/ButtonV3";
import { setTrackingSource } from "~/renderer/analytics/TrackPage";
import { useHistory } from "react-router-dom";
import { useFeature } from "@ledgerhq/live-common/featureFlags/index";
import { Text } from "@ledgerhq/react-ui";
import PilldDaysSelect from "../PillsDaysSelect";
import { useResize } from "~/renderer/hooks/useResize";
import { PlaceholderLine } from "./Placeholder";

type BalanceSinceProps = {
valueChange: ValueChange;
Expand All @@ -35,6 +39,7 @@ type BalanceTotalProps = {
};
type Props = {
unit: Unit;
counterValueId?: string;
} & BalanceSinceProps;
export function BalanceDiff({ valueChange, unit, isAvailable, ...boxProps }: Props) {
if (!isAvailable) return null;
Expand Down Expand Up @@ -115,11 +120,28 @@ export function BalanceTotal({
</Box>
);
}
export default function BalanceInfos({ totalBalance, valueChange, isAvailable, unit }: Props) {
export default function BalanceInfos({
totalBalance,
valueChange,
isAvailable,
unit,
counterValueId,
}: Props) {
const swapDefaultTrack = useGetSwapTrackingProperties();
const { t } = useTranslation();
const history = useHistory();

const allAccounts = useSelector(accountsSelector);
const flattenedAccounts = useMemo(() => flattenAccounts(allAccounts), [allAccounts]);

const defaultAccount = useMemo(
() =>
counterValueId
? getAvailableAccountsById(counterValueId, flattenedAccounts).find(Boolean)
: undefined,
[counterValueId, flattenedAccounts],
);

// Remove "SWAP" and "BUY" redundant buttons when portafolio exchange banner is available
const portfolioExchangeBanner = useFeature("portfolioExchangeBanner");
const onBuy = useCallback(() => {
Expand All @@ -135,8 +157,12 @@ export default function BalanceInfos({ totalBalance, valueChange, isAvailable, u
setTrackingSource("Page Portfolio");
history.push({
pathname: "/swap",
state: {
from: history.location.pathname,
defaultAccount,
},
});
}, [history]);
}, [history, defaultAccount]);

const ref = useRef<HTMLDivElement>(null);
const { width } = useResize(ref);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export default function AccountContextMenu({
defaultCurrency: currency,
defaultAccount: account,
defaultParentAccount: parentAccount,
from: history.location.pathname,
},
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const NoFundsStakeModal = ({ account, parentAccount, entryPoint }: NoFundsStakeM
defaultCurrency: currency,
defaultAccount: account,
defaultParentAccount: parentAccount,
from: history.location.pathname,
},
});
}, [currency, account, parentAccount, history, dispatch]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ const AccountHeaderActions = ({ account, parentAccount, openModal }: Props) => {
defaultCurrency: currency,
defaultAccount: account,
defaultParentAccount: parentAccount,
from: history.location.pathname,
},
});
}, [currency, swapDefaultTrack, history, account, parentAccount, buttonSharedTrackingFields]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { useRampCatalog } from "@ledgerhq/live-common/platform/providers/RampCat
import { useFeature } from "@ledgerhq/live-common/featureFlags/index";
import useStakeFlow from "~/renderer/screens/stake";
import { stakeDefaultTrack } from "~/renderer/screens/stake/constants";
import { BalanceHistoryWithCountervalue, ValueChange } from "@ledgerhq/types-live";
import { AccountLike, BalanceHistoryWithCountervalue, ValueChange } from "@ledgerhq/types-live";
import { useFetchCurrencyAll } from "@ledgerhq/live-common/exchange/swap/hooks/index";
type Props = {
isAvailable: boolean;
Expand All @@ -31,6 +31,7 @@ type Props = {
countervalueFirst: boolean;
currency: CryptoCurrency | TokenCurrency;
unit: Unit;
account: AccountLike;
};
export default function AssetBalanceSummaryHeader({
counterValue,
Expand All @@ -41,6 +42,7 @@ export default function AssetBalanceSummaryHeader({
countervalueFirst,
currency,
unit,
account,
}: Props) {
const { data: currenciesAll } = useFetchCurrencyAll();
const swapDefaultTrack = useGetSwapTrackingProperties();
Expand Down Expand Up @@ -106,10 +108,12 @@ export default function AssetBalanceSummaryHeader({
history.push({
pathname: "/swap",
state: {
defaultAccount: account,
defaultCurrency: currency,
from: history.location.pathname,
},
});
}, [currency, history, swapDefaultTrack]);
}, [currency, history, swapDefaultTrack, account]);

const onStake = useCallback(() => {
track("button_clicked2", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useCurrencyPortfolio, usePortfolio } from "~/renderer/actions/portfolio
import AssetBalanceSummaryHeader from "./AssetBalanceSummaryHeader";
import { discreetModeSelector } from "~/renderer/reducers/settings";
import { Data } from "~/renderer/components/Chart/types";
import { PortfolioRange } from "@ledgerhq/types-live";
import { AccountLike, PortfolioRange } from "@ledgerhq/types-live";
import PlaceholderChart from "~/renderer/components/PlaceholderChart";
import Alert from "~/renderer/components/Alert";
import { useTranslation } from "react-i18next";
Expand All @@ -24,6 +24,7 @@ type Props = {
unit: Unit;
range: PortfolioRange;
countervalueFirst: boolean;
account: AccountLike;
};
export default function BalanceSummary({
unit,
Expand All @@ -32,6 +33,7 @@ export default function BalanceSummary({
range,
chartColor,
currency,
account,
}: Props) {
const { t } = useTranslation();
const portfolio = usePortfolio();
Expand Down Expand Up @@ -85,6 +87,7 @@ export default function BalanceSummary({
<Card p={0} py={5}>
<Box px={6}>
<AssetBalanceSummaryHeader
account={account}
currency={currency}
unit={unit}
counterValue={counterValue}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function AssetPage({ match }: Props) {
<AssetHeader account={accounts[0]} parentAccount={parentAccount} />
</Box>
<BalanceSummary
account={accounts[0]}
key={currency.id}
countervalueFirst={countervalueFirst}
currency={currency}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function PortfolioBalanceSummary({ range, chartColor, counterValu
<Card p={0} py={5} grow>
<Box px={6}>
<BalanceInfos
counterValueId={counterValue.type !== "FiatCurrency" ? counterValue.id : undefined}
unit={counterValue.units[0]}
isAvailable={portfolio.balanceAvailable}
valueChange={portfolio.countervalueChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ const SwapWebAppWrapper = styled.div`
flex: 1;
`;

// remove the account id from the from path
function simplifyFromPath(path: string): string {
return path.replace(/^\/account.*/, "/account/{id}");
}

const SWAP_API_BASE = getEnv("SWAP_API_BASE");
const getSegWitAbandonSeedAddress = (): string => "bc1qed3mqr92zvq2s782aqkyx785u23723w02qfrgs";

Expand All @@ -112,7 +117,11 @@ const SwapWebView = ({ manifest, liveAppUnavailable }: SwapWebProps) => {
const accounts = useSelector(flattenAccountsSelector);
const { t } = useTranslation();
const swapDefaultTrack = useGetSwapTrackingProperties();
const { state } = useLocation<{ defaultAccount?: AccountLike; defaultParentAccount?: Account }>();
const { state } = useLocation<{
defaultAccount?: AccountLike;
defaultParentAccount?: Account;
from?: string;
}>();
const redirectToHistory = useRedirectToSwapHistory();

const { networkStatus } = useNetworkStatus();
Expand Down Expand Up @@ -283,8 +292,9 @@ const SwapWebView = ({ manifest, liveAppUnavailable }: SwapWebProps) => {
).id,
}
: {}),
...(state?.from ? { fromPath: simplifyFromPath(state.from) } : {}),
}).toString(),
[isOffline, state?.defaultAccount, state?.defaultParentAccount, walletState],
[isOffline, state?.defaultAccount, state?.defaultParentAccount, walletState, state?.from],
);

const onSwapWebviewError = (error?: SwapLiveError) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const useMarketActions = ({ currency, page, currenciesAll }: MarketAction
"parentId" in defaultAccount && defaultAccount.parentId
? flattenedAccounts.find(a => a.id === defaultAccount.parentId)
: null,
from: history.location.pathname,
},
});
}
Expand Down
Loading