# samit26/readwisely architecture

> Architecture diagram of the GitHub repository samit26/readwisely, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/samit26/readwisely
- Repository: https://github.com/samit26/readwisely

## Overview

#### Purpose and Runtime

BookReader Web is a Vite-built, fully client-side React ebook reader for EPUB and PDF files. It has no application backend or accounts: browser storage holds library and reading state, while optional translation calls Google Gemini directly using the user’s API key. Static deployment is configured for Netlify.

Entry points: `src/main.jsx`, `src/App.jsx`, `index.html`, `netlify.toml`.

#### Application Composition

`App` assembles global providers and switches the primary experience between the library, reader, and settings surfaces. Shared contexts centralize state and cross-cutting UI notifications rather than introducing a server-side domain layer.

Core paths:
- `src/App.jsx`
- `src/context/AppProviders.jsx`
- `src/context/LibraryContext.jsx`
- `src/context/SettingsContext.jsx`
- `src/context/ToastContext.jsx`

#### Library and Import Workflow

The library is the book-management boundary: it displays shelves, search/status information, import UI, storage warnings, backup actions, and book cards. Drag/drop or file upload initiates metadata extraction and persistence of the book record plus its raw bytes; opening a card transitions into the reader for that book.

Core paths:
- `src/components/Library/LibraryView.jsx`
- `src/components/Library/UploadZone.jsx`
- `src/components/Library/BookCard.jsx`
- `src/lib/metadata.js`
- `src/lib/pdfToEpub.js`

#### Persistent Client Data

IndexedDB is the durable content store, separating lightweight `books` metadata from raw `blobs` so library listing does not require loading files. It also stores highlights and bookmarks. localStorage holds settings, per-book reading positions, and the Gemini key. Backup/export-import serializes the local library as a ZIP, providing the portability/sync boundary.

Core paths:
- `src/lib/db.js`
- `src/lib/storage.js`
- `src/lib/backup.js`
- `src/components/Library/BackupBar.jsx`

#### Reader Engine Abstraction

The reader UI talks to a common engine interface and treats locations as opaque. Engine implementations expose lifecycle, navigation, location/progress, TOC, search, and annotation operations, then emit events such as loaded, relocated, selected, and error. This preserves a unified toolbar/sidebar workflow while respecting fundamentally different document models.

Core paths:
- `src/lib/reader/readerEngine.js`
- `src/lib/reader/emitter.js`
- `src/lib/reader/epubEngine.js`
- `src/lib/reader/pdfEngine.js`

#### Format-Specific Rendering and Reader UI

EPUB uses epub.js-style paginated, reflowable rendering and CFI-based locations/selections. PDF uses pdf.js-style continuous scrolling with lazy page rendering and page/normalized-rectangle highlights. `ReaderView` owns engine integration; toolbar, sidebar, selection menu, recap, translation, and typography panels consume the reader state/events.

Core paths:
- `src/components/Reader/ReaderView.jsx`
- `src/components/Reader/ReaderToolbar.jsx`
- `src/components/Reader/ReaderSidebar.jsx`
- `src/components/Reader/SelectionMenu.jsx`
- `src/components/Reader/TypographyPanel.jsx`

#### Annotation, Position, and Presentation Flow

Reader actions create highlights, notes, and bookmarks through the active engine and IndexedDB. Relocation events update per-book position storage, enabling “continue reading” and library progress. Settings context supplies themes and typography preferences consumed by reader styles and controls.

Core paths:
- `src/components/Reader/ReaderView.jsx`
- `src/components/Reader/ReaderSidebar.jsx`
- `src/context/SettingsContext.jsx`
- `src/styles/reader.css`

#### External Boundary and Deployment

Gemini translation is the sole data-bearing network integration. Selected text is sent browser-to-Google with the locally stored BYOK credential; typed failures drive explicit UI states instead of silent failures. The app otherwise runs offline after assets load and deploys as a static SPA.

Core paths:
- `src/lib/gemini.js`
- `src/components/Reader/TranslatePopover.jsx`
- `netlify.toml`
- `vite.config.js`

Main entry points: [`src/components/Reader/ReaderView.jsx`](https://github.com/samit26/readwisely/blob/main/src/components/Reader/ReaderView.jsx) (Reader view), [`src/App.jsx`](https://github.com/samit26/readwisely/blob/main/src/App.jsx) (App router shell), [`src/components/Library/LibraryView.jsx`](https://github.com/samit26/readwisely/blob/main/src/components/Library/LibraryView.jsx) (Library view), [`src/lib/db.js`](https://github.com/samit26/readwisely/blob/main/src/lib/db.js) (IndexedDB library store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["App shell"]
  node_entry["Vite entry<br/>bootstrap<br/>[main.jsx]"]
  node_app_shell["App router shell<br/>React composition<br/>[App.jsx]"]
  node_providers["Global providers<br/>React contexts<br/>[AppProviders.jsx]"]
  node_library_context["Library state<br/>context<br/>[LibraryContext.jsx]"]
  node_settings_context["Settings state<br/>context"]
end

subgraph group_library["Library workflow"]
  node_library_view["Library view<br/>library UI<br/>[LibraryView.jsx]"]
  node_upload["Book import<br/>upload UI<br/>[UploadZone.jsx]"]
  node_metadata["Metadata extraction<br/>import processing<br/>[metadata.js]"]
  node_book_card["Book cards<br/>library UI<br/>[BookCard.jsx]"]
  node_backup["Backup / restore<br/>portability UI<br/>[BackupBar.jsx]"]
end

subgraph group_storage["Client persistence"]
  node_indexeddb[("IndexedDB library store<br/>durable browser DB<br/>[db.js]")]
  node_local_storage[("localStorage state<br/>browser storage<br/>[storage.js]")]
  node_backup_codec["ZIP backup codec<br/>backup serialization<br/>[backup.js]"]
end

subgraph group_reader["Reader runtime"]
  node_reader_view["Reader view<br/>reader orchestration<br/>[ReaderView.jsx]"]
  node_engine_interface{{"Reader engine interface<br/>engine abstraction<br/>[readerEngine.js]"}}
  node_epub_engine["EPUB engine<br/>reflowable renderer<br/>[epubEngine.js]"]
  node_pdf_engine["PDF engine<br/>scrolling renderer<br/>[pdfEngine.js]"]
  node_reader_controls["Reader controls<br/>toolbar and sidebar<br/>[ReaderSidebar.jsx]"]
end

subgraph group_external["External delivery"]
  node_translation["Gemini translation<br/>BYOK network client<br/>[gemini.js]"]
  node_translate_ui["Translation popover<br/>reader UI"]
  node_netlify["Netlify static deployment<br/>deployment config<br/>[netlify.toml]"]
end

node_entry -->|"mounts"| node_app_shell
node_app_shell -->|"wraps with"| node_providers
node_providers -->|"provides"| node_library_context
node_providers -->|"provides"| node_settings_context
node_app_shell -->|"shows library"| node_library_view
node_app_shell -->|"opens reader"| node_reader_view
node_library_view -->|"hosts"| node_upload
node_upload -->|"imports files through"| node_metadata
node_metadata -->|"stores metadata and blobs"| node_indexeddb
node_library_context -->|"loads library"| node_indexeddb
node_library_view -->|"renders"| node_book_card
node_book_card -->|"selects book"| node_reader_view
node_library_view -->|"offers"| node_backup
node_backup -->|"uses"| node_backup_codec
node_backup_codec -->|"exports/imports"| node_indexeddb
node_settings_context -->|"persists"| node_local_storage
node_reader_view -->|"controls"| node_engine_interface
node_engine_interface -->|"implemented by"| node_epub_engine
node_engine_interface -->|"implemented by"| node_pdf_engine
node_reader_view -->|"reads books and saves annotations"| node_indexeddb
node_reader_view -->|"updates reading position"| node_local_storage
node_reader_view -->|"drives state for"| node_reader_controls
node_reader_view -->|"provides selection"| node_translate_ui
node_translate_ui -->|"requests translation"| node_translation
node_translation -->|"reads BYOK credential"| node_local_storage
node_netlify -.->|"serves static SPA"| node_entry

click node_entry "https://github.com/samit26/readwisely/blob/main/src/main.jsx"
click node_app_shell "https://github.com/samit26/readwisely/blob/main/src/App.jsx"
click node_providers "https://github.com/samit26/readwisely/blob/main/src/context/AppProviders.jsx"
click node_library_context "https://github.com/samit26/readwisely/blob/main/src/context/LibraryContext.jsx"
click node_settings_context "https://github.com/samit26/readwisely/blob/main/src/context/SettingsContext.jsx"
click node_library_view "https://github.com/samit26/readwisely/blob/main/src/components/Library/LibraryView.jsx"
click node_upload "https://github.com/samit26/readwisely/blob/main/src/components/Library/UploadZone.jsx"
click node_metadata "https://github.com/samit26/readwisely/blob/main/src/lib/metadata.js"
click node_book_card "https://github.com/samit26/readwisely/blob/main/src/components/Library/BookCard.jsx"
click node_backup "https://github.com/samit26/readwisely/blob/main/src/components/Library/BackupBar.jsx"
click node_indexeddb "https://github.com/samit26/readwisely/blob/main/src/lib/db.js"
click node_local_storage "https://github.com/samit26/readwisely/blob/main/src/lib/storage.js"
click node_backup_codec "https://github.com/samit26/readwisely/blob/main/src/lib/backup.js"
click node_reader_view "https://github.com/samit26/readwisely/blob/main/src/components/Reader/ReaderView.jsx"
click node_engine_interface "https://github.com/samit26/readwisely/blob/main/src/lib/reader/readerEngine.js"
click node_epub_engine "https://github.com/samit26/readwisely/blob/main/src/lib/reader/epubEngine.js"
click node_pdf_engine "https://github.com/samit26/readwisely/blob/main/src/lib/reader/pdfEngine.js"
click node_reader_controls "https://github.com/samit26/readwisely/blob/main/src/components/Reader/ReaderSidebar.jsx"
click node_translation "https://github.com/samit26/readwisely/blob/main/src/lib/gemini.js"
click node_translate_ui "https://github.com/samit26/readwisely/blob/main/src/components/Reader/TranslatePopover.jsx"
click node_netlify "https://github.com/samit26/readwisely/blob/main/netlify.toml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_app_shell,node_providers,node_library_context,node_settings_context toneBlue
class node_library_view,node_upload,node_metadata,node_book_card,node_backup toneAmber
class node_indexeddb,node_local_storage,node_backup_codec toneMint
class node_reader_view,node_engine_interface,node_epub_engine,node_pdf_engine,node_reader_controls toneRose
class node_translation,node_translate_ui,node_netlify toneIndigo
```

## Components

### App shell

- [Vite entry](https://github.com/samit26/readwisely/blob/main/src/main.jsx): bootstrap · `src/main.jsx`
- [App router shell](https://github.com/samit26/readwisely/blob/main/src/App.jsx): React composition · Switches between library, reader, and settings surfaces. · `src/App.jsx`
- [Global providers](https://github.com/samit26/readwisely/blob/main/src/context/AppProviders.jsx): React contexts · Supplies library, settings, and toast state. · `src/context/AppProviders.jsx`
- [Library state](https://github.com/samit26/readwisely/blob/main/src/context/LibraryContext.jsx): context · `src/context/LibraryContext.jsx`
- [Settings state](https://github.com/samit26/readwisely/blob/main/src/context/SettingsContext.jsx): context · Persists preferences, positions, and Gemini credential. · `src/context/SettingsContext.jsx`

### Library workflow

- [Library view](https://github.com/samit26/readwisely/blob/main/src/components/Library/LibraryView.jsx): library UI · Displays shelves, search, status, import, and storage actions. · `src/components/Library/LibraryView.jsx`
- [Book import](https://github.com/samit26/readwisely/blob/main/src/components/Library/UploadZone.jsx): upload UI · Accepts dropped or selected EPUB/PDF files. · `src/components/Library/UploadZone.jsx`
- [Metadata extraction](https://github.com/samit26/readwisely/blob/main/src/lib/metadata.js): import processing · Extracts book metadata and converts PDF when needed. · `src/lib/metadata.js`
- [Book cards](https://github.com/samit26/readwisely/blob/main/src/components/Library/BookCard.jsx): library UI · Opens a selected book in the reader. · `src/components/Library/BookCard.jsx`
- [Backup / restore](https://github.com/samit26/readwisely/blob/main/src/components/Library/BackupBar.jsx): portability UI · Exports or imports the local library ZIP. · `src/components/Library/BackupBar.jsx`

### Client persistence

- [IndexedDB library store](https://github.com/samit26/readwisely/blob/main/src/lib/db.js): durable browser DB · Separates book metadata, raw blobs, highlights, and bookmarks. · `src/lib/db.js`
- [localStorage state](https://github.com/samit26/readwisely/blob/main/src/lib/storage.js): browser storage · Stores settings, reading positions, and Gemini key. · `src/lib/storage.js`
- [ZIP backup codec](https://github.com/samit26/readwisely/blob/main/src/lib/backup.js): backup serialization · `src/lib/backup.js`

### Reader runtime

- [Reader view](https://github.com/samit26/readwisely/blob/main/src/components/Reader/ReaderView.jsx): reader orchestration · Owns active-engine lifecycle and reader event state. · `src/components/Reader/ReaderView.jsx`
- [Reader engine interface](https://github.com/samit26/readwisely/blob/main/src/lib/reader/readerEngine.js): engine abstraction · Unifies lifecycle, navigation, location, search, and annotations. · `src/lib/reader/readerEngine.js`
- [EPUB engine](https://github.com/samit26/readwisely/blob/main/src/lib/reader/epubEngine.js): reflowable renderer · Uses paginated rendering with CFI locations. · `src/lib/reader/epubEngine.js`
- [PDF engine](https://github.com/samit26/readwisely/blob/main/src/lib/reader/pdfEngine.js): scrolling renderer · Uses lazy continuous pages and rectangle highlights. · `src/lib/reader/pdfEngine.js`
- [Reader controls](https://github.com/samit26/readwisely/blob/main/src/components/Reader/ReaderSidebar.jsx): toolbar and sidebar · Provides navigation, TOC, search, and annotation workflows. · `src/components/Reader/ReaderSidebar.jsx`

### External delivery

- [Gemini translation](https://github.com/samit26/readwisely/blob/main/src/lib/gemini.js): BYOK network client · Sends selected text directly to Google Gemini. · `src/lib/gemini.js`
- [Translation popover](https://github.com/samit26/readwisely/blob/main/src/components/Reader/TranslatePopover.jsx): reader UI · Shows translation and typed error states. · `src/components/Reader/TranslatePopover.jsx`
- [Netlify static deployment](https://github.com/samit26/readwisely/blob/main/netlify.toml): deployment config · `netlify.toml`

## Connections

- Vite entry → App router shell: mounts
- App router shell → Global providers: wraps with
- Global providers → Library state: provides
- Global providers → Settings state: provides
- App router shell → Library view: shows library
- App router shell → Reader view: opens reader
- Library view → Book import: hosts
- Book import → Metadata extraction: imports files through
- Metadata extraction → IndexedDB library store: stores metadata and blobs
- Library state → IndexedDB library store: loads library
- Library view → Book cards: renders
- Book cards → Reader view: selects book
- Library view → Backup / restore: offers
- Backup / restore → ZIP backup codec: uses
- ZIP backup codec → IndexedDB library store: exports/imports
- Settings state → localStorage state: persists
- Reader view → Reader engine interface: controls
- Reader engine interface → EPUB engine: implemented by
- Reader engine interface → PDF engine: implemented by
- Reader view → IndexedDB library store: reads books and saves annotations
- Reader view → localStorage state: updates reading position
- Reader view → Reader controls: drives state for
- Reader view → Translation popover: provides selection
- Translation popover → Gemini translation: requests translation
- Gemini translation → localStorage state: reads BYOK credential
- Netlify static deployment → Vite entry: serves static SPA
