From 763f207651060116dbeec1acaca60ac00b825ade Mon Sep 17 00:00:00 2001 From: projectmoon Date: Mon, 11 Nov 2024 00:03:56 +0100 Subject: [PATCH] Download a CSV!! --- src/background/message-handler.ts | 19 ++++++++++++++++++- todo.org | 14 ++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/background/message-handler.ts b/src/background/message-handler.ts index f758f9c..2a6022f 100644 --- a/src/background/message-handler.ts +++ b/src/background/message-handler.ts @@ -112,6 +112,21 @@ const getAllTransactions = async (message: DownloadTransactionsAction) => const mapToCsv = (transactions: GroupedRawTransaction[]) => Papa.unparse(transactions); +function createdDownload(csv: string, filename: string = 'transactions.csv') { + const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); + + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = filename; + + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + + URL.revokeObjectURL(url); +} + async function downloadStatements( message: DownloadTransactionsMessage ): Promise { @@ -122,7 +137,9 @@ async function downloadStatements( // Should always be populated, but we can be careful anyway. if (isStatePopulated(state)) { const transactions = (await getAllTransactions(message)).flat(); - return { csv: mapToCsv(transactions), error: null }; + const csv = mapToCsv(transactions); + createdDownload(csv); + return { csv: csv, error: null }; // TODO return useful info or something. } else { return { csv: undefined, diff --git a/todo.org b/todo.org index 912cdfe..48fed11 100644 --- a/todo.org +++ b/todo.org @@ -1,9 +1,9 @@ How to get it working: - [X] Landsbankinn API client for arbitrary transaction lists - - [ ] Transform raw transactions into friendlier ones (i.e. date objects) + - [X] Transform raw transactions into friendlier ones (i.e. date objects) - [X] Content script that can run all the time and puts a button somewhere (statements tab, and extension icon) - [X] Use config.ts to set TTL for state - - [ ] Background page that downloads all the shit and combines into CSV, then 'downloads' file. + - [X] Background page that downloads all the shit and combines into CSV, then 'downloads' file. - [X] Build up communication message passing to background page for downloads - [X] Don't run level on client. - [-] Investigate persistent = false + setTimeout @@ -14,3 +14,13 @@ How to get it working: - [X] Set API key - [X] Set bearer token - [X] Intercept bearer token by yoinking it out of a request + - [ ] Deal with state disappearing after 15 sec (I think 15 sec was a test). + - We should always reload state if it's not present. And we are doing this. + - Why does state not re-build itself properly? It auths but does not set itself to "ready to request" mode. + - [ ] Make the date picker and download button nice to use. + - [ ] Validate date picker options when downloading. + - [ ] Show a download status on the web page to indicate where it is in the download process. + - [ ] Error handling: try-catch in various places, send messages back to content script that error occurred. + - [ ] Txn download + - [ ] CSV creation + - [ ] Ensure auth state API calls