Workspace and persist
The workspace is everything a user sets up in the terminal: the layout, each chart's market, timeframe and scripts, their drawings, the panels and link groups. persist decides where it is kept.
| Kept in | Use it when |
|---|---|---|
| The user's browser | You want nothing to set up |
| A chartbook in the user's own Chartnaut account, named after your app | Your users are linked to Chartnaut and should find their setup on chartnaut.com too |
| Your own database | You want each user's setup on your side, on any device |
| Nowhere | Every visit starts fresh, such as a drill |
"account" works for linked users only: managed users have no chartbooks.
In your own database
import { Terminal, useTerminal, useWorkspace } from "@chartnaut/charts/react";
export function SavedTerminal({ userId }: { userId: string }) {
const terminal = useTerminal();
const saving = useWorkspace(terminal);
return (
<div style={{ height: 720 }}>
<span>{saving.status === "saved" ? "Saved" : saving.status === "saving" ? "Saving…" : ""}</span>
<Terminal
ref={terminal.ref}
persist={{ load: () => loadWorkspace(userId), save: (w) => saveWorkspace(userId, w) }}
/>
</div>
);
}load runs once before the terminal opens; return the workspace you saved, or nothing for a first visit. save runs on every change with the whole workspace and { revision, changes, cause, keepalive }. A failed save is tried again with backoff, and status shows error until one succeeds. When the tab closes, keepalive is true: start a fetch(..., { keepalive: true }) or a beacon right away.
Chartnaut keeps nothing in this mode.
The format
{
"v": 1,
"layout": { "direction": "row", "sizes": [60, 40], "children": ["main", "htf"] },
"charts": {
"main": { "instrument": { "id": "sym_4812", "symbol": "NAS100" }, "timeframe": "5m", "type": "candlesticks",
"scripts": [{ "ref": { "id": "ind_61" }, "inputs": { "length": 14 } }] },
"htf": { "instrument": { "id": "sym_4812", "symbol": "NAS100" }, "timeframe": "4h", "type": "candlesticks" }
},
"links": [{ "id": "a", "charts": ["main", "htf"], "link": ["symbol"] }],
"focused_chart": "main"
}Store it as it is. v is the format's version: Chartnaut loads every version it has ever written, forever, so a workspace you saved years ago still opens. Markets are kept by their stable id and symbol, so a renamed market still loads. A script ref without a version follows the script's updates; with one, it stays pinned.
Parts that can't load any more (a script that was deleted, a market the user's plan no longer reaches) are skipped, and the terminal says which in workspace_warning events.
Read or change it from code
import { createTerminal } from "@chartnaut/charts";
const terminal = createTerminal(document.getElementById("terminal")!, {});
async function resetCharts() {
const { workspace, revision } = await terminal.workspace.get();
console.log("Revision", revision, Object.keys(workspace));
const result = await terminal.workspace.patch({ charts: { htf: null } }); // drops one chart
result.warnings.forEach((w) => console.warn(w.path, w.message));
}
resetCharts();get reads it, set replaces all of it, patch merges: sections you leave out stay, and charts and panels merge by id, with null removing one.
