From c2ebfc72ef535c6c82860b6db471d8c44ba1d5fb Mon Sep 17 00:00:00 2001
From: Jarek Samic <jarek.samic@cldfire.dev>
Date: Sun, 16 Jul 2023 10:52:16 -0400
Subject: [PATCH] Fix importing transactions in Safari (#1349)

---
 .../src/browser-preload.browser.js            | 33 +++++++++++++------
 .../components/modals/ImportTransactions.js   |  4 +--
 upcoming-release-notes/1349.md                |  6 ++++
 3 files changed, 31 insertions(+), 12 deletions(-)
 create mode 100644 upcoming-release-notes/1349.md

diff --git a/packages/desktop-client/src/browser-preload.browser.js b/packages/desktop-client/src/browser-preload.browser.js
index f71812e1d..ddb1928b7 100644
--- a/packages/desktop-client/src/browser-preload.browser.js
+++ b/packages/desktop-client/src/browser-preload.browser.js
@@ -52,8 +52,19 @@ global.Actual = {
 
   openFileDialog: async ({ filters = [], properties }) => {
     return new Promise(resolve => {
-      let input = document.createElement('input');
+      let createdElement = false;
+      // Attempt to reuse an already-created file input.
+      let input = document.body.querySelector(
+        'input[id="open-file-dialog-input"]',
+      );
+      if (!input) {
+        createdElement = true;
+        input = document.createElement('input');
+      }
+
       input.type = 'file';
+      input.id = 'open-file-dialog-input';
+      input.value = null;
 
       let filter = filters.find(filter => filter.extensions);
       if (filter) {
@@ -63,15 +74,9 @@ global.Actual = {
       input.style.position = 'absolute';
       input.style.top = '0px';
       input.style.left = '0px';
-      input.dispatchEvent(
-        new MouseEvent('click', {
-          view: window,
-          bubbles: true,
-          cancelable: true,
-        }),
-      );
+      input.style.display = 'none';
 
-      input.addEventListener('change', e => {
+      input.onchange = e => {
         let file = e.target.files[0];
         let filename = file.name.replace(/.*(\.[^.]*)/, 'file$1');
 
@@ -89,7 +94,15 @@ global.Actual = {
             alert('Error reading file');
           };
         }
-      });
+      };
+
+      // In Safari the file input has to be in the DOM for change events to
+      // reliably fire.
+      if (createdElement) {
+        document.body.appendChild(input);
+      }
+
+      input.click();
     });
   },
 
diff --git a/packages/desktop-client/src/components/modals/ImportTransactions.js b/packages/desktop-client/src/components/modals/ImportTransactions.js
index 0ac731ca0..94fe6b0ac 100644
--- a/packages/desktop-client/src/components/modals/ImportTransactions.js
+++ b/packages/desktop-client/src/components/modals/ImportTransactions.js
@@ -671,8 +671,8 @@ function ImportTransactions({
     setFieldMappings({ ...fieldMappings, ...newFieldMappings });
   }
 
-  function onNewFile() {
-    const res = window.Actual.openFileDialog({
+  async function onNewFile() {
+    const res = await window.Actual.openFileDialog({
       filters: [
         { name: 'Financial Files', extensions: ['qif', 'ofx', 'qfx', 'csv'] },
       ],
diff --git a/upcoming-release-notes/1349.md b/upcoming-release-notes/1349.md
new file mode 100644
index 000000000..2744228ee
--- /dev/null
+++ b/upcoming-release-notes/1349.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [Cldfire]
+---
+
+Fix bug causing transaction import in Safari to be unreliable
-- 
GitLab