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(lld): fix export to csv account name #7813

Merged
merged 1 commit into from
Sep 13, 2024
Merged
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
6 changes: 6 additions & 0 deletions .changeset/thick-glasses-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"ledger-live-desktop": patch
"@ledgerhq/live-common": patch
---

fix issue following CVS migration. The export didn't include account name
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ipcRenderer } from "electron";
import React, { memo, useState, useCallback } from "react";
import { Trans } from "react-i18next";
import { connect } from "react-redux";
import { connect, useSelector } from "react-redux";
import styled from "styled-components";
import { createStructuredSelector } from "reselect";
import { useCountervaluesState } from "@ledgerhq/live-countervalues-react";
Expand All @@ -23,6 +23,7 @@ import IconCheckCircle from "~/renderer/icons/CheckCircle";
import Alert from "~/renderer/components/Alert";
import { ModalData } from "../types";
import { useTechnicalDateFn } from "~/renderer/hooks/useDateFormatter";
import { walletSelector } from "~/renderer/reducers/wallet";

type OwnProps = {};
type Props = OwnProps & {
Expand Down Expand Up @@ -57,6 +58,8 @@ function ExportOperations({ accounts, closeModal, countervalueCurrency }: Props)
const [success, setSuccess] = useState(false);
const countervalueState = useCountervaluesState();
const getDateTxt = useTechnicalDateFn();
const walletState = useSelector(walletSelector);

const exportCsv = useCallback(async () => {
const path = await ipcRenderer.invoke("show-save-dialog", {
title: "Exported account transactions",
Expand All @@ -75,13 +78,14 @@ function ExportOperations({ accounts, closeModal, countervalueCurrency }: Props)
accounts.filter(account => checkedIds.includes(account.id)),
countervalueCurrency,
countervalueState,
walletState,
),
() => {
setSuccess(true);
},
);
}
}, [accounts, checkedIds, countervalueCurrency, countervalueState, getDateTxt]);
}, [accounts, checkedIds, countervalueCurrency, countervalueState, getDateTxt, walletState]);
const onClose = useCallback(() => closeModal("MODAL_EXPORT_OPERATIONS"), [closeModal]);
const handleButtonClick = useCallback(() => {
let exporting = false;
Expand Down
31 changes: 24 additions & 7 deletions libs/ledger-live-common/src/csvExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { calculate } from "@ledgerhq/live-countervalues/logic";
import type { CounterValuesState } from "@ledgerhq/live-countervalues/types";
import type { Currency } from "@ledgerhq/types-cryptoassets";
import { getDefaultAccountName } from "@ledgerhq/live-wallet/accountName";
import { WalletState, accountNameWithDefaultSelector } from "@ledgerhq/live-wallet/store";

type Field = {
title: string;
Expand All @@ -16,6 +17,7 @@ type Field = {
arg2: Operation,
arg3: Currency | null | undefined,
arg4: CounterValuesState | null | undefined,
arg5: WalletState | null | undefined,
) => string;
};

Expand Down Expand Up @@ -64,8 +66,10 @@ const fields: Field[] = [
},
{
title: "Account Name",
// FIXME: we need to inject wallet state if we want the actual user's account name
cell: (account, parentAccount) => getDefaultAccountName(getMainAccount(account, parentAccount)),
cell: (account, parentAccount, _op, _counterValueCurrency, _countervalueState, walletState) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this 3 ? _op, _counterValueCurrency, _countervalueState

Copy link
Contributor Author

@LucasWerey LucasWerey Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because how it's done today if walletState is at the third place it would be the operation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed ^^

walletState
? accountNameWithDefaultSelector(walletState, account)
: getDefaultAccountName(getMainAccount(account, parentAccount)),
},
{
title: "Account xpub",
Expand Down Expand Up @@ -128,33 +132,46 @@ const accountRows = (
parentAccount: Account | null | undefined,
counterValueCurrency?: Currency,
countervalueState?: CounterValuesState,
walletState?: WalletState,
): Array<string[]> =>
account.operations
.reduce((ops: Operation[], op) => ops.concat(flattenOperationWithInternalsAndNfts(op)), [])
.map(operation =>
fields.map(field =>
field.cell(account, parentAccount, operation, counterValueCurrency, countervalueState),
field.cell(
account,
parentAccount,
operation,
counterValueCurrency,
countervalueState,
walletState,
),
),
);

const accountsRows = (
accounts: Account[],
counterValueCurrency?: Currency,
countervalueState?: CounterValuesState,
): Array<string[]> =>
flattenAccounts(accounts).reduce((all: Array<string[]>, account) => {
walletState?: WalletState,
): Array<string[]> => {
return flattenAccounts(accounts).reduce((all: Array<string[]>, account) => {
const parentAccount =
account.type !== "Account" ? accounts.find(a => a.id === account.parentId) : null;
return all.concat(accountRows(account, parentAccount, counterValueCurrency, countervalueState));
return all.concat(
accountRows(account, parentAccount, counterValueCurrency, countervalueState, walletState),
);
}, []);
};

export const accountsOpToCSV = (
accounts: Account[],
counterValueCurrency?: Currency,
countervalueState?: CounterValuesState, // cvs state required for countervalues export
walletState?: WalletState, // wallet state required for account name
): string =>
fields.map(field => field.title).join(",") +
newLine +
accountsRows(accounts, counterValueCurrency, countervalueState)
accountsRows(accounts, counterValueCurrency, countervalueState, walletState)
.map(row => row.map(value => value.replace(/[,\n\r]/g, "")).join(","))
.join(newLine);
Loading