# Dropbox File Export

## Objective

Send generated reports and exports straight into a connected Dropbox folder.

A per-user Dropbox connection that writes app-generated files into a folder the user chose.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Find the places the app already produces a file — report generation, data exports, invoice documents, attachment bundles — and offer Dropbox delivery as an additional destination beside the existing download.
2. Store the connection per user, or per workspace where the destination is shared, and keep tokens on the server. Nothing that can write to someone's Dropbox may reach the browser.
3. Ask for the narrowest access the provider offers and let the user pick one destination folder during connection. Do not request account-wide write access because it makes the folder picker easier to skip.
4. Run every transfer through the app's existing background-job system with retries and backoff, and record a per-file status the user can see. A large export must not be tied to the lifetime of a web request.
5. This entry owns Dropbox only. If OneDrive File Export or Amazon S3 Backup Export is also being built, share one destination abstraction — connection record, folder selection, job, status log — and let each provider supply only the transfer details.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Access tokens expire and refresh tokens can be revoked from the provider's own settings page. Refresh transparently on expiry, and when the refresh itself fails, mark the connection broken and ask the user to reconnect rather than retrying forever.
- A file with the same name may already exist at the destination. Pick one rule — overwrite, or append a suffix — state it where the user chose the folder, and apply it consistently instead of letting the provider decide.
- Large exports exceed the size a single upload request accepts and must be sent in chunks that can resume. A transfer that fails at ninety percent should continue rather than start again.
- The chosen folder can be renamed, moved, or deleted after the connection was made. Detect the missing destination, stop the transfer, and ask the user to choose again rather than silently creating a new folder at the account root.
- Disconnecting must revoke the stored credential with the provider as well as deleting the local record. Files already delivered stay where they are, and the user should be told that they do.
- The provider will rate-limit a burst of uploads. Honour the retry delay it returns, back off progressively, and queue the remainder rather than failing the export.
- When Dropbox is unavailable the export itself must still succeed and remain downloadable inside the app. Show delivery as pending or failed with a retry, and never lose the generated file.
- The account can be out of storage or over quota. Surface that as its own actionable message rather than a generic upload failure.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Every export the app generates can be delivered to a connected Dropbox folder as well as downloaded.
- [ ] Tokens are stored server-side, refreshed automatically, and never reach the browser.
- [ ] The connection requests access to the selected destination only, not the whole account.
- [ ] Large files transfer in resumable chunks and survive an interrupted connection.
- [ ] Filename conflicts follow one documented rule, and a missing destination folder prompts reselection.
- [ ] Disconnecting revokes the credential with the provider and removes it locally.
- [ ] A Dropbox outage leaves the generated file intact and the delivery retryable.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
