# eduardomar/edi-viewer architecture

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

- Interactive diagram: https://gitdiagram.com/eduardomar/edi-viewer
- Repository: https://github.com/eduardomar/edi-viewer

## Overview

##### Project type and purpose
- This is a front‑end only, single‑page React application (bootstrapped with Create React App) written in TypeScript.
- Its core purpose is to let users upload, parse, browse, and view EDI (Electronic Data Interchange) files. It uses a large set of static JSON “record” definitions as schemas to interpret segments in an uploaded EDI file, then displays both a structured tree and a rich view.

##### File structure
- Top‑level: standard CRA files (`public/`, `src/`, config files).
- `public/`: static HTML, manifest, robots.
- `src/`: contains assets, components, constants, hooks, interfaces, pages, record definitions (in `src/records`), utils, and bootstrap (`index.tsx`).
- Pages:
- `UploadEDI` – file‑upload UI
- `ViewerEDI` – main viewer with subcomponents (Accordion, JsonView, RichView, Navbar, Offcanvas, MissingRecords, etc.)
- `NotFound` – 404
- Utilities: EDI parsing (`ediToObject.ts`, `writeSegmentEdi.ts`), file download, clipboard copying.
- Data: `src/records/*.json` and nested directories – these are static schemas for all supported EDI record types.
- Hooks: custom React hooks for clipboard and URL parameter handling.
- Types and interfaces: defined in `src/interfaces`.

##### README
- It’s the standard CRA README without any architecture notes. We must infer architecture from the code.

a. Main components
- Browser / User
- React SPA container
- UI Layer
– Pages (UploadEDI, ViewerEDI, NotFound)
– Shared Components (ButtonClipboard, EDI display helpers)
- Business Logic Layer
– Hooks (useClipboard, useParams)
– Utils (ediToObject parser, writeSegmentEdi, downloadFile, copyToClipboard)
- Data Layer
– Static records JSON schemas (`src/records`)
– Constants (`countries.json`, other mappings)
- Type Definitions / Interfaces
b. Relationships & interactions
1. User interacts with UploadEDI page → chooses EDI file
2. File is read by browser FileReader and passed to `ediToObject` (utility)
3. Parser uses `src/records/*.json` schema definitions + interfaces to build a typed object tree
4. Parsed object is stored in React state and passed to ViewerEDI page
5. ViewerEDI subcomponents (AccordionItem, JsonView, RichView, MissingRecords) render based on the parsed data
6. Hooks (`useClipboard`, `useParams`) manage URL parameters and copy‑to‑clipboard actions from UI
7. Utilities like `downloadFile` allow exporting modified EDI
c. Architectural patterns & principles
- Single Page Application (SPA)
- Separation of concerns:
- UI vs. parsing logic vs. data schemas
- Reusable hooks for cross‑cutting concerns (clipboard, routing params)
- Static data assets (JSON) packaged at build time for offline schema lookup
- Type‑safe data flows guaranteed by TypeScript interfaces
d. Key technologies / frameworks
- React (with CRA)
- TypeScript
- Webpack (implicitly via CRA)
- ESLint, Prettier, Husky (dev tooling)

##### Elements
- from Browser → UploadEDI UI → Util (parser)
- from Util → Data Layer (records JSON)
- back to React state → Viewer components

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Browser Boundary
    subgraph "Browser" 
        direction TB
        Browser["User Browser"]:::external
    end

    %% React App Container
    subgraph "React SPA (CRA)" 
        direction TB
        Entry["Entry Point\n(src/index.tsx)"]:::init
        Styles["Global Styles\n(src/index.css)"]:::init

        %% UI Layer
        subgraph "UI Layer" 
            direction TB
            subgraph "Pages" 
                direction TB
                UploadPage["UploadEDI Page\n(src/pages/UploadEDI)"]:::ui
                ViewerPage["ViewerEDI Page\n(src/pages/ViewerEDI)"]:::ui
                NotFoundPage["NotFound Page\n(src/pages/NotFound)"]:::ui
            end
            subgraph "Shared Components" 
                direction TB
                ButtonClipboard["ButtonClipboard\n(src/components/ButtonClipboard.tsx)"]:::ui
                EDIComponent["EDI Renderer\n(src/components/EDI.tsx)"]:::ui
            end
        end

        %% Logic Layer
        subgraph "Logic Layer" 
            direction TB
            subgraph "Hooks" 
                direction TB
                UseClipboard["useClipboard Hook\n(src/hooks/useClipboard.tsx)"]:::logic
                UseParams["useParams Hook\n(src/hooks/useParams.tsx)"]:::logic
            end
            subgraph "Utils" 
                direction TB
                Parser["ediToObject Parser\n(src/utils/ediToObject.ts)"]:::logic
                Writer["writeSegmentEdi Writer\n(src/utils/writeSegmentEdi.ts)"]:::logic
                Download["downloadFile Helper\n(src/utils/downloadFile.ts)"]:::logic
                CopyHelper["copyToClipboard Helper\n(src/utils/copyToClipboard.ts)"]:::logic
            end
        end

        %% Data Layer
        subgraph "Data Layer" 
            direction TB
            RecordsDir["Static Schemas\n(src/records)"]:::data
            RecordsIndex["Records Index\n(src/records/index.tsx)"]:::data
            subgraph "Constants" 
                direction TB
                Countries["Country Codes\n(src/constants/countries.json)"]:::data
                ConstIndex["Constants Index\n(src/constants/index.tsx)"]:::data
            end
        end

        %% Interfaces
        subgraph "Interfaces" 
            direction TB
            CountriesIF["Countries.ts\n(src/interfaces/Countries.ts)"]:::iface
            ElementIF["Element.ts\n(src/interfaces/Element.ts)"]:::iface
            ElementJSONIF["ElementJSON.ts\n(src/interfaces/ElementJSON.ts)"]:::iface
            RecordIF["Record.ts\n(src/interfaces/Record.ts)"]:::iface
            RecordJSONIF["RecordJSON.ts\n(src/interfaces/RecordJSON.ts)"]:::iface
            SegmentIF["Segment.ts\n(src/interfaces/Segment.ts)"]:::iface
        end

    end

    %% Connections
    Browser -->|loads| Entry
    Entry --> Styles
    Entry --> UploadPage
    UploadPage -->|uses FileReader| Parser
    Parser -->|reads schemas| RecordsDir
    RecordsDir --> RecordsIndex
    Parser -->|outputs typed object| ViewerPage
    ViewerPage --> AccordionItem
    ViewerPage --> JsonView
    ViewerPage --> RichView
    ViewerPage --> MissingRecords
    ViewerPage --> Navbar
    ViewerPage --> Offcanvas
    ViewerPage --> ViewerSelector
    UploadPage --> UseParams
    ViewerPage --> UseParams
    ButtonClipboard --> CopyHelper
    UseClipboard --> CopyHelper
    ViewerPage --> UseClipboard
    Parser --> ElementIF
    Parser --> RecordIF
    Parser --> RecordJSONIF
    Parser --> ElementJSONIF
    Parser --> SegmentIF
    ViewerPage --> Download

    %% Viewer subcomponents
    AccordionItem["AccordionItem\n(src/pages/ViewerEDI/AccordionItem.tsx)"]:::ui
    JsonView["JsonView\n(src/pages/ViewerEDI/JsonView.tsx)"]:::ui
    RichView["RichView\n(src/pages/ViewerEDI/RichView.tsx)"]:::ui
    MissingRecords["MissingRecords\n(src/pages/ViewerEDI/MissingRecords.tsx)"]:::ui
    Navbar["Navbar\n(src/pages/ViewerEDI/Navbar.tsx)"]:::ui
    Offcanvas["Offcanvas\n(src/pages/ViewerEDI/Offcanvas.tsx)"]:::ui
    ViewerSelector["ViewerSelector\n(src/pages/ViewerEDI/ViewerSelector.tsx)"]:::ui

    %% Click Events
    click Entry "https://github.com/eduardomar/edi-viewer/blob/master/src/index.tsx"
    click Styles "https://github.com/eduardomar/edi-viewer/blob/master/src/index.css"
    click UploadPage "https://github.com/eduardomar/edi-viewer/tree/master/src/pages/UploadEDI"
    click ViewerPage "https://github.com/eduardomar/edi-viewer/tree/master/src/pages/ViewerEDI"
    click NotFoundPage "https://github.com/eduardomar/edi-viewer/tree/master/src/pages/NotFound"
    click ButtonClipboard "https://github.com/eduardomar/edi-viewer/blob/master/src/components/ButtonClipboard.tsx"
    click EDIComponent "https://github.com/eduardomar/edi-viewer/blob/master/src/components/EDI.tsx"
    click UseClipboard "https://github.com/eduardomar/edi-viewer/blob/master/src/hooks/useClipboard.tsx"
    click UseParams "https://github.com/eduardomar/edi-viewer/blob/master/src/hooks/useParams.tsx"
    click Parser "https://github.com/eduardomar/edi-viewer/blob/master/src/utils/ediToObject.ts"
    click Writer "https://github.com/eduardomar/edi-viewer/blob/master/src/utils/writeSegmentEdi.ts"
    click Download "https://github.com/eduardomar/edi-viewer/blob/master/src/utils/downloadFile.ts"
    click CopyHelper "https://github.com/eduardomar/edi-viewer/blob/master/src/utils/copyToClipboard.ts"
    click RecordsDir "https://github.com/eduardomar/edi-viewer/tree/master/src/records"
    click RecordsIndex "https://github.com/eduardomar/edi-viewer/blob/master/src/records/index.tsx"
    click Countries "https://github.com/eduardomar/edi-viewer/blob/master/src/constants/countries.json"
    click ConstIndex "https://github.com/eduardomar/edi-viewer/blob/master/src/constants/index.tsx"
    click CountriesIF "https://github.com/eduardomar/edi-viewer/blob/master/src/interfaces/Countries.ts"
    click ElementIF "https://github.com/eduardomar/edi-viewer/blob/master/src/interfaces/Element.ts"
    click ElementJSONIF "https://github.com/eduardomar/edi-viewer/blob/master/src/interfaces/ElementJSON.ts"
    click RecordIF "https://github.com/eduardomar/edi-viewer/blob/master/src/interfaces/Record.ts"
    click RecordJSONIF "https://github.com/eduardomar/edi-viewer/blob/master/src/interfaces/RecordJSON.ts"
    click SegmentIF "https://github.com/eduardomar/edi-viewer/blob/master/src/interfaces/Segment.ts"

    %% Styles
    classDef external fill:#f2f2f2,stroke:#777,stroke-width:1px;
    classDef init fill:#d9d9d9,stroke:#444,stroke-width:1px;
    classDef ui fill:#cce5ff,stroke:#4d94ff,stroke-width:1px;
    classDef logic fill:#ffe0b3,stroke:#ff9900,stroke-width:1px;
    classDef data fill:#d6ffd6,stroke:#33cc33,stroke-width:1px;
    classDef iface fill:#e5ccff,stroke:#9933ff,stroke-width:1px;
```
