# polkassembly/polkassembly architecture

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

- Interactive diagram: https://gitdiagram.com/polkassembly/polkassembly
- Repository: https://github.com/polkassembly/polkassembly

## Overview

##### Project type and purpose
- This is a full-stack, serverless Next.js application (“polkassembly”) for browsing, discussing, and voting on Polkadot/Kusama governance proposals.
- The frontend is a React/Next.js UI with server-side rendering (SSR) + static generation.
- The backend consists of Next.js API routes plus standalone Firebase Cloud Functions for background jobs (analytics, search indexing).

##### High-level file-structure overview
- “pages/” → Next.js pages (UI routes) and API routes under pages/api/v1/…
- “src/” → shared client & server utilities, React components, Redux store, hooks, auth, services.
- “cloud-functions/” → Firebase Cloud Functions for scheduled/indexing tasks (trackLevelAnalytics, updateNewProposalsInAlgolia).
- Config & infra: firebase.json, .firebaserc, next.config.js, tsconfig.json.

##### Key README insights
- A Next.js full-stack app deployed to Vercel or Firebase Hosting.
- API routes are under pages/api and ephemeral Cloud Functions handle heavy or scheduled backend work.

##### Main components
1. Client (browser or mobile web)
2. CDN/Hosting layer (Vercel or Firebase Hosting + Cloud Front)
3. Next.js Serverless Functions
- SSR/SSG renderer (handles pages/*)
- API endpoints (pages/api/v1/*)
4. Firebase Cloud Functions (cloud-functions folder)
- Analytics jobs (trackLevelAnalytics)
- Search indexing jobs (updateNewProposalsInAlgolia)
5. Data stores & caches
- Firestore (primary document DB for posts, comments, profiles, notifications)
- Redis (short-lived caching, auth token storage)
6. External blockchain & data APIs
- Polkadot/Substrate RPC via polkadot.js
- Subsquid & Subscan for on-chain query data
7. Third-party services
- Algolia (search index)
- Sentry (error tracking)
- Google Analytics / instrumentation.ts
- Email/2FA/Social callbacks for auth
8. Client-side state store
- Redux (store.ts) for in-app state management

##### Component relationships & data flows
- Browser → CDN/Hosting → Next.js SSR Function → Firestore / Redis / Blockchain RPC → returns fully-rendered page.
- Browser (SPA navigation) → Next.js API Function → Firestore + external APIs → JSON response.
- Cloud Functions (scheduled or HTTP-triggered) → Subsquid/Subscan → compute analytics → update Firestore or Algolia.
- On-chain events (via Firestore triggers or schedules) → Cloud Functions → update search index.
- User actions (vote/comment) → Next.js API → Firestore writes → in-app notification updates.

##### Architectural patterns & design principles
- Serverless JAMstack: SSR pages + static pages + lightweight API endpoints.
- Microservices-style separation: UI/API vs. background Cloud Functions.
- Single-page app navigation with client-side hydration (Redux).
- CQRS-lite: user requests handled by API; analytics/indexing offloaded to separate functions.

##### Technologies & frameworks
- Frontend: Next.js, React, TypeScript, Redux, Tailwind CSS.
- Backend (serverless API): Next.js API routes (Node.js/Express style), Firebase Cloud Functions (TypeScript).
- Data: Firebase Firestore, Redis.
- External: Polkadot.js RPC, Subsquid, Subscan.
- Search: Algolia.
- Monitoring: Sentry, Google Analytics.
- Auth: JWT, MetaMask, WalletConnect, email+2FA.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "UI Layer"
        Browser["Browser (Client)"]:::ui
        CDN["CDN/Hosting"]:::ui
        Redux["Redux Store"]:::ui
        GA["Google Analytics"]:::external
    end

    %% Hosting & Compute
    subgraph "Hosting & Compute"
        subgraph "Next.js Serverless Functions"
            SSR["Next.js SSR/SSG Renderer"]:::compute
            API["Next.js API Endpoints"]:::compute
        end
        subgraph "Firebase Cloud Functions"
            Analytics["Analytics Jobs"]:::compute
            Indexing["Search Indexing Jobs"]:::compute
        end
    end

    %% Data Layer
    subgraph "Data Layer"
        Firestore["Firestore DB"]:::data
        Redis["Redis Cache"]:::data
    end

    %% External Integrations
    subgraph "External Integrations"
        Polka["Polkadot/Substrate RPC"]:::external
        Subsquid["Subsquid & Subscan Fetchers"]:::external
        Algolia["Algolia Index"]:::external
        Sentry["Sentry Error Tracking"]:::external
    end

    %% Data Flows
    Browser -->|Request| CDN
    CDN -->|SSR Page Request| SSR
    SSR -->|Reads/Writes| Firestore
    SSR -->|Cache Get/Set| Redis
    SSR -->|RPC Calls| Polka

    CDN -->|API Request| API
    API -->|Reads/Writes| Firestore
    API -->|Cache Get/Set| Redis
    API -->|RPC Calls| Polka
    API -->|On-chain Data| Subsquid

    Analytics -->|Fetch On-chain Data| Subsquid
    Analytics -->|Compute & Write| Firestore
    Analytics -->|Update| Algolia

    Indexing -->|Listen Triggers| Firestore
    Indexing -->|Update| Algolia

    Firestore -.->|Trigger| Analytics
    Firestore -.->|Trigger| Indexing

    Browser -->|Client-side State| Redux
    Browser -->|Instrument| GA
    SSR -->|Errors| Sentry
    API -->|Errors| Sentry
    Analytics -->|Errors| Sentry
    Indexing -->|Errors| Sentry

    %% Click Events
    click SSR "https://github.com/polkassembly/polkassembly/tree/main//pages/"
    click API "https://github.com/polkassembly/polkassembly/tree/main//pages/api/v1/"
    click Analytics "https://github.com/polkassembly/polkassembly/tree/main//cloud-functions/src/trackLevelAnalytics/"
    click Indexing "https://github.com/polkassembly/polkassembly/blob/main//cloud-functions/src/updateNewProposalsInAlgolia/index.ts"
    click Firestore "https://github.com/polkassembly/polkassembly/blob/main//src/services/firebaseInit.ts"
    click Redis "https://github.com/polkassembly/polkassembly/blob/main//src/auth/redis.ts"
    click Polka "https://github.com/polkassembly/polkassembly/tree/main//src/polkassemblyutils/"
    click Subsquid "https://github.com/polkassembly/polkassembly/blob/main//cloud-functions/src/utils/fetchSubsquid.ts"
    click Algolia "https://github.com/polkassembly/polkassembly/blob/main//cloud-functions/src/updateNewProposalsInAlgolia/index.ts"
    click GA "https://github.com/polkassembly/polkassembly/blob/main//instrumentation.ts"
    click GA "https://github.com/polkassembly/polkassembly/blob/main//analytics.ts"
    click Sentry "https://github.com/polkassembly/polkassembly/blob/main//sentry.server.config.ts"
    click Redux "https://github.com/polkassembly/polkassembly/blob/main//src/redux/store.ts"

    %% Styles
    classDef ui fill:#add8e6,stroke:#000
    classDef compute fill:#98fb98,stroke:#000
    classDef data fill:#ffa500,stroke:#000
    classDef external fill:#dda0dd,stroke:#000
```
