Skip to content

Architecture

Squirrel's frontend is a single-page React app. It runs in a browser during development, and inside a Tauri desktop shell when deployed. All of its data comes from a Canopy server.

graph LR
    subgraph App["Squirrel (React)"]
        UI["Pages and components"]
        Q["TanStack Query"]
        Ctx["Contexts: snapshot and restore jobs, live data"]
    end
    UI --> Q
    UI --> Ctx
    Q -->|"REST"| Sq["/squirrel/v1"]
    Ctx -->|"poll jobs"| Jobs["/jobs/v1"]
    Ctx -->|"WebSocket"| Hare["/hare/v1/ws/pvs"]
    Q -->|"REST"| Elog["/elog/v1"]
    Q -->|"REST"| Core["/v1/facilities"]
    UI -->|"usage events"| An["/analytics/v1"]
    subgraph Canopy["Canopy server"]
        Sq
        Jobs
        Hare
        Elog
        Core
        An
    end

Stack

Concern Library
UI React 19, IBM Carbon (@carbon/react)
Routing TanStack Router, file-based (src/routes/, generated into src/routeTree.gen.ts)
Server data TanStack Query
Tables TanStack Table and TanStack Virtual, for large PV lists
Build Vite, TypeScript
Desktop shell Tauri 1 (src-tauri/)
Tests Vitest with jsdom

Source Layout

Directory Holds
src/routes/ One file per URL. A route loads its data and passes it, with callbacks, to a page
src/pages/ The page components, which mostly render what their route gives them
src/components/ Shared components: layout, dialogs, the virtual table and its cells
src/contexts/ App-wide state; see below
src/hooks/ Data hooks (hooks/queries/), live values, and modal helpers
src/services/ One module per Canopy area: snapshots, PVs, tags, facilities, jobs, the WebSocket, the e-log, analytics
src/config/ api.ts: endpoint paths, and loading config.json and the active instance
src/utils/ Pure helpers: CSV parsing, tolerance checks, restore counts, address formatting
src/types/ Types for Canopy's wire format (api.ts) and the app's own models (models.ts)
src-tauri/ The desktop shell, in Rust

Routes

Every route takes an optional facility prefix, so /snapshots and /lcls-sc/snapshots are the same page. With a facility, lists show that facility and every facility below it in the hierarchy, and new snapshots capture only its PVs. Without one, nothing is filtered. All page routes live in src/routes/{-$facility}/.

Route Description
/{facility}/ Redirects to /{facility}/snapshots
/{facility}/snapshots Browse and search snapshots
/{facility}/snapshot-details?id= View snapshot with live values
/{facility}/comparison?mainId=&compId= Compare two snapshots
/{facility}/pv-browser Browse, add, and manage PVs
/{facility}/tags Manage tag groups
/{facility}/meta-pvs Manage meta PVs (admin)
/{facility}/api-keys Manage API keys (admin, not in sidebar)

{facility} is a facility slug from GET /v1/facilities, e.g. lcls or lcls-sc; leave it out for every facility. A slug the backend doesn't know (including a mistyped page name such as /snapshotz, which routes as a facility) shows a not-found page.

Startup

Before rendering, loadRuntimeConfig (src/config/api.ts) reads config.json and picks the active instance; see the Configuration Reference. If several instances are configured, the instance picker is shown before the rest of the app. Once an instance is chosen, the app's providers wrap the router:

ApiKeyErrorProvider
└─ InstanceProvider
   └─ QueryClientProvider
      └─ AdminModeProvider
         └─ HeartbeatProvider        live-data liveness
            └─ WebSocketProvider     the live-data socket
               └─ SnapshotProvider   the running snapshot job
                  └─ RestoreProvider the running restore job
                     └─ RouterProvider

The root route adds ThemeProvider (light or dark mode) and the layout: header, sidebar, and banners.

Talking to Canopy

All requests go through apiClient (src/services/apiClient.ts). It prefixes the active instance's apiUrl, adds the X-API-Key header, and times out after 30 seconds. A 401 or 403 becomes an ApiKeyError, which sets the API key error state behind the banner; any other failure becomes an ApiError carrying Canopy's error code when the response has one, and a plain Error otherwise.

Canopy path Used for Where
/squirrel/v1 Snapshots, PVs, meta PVs, tags, restore snapshotService, pvService, tagsService
/jobs/v1/{id} Progress of a snapshot or restore jobService
/hare/v1/ws/pvs Live PV values websocketService
/analytics/v1 Page views and usage events analyticsService
/elog/v1/apps/squirrel Post to Elog (logbooks, tags, entries) elogService
/v1/facilities The facility list, and the key's access to each facilityService

Snapshot and restore jobs

Saving and restoring a snapshot are background jobs in Canopy. The request returns a job id, and SnapshotProvider or RestoreProvider polls /jobs/v1/{id} every second until the job finishes. Because the job's state lives in a provider above the router, the sidebar's status cards and the restore panel keep working while you move between pages.

Live values

One WebSocket to Hare carries every live value. useLiveValues subscribes to the PVs a table is showing, and unsubscribes when they leave; the service sends subscriptions in chunks of at most 5,000 PVs, Hare's per-message limit. Hare sends a heartbeat about every 5 seconds. HeartbeatProvider shows the LIVE DATA DISCONNECTED banner if none arrives for 15 seconds, after a 10-second grace period at startup.

E-log posting

Post to Elog talks only to Canopy's elog module, which holds Squirrel's e-log application token server-side, so neither the browser nor the desktop app ever sees an e-log credential. The button appears only when GET /elog/v1/apps/squirrel/config reports enabled: true; otherwise it stays hidden and the reason is written to the console (E-log is disabled: ..., or Failed to load e-log config if the request itself failed).

The config endpoint needs no key, so a key without the elog scope still shows the button, but the dialog fails with "This API key isn't allowed to use the e-log module." See E-log Access.

Server data

Lists and details are fetched with TanStack Query. The snapshot list uses an infinite query over Canopy's paged results, and the PV Browser fetches 100 PVs per page as you scroll. Searches and filters are sent to Canopy, so they cover everything, not only the rows already loaded.

Admin Mode

Admin mode is a flag in AdminModeProvider. It only decides which controls are shown, and is off at every start. Canopy enforces access through the API key, whatever the mode.