A proof-of-concept charting tool for the Atmosphere using Spaces and Web Tiles
README.md

Chartile #

Chartile is a proof-of-concept charting tool for the Atmosphere. It allows you to define custom themes for your charts and manage drafts via a private Space. Charts are published as Web Tiles to your PDS and embedded into a Bluesky post on your account.

Getting started #

You need:

  • Node 24.3 or later
  • pnpm
  • Postgres, such as Postgres.app, with a database called chartile
  • Google Chrome, for the browser tests
pnpm install
createdb chartile
cp .env.example .env   # then fill in SESSION_SECRET and STORE_SECRET
npm run dev

Open http://127.0.0.1:44100, not localhost. On your own computer, Chartile signs in with atproto's localhost client, which sends you back to 127.0.0.1, and the session cookie only works on the host that set it.

Environment #

Variable When What it does
SESSION_SECRET Always Signs the browser session cookie
STORE_SECRET Always Encrypts account tokens in the database. Changing it signs everyone out
DATABASE_URL Deployed Postgres. Defaults to postgres://localhost/chartile
PUBLIC_URL Deployed The public address, such as https://chartile.space. Setting it switches sign-in to a confidential client
OAUTH_PRIVATE_KEY Deployed Signs sign-in requests. .env.example has the command that makes one
PORT Optional Defaults to 44100

Make the two secrets with openssl rand -hex 32.

Commands #

npm run dev         # Server, restarting when an imported file changes
npm run hmr         # Server and browser, updating live
npm run start       # Production mode. There's no separate build
npm test
npm run test:server
npm run test:e2e
npm run typecheck

How it works #

The editor #

The editor is one page with multiple steps: Upload data, then Chart type, settings, text, and theme, then Publish.

  • Every step is part of one <form>, which the editor reads by field name. Steps you aren't on are hidden, not removed, so they keep their settings.
  • As the author works, the editor posts the form to /preview. The server reads it into a Chart and sends back the finished tile's HTML, which the preview shows in a sandboxed iframe.
  • A moment after each change, the editor also saves the chart's state to the author's private space in their PDS.

From form to tile #

Each chart goes through the same stages, in app/charts/:

  1. Read (read/, server only): checks the form and the CSV, and builds a Chart, such as a LineChart or BarChart.
  2. Model (public/model/): the types and data the server and the editor share, such as the chart types, themes, fonts and locales.
  3. Draw (public/draw/): lays the chart out and draws it as SVG with interactivity.
  4. Tile (tile/, server only): renderTile() in tile/render.ts builds one self-contained HTML page. It inlines the chart's drawing scripts with their types stripped, the chart data as JSON, the tile CSS and any theme fonts as base64.

Rules for drawing code #

Files in app/charts/public/draw/ are pasted into a tile's single script, so:

  • They can only import types, plus the shared files render.ts adds before them: plot.ts, annotation-layer.ts, axes.ts and search.ts.
  • Two files can't declare the same top-level name, because imports are stripped and both would land in the same scope.
  • They can't contain a closing script tag.

test/charts/draw-imports.test.ts checks all three.

Accounts and storage #

  • Sign-in: atproto OAuth through @atproto/oauth-client-node, in app/atproto/oauth.ts. On your own computer it uses the localhost client. With PUBLIC_URL set, it's a confidential client that serves /oauth/client-metadata.json and /oauth/jwks.json.
  • Sessions: browser sessions and OAuth state share one Postgres table, in app/atproto/storage.ts. Account tokens are encrypted with STORE_SECRET. Postgres advisory locks stop two server processes from using the same single-use refresh token, so any number of processes can share the database.
  • Drafts, themes and settings: saved to a private atproto space in the author's account, space.chartile.workspace, which is created on first save. app/atproto/spaces.ts opens it, and drafts.ts and themes.ts read and write their records. A chart's record keeps the editor's state as it is, so new settings don't need lexicon changes, and its CSV as a blob.
  • Publishing: app/atproto/publish.ts uploads the tile and a thumbnail as blobs, writes the tile record, and checks that the server stored exactly what was sent, by CID.

Lexicons #

The record types are defined in lexicons/space/chartile/. app/lexicons/ is generated from them, so don't edit it by hand. After changing a lexicon:

npx lex build --lexicons lexicons --out app/lexicons --clear --import-ext .ts
scripts/publish-lexicons.sh   # needs goat, signed in to the account that publishes them

Layout #

Any directory named public/ under app/ can be loaded by the browser, so keep secrets and trusted server logic out of them.

app/
  routes.ts            The route contract, shared by server and browser for typed links
  router.ts            Wires routes, middleware and actions together
  actions/             Route actions: the code that handles each request
  middleware/          Middleware several actions share, such as sign-in and JSON errors
  atproto/             Sign-in, storage, the private space, drafts, themes and publishing
  charts/
    read/              Form → Chart (server)
    tile/              Chart → tile HTML and CSS (server)
    public/model/      Types and data shared by server and editor
    public/draw/       Drawing, run on the server, in the editor and in tiles
  ui/                  Server-rendered pages, such as document.tsx and the chart list
  ui/public/           Browser entry, global CSS and the light/dark switch
  ui/public/editor/    The chart editor, one file for each step
  ui/public/themes/    The theme editor
  utils/public/        Browser logic with no UI: spreadsheets, thumbnails, examples
  lexicons/            Generated from lexicons/. Don't edit
lexicons/              The record types, as lexicon JSON
public/                Static files served unchanged: fonts, example CSVs, the favicon
scripts/               Scripts that generate data files
test/                  Tests, mirroring app/
data/                  Census source data for some examples

Each chart type has the same name in every folder under app/charts/. For bar charts, read/bar.ts reads the form and public/draw/bar.ts draws it.

Adding a chart type #

  1. Add its type to app/charts/public/model/chart.ts, and a reader in app/charts/read/, called from readChart() in read/chart.ts.
  2. Draw it in app/charts/public/draw/. For a chart on axes, return a PlotLayout and draw it with renderPlot() and mountPlot() from axes.ts.
  3. Add it to PLOTS in app/charts/tile/render.ts, with its script and how to start it.
  4. Add a card to app/ui/public/editor/chart-type-picker.tsx, and any settings it needs to chart-settings.tsx.
  5. Add an example to public/examples/ and app/utils/public/examples.ts.

Tests #

Tests live in test/ and mirror app/:

  • Server tests in test/actions/, test/atproto/ and test/charts/ cover reading, layout, tiles and storage. test/atproto/storage.test.ts uses your local chartile database.
  • Browser tests in test/e2e/*.test.e2e.ts drive the app in Chrome with Playwright: every editor step, every example, uploads, drafts, annotations, themes, and published tiles at phone and desktop widths.

The browser tests start a copy of the app from createAppRouter() with in-memory sessions and a test-only route that signs in a fake account. They can't save to an account or publish. Pass openEditor() in test/support/app.tsx a draft, or themes, to open the editor with them.

The test scripts set their own throwaway secrets, so tests never read .env. Without Chrome, run pnpm exec playwright install chromium and remove channel from playwright.config.ts.

Deploying #

Set PUBLIC_URL, DATABASE_URL, OAUTH_PRIVATE_KEY, SESSION_SECRET and STORE_SECRET, in .env or the host's environment, and run npm run start. There's no build step. Session cookies are only sent over HTTPS when PUBLIC_URL is set.