# tjtanjin/react-chatbotify architecture

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

- Interactive diagram: https://gitdiagram.com/tjtanjin/react-chatbotify
- Repository: https://github.com/tjtanjin/react-chatbotify

## Overview

Based on your provided file tree and README, this project is clearly a React library—React ChatBotify—that enables developers to integrate a full-featured chatbot into their React applications. Its main purpose is to provide an extensible, highly customizable conversational interface that supports various features (such as dynamic message flows, custom components, themes, plugins, audio/voice support, and more) while remaining compatible with multiple React versions.

##### Main Components
a. User Interface (UI) Components:
- These include all the React components found in the src/components folder such as ChatBot, ChatBotHeader, ChatBotBody (with sub-components like BotMessage, UserMessage, MediaDisplay, etc.), ChatBotInput, ChatBotFooter, ChatBotButton, and multiple button components (AudioButton, SendButton, EmojiButton, etc.).

b. State Management and Context:
- The src/context folder shows that this project uses several React Contexts (BotRefsContext, BotStatesContext, ChatBotContext, MessagesContext, PathsContext, SettingsContext, StylesContext, ToastsContext).
- These contexts hold the global state and provide the data connectivity for the chatbot’s UI interactions.

c. Business Logic and Services:
- Found in src/services (e.g., AudioService, ChatHistoryService, RcbEventService, ThemeService, and the detailed BlockService with its multiple processors such as ChatDisabledProcessor, CheckboxProcessor, ComponentProcessor, etc.).
- These components ensure that business rules (like message processing, theme management, and event handling) are encapsulated and modular.

d. Custom Hooks and Utilities:
- In the src/hooks and src/utils folders, there are numerous custom hooks (useAudio, useChatHistory, useMessages, etc.) and utilities (idGenerator, configParser, mediaFileParser, etc.) that abstract and manage functionalities across the application.

##### And Map the Relationships/Interactions
- The UI components interact with the custom hooks to retrieve or push state.
- These hooks then collaborate with the Contexts to maintain application state and pass data between various UI components.
- The services are invoked (often indirectly via hooks or contexts) to perform tasks like processing messages, handling file attachments, or managing audio.
- Data flows in a unidirectional manner—from the user interface to the hooks, then through the contexts and services, with responses propagated back to update the UI.
- The architecture clearly delineates presentation (UI), state management (contexts and hooks), and business logic (services and processors), providing a natural separation of concerns.

##### Design Principles and Architectural Patterns
- Componentization: The project splits the UI into many reusable and isolated functional components.
- Layered Architecture: There is a clear separation between the presentation layer (UI components), the state management layer (contexts and hooks), and the business logic layer (services/processors and utilities).
- Extensibility & Modularity: With configurable themes, plugins, and custom events, the design emphasizes flexibility, making it straightforward to extend or replace components without impacting the whole system.
- Testability: With folders for unit tests, integration tests (using Jest and Cypress), the design follows best practices in ensuring each component and service is isolated, testable, and maintainable.

##### Relevant Technologies, Frameworks, or Libraries
- ReactJS as the core UI framework.
- TypeScript for static type-checking.
- Various workflows (CI/CD, linting, testing) integrated via GitHub Actions and Husky for pre-commit hooks.
- Cypress for end-to-end tests and Jest for unit tests.
- Community integrations (plugins/themes) that extend the core capabilities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "UI Components"
        ChatBot["ChatBot"]
        ChatBotHeader["ChatBotHeader"]
        ChatBotBody["ChatBotBody"]
        BotMessage["BotMessage"]
        UserMessage["UserMessage"]
        MediaDisplay["MediaDisplay"]
        ChatBotInput["ChatBotInput"]
        ChatBotFooter["ChatBotFooter"]
        ChatBotButton["ChatBotButton"]
        AudioButton["AudioButton"]
        SendButton["SendButton"]
        EmojiButton["EmojiButton"]
    end

    subgraph "State Management & Custom Hooks"
        BotRefsContext["BotRefsContext"]
        BotStatesContext["BotStatesContext"]
        ChatBotContext["ChatBotContext"]
        MessagesContext["MessagesContext"]
        PathsContext["PathsContext"]
        SettingsContext["SettingsContext"]
        StylesContext["StylesContext"]
        ToastsContext["ToastsContext"]
        useAudio["useAudio"]
        useChatHistory["useChatHistory"]
        useMessages["useMessages"]
        useChatWindow["useChatWindow"]
    end

    subgraph "Business Logic & Services"
        AudioService["AudioService"]
        ChatHistoryService["ChatHistoryService"]
        BlockService["BlockService"]
        ChatDisabledProcessor["ChatDisabledProcessor"]
        CheckboxProcessor["CheckboxProcessor"]
        ComponentProcessor["ComponentProcessor"]
        FunctionProcessor["FunctionProcessor"]
        IsSensitiveProcessor["IsSensitiveProcessor"]
        MessageProcessor["MessageProcessor"]
        OptionProcessor["OptionProcessor"]
        PathProcessor["PathProcessor"]
        TransitionProcessor["TransitionProcessor"]
        RcbEventService["RcbEventService"]
        ThemeService["ThemeService"]
        VoiceService["VoiceService"]
    end

    subgraph "Utilities"
        idGenerator["idGenerator"]
        configParser["configParser"]
        mediaFileParser["mediaFileParser"]
        messageBuilder["messageBuilder"]
    end

    %% UI Components interact with State Management & Custom Hooks
    ChatBot -->|"triggersUpdate"| ChatBotContext
    ChatBotInput -->|"handlesInput"| useChatWindow
    ChatBotButton -->|"onClick"| useMessages
    ChatBotBody -->|"displaysMsg"| MessagesContext

    %% Custom Hooks and Contexts invoke Services
    useAudio -->|"invokesAudio"| AudioService
    useChatHistory -->|"recordsHistory"| ChatHistoryService
    useChatWindow -->|"updatesWindow"| ChatBotContext
    ChatBotContext -->|"callsBlockService"| BlockService
    MessagesContext -->|"routesMessages"| BlockService
    BotStatesContext -->|"updatesState"| BlockService
    BotRefsContext -->|"providesRefs"| BlockService

    %% Business Logic internal processing
    BlockService -->|"usesProcessor"| ChatDisabledProcessor
    BlockService -->|"usesProcessor"| CheckboxProcessor
    BlockService -->|"usesProcessor"| ComponentProcessor
    BlockService -->|"usesProcessor"| FunctionProcessor
    BlockService -->|"usesProcessor"| IsSensitiveProcessor
    BlockService -->|"usesProcessor"| MessageProcessor
    BlockService -->|"usesProcessor"| OptionProcessor
    BlockService -->|"usesProcessor"| PathProcessor
    BlockService -->|"usesProcessor"| TransitionProcessor

    %% Business Logic updates UI and coordinates events
    AudioService -->|"processesAudio"| VoiceService
    ThemeService -->|"updatesTheme"| ChatBotHeader
    ChatHistoryService -->|"logsHistory"| ChatBotBody
    RcbEventService -->|"sendsEvents"| ChatBotContext

    %% Business Logic utilizes Utilities
    BlockService -->|"utilizes"| idGenerator
    BlockService -->|"parsesConfig"| configParser
    BlockService -->|"parsesMedia"| mediaFileParser
    BlockService -->|"buildsMsg"| messageBuilder

    %% Styles for layers
    classDef ui fill:#D1E8FF,stroke:#333,stroke-width:2px;
    classDef state fill:#C2F0C2,stroke:#333,stroke-width:2px;
    classDef business fill:#FFEFD5,stroke:#333,stroke-width:2px;
    classDef utilities fill:#FDEBD0,stroke:#333,stroke-width:2px;

    class ChatBot,ChatBotHeader,ChatBotBody,BotMessage,UserMessage,MediaDisplay,ChatBotInput,ChatBotFooter,ChatBotButton,AudioButton,SendButton,EmojiButton ui;
    class BotRefsContext,BotStatesContext,ChatBotContext,MessagesContext,PathsContext,SettingsContext,StylesContext,ToastsContext,useAudio,useChatHistory,useMessages,useChatWindow state;
    class AudioService,ChatHistoryService,BlockService,ChatDisabledProcessor,CheckboxProcessor,ComponentProcessor,FunctionProcessor,IsSensitiveProcessor,MessageProcessor,OptionProcessor,PathProcessor,TransitionProcessor,RcbEventService,ThemeService,VoiceService business;
    class idGenerator,configParser,mediaFileParser,messageBuilder utilities;

    %% Click Events for UI Components
    click ChatBot "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/ChatBot.tsx"
    click ChatBotHeader "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/ChatBotHeader/ChatBotHeader.tsx"
    click ChatBotBody "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/ChatBotBody/ChatBotBody.tsx"
    click BotMessage "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/ChatBotBody/BotMessage/BotMessage.tsx"
    click UserMessage "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/ChatBotBody/UserMessage/UserMessage.tsx"
    click MediaDisplay "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/ChatBotBody/MediaDisplay/MediaDisplay.tsx"
    click ChatBotInput "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/ChatBotInput/ChatBotInput.tsx"
    click ChatBotFooter "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/ChatBotFooter/ChatBotFooter.tsx"
    click ChatBotButton "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/ChatBotButton/ChatBotButton.tsx"
    click AudioButton "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/Buttons/AudioButton/AudioButton.tsx"
    click SendButton "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/Buttons/SendButton/SendButton.tsx"
    click EmojiButton "https://github.com/tjtanjin/react-chatbotify/blob/main/src/components/Buttons/EmojiButton/EmojiButton.tsx"

    %% Click Events for State Management & Custom Hooks
    click BotRefsContext "https://github.com/tjtanjin/react-chatbotify/blob/main/src/context/BotRefsContext.tsx"
    click BotStatesContext "https://github.com/tjtanjin/react-chatbotify/blob/main/src/context/BotStatesContext.tsx"
    click ChatBotContext "https://github.com/tjtanjin/react-chatbotify/blob/main/src/context/ChatBotContext.tsx"
    click MessagesContext "https://github.com/tjtanjin/react-chatbotify/blob/main/src/context/MessagesContext.tsx"
    click PathsContext "https://github.com/tjtanjin/react-chatbotify/blob/main/src/context/PathsContext.tsx"
    click SettingsContext "https://github.com/tjtanjin/react-chatbotify/blob/main/src/context/SettingsContext.tsx"
    click StylesContext "https://github.com/tjtanjin/react-chatbotify/blob/main/src/context/StylesContext.tsx"
    click ToastsContext "https://github.com/tjtanjin/react-chatbotify/blob/main/src/context/ToastsContext.tsx"
    click useAudio "https://github.com/tjtanjin/react-chatbotify/blob/main/src/hooks/useAudio.ts"
    click useChatHistory "https://github.com/tjtanjin/react-chatbotify/blob/main/src/hooks/useChatHistory.ts"
    click useMessages "https://github.com/tjtanjin/react-chatbotify/blob/main/src/hooks/useMessages.ts"
    click useChatWindow "https://github.com/tjtanjin/react-chatbotify/blob/main/src/hooks/useChatWindow.ts"

    %% Click Events for Business Logic & Services
    click AudioService "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/AudioService.ts"
    click ChatHistoryService "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/ChatHistoryService.tsx"
    click BlockService "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/BlockService/BlockService.ts"
    click ChatDisabledProcessor "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/BlockService/ChatDisabledProcessor.ts"
    click CheckboxProcessor "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/BlockService/CheckboxProcessor.tsx"
    click ComponentProcessor "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/BlockService/ComponentProcessor.ts"
    click FunctionProcessor "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/BlockService/FunctionProcessor.ts"
    click IsSensitiveProcessor "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/BlockService/IsSensitiveProcessor.ts"
    click MessageProcessor "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/BlockService/MessageProcessor.ts"
    click OptionProcessor "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/BlockService/OptionProcessor.tsx"
    click PathProcessor "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/BlockService/PathProcessor.ts"
    click TransitionProcessor "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/BlockService/TransitionProcessor.ts"
    click RcbEventService "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/RcbEventService.ts"
    click ThemeService "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/ThemeService.ts"
    click VoiceService "https://github.com/tjtanjin/react-chatbotify/blob/main/src/services/VoiceService.ts"

    %% Click Events for Utilities
    click idGenerator "https://github.com/tjtanjin/react-chatbotify/blob/main/src/utils/idGenerator.ts"
    click configParser "https://github.com/tjtanjin/react-chatbotify/blob/main/src/utils/configParser.ts"
    click mediaFileParser "https://github.com/tjtanjin/react-chatbotify/blob/main/src/utils/mediaFileParser.ts"
    click messageBuilder "https://github.com/tjtanjin/react-chatbotify/blob/main/src/utils/messageBuilder.ts"
```
