# matiasbattocchia/open-bsp-ui architecture

> Architecture diagram of the GitHub repository matiasbattocchia/open-bsp-ui, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/matiasbattocchia/open-bsp-ui
- Repository: https://github.com/matiasbattocchia/open-bsp-ui

## Overview

1. **High-level shape**
   - This repository is a **single-page frontend application** called OpenBSP UI.
   - It is built with **React + TypeScript + Vite** and deployed as static assets.
   - The app is the user interface for the external **OpenBSP API / Supabase-backed backend**; there is no application server in this repo.

2. **Primary boundary: UI vs backend**
   - The frontend talks to **Supabase** through `src/supabase/client.ts` and typed DB models in `src/supabase/db_types.ts`.
   - Backend interaction is organized mostly through `src/queries/*`, which encapsulate remote reads/writes for domain resources.
   - Real-time chat updates come from **Supabase Realtime**, not polling.

3. **Navigation and page architecture**
   - Routing is handled by **TanStack Router** with a nested route tree under `src/routes/`.
   - The app has a public/auth boundary: `login`, `oauth/callback`, and onboarding routes are separate from the authenticated shell in `_auth`.
   - The authenticated area contains resource-specific subtrees for **agents, contacts, conversations, integrations, settings, and stats**.

4. **Domain modules**
   - The authenticated experience is split into clear product domains:
     - **Conversations**: chat list, chat view, message composer, filters, search, and footer/header controls.
     - **Contacts**: CRUD screens for address book entries.
     - **AI Agents**: agent creation/configuration, tool setup, instructions.
     - **Templates**: WhatsApp template editor, picker, preview, and variable handling.
     - **Integrations**: WhatsApp onboarding/signup and media preprocessing.
     - **Settings**: organization, members/roles, API keys, webhooks.
     - **Stats**: usage and quota dashboards.

5. **Chat and messaging subsystem**
   - The chat UI is decomposed into dedicated components under `src/components/Chat*` and `src/components/Message/*`.
   - Messages support **text, images, audio, and documents**, plus delivery/status indicators.
   - Conversation state is managed with a combination of **query data**, local UI state, and the `chatSlice` Zustand store.
   - Utility modules such as `MessageUtils`, `ConversationUtils`, and `MessageStatusUtils` centralize message formatting and status logic.

6. **State management layers**
   - **TanStack Query** is used for server state and resource fetching/mutations across agents, contacts, organizations, templates, webhooks, billing, and WhatsApp signup.
   - **Zustand** is used for local app state, especially UI and chat-related transient state in `src/stores/`.
   - There are also shared React contexts and hooks for cross-cutting concerns like WhatsApp integration and timed refresh ticks.

7. **Realtime and lifecycle flows**
   - The app’s most important live flow is the **conversation stream**: backend events arrive via Supabase Realtime and update chat UI immediately.
   - `src/hooks/useRealtimeSubscription.ts` and the query layer likely coordinate these updates with cached data.
   - Initial app data and active organization context are handled by hooks like `useInitalDataFetch` and `useSetActiveOrg`, which establish the authenticated tenant scope before rendering domain pages.

8. **Multi-tenant organization model**
   - The UI is explicitly **multi-org**: users switch organizations and all domain resources are scoped to the active org.
   - Organization-specific resources include members, addresses, templates, webhooks, billing/statistics, and WhatsApp integrations.
   - This tenant boundary is a core architectural constraint; most queries and routes are organized around org membership and org address IDs.

9. **WhatsApp integration flow**
   - WhatsApp Embedded Signup and onboarding are first-class flows under `src/routes/_auth/integrations/whatsapp/*`.
   - The UI supports connecting WhatsApp Business accounts, then managing org addresses and message templates for that integration.
   - Optional environment variables in the README (`VITE_META_APP_ID`, `VITE_FB_LOGIN_CONFIG_ID`) indicate external Meta/Facebook integration points that shape the onboarding flow.

10. **Internationalization and presentation layer**
   - All user-facing text goes through `src/hooks/useTranslation.tsx`, with source strings in Spanish and translation files in `public/locales/*.json`.
   - The UI includes **Spanish, English, Portuguese, French, and Swahili** resources, with a script to sync translation keys.
   - The component layer is broad but reusable: forms, headers, modals/menus, avatar/button primitives, chat controls, and stats widgets are shared across routes.

11. **Offline/assets/runtime concerns**
   - `public/service-worker.js`, `manifest.json`, and `notification.mp3` show some PWA-like client capabilities, especially for notifications and app shell behavior.
   - Static assets and locale files are bundled with the frontend, while the app itself remains a browser-executed SPA.
   - `src/utils/IdbUtils.ts` suggests some local persistence or browser storage support for selected data flows.

12. **Build and deployment pipeline**
   - The project is built with **Vite**, linted with ESLint, formatted with Prettier, and checked in CI via GitHub Actions.
   - Deployment is intentionally simple: build to `dist` and host on any static platform, with Cloudflare Pages called out in the README.
   - Runtime configuration is entirely environment-driven through `VITE_*` variables, reinforcing the frontend/backend boundary.

13. **How to draw the architecture**
   - At the center is a **React SPA shell** with router-driven screens.
   - On one side, the SPA consumes **Supabase/OpenBSP API** for auth, data CRUD, file/media metadata, billing, and realtime events.
   - On the other side, shared UI components, query hooks, and store slices fan out into the product domains: conversations, contacts, agents, templates, integrations, settings, and stats.

Main entry points: [`src/routes/_auth.tsx`](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth.tsx) (Auth Shell), [`src/queries/queryKeys.ts`](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/queries/queryKeys.ts) (Queries), [`src/routes/_auth/conversations/index.tsx`](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/conversations/index.tsx) (Conversations), [`src/routes/__root.tsx`](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/__root.tsx) (Root Router)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_shell["App Shell"]
  node_n_main["Main App<br/>react-spa<br/>[main.tsx]"]
  node_n_root_route["Root Router<br/>tanstack-router<br/>[__root.tsx]"]
  node_n_auth_route["Auth Shell<br/>authenticated-layout<br/>[_auth.tsx]"]
  node_n_public_routes["Public Routes<br/>auth-entrypoints<br/>[login.tsx]"]
end

subgraph group_grp_domain["Product Domains"]
  node_n_conv_routes["Conversations<br/>chat-pages<br/>[index.tsx]"]
  node_n_contacts_routes["Contacts<br/>crm-pages<br/>[index.tsx]"]
  node_n_agents_routes["Agents<br/>agent-pages<br/>[index.tsx]"]
  node_n_templates_routes["Templates<br/>template-pages<br/>[index.tsx]"]
  node_n_whatsapp_routes["WhatsApp<br/>integration-pages<br/>[index.tsx]"]
  node_n_settings_routes["Settings<br/>org-admin-pages<br/>[index.tsx]"]
  node_n_stats_routes["Stats<br/>usage-pages<br/>[index.tsx]"]
end

subgraph group_grp_data["Data Layer"]
  node_n_queries["Queries<br/>server-state<br/>[queryKeys.ts]"]
  node_n_stores["Stores<br/>zustand-state<br/>[useBoundStore.ts]"]
  node_n_hooks["App Hooks<br/>lifecycle-hooks"]
  node_n_supabase[("Supabase Client<br/>backend-adapter<br/>[client.ts]")]
end

subgraph group_grp_support["Shared Support"]
  node_n_chat_ui["Chat UI<br/>messaging-components<br/>[Chat.tsx]"]
  node_n_message_ui["Messages<br/>message-components<br/>[Message.tsx]"]
  node_n_template_ui["Template UI<br/>template-components<br/>[TemplateEditor.tsx]"]
  node_n_whatsapp_ui["WhatsApp UI<br/>integration-components"]
  node_n_stats_ui["Stats UI<br/>dashboard-components<br/>[StatsCenter.tsx]"]
  node_n_i18n["Translations<br/>i18n-assets<br/>[translations.ts]"]
end

node_n_main -->|"bootstraps"| node_n_root_route
node_n_root_route -->|"public"| node_n_public_routes
node_n_root_route -->|"protected"| node_n_auth_route
node_n_auth_route -->|"screens"| node_n_conv_routes
node_n_auth_route -->|"screens"| node_n_contacts_routes
node_n_auth_route -->|"screens"| node_n_agents_routes
node_n_auth_route -->|"screens"| node_n_whatsapp_routes
node_n_auth_route -->|"screens"| node_n_templates_routes
node_n_auth_route -->|"screens"| node_n_settings_routes
node_n_auth_route -->|"screens"| node_n_stats_routes
node_n_conv_routes -->|"uses"| node_n_chat_ui
node_n_conv_routes -->|"renders"| node_n_message_ui
node_n_templates_routes -->|"uses"| node_n_template_ui
node_n_whatsapp_routes -->|"uses"| node_n_whatsapp_ui
node_n_stats_routes -->|"uses"| node_n_stats_ui
node_n_conv_routes -->|"fetches"| node_n_queries
node_n_contacts_routes -->|"fetches"| node_n_queries
node_n_agents_routes -->|"fetches"| node_n_queries
node_n_templates_routes -->|"fetches"| node_n_queries
node_n_whatsapp_routes -->|"fetches"| node_n_queries
node_n_settings_routes -->|"fetches"| node_n_queries
node_n_stats_routes -->|"fetches"| node_n_queries
node_n_hooks -->|"connects"| node_n_supabase
node_n_queries -->|"reads/writes"| node_n_supabase
node_n_stores -->|"state"| node_n_chat_ui
node_n_hooks -->|"loads"| node_n_i18n
node_n_chat_ui -->|"uses"| node_n_stores
node_n_auth_route -->|"initializes"| node_n_hooks

click node_n_main "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/main.tsx"
click node_n_root_route "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/__root.tsx"
click node_n_auth_route "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth.tsx"
click node_n_public_routes "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/login.tsx"
click node_n_conv_routes "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/conversations/index.tsx"
click node_n_contacts_routes "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/contacts/index.tsx"
click node_n_agents_routes "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/agents/index.tsx"
click node_n_templates_routes "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/integrations/whatsapp/$orgAddressId/templates/index.tsx"
click node_n_whatsapp_routes "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/integrations/whatsapp/index.tsx"
click node_n_settings_routes "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/settings/index.tsx"
click node_n_stats_routes "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/stats/index.tsx"
click node_n_chat_ui "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/components/Chat.tsx"
click node_n_message_ui "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/components/Message/Message.tsx"
click node_n_template_ui "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/components/TemplateEditor.tsx"
click node_n_whatsapp_ui "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/components/WhatsAppIntegration.tsx"
click node_n_stats_ui "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/components/stats/StatsCenter.tsx"
click node_n_queries "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/queries/queryKeys.ts"
click node_n_stores "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/stores/useBoundStore.ts"
click node_n_hooks "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/hooks/useInitalDataFetch.ts"
click node_n_supabase "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/supabase/client.ts"
click node_n_i18n "https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/i18n/translations.ts"

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_n_main,node_n_root_route,node_n_auth_route,node_n_public_routes toneBlue
class node_n_conv_routes,node_n_contacts_routes,node_n_agents_routes,node_n_templates_routes,node_n_whatsapp_routes,node_n_settings_routes,node_n_stats_routes toneAmber
class node_n_queries,node_n_stores,node_n_hooks,node_n_supabase toneMint
class node_n_chat_ui,node_n_message_ui,node_n_template_ui,node_n_whatsapp_ui,node_n_stats_ui,node_n_i18n toneRose
```

## Components

### App Shell

Routing, auth boundary, and bootstrapping for the SPA.

- [Main App](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/main.tsx): react-spa · Vite entrypoint that mounts the client application. · `src/main.tsx`
- [Root Router](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/__root.tsx): tanstack-router · Top-level route tree and app shell composition. · `src/routes/__root.tsx`
- [Auth Shell](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth.tsx): authenticated-layout · Protected layout that wraps the signed-in application. · `src/routes/_auth.tsx`
- [Public Routes](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/login.tsx): auth-entrypoints · Login, OAuth callback, and onboarding entry screens. · `src/routes/login.tsx`

### Product Domains

Authenticated feature areas exposed through the route tree.

- [Conversations](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/conversations/index.tsx): chat-pages · Conversation list and conversation detail routes. · `src/routes/_auth/conversations/index.tsx`
- [Contacts](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/contacts/index.tsx): crm-pages · Contact list and CRUD screens for address book entries. · `src/routes/_auth/contacts/index.tsx`
- [Agents](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/agents/index.tsx): agent-pages · AI agent creation and configuration screens. · `src/routes/_auth/agents/index.tsx`
- [Templates](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/integrations/whatsapp/$orgAddressId/templates/index.tsx): template-pages · WhatsApp template management under integration scope. · `src/routes/_auth/integrations/whatsapp/$orgAddressId/templates/index.tsx`
- [WhatsApp](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/integrations/whatsapp/index.tsx): integration-pages · WhatsApp onboarding, signup, and org address management. · `src/routes/_auth/integrations/whatsapp/index.tsx`
- [Settings](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/settings/index.tsx): org-admin-pages · Organization, members, API keys, and webhooks screens. · `src/routes/_auth/settings/index.tsx`
- [Stats](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/routes/_auth/stats/index.tsx): usage-pages · Usage and quota dashboards. · `src/routes/_auth/stats/index.tsx`

### Data Layer

Server-state, local state, and backend access adapters.

- [Queries](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/queries/queryKeys.ts): server-state · TanStack Query hooks for agents, contacts, orgs, templates, billing, keys, webhooks, and signup. · `src/queries/queryKeys.ts`
- [Stores](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/stores/useBoundStore.ts): zustand-state · Transient UI and chat state slices. · `src/stores/useBoundStore.ts`
- [App Hooks](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/hooks/useInitalDataFetch.ts): lifecycle-hooks · Bootstrapping, auth, active org, realtime subscription, translation, and timing hooks. · `src/hooks/useInitalDataFetch.ts`
- [Supabase Client](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/supabase/client.ts): backend-adapter · Supabase client and typed database models used for auth, CRUD, and realtime. · `src/supabase/client.ts`

### Shared Support

Cross-cutting UI, i18n, and lifecycle helpers.

- [Chat UI](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/components/Chat.tsx): messaging-components · Conversation view, list, filters, search, header, footer, and composer controls. · `src/components/Chat.tsx`
- [Messages](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/components/Message/Message.tsx): message-components · Renderers for text, image, audio, and document messages plus status indicators. · `src/components/Message/Message.tsx`
- [Template UI](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/components/TemplateEditor.tsx): template-components · Editor, picker, preview, and variable handling for templates. · `src/components/TemplateEditor.tsx`
- [WhatsApp UI](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/components/WhatsAppIntegration.tsx): integration-components · Embedded signup and WhatsApp setup components. · `src/components/WhatsAppIntegration.tsx`
- [Stats UI](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/components/stats/StatsCenter.tsx): dashboard-components · Quota and usage charts used by the stats pages. · `src/components/stats/StatsCenter.tsx`
- [Translations](https://github.com/matiasbattocchia/open-bsp-ui/blob/main/src/i18n/translations.ts): i18n-assets · Translation runtime and locale bundles for multilingual UI copy. · `src/i18n/translations.ts`

## Connections

- Main App → Root Router: bootstraps · Application entry mounts the router-driven SPA.
- Root Router → Public Routes: public · Routes for login, OAuth callback, and onboarding.
- Root Router → Auth Shell: protected · Authenticated shell wraps the main application.
- Auth Shell → Conversations: screens · Conversation area under authenticated navigation.
- Auth Shell → Contacts: screens · Contact management area.
- Auth Shell → Agents: screens · Agent management area.
- Auth Shell → WhatsApp: screens · WhatsApp integration and onboarding area.
- Auth Shell → Templates: screens · Template management nested under WhatsApp integration.
- Auth Shell → Settings: screens · Organization administration area.
- Auth Shell → Stats: screens · Usage and quota dashboards.
- Conversations → Chat UI: uses · Conversation pages are built from chat components.
- Conversations → Messages: renders · Message body types and status indicators.
- Templates → Template UI: uses · Template screens rely on editor and preview components.
- WhatsApp → WhatsApp UI: uses · Onboarding and connection screens use WhatsApp-specific UI.
- Stats → Stats UI: uses · Stats pages render charts and quota widgets.
- Conversations → Queries: fetches · Conversation data and mutations flow through query hooks.
- Contacts → Queries: fetches · Contacts and addresses are server-state resources.
- Agents → Queries: fetches · Agent CRUD uses query hooks.
- Templates → Queries: fetches · Template CRUD and org-address scoped data.
- WhatsApp → Queries: fetches · WhatsApp signup and onboarding state.
- Settings → Queries: fetches · Org admin resources such as members, keys, and webhooks.
- Stats → Queries: fetches · Usage and billing data come from query hooks.
- App Hooks → Supabase Client: connects · Auth, active org, and realtime hooks bind to Supabase.
- Queries → Supabase Client: reads/writes · Server-state hooks call the backend adapter.
- Stores → Chat UI: state · Chat slice supports transient UI and message stream state.
- App Hooks → Translations: loads · Translation hook resolves locale bundles and strings.
- Chat UI → Stores: uses · Chat interactions coordinate with local UI state.
- Auth Shell → App Hooks: initializes · Authenticated shell depends on bootstrapping hooks.
