-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Josh Madewell <joshmadewell@users.noreply.github.com>
- Loading branch information
1 parent
69011dc
commit 670132b
Showing
14 changed files
with
359 additions
and
260 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/extension/features/general/category-activity-copy/CopyTransactionsButtons.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { useState } from 'react'; | ||
import { YNABTransaction } from 'toolkit/types/ynab/data/transaction'; | ||
import copyTransactionsToClipboard from './copyTransactionsToClipboard'; | ||
|
||
interface Props { | ||
transactions: YNABTransaction[]; | ||
} | ||
|
||
export default function CopyTransactionsButton({ transactions }: Props) { | ||
const [isCopied, setIsCopied] = useState(false); | ||
|
||
const handleCopyTransactions = () => { | ||
copyTransactionsToClipboard(transactions); | ||
|
||
setIsCopied(true); | ||
|
||
setTimeout(() => { | ||
setIsCopied(false); | ||
}, 2000); | ||
}; | ||
|
||
if (!(Array.isArray(transactions) && transactions.length > 0)) return null; | ||
|
||
return ( | ||
<button | ||
id="tk-copy-transactions" | ||
className="button button-primary" | ||
onClick={handleCopyTransactions} | ||
> | ||
{isCopied ? 'Copied!' : 'Copy Transactions'} | ||
</button> | ||
); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/extension/features/general/category-activity-copy/copyTransactionsToClipboard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { getEntityManager } from 'toolkit/extension/utils/ynab'; | ||
import { YNABTransaction } from 'toolkit/types/ynab/data/transaction'; | ||
|
||
interface Activities { | ||
Account: string; | ||
Date: string; | ||
Payee: any; | ||
Category: string; | ||
Memo: string; | ||
Amount: string; | ||
} | ||
|
||
export default function copyTransactionsToClipboard(transactions: YNABTransaction[]) { | ||
const entityManager = getEntityManager(); | ||
const activities = transactions.map<Activities>((transaction) => { | ||
const parentEntityId = transaction.get('parentEntityId'); | ||
let payeeId = transaction.get('payeeId'); | ||
|
||
if (parentEntityId) { | ||
payeeId = entityManager.transactionsCollection | ||
.findItemByEntityId(parentEntityId) | ||
.get('payeeId'); | ||
} | ||
|
||
const payee = entityManager.payeesCollection.findItemByEntityId(payeeId); | ||
return { | ||
Account: transaction.get('accountName'), | ||
Date: ynab.formatDateLong(transaction.get('date').toString()), | ||
Payee: payee && payee.get('name') ? payee.get('name') : 'Unknown', | ||
Category: transaction.get('subCategoryNameWrapped'), | ||
Memo: transaction.get('memo'), | ||
Amount: ynab.formatCurrency(transaction.get('amount')), | ||
}; | ||
}); | ||
|
||
const replacer = (_key: string, value: null | string) => (value === null ? '' : value); | ||
const header = Object.keys(activities[0]) as (keyof Activities)[]; | ||
let csv = activities.map((row) => | ||
header.map((fieldName) => JSON.stringify(row[fieldName], replacer)).join('\t') | ||
); | ||
csv.unshift(header.join('\t')); | ||
navigator.clipboard.writeText(csv.join('\r\n')); | ||
} |
123 changes: 0 additions & 123 deletions
123
src/extension/features/general/category-activity-copy/index.jsx
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
src/extension/features/general/category-activity-copy/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React from 'react'; | ||
import { Feature } from 'toolkit/extension/features/feature'; | ||
import { | ||
getModalService, | ||
isCurrentRouteBudgetPage, | ||
isCurrentRouteReportPage, | ||
} from 'toolkit/extension/utils/ynab'; | ||
import { componentAppend } from 'toolkit/extension/utils/react'; | ||
import CopyTransactionsButton from './CopyTransactionsButtons'; | ||
import { YNABTransaction } from 'toolkit/types/ynab/data/transaction'; | ||
|
||
export class CategoryActivityCopy extends Feature { | ||
shouldInvoke() { | ||
return ( | ||
$('#tk-copy-transactions').length === 0 && | ||
!!getModalService()?.isModalOpen && | ||
!!document.querySelector('.modal-budget-activity') | ||
); | ||
} | ||
|
||
invoke() { | ||
const modal = document.querySelector('.modal-budget-activity'); | ||
const modalService = getModalService(); | ||
|
||
if (modal && modalService.isModalOpen) { | ||
let transactions: YNABTransaction[] | undefined = undefined; | ||
if (modalService.currentModal === 'modals/budget/activity' && isCurrentRouteBudgetPage()) { | ||
transactions = modalService.modalValue?.selectedActivityTransactions; | ||
} else if ( | ||
modalService.currentModal === 'modals/reports/transactions' && | ||
isCurrentRouteReportPage('any') | ||
) { | ||
transactions = modalService.modalValue?.modalTransactions; | ||
} | ||
|
||
if (Array.isArray(transactions) && transactions.length > 0) { | ||
componentAppend( | ||
<CopyTransactionsButton transactions={transactions} />, | ||
modal.querySelector('.modal-actions') | ||
); | ||
} | ||
} | ||
} | ||
|
||
observe(nodes: Set<string>) { | ||
if (!this.shouldInvoke()) return; | ||
|
||
if ( | ||
nodes.has('modal-overlay active ynab-u modal-popup modal-budget-activity') || | ||
nodes.has('modal-overlay active pure-u modal-popup modal-budget-activity') | ||
) { | ||
this.invoke(); | ||
} | ||
} | ||
|
||
destroy() { | ||
$('#tk-copy-transactions').remove(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
interface YNABPayeeCollection extends YNABCollection<YNABPayee> { | ||
findItemByEntityId(entityId: string | null): YNABPayee; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { YNABTransaction } from './transaction'; | ||
|
||
interface YNABTransactionCollection extends YNABCollection<YNABTransaction> {} | ||
interface YNABTransactionCollection extends YNABCollection<YNABTransaction> { | ||
findItemByEntityId(entityId: string | null): YNABTransaction; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,19 @@ | ||
interface YNABModalService { | ||
import { YNABTransaction } from '../data/transaction'; | ||
|
||
type YNABModalService = { | ||
isModalOpen?: boolean; | ||
closeModal?: (e?: unknown) => void; | ||
} | ||
} & ( | ||
| { | ||
currentModal?: 'modals/budget/activity'; | ||
modalValue?: { | ||
selectedActivityTransactions?: YNABTransaction[]; | ||
}; | ||
} | ||
| { | ||
currentModal?: 'modals/reports/transactions'; | ||
modalValue?: { | ||
modalTransactions?: YNABTransaction[]; | ||
}; | ||
} | ||
); |
Oops, something went wrong.