fix(desktop): save attachment downloads and confirm them in the app

Merged
HO
3mo ago
+558-214

Problem

Follow-up to #3480. In the macOS desktop app, files that Sure sends as downloads were displayed in the window instead of being saved whenever WebKit can render their type. wry only converts a response into a download when canShowMIMEType is false and ignores Content-Disposition: attachment. As a result:

  • statement PDFs (rails_blob_path(..., disposition: "attachment")), transaction attachment downloads and the import sample CSV replaced the app page, with no way back;
  • viewing a CSV statement opened a server window, and ActiveStorage serves text/csv as an attachment;
  • the report CSV export only worked because bridge.ts added a download attribute to that one link;
  • after a download, the app gave no visible confirmation apart from a native notification, which macOS may not show.

Changes

Desktop (desktop/src-tauri)

  • attachments.rs puts a navigation delegate in front of wry's, on the main window and on server windows. It answers the response policy with Download when Content-Disposition is attachment. For the other delegate methods wry 0.55 implements, it calls wry's delegate, held weakly, and does nothing (or Cancel for policy calls) once that delegate is gone. It also releases the three references tauri passes to with_webview.
  • A server window whose address answers with a file never shows a page. It is hidden when the download starts and closed when it ends.
  • Finished downloads are confirmed with Sure's own toast, cloned from a template. A native notification replaces it when no visible window can show it or the server has no template, and is also sent when the window is not in front. It uses the toast's translated text, read from the templates' data-message and data-description, and falls back to English only without the templates.
  • The report-only bridge.ts hook, its Node tests and the npm test script are removed, since the delegate covers that export.
  • objc2, objc2-foundation, objc2-web-kit and block2 become macOS-only direct dependencies. They were already in the tree through wry, at the same versions.

Rails

  • layouts/shared/_notification_tray renders the existing shared/notifications/notice and alert partials inside two <template> elements, which also carry the translated text as data attributes for the native notification. They stay inert in a browser.
  • New layouts.shared.notification_tray.* keys in en and fr. Other locales fall back to English.

Validation

  • cargo test --locked in desktop/src-tauri (35 tests), cargo clippy (only the 2 warnings already on main), rustfmt --check, npm run build.
  • On this branch rebased on main: bin/rails test (9101 runs, 0 failures, 0 errors), bin/rubocop (no offenses), npm run lint (clean) and bin/brakeman (no warnings) all pass. erb_lint reports one error, in app/views/pages/dashboard/_balance_sheet.html.erb:154 (javascript_tag do), which already exists on main since #3551; the ERB changed here is clean.
  • test/system/accounts_sync_ui_test.rb, which exercises the notification tray, fails locally with 2 failures and 1 error, because local Chrome negotiates French and the test looks for English labels ("Cancel sync", aria-label="Sync all"). main without this change fails the same way on the same machine, so the failure is not caused by this PR; CI runs system tests in English.
  • Ran the app built from this branch against a local mock server, one launch per scenario. Downloads went to a sandboxed $HOME/Downloads. The mock reproduced Sure's responses:
    • report CSV export: one request, file saved, toast shown, page kept;
    • statement PDF with Content-Disposition: attachment: file saved, page kept (on main it replaced the page);
    • import sample CSV: file saved, page kept (on main it replaced the page);
    • CSV statement opened in a server window: file saved, empty window closed, toast in the main window;
    • Print Report: report window opens with the print dialog.
  • Captured the desktop app window on a test page that used the real notice partial (rendered in French) and Sure's compiled stylesheet. The toast shows "Téléchargement terminé" and its description, and closes itself as usual. The screenshot can be added to this PR on request.
  • Tested by the author in the rebuilt app against a Docker server running ghcr.io/we-promise/sure:latest: downloads work. The toast needs a server that includes this change, so it falls back to the native notification until then.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Desktop downloads now show localized completion or failure notifications, including the Downloads folder location.
    • macOS attachment downloads are saved instead of opened in the browser.
    • Notifications use in-app toasts when possible, with native notifications as a fallback.
  • Bug Fixes

    • Failed downloads are reported clearly.
  • Documentation

    • Updated desktop download behavior and verification guidance.
  • Tests

    • Added coverage for attachment detection and download notifications.