How alcumus/react-doc-viewer works
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.