Skip to content

Commit

Permalink
🐛fix(lld): fix export to csv account name (#7813)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasWerey committed Sep 13, 2024
1 parent 0bad0cb commit 5762905
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
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) =>
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);

0 comments on commit 5762905

Please sign in to comment.