# casualoffice/sheets architecture

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

- Interactive diagram: https://gitdiagram.com/casualoffice/sheets
- Repository: https://github.com/casualoffice/sheets

## Overview

1. **Top-level shape**
   - This is a monorepo for a spreadsheet product with three main runtime surfaces: the self-hosted web app in `apps/web`, the backend in `apps/server`, and the embeddable SDK in `packages/sdk`.
   - The repo also contains docs, Playwright E2E tests, Docker assets, and a pinned Univer fork workspace under `vendor/` for upstream spreadsheet-engine work.

2. **Primary runtime deployment**
   - In the deployed product, a single container serves the built SPA, exposes HTTP APIs, and hosts the collaboration WebSocket endpoint.
   - Redis is an optional sidecar used for room persistence and Yjs update storage; without it, the app can run in transient in-memory mode.
   - The same server handles both anonymous “open a workbook” usage and named room-based co-editing via `/r/:roomId`.

3. **Backend boundary: HTTP + WebSocket collaboration**
   - `apps/server` is the backend boundary for room lifecycle, seed upload/download, snapshot upload/download, room diagnostics, health checks, and the `/yjs` WebSocket upgrade path.
   - The collaboration protocol is Yjs/Hocuspocus over WebSocket, with Fastify used for the HTTP layer and direct WS routing for sync traffic.
   - Room password checks, snapshot preflight, and client counts are all server concerns rather than frontend concerns.

4. **Frontend editor shell**
   - `apps/web` is the main browser application, built with React, TypeScript, and Vite.
   - Its role is not just rendering sheets, but providing the Office-like shell around the engine: title bar, menus, ribbon, dialogs, right-side panels, status bar, and mobile variants.
   - The large `shell/` subtree is the UI command surface; `home/`, `auth/`, `admin/`, and `collab/` are separate app-level flows around the editor.

5. **Spreadsheet engine integration**
   - The editor is layered on Univer OSS, with `apps/web/src/univer*` and related context/hooks acting as the glue between the custom UI and the spreadsheet engine.
   - This layer is where workbook, sheet, theme, zoom, plugins, formula execution, and engine-level policy are wired together.
   - The repo emphasizes an engine boundary: the shell drives commands into Univer, rather than reimplementing spreadsheet mechanics in React.

6. **Collaboration data flow**
   - Shared editing state flows through Yjs documents, presence tracking, and room metadata.
   - The `collab/` area handles avatar presence, live cursors, view-only mode, share links, room join metadata, and op-log related UI.
   - The key architectural boundary is that collaboration is not only visual: some enforcement, such as view-only mutation blocking, happens at the engine layer.

7. **File I/O pipeline**
   - Workbook import/export is split off from the main UI and done in workers to avoid blocking the main thread on large files.
   - `apps/web/src/xlsx` and `packages/sdk/src/xlsx` handle `.xlsx` parsing/serialization; ODS and CSV/TSV support is also present.
   - The repository treats Excel fidelity as a first-class pipeline: comments, drawings, tables, pivots, conditional formatting, data validation, and other workbook metadata are round-tripped where possible.

8. **Worker and off-main-thread architecture**
   - Heavy operations are intentionally isolated in Web Workers: xlsx parsing/export, formula worker execution, and some embed/runtime work.
   - This keeps interaction responsive while opening large files, exporting, or recalculating formulas.
   - The `apps/web/src/ai/*`, `univer/formula-worker.ts`, and `packages/sdk/src/xlsx/*worker*` files show this pattern repeated across features.

9. **Storage and persistence modes**
   - The product supports multiple storage modes: anonymous ephemeral use, personal mode with persisted user/workbook data, and WOPI-style integration for host-managed documents.
   - Personal mode introduces an admin panel and authenticated user/workbook scoping, with local storage or other configured backends.
   - Autosave and recent-file features use browser-side persistence such as IndexedDB, while room collaboration persistence lives server-side.

10. **SDK / embed surface**
   - `packages/sdk` is a separate package boundary for embedding the editor in another product.
   - It exposes a `CasualSheets` React component, embed transports, chrome primitives, collab helpers, and workbook import/export utilities.
   - The SDK is designed as a data boundary: the host app provides initial data and receives snapshots back on save/exit, rather than the SDK owning persistence.

11. **Admin and customization layer**
   - The `apps/web/src/admin` subtree is the runtime admin UI for configuring deployment concerns such as branding, storage, networking, auth, limits, and webhooks.
   - This is distinct from the editor surface; it configures the server/product instance rather than workbook content.
   - The docs indicate this is the place to wire JWT/OIDC/SAML-style customization and webhook integrations.

12. **Feature modules around the core editor**
   - The repo has feature-oriented subsystems for charts, pivots, outlines, sparklines, comments, version history, file sources, recovery, print/export, and autosave.
   - These modules generally sit between the shell and the engine: they present UI, issue workbook commands, and sometimes persist auxiliary resources.
   - They are important diagram nodes because they explain the product’s breadth without being separate services.

13. **External dependencies that matter architecturally**
   - Univer OSS is the spreadsheet engine, Yjs/Hocuspocus is the collaboration fabric, Redis is the optional state backend, and Playwright is the system test harness.
   - ExcelJS and SheetJS are the key file-format dependencies; React/Vite/TypeScript define the UI runtime; Docker is the default deployment unit.
   - The repository also carries a Univer fork reference in `vendor/`, but the main product is built around the OSS upstream package set.

14. **Quality gates and delivery**
   - The architecture is backed by extensive unit and E2E coverage, with many tests focused on collaboration, file fidelity, and spreadsheet UX regressions.
   - CI/CD, Docker publishing, and Pages deployment are first-class in the repo, indicating the project is shipped as both a hosted app and a distributable image/package.

Main entry points: [`apps/web/src/shell`](https://github.com/casualoffice/sheets/tree/main/apps/web/src/shell) (Editor shell), [`apps/web/src/main.tsx`](https://github.com/casualoffice/sheets/blob/main/apps/web/src/main.tsx) (Web entry), [`packages/sdk/src/index.ts`](https://github.com/casualoffice/sheets/blob/main/packages/sdk/src/index.ts) (SDK entry), [`tests/e2e`](https://github.com/casualoffice/sheets/tree/main/tests/e2e) (E2E tests)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_web["Web app"]
  node_apps_web_entry(("Web entry<br/>React/Vite app<br/>[main.tsx]"))
  node_apps_web_shell["Editor shell<br/>UI command surface"]
  node_apps_web_univer["Univer glue<br/>Engine integration"]
  node_apps_web_collab["Collab UI<br/>Presence and room UX"]
  node_apps_web_io["File pipeline<br/>Import/export pipeline"]
  node_apps_web_home["Home flows<br/>Landing and recent files"]
  node_apps_web_auth["Auth flows<br/>Personal mode UI"]
  node_apps_web_admin["Admin UI<br/>Deployment config UI"]
  node_apps_web_features["Feature modules<br/>Workbook features"]
end

subgraph group_group_server["Server"]
  node_apps_server_entry(("Server entry<br/>Fastify app"))
  node_apps_server_collab["Yjs bridge<br/>Hocuspocus/WS"]
  node_apps_server_storage["Room storage<br/>Persistence layer"]
  node_apps_server_api["HTTP APIs<br/>Room and admin APIs"]
end

subgraph group_group_sdk["SDK"]
  node_packages_sdk_entry(("SDK entry<br/>Embed package<br/>[index.ts]"))
  node_packages_sdk_sheets["CasualSheets<br/>Embed shell<br/>[CasualSheets.tsx]"]
  node_packages_sdk_embed["Embed transport<br/>Host bridge"]
  node_packages_sdk_collab["SDK collab<br/>Embed collaboration"]
  node_packages_sdk_xlsx["SDK xlsx<br/>Workbook codec"]
end

subgraph group_group_ops["Ops & docs"]
  node_docs["Docs"]
  node_tests_e2e["E2E tests<br/>Playwright suite"]
  node_root_ops["Ops & build<br/>CI/CD and deploy"]
end

subgraph group_group_vendor["Vendor"]
  node_vendor_univer["Univer fork<br/>Upstream workspace"]
end

node_apps_web_entry -->|"renders"| node_apps_web_shell
node_apps_web_entry -->|"initializes"| node_apps_web_univer
node_apps_web_entry -->|"routes"| node_apps_web_home
node_apps_web_entry -->|"routes"| node_apps_web_auth
node_apps_web_entry -->|"routes"| node_apps_web_admin
node_apps_web_shell -->|"drives"| node_apps_web_univer
node_apps_web_shell -->|"shows"| node_apps_web_collab
node_apps_web_shell -->|"imports/exports"| node_apps_web_io
node_apps_web_shell -->|"hosts"| node_apps_web_features
node_apps_web_collab -->|"feeds"| node_apps_web_features
node_apps_web_io -->|"serializes"| node_apps_web_univer
node_apps_web_features -->|"commands"| node_apps_web_univer
node_apps_web_auth -->|"gates"| node_apps_web_home
node_apps_web_admin -->|"configures"| node_apps_server_api
node_apps_server_entry -->|"serves"| node_apps_server_api
node_apps_server_entry -->|"upgrades"| node_apps_server_collab
node_apps_server_api -->|"reads/writes"| node_apps_server_storage
node_apps_server_collab -->|"syncs"| node_apps_server_storage
node_packages_sdk_entry -->|"exports"| node_packages_sdk_sheets
node_packages_sdk_entry -->|"exports"| node_packages_sdk_embed
node_packages_sdk_entry -->|"exports"| node_packages_sdk_collab
node_packages_sdk_entry -->|"exports"| node_packages_sdk_xlsx
node_packages_sdk_sheets -->|"uses"| node_packages_sdk_embed
node_packages_sdk_sheets -->|"loads/saves"| node_packages_sdk_xlsx
node_packages_sdk_collab -->|"connects to"| node_apps_server_collab
node_packages_sdk_xlsx -->|"shares codec"| node_apps_web_io
node_tests_e2e -->|"covers"| node_apps_web_shell
node_tests_e2e -->|"covers"| node_apps_server_collab
node_tests_e2e -->|"covers"| node_packages_sdk_sheets
node_tests_e2e -->|"covers"| node_packages_sdk_xlsx
node_docs -.->|"documents"| node_apps_server_entry
node_docs -.->|"documents"| node_packages_sdk_entry
node_root_ops -->|"builds"| node_apps_server_entry
node_root_ops -->|"runs"| node_tests_e2e
node_root_ops -->|"pins"| node_vendor_univer

click node_apps_web_entry "https://github.com/casualoffice/sheets/blob/main/apps/web/src/main.tsx"
click node_apps_web_shell "https://github.com/casualoffice/sheets/tree/main/apps/web/src/shell"
click node_apps_web_univer "https://github.com/casualoffice/sheets/tree/main/apps/web/src/univer"
click node_apps_web_collab "https://github.com/casualoffice/sheets/tree/main/apps/web/src/collab"
click node_apps_web_io "https://github.com/casualoffice/sheets/tree/main/apps/web/src/xlsx"
click node_apps_web_home "https://github.com/casualoffice/sheets/tree/main/apps/web/src/home"
click node_apps_web_auth "https://github.com/casualoffice/sheets/tree/main/apps/web/src/auth"
click node_apps_web_admin "https://github.com/casualoffice/sheets/tree/main/apps/web/src/admin"
click node_apps_web_features "https://github.com/casualoffice/sheets/tree/main/apps/web/src"
click node_apps_server_entry "https://github.com/casualoffice/sheets/tree/main/apps/server"
click node_apps_server_collab "https://github.com/casualoffice/sheets/tree/main/apps/server"
click node_apps_server_storage "https://github.com/casualoffice/sheets/tree/main/apps/server"
click node_apps_server_api "https://github.com/casualoffice/sheets/tree/main/apps/server"
click node_packages_sdk_entry "https://github.com/casualoffice/sheets/blob/main/packages/sdk/src/index.ts"
click node_packages_sdk_sheets "https://github.com/casualoffice/sheets/blob/main/packages/sdk/src/sheets/CasualSheets.tsx"
click node_packages_sdk_embed "https://github.com/casualoffice/sheets/tree/main/packages/sdk/src/embed"
click node_packages_sdk_collab "https://github.com/casualoffice/sheets/tree/main/packages/sdk/src/collab"
click node_packages_sdk_xlsx "https://github.com/casualoffice/sheets/tree/main/packages/sdk/src/xlsx"
click node_docs "https://github.com/casualoffice/sheets/tree/main/docs"
click node_tests_e2e "https://github.com/casualoffice/sheets/tree/main/tests/e2e"
click node_vendor_univer "https://github.com/casualoffice/sheets/tree/main/vendor"
click node_root_ops "https://github.com/casualoffice/sheets/tree/main/Dockerfile"

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_apps_web_entry,node_apps_web_shell,node_apps_web_univer,node_apps_web_collab,node_apps_web_io,node_apps_web_home,node_apps_web_auth,node_apps_web_admin,node_apps_web_features toneBlue
class node_apps_server_entry,node_apps_server_collab,node_apps_server_storage,node_apps_server_api toneAmber
class node_packages_sdk_entry,node_packages_sdk_sheets,node_packages_sdk_embed,node_packages_sdk_collab,node_packages_sdk_xlsx toneMint
class node_docs,node_tests_e2e,node_root_ops toneRose
class node_vendor_univer toneIndigo
```

## Components

### Web app

Main browser application for the spreadsheet editor and product shell.

- [Web entry](https://github.com/casualoffice/sheets/blob/main/apps/web/src/main.tsx): React/Vite app · Browser application bootstrap and routing entrypoints. · `apps/web/src/main.tsx`
- [Editor shell](https://github.com/casualoffice/sheets/tree/main/apps/web/src/shell): UI command surface · Office-like chrome around the spreadsheet engine: menus, ribbon, dialogs, panels, status, and mobile UI. · `apps/web/src/shell`
- [Univer glue](https://github.com/casualoffice/sheets/tree/main/apps/web/src/univer): Engine integration · Adapters, hooks, and facades that wire the custom shell into Univer OSS. · `apps/web/src/univer`
- [Collab UI](https://github.com/casualoffice/sheets/tree/main/apps/web/src/collab): Presence and room UX · Avatar, cursor, room metadata, view-only behavior, and share-link collaboration surfaces. · `apps/web/src/collab`
- [File pipeline](https://github.com/casualoffice/sheets/tree/main/apps/web/src/xlsx): Import/export pipeline · Spreadsheet import/export, worker offloading, and workbook format fidelity handling. · `apps/web/src/xlsx`
- [Home flows](https://github.com/casualoffice/sheets/tree/main/apps/web/src/home): Landing and recent files · Home screen, templates, recent files, restore banners, and pinned-folder UX. · `apps/web/src/home`
- [Auth flows](https://github.com/casualoffice/sheets/tree/main/apps/web/src/auth): Personal mode UI · Login, signup, account, and personal-mode gating for authenticated workbook access. · `apps/web/src/auth`
- [Admin UI](https://github.com/casualoffice/sheets/tree/main/apps/web/src/admin): Deployment config UI · Runtime admin console for branding, storage, networking, auth, limits, and webhooks. · `apps/web/src/admin`
- [Feature modules](https://github.com/casualoffice/sheets/tree/main/apps/web/src): Workbook features · Charts, pivots, outlines, sparklines, autosave, recovery, version history, and other feature subsystems. · `apps/web/src`

### Server

Backend boundary for HTTP APIs, collaboration, and deployment serving.

- [Server entry](https://github.com/casualoffice/sheets/tree/main/apps/server): Fastify app · Backend bootstrap for HTTP APIs, health, diagnostics, room lifecycle, and collaboration upgrade routing. · `apps/server`
- [Yjs bridge](https://github.com/casualoffice/sheets/tree/main/apps/server): Hocuspocus/WS · WebSocket collaboration endpoint and sync traffic handling for shared workbook rooms. · `apps/server`
- [Room storage](https://github.com/casualoffice/sheets/tree/main/apps/server): Persistence layer · Room metadata, snapshots, seed upload/download, and optional Redis-backed Yjs update storage. · `apps/server`
- [HTTP APIs](https://github.com/casualoffice/sheets/tree/main/apps/server): Room and admin APIs · HTTP surface for room lifecycle, snapshot preflight, password checks, client counts, health checks, and diagnostics. · `apps/server`

### SDK

Embeddable editor package and its supporting transports and workbook pipelines.

- [SDK entry](https://github.com/casualoffice/sheets/blob/main/packages/sdk/src/index.ts): Embed package · Public package surface for embedding and browser-hosted editor integration. · `packages/sdk/src/index.ts`
- [CasualSheets](https://github.com/casualoffice/sheets/blob/main/packages/sdk/src/sheets/CasualSheets.tsx): Embed shell · Embeddable React editor surface with host-driven data boundaries and save/exit snapshot flow. · `packages/sdk/src/sheets/CasualSheets.tsx`
- [Embed transport](https://github.com/casualoffice/sheets/tree/main/packages/sdk/src/embed): Host bridge · Host/editor messaging protocol and transport helpers for embedded deployments. · `packages/sdk/src/embed`
- [SDK collab](https://github.com/casualoffice/sheets/tree/main/packages/sdk/src/collab): Embed collaboration · WebSocket URL, attach helpers, and replay logic for collaborative embed mode. · `packages/sdk/src/collab`
- [SDK xlsx](https://github.com/casualoffice/sheets/tree/main/packages/sdk/src/xlsx): Workbook codec · Worker-based parse/serialize/export pipeline with format passthrough resources and fidelity handling. · `packages/sdk/src/xlsx`

### Ops & docs

Deployment, documentation, CI, and validation assets.

- [Docs](https://github.com/casualoffice/sheets/tree/main/docs): Architecture docs · Repository documentation for deployment, storage modes, collaboration, SDK, and product behavior. · `docs`
- [E2E tests](https://github.com/casualoffice/sheets/tree/main/tests/e2e): Playwright suite · End-to-end coverage for editor UX, collaboration, file fidelity, embed flows, and regression scenarios. · `tests/e2e`
- [Ops & build](https://github.com/casualoffice/sheets/tree/main/Dockerfile): CI/CD and deploy · Docker, compose, workflows, scripts, and package-level build/release infrastructure. · `Dockerfile`

### Vendor

Pinned upstream spreadsheet-engine workspace and fork-related assets.

- [Univer fork](https://github.com/casualoffice/sheets/tree/main/vendor): Upstream workspace · Pinned vendor workspace for upstream spreadsheet-engine work and fork integration. · `vendor`

## Connections

- Web entry → Editor shell: renders · Bootstraps the editor shell and app-level UI surfaces.
- Web entry → Univer glue: initializes · Wires the browser app into the spreadsheet engine layer.
- Web entry → Home flows: routes · Switches between home and editor-style application flows.
- Web entry → Auth flows: routes · Mounts login, signup, and personal-mode gates.
- Web entry → Admin UI: routes · Mounts the admin configuration console.
- Editor shell → Univer glue: drives · Issues workbook commands into Univer rather than reimplementing spreadsheet mechanics.
- Editor shell → Collab UI: shows · Surfaces room state, presence, and view-only indicators.
- Editor shell → File pipeline: imports/exports · Connects user actions to workbook file I/O and export flows.
- Editor shell → Feature modules: hosts · Provides UI entry points for charts, pivots, version history, recovery, and related modules.
- Collab UI → Feature modules: feeds · Collaboration state informs comments, presence, and room-aware UX.
- File pipeline → Univer glue: serializes · Transforms engine state to and from workbook file formats.
- Feature modules → Univer glue: commands · Feature modules coordinate workbook actions through the engine boundary.
- Auth flows → Home flows: gates · Personal mode and account state influence landing and reopening flows.
- Admin UI → HTTP APIs: configures · Admin settings are persisted and applied through backend-facing APIs.
- Server entry → HTTP APIs: serves · Exposes HTTP endpoints for product and room lifecycle concerns.
- Server entry → Yjs bridge: upgrades · Routes `/yjs` collaboration traffic to WebSocket sync handling.
- HTTP APIs → Room storage: reads/writes · Room, snapshot, and diagnostic operations persist via the storage layer.
- Yjs bridge → Room storage: syncs · Collaboration updates and room state can be persisted or kept transient.
- SDK entry → CasualSheets: exports · Primary public editor component surface.
- SDK entry → Embed transport: exports · Embedding transport and protocol primitives.
- SDK entry → SDK collab: exports · Collaboration helpers for embedded use.
- SDK entry → SDK xlsx: exports · Workbook import/export utilities are part of the SDK boundary.
- CasualSheets → Embed transport: uses · Embed host transport coordinates data exchange with the parent app.
- CasualSheets → SDK xlsx: loads/saves · Embedded sheets rely on the SDK workbook codec pipeline.
- SDK collab → Yjs bridge: connects to · SDK collaboration clients speak the same WebSocket sync protocol as the server.
- SDK xlsx → File pipeline: shares codec · Web app and SDK share the workbook import/export architecture.
- E2E tests → Editor shell: covers · Validates the main editor UX and command surface.
- E2E tests → Yjs bridge: covers · Exercises room co-editing and sync behavior.
- E2E tests → CasualSheets: covers · Checks embed surface behavior and host integration.
- E2E tests → SDK xlsx: covers · Covers fidelity and round-trip workbook pipelines.
- Docs → Server entry: documents · Explains deployment, storage modes, collaboration, and self-hosting.
- Docs → SDK entry: documents · Describes the SDK and embed integration model.
- Ops & build → Server entry: builds · Container and release tooling package the backend and serving model.
- Ops & build → E2E tests: runs · CI and local scripts execute the Playwright suite.
- Ops & build → Univer fork: pins · Repository maintenance includes a vendorized upstream workspace.
