Skip to content

A local-first personal finance app

[AI] Write exact bytes when saving exported files from desktop app (#9286)

Stephen Brown II61775be12503
diff --git a/packages/desktop-client/src/components/accounts/Account.tsx b/packages/desktop-client/src/components/accounts/Account.tsx
index 856c995703..077bbe5ee6 100644
--- a/packages/desktop-client/src/components/accounts/Account.tsx
+++ b/packages/desktop-client/src/components/accounts/Account.tsx
@@ -660,11 +660,22 @@ class AccountInternal extends PureComponent<
accountName && accountName.replace(/[()]/g, '').replace(/\s+/g, '-');
const filename = `${normalizedName || 'transactions'}.csv`;
- void window.Actual.saveFile(
- exportedTransactions,
- filename,
- t('Export transactions'),
- );
+ try {
+ await window.Actual.saveFile(
+ exportedTransactions,
+ filename,
+ t('Export transactions'),
+ );
+ } catch {
+ this.props.dispatch(
+ addNotification({
+ notification: {
+ type: 'error',
+ message: t('Failed to export transactions.'),
+ },
+ }),
+ );
+ }
};
onTransactionsChange = (updatedTransaction: TransactionEntity) => {
diff --git a/packages/desktop-client/src/components/reports/Overview.tsx b/packages/desktop-client/src/components/reports/Overview.tsx
index 7b0594f1c5..46fff7bba1 100644
--- a/packages/desktop-client/src/components/reports/Overview.tsx
+++ b/packages/desktop-client/src/components/reports/Overview.tsx
@@ -298,7 +298,7 @@ export function Overview({ dashboard }: OverviewProps) {
});
};
- const onExport = () => {
+ const onExport = async () => {
const data = {
version: 1,
widgets: desktopLayout.map(item => {
@@ -327,11 +327,22 @@ export function Overview({ dashboard }: OverviewProps) {
}),
} satisfies ExportImportDashboard;
- void window.Actual.saveFile(
- JSON.stringify(data, null, 2),
- 'dashboard.json',
- t('Export Dashboard'),
- );
+ try {
+ await window.Actual.saveFile(
+ JSON.stringify(data, null, 2),
+ 'dashboard.json',
+ t('Export Dashboard'),
+ );
+ } catch {
+ dispatch(
+ addNotification({
+ notification: {
+ type: 'error',
+ message: t('Failed to export the dashboard file.'),
+ },
+ }),
+ );
+ }
};
const importDashboardPageMutation = useImportDashboardPageMutation();
@@ -696,7 +707,7 @@ export function Overview({ dashboard }: OverviewProps) {
void onResetDashboard();
break;
case 'export':
- onExport();
+ void onExport();
break;
case 'import':
void onImport();
diff --git a/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx b/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx
index bc4d095635..4944272050 100644
--- a/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx
+++ b/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx
@@ -341,17 +341,28 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {
);
}
- const onExportCsv = () => {
+ const onExportCsv = async () => {
if (!data) return;
const csv = buildBudgetAnalysisCsv(data.intervalData);
const reportName = (widget?.meta?.name || t('Budget Analysis'))
.replace(/[^a-z0-9]/gi, '-')
.toLowerCase();
- void window.Actual.saveFile(
- csv,
- `${reportName}-${start}-${end}.csv`,
- t('Export budget analysis'),
- );
+ try {
+ await window.Actual.saveFile(
+ csv,
+ `${reportName}-${start}-${end}.csv`,
+ t('Export budget analysis'),
+ );
+ } catch {
+ dispatch(
+ addNotification({
+ notification: {
+ type: 'error',
+ message: t('Failed to export budget analysis.'),
+ },
+ }),
+ );
+ }
};
if (!data || !allMonths) {
diff --git a/packages/desktop-client/src/components/settings/Export.tsx b/packages/desktop-client/src/components/settings/Export.tsx
index 35d4c1b03c..cdf8316637 100644
--- a/packages/desktop-client/src/components/settings/Export.tsx
+++ b/packages/desktop-client/src/components/settings/Export.tsx
@@ -35,11 +35,15 @@ export function ExportBudget() {
if (response.data) {
setWarnings(response.warnings ?? []);
- void window.Actual.saveFile(
- response.data,
- `${format(new Date(), 'yyyy-MM-dd')}-${budgetName}.zip`,
- t('Export budget'),
- );
+ try {
+ await window.Actual.saveFile(
+ response.data,
+ `${format(new Date(), 'yyyy-MM-dd')}-${budgetName}.zip`,
+ t('Export budget'),
+ );
+ } catch {
+ setError('export-error');
+ }
}
setIsLoading(false);
}
diff --git a/packages/desktop-electron/index.ts b/packages/desktop-electron/index.ts
index 8588acc5e5..18ac589cba 100644
--- a/packages/desktop-electron/index.ts
+++ b/packages/desktop-electron/index.ts
@@ -821,18 +821,12 @@ ipcMain.handle(
) => {
const fileLocation = await dialog.showSaveDialog({ title, defaultPath });
- return new Promise<void>((resolve, reject) => {
- if (fileLocation) {
- const contents =
- typeof fileContents === 'string'
- ? fileContents
- : new Uint8Array(fileContents.buffer);
- fs.writeFile(fileLocation.filePath, contents, error => {
- return reject(error);
- });
- }
- resolve();
- });
+ if (fileLocation.canceled || !fileLocation.filePath) {
+ return;
+ }
+
+ // A Buffer's .buffer can be a shared pool slab, so write the Buffer view itself.
+ await fs.promises.writeFile(fileLocation.filePath, fileContents);
},
);
diff --git a/upcoming-release-notes/fix-export-small-zip.md b/upcoming-release-notes/fix-export-small-zip.md
new file mode 100644
index 0000000000..b65ad6a3cb
--- /dev/null
+++ b/upcoming-release-notes/fix-export-small-zip.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [StephenBrown2]
+---
+
+Fixes an edge case where very small budgets exported from the desktop app could not be reimported.