# alcumus/react-doc-viewer architecture

> Architecture diagram of the GitHub repository alcumus/react-doc-viewer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alcumus/react-doc-viewer
- Repository: https://github.com/alcumus/react-doc-viewer

## Overview

##### Project Type and Purpose
- This is a frontend React component library (“react-doc-viewer”) for rendering various document types (PDF, images, Office docs, HTML, etc.) in-browser.
- It provides a single `DocViewer` entry point component, a plugin system for per-file-type renderers, theming, configuration, and internal state management.

##### File Structure Analysis
- src/index.tsx: Exports the public `DocViewer` API.
- src/components/: Core UI subcomponents (HeaderBar, DocumentNav, FileName, ProxyRenderer).
- src/plugins/: A folder of “renderer plugins,” one subdirectory per file type (pdf, jpg, png, txt, etc.), each exporting a `DocRenderer`.
- src/state/: Central state store using React context / useReducer (actions.ts, reducer.ts, index.tsx).
- src/utils/: Helper functions and custom hooks (file loaders, renderer selector, window sizing).
- src/theme/, src/types/: Theme interfaces and TypeScript types.
- Tests live under src/__tests__, with snapshots.
- Config and build files at root: package.json, tsconfig.json, service worker stub, etc.

##### README Insights
- Documents are passed as an array of `{ uri, fileType? }`.
- A plugin array (`pluginRenderers`) drives which renderer component handles which MIME/types.
- Internal file loading can be overridden by plugins.
- State: currentDocument, currentFileNo, documentLoading, config, theme.
- Styling via className/style/theme/config/header.

##### Main Components & Layers
a. Public API Layer
- Entry point: DocViewer component
- Props: documents[], pluginRenderers[], theme, config, className, style

b. State Management Layer
- Context Provider / useReducer: holds `IMainState`
- Actions → Reducer → State updates

c. Plugin Resolver & File Loader
- Util hook selects the appropriate `DocRenderer` based on file extension/MIME (plugins/index.ts)
- File loading flow: core or plugin.fileLoader → FileReader or fetch → fileData

d. Rendering Layer
- ProxyRenderer: picks plugin, passes currentDocument.fileData
- Plugin components (PDFControls, PDFPagination, image renderers, text renderer…)
- Common UI components wrapped by HeaderBar, DocumentNav, FileName

e. Theming & Styling
- Theme object flows via context or props into styled-components or inline styles

f. External Interfaces
- Browser environment receives URIs, loads assets over network or from local import
- Optional service worker (serviceWorker.ts) for offline/demo

##### Relationships & Data Flow
1. User passes `documents` and optional props to `DocViewer`.
2. `DocViewer` initializes state via Context + Reducer.
3. On mount or document change, dispatch `LOAD_DOCUMENT` action.
4. Plugin Resolver picks matching `DocRenderer`.
5. File Loader (core or plugin-level) fetches or reads the file → dispatch `LOAD_SUCCESS`.
6. State updates `currentDocument.fileData`, triggers re-render.
7. `ProxyRenderer` passes fileData to selected plugin component.
8. UI subcomponents (HeaderBar, DocumentNav, FileName) render around the document area.

##### Architectural Patterns & Principles
- Plugin pattern: renderer discovery via fileTypes array and weight.
- Flux-style state management: actions, reducer, context.
- Separation of concerns: core, plugins, utils, UI.
- Theming via prop drilling/context, styled-components support.
- Testable units: each plugin and component has its own tests.

##### Final Advice
- Be exhaustive but clear: capture all layers without overcrowding.
- Similar plugins into one “Plugins” module for readability, but call out the PDF plugin internals (state actions, PDFControls).
- Don’t forget utility hooks (useDocumentLoader, useRendererSelector) as part of the data-flow path.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Public API Layer
    subgraph "Public API Layer"
        DV["DocViewer"]:::api
    end

    %% State Layer
    subgraph "State Layer"
        SC["State Context & Reducer"]:::state
        Actions["Actions"]:::state
        Reducer["Reducer"]:::state
    end

    %% Plugin Layer
    subgraph "Plugin Layer"
        PR["Plugin Resolver"]:::plugin
        RS["useRendererSelector"]:::plugin
        DL["useDocumentLoader"]:::plugin
        FL["fileLoaders"]:::plugin
        subgraph "Plugins"
            PDF["PDF Plugin"]:::plugin
            BMP["BMP Renderer"]:::plugin
            JPG["JPG Renderer"]:::plugin
            PNG["PNG Renderer"]:::plugin
            TIFF["TIFF Renderer"]:::plugin
            HTML["HTML Renderer"]:::plugin
            MSDOC["DOC/MSDOC Renderer"]:::plugin
            MSG["MSG Renderer"]:::plugin
            TXT["TXT Renderer"]:::plugin
            IMG["Image Generic Renderer"]:::plugin
        end
    end

    %% UI Layer
    subgraph "UI Layer"
        Proxy["ProxyRenderer"]:::ui
        HB["HeaderBar"]:::ui
        DN["DocumentNav"]:::ui
        FN["FileName"]:::ui
    end

    %% Theming & External
    Theme["Theme/Config"]:::config
    Browser["Browser/Network"]:::external

    %% Connections
    DV -->|"init state"| SC
    SC --> Actions
    SC --> Reducer
    SC --> PR
    PR --> RS
    PR --> FL
    RS --> DL
    FL --> DL
    DL -->|"dispatch LOAD_SUCCESS"| SC
    SC --> Proxy
    Proxy --> PDF
    Proxy --> BMP
    Proxy --> JPG
    Proxy --> PNG
    Proxy --> TIFF
    Proxy --> HTML
    Proxy --> MSDOC
    Proxy --> MSG
    Proxy --> TXT
    Proxy --> IMG
    Theme --> HB
    Theme --> DN
    Theme --> FN
    Proxy --> HB
    Proxy --> DN
    Proxy --> FN
    Browser --> DL

    %% Click Events
    click DV "https://github.com/alcumus/react-doc-viewer/blob/master/src/index.tsx"
    click SC "https://github.com/alcumus/react-doc-viewer/blob/master/src/state/index.tsx"
    click Actions "https://github.com/alcumus/react-doc-viewer/blob/master/src/state/actions.ts"
    click Reducer "https://github.com/alcumus/react-doc-viewer/blob/master/src/state/reducer.ts"
    click PR "https://github.com/alcumus/react-doc-viewer/blob/master/src/plugins/index.ts"
    click RS "https://github.com/alcumus/react-doc-viewer/blob/master/src/utils/useRendererSelector.ts"
    click DL "https://github.com/alcumus/react-doc-viewer/blob/master/src/utils/useDocumentLoader.ts"
    click FL "https://github.com/alcumus/react-doc-viewer/blob/master/src/utils/fileLoaders.ts"
    click Proxy "https://github.com/alcumus/react-doc-viewer/blob/master/src/components/ProxyRenderer.tsx"
    click HB "https://github.com/alcumus/react-doc-viewer/blob/master/src/components/HeaderBar.tsx"
    click DN "https://github.com/alcumus/react-doc-viewer/blob/master/src/components/DocumentNav.tsx"
    click FN "https://github.com/alcumus/react-doc-viewer/blob/master/src/components/FileName.tsx"
    click BMP "https://github.com/alcumus/react-doc-viewer/blob/master/src/plugins/bmp/index.tsx"
    click JPG "https://github.com/alcumus/react-doc-viewer/blob/master/src/plugins/jpg/index.tsx"
    click PNG "https://github.com/alcumus/react-doc-viewer/blob/master/src/plugins/png/index.tsx"
    click TIFF "https://github.com/alcumus/react-doc-viewer/blob/master/src/plugins/tiff/index.tsx"
    click HTML "https://github.com/alcumus/react-doc-viewer/blob/master/src/plugins/html/index.tsx"
    click MSDOC "https://github.com/alcumus/react-doc-viewer/blob/master/src/plugins/msdoc/index.tsx"
    click MSG "https://github.com/alcumus/react-doc-viewer/blob/master/src/plugins/msg/index.tsx"
    click TXT "https://github.com/alcumus/react-doc-viewer/blob/master/src/plugins/txt/index.tsx"
    click IMG "https://github.com/alcumus/react-doc-viewer/blob/master/src/plugins/image/index.tsx"
    click PDF "https://github.com/alcumus/react-doc-viewer/blob/master/src/plugins/pdf/index.tsx"

    %% Styles
    classDef api fill:#f9f,stroke:#333,stroke-width:1px;
    classDef state fill:#bbf,stroke:#333,stroke-width:1px;
    classDef plugin fill:#bfb,stroke:#333,stroke-width:1px;
    classDef ui fill:#fbf,stroke:#333,stroke-width:1px;
    classDef config fill:#ffd,stroke:#333,stroke-width:1px;
    classDef external fill:#ccc,stroke:#333,stroke-width:1px;
```
