@zenera/cli 1.3.22 → 1.3.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -6
- package/templates/editor/.github/skills/zen-review/scripts/check-paths.sh +1 -1
- package/templates/project/agents/skills/html-page/SKILL.md +164 -57
- package/templates/project/agents/skills/html-page/assets/app/api/items.api.py +3 -2
- package/templates/project/agents/skills/html-page/assets/app/app.tsx +140 -0
- package/templates/project/agents/skills/html-page/assets/dashboard.tsx +162 -0
- package/templates/project/agents/skills/html-page/kit/Chart.tsx +79 -0
- package/templates/project/agents/skills/html-page/kit/Table.tsx +89 -0
- package/templates/project/agents/skills/html-page/kit/api.ts +93 -0
- package/templates/project/agents/skills/html-page/kit/components.tsx +302 -0
- package/templates/project/agents/skills/html-page/kit/format.ts +60 -0
- package/templates/project/agents/skills/html-page/kit/hooks.ts +159 -0
- package/templates/project/agents/skills/html-page/kit/index.ts +36 -0
- package/templates/project/agents/skills/html-page/kit/modules.d.ts +37 -0
- package/templates/project/agents/skills/html-page/kit/mount.tsx +62 -0
- package/templates/project/agents/skills/html-page/kit/shell.html +19 -0
- package/templates/project/agents/skills/html-page/kit/theme.css +356 -0
- package/templates/project/agents/skills/html-page/references/charts-and-tables.md +105 -53
- package/templates/project/agents/skills/html-page/references/python-functions.md +37 -29
- package/templates/project/agents/skills/html-page/scripts/build.ts +990 -0
- package/templates/project/agents/skills/html-page/assets/api.js +0 -87
- package/templates/project/agents/skills/html-page/assets/app/index.html +0 -278
- package/templates/project/agents/skills/html-page/assets/dashboard.html +0 -423
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenera/cli",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.24",
|
|
4
4
|
"description": "Command-line front end for @zenera/neo: agentic projects you can run, share and commit.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agents",
|
|
@@ -56,11 +56,11 @@
|
|
|
56
56
|
"@google/genai": "^2.30.0",
|
|
57
57
|
"@inkjs/ui": "^2.0.0",
|
|
58
58
|
"@openrouter/sdk": "^1.4.25",
|
|
59
|
-
"@zenera/engine": "^1.3.
|
|
60
|
-
"@zenera/neo": "^1.3.
|
|
61
|
-
"@zenera/sandbox": "^1.3.
|
|
62
|
-
"@zenera/server": "^1.3.
|
|
63
|
-
"@zenera/view": "^1.3.
|
|
59
|
+
"@zenera/engine": "^1.3.24",
|
|
60
|
+
"@zenera/neo": "^1.3.24",
|
|
61
|
+
"@zenera/sandbox": "^1.3.24",
|
|
62
|
+
"@zenera/server": "^1.3.24",
|
|
63
|
+
"@zenera/view": "^1.3.24",
|
|
64
64
|
"google-auth-library": "^10.0.0",
|
|
65
65
|
"ink": "^7.1.1",
|
|
66
66
|
"openai": "^6.0.0",
|
|
@@ -54,7 +54,7 @@ strip_urls() {
|
|
|
54
54
|
# any byte that differs as `rules.stale`.
|
|
55
55
|
#
|
|
56
56
|
# The html-page skill is left out too. Its paths are the page's own urls,
|
|
57
|
-
# relative to the page (`api('api/orders')`,
|
|
57
|
+
# relative to the page (`api('api/orders')`, `useData('../data.json')`), and to this sweep
|
|
58
58
|
# they look exactly like files the agent cannot open.
|
|
59
59
|
files() {
|
|
60
60
|
{
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: html-page
|
|
3
|
-
description: Building an interactive
|
|
4
|
-
tags: [html, dashboard, report, app, charts, api, python, ui]
|
|
5
|
-
version:
|
|
3
|
+
description: Building an interactive page the user opens from the chat - a dashboard or report with charts, tables, filters and headline numbers, or a small app whose page calls Python functions to read and change data (a tracker, a CRUD tool, a mail or ticket view, a form that saves, a page that queries a database each time it opens). The page is a React component in TypeScript (app.tsx) that a build checks and turns into one HTML file. Where to write it, how it gets its data, the kit of components, the layout that works in the chat's side panel and full page, and how to build and check it. Load when an answer has more than one chart or table, data the user will want to filter or explore, or the user wants something to use and change.
|
|
4
|
+
tags: [html, react, typescript, dashboard, report, app, charts, api, python, ui]
|
|
5
|
+
version: 2.0.0
|
|
6
6
|
---
|
|
7
7
|
|
|
8
|
-
# Interactive
|
|
8
|
+
# Interactive pages
|
|
9
9
|
|
|
10
10
|
A page is the answer when a reply would be too long or too static: several
|
|
11
11
|
related charts and tables, a breakdown to filter, sort or drill into, or
|
|
@@ -13,6 +13,11 @@ something the user will use and change (add items, edit records, run a
|
|
|
13
13
|
query). One chart or a short table is better inline in the chat (load `a2ui`
|
|
14
14
|
and `a2ui-charts`).
|
|
15
15
|
|
|
16
|
+
You write the page as **one React component in TypeScript, `app.tsx`**. The
|
|
17
|
+
build checks it (types, what a sandboxed page may do, a first render) and
|
|
18
|
+
writes **`index.html`**, the file the user opens: React, the kit and your code
|
|
19
|
+
inlined, nothing else to copy.
|
|
20
|
+
|
|
16
21
|
## First: where its data comes from
|
|
17
22
|
|
|
18
23
|
| The page | Its data | Read next |
|
|
@@ -35,11 +40,12 @@ One folder per page, under `/workspace/pages/`:
|
|
|
35
40
|
|
|
36
41
|
```
|
|
37
42
|
/workspace/pages/sales-q1/
|
|
38
|
-
├──
|
|
43
|
+
├── app.tsx the page: export default function App() - you write this
|
|
44
|
+
├── components/ more components it imports, if it grows (optional)
|
|
45
|
+
├── index.html written by the build - never edit it
|
|
39
46
|
├── data.json data it shows, written by your script (static data)
|
|
40
|
-
├── api.js calls the functions - copied, never edited (functions)
|
|
41
47
|
└── api/
|
|
42
|
-
├── orders.api.py functions the page calls
|
|
48
|
+
├── orders.api.py functions the page calls (functions)
|
|
43
49
|
├── db.py helpers they import (not callable from the page)
|
|
44
50
|
└── .data/ the page's own data (SQLite); never downloadable
|
|
45
51
|
```
|
|
@@ -49,97 +55,198 @@ One folder per page, under `/workspace/pages/`:
|
|
|
49
55
|
data.
|
|
50
56
|
- A script that builds `data.json` goes under `/workspace/.tmp/`, so the page
|
|
51
57
|
can be refreshed by running it again.
|
|
52
|
-
-
|
|
58
|
+
- Every `.tsx` directly in the folder is a page: `app.tsx` builds
|
|
59
|
+
`index.html`, `report.tsx` builds `report.html`. Components it imports go
|
|
60
|
+
in a subfolder (`components/Inbox.tsx`), never beside `app.tsx`.
|
|
53
61
|
|
|
54
62
|
## Start from a template
|
|
55
63
|
|
|
56
64
|
Copy one and edit it rather than writing a page from scratch:
|
|
57
65
|
|
|
58
66
|
```sh
|
|
59
|
-
# a dashboard or report:
|
|
67
|
+
# a dashboard or report: filters, KPIs, charts, table, from data.json
|
|
60
68
|
mkdir -p /workspace/pages/sales-q1
|
|
61
|
-
cp /skills/html-page/assets/dashboard.
|
|
69
|
+
cp /skills/html-page/assets/dashboard.tsx /workspace/pages/sales-q1/app.tsx
|
|
62
70
|
|
|
63
71
|
# an app: a list kept in SQLite, with add, check off, filter and delete
|
|
64
72
|
cp -r /skills/html-page/assets/app /workspace/pages/todo
|
|
65
|
-
cp /skills/html-page/assets/api.js /workspace/pages/todo/
|
|
66
73
|
```
|
|
67
74
|
|
|
68
|
-
Keep the
|
|
69
|
-
`The page: replace from here`. A
|
|
70
|
-
|
|
71
|
-
change.
|
|
75
|
+
Keep the template's structure - imports from `@page/kit`, the data's types,
|
|
76
|
+
then `App` - and replace the part marked `The page: replace from here`. A
|
|
77
|
+
dashboard with live data is the dashboard template with `useCall` and an
|
|
78
|
+
`*.api.py`: its comment says what to change.
|
|
79
|
+
|
|
80
|
+
## The kit: `@page/kit`
|
|
81
|
+
|
|
82
|
+
Import everything from `@page/kit`; React from `react`. Nothing else is
|
|
83
|
+
installed - no npm packages, no CSS frameworks. The kit's source is in
|
|
84
|
+
`/skills/html-page/kit/` if you need to look something up.
|
|
85
|
+
|
|
86
|
+
| Need | Use |
|
|
87
|
+
| -------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
|
|
88
|
+
| Call `*.api.py` functions | `api<ItemsApi>('api/items')` - an interface of the functions you call (python-functions.md) |
|
|
89
|
+
| Load data, with loading and error state | `useCall(() => items.list_items({ show }), [show])`, `useData<Report>('data.json')` |
|
|
90
|
+
| A change the user makes, then reload | `useAction((title: string) => items.add_item({ title }), list.reload)`: `.run`, `.busy`, `.error` |
|
|
91
|
+
| View state that survives a reload (tab, open item) | `useHashState('tab', 'orders')` - kept in `#tab=...` |
|
|
92
|
+
| Layout | `Page` (`width="narrow"` for an app), `Stack`, `Toolbar`, `Grid`, `Card` (`wide`) - see "Layout" |
|
|
93
|
+
| A list and what opens from it (item, edit, new) | `Split` - list or detail when narrow, side by side when wide; see "Layout" |
|
|
94
|
+
| Controls | `Select`, `Field` around any input, plain `<input>`, `<button className="primary">` |
|
|
95
|
+
| Headline numbers | `Kpis` with `Kpi` (`value` formatted, `delta` a fraction) |
|
|
96
|
+
| Charts | `Chart` with an ECharts `option` (charts-and-tables.md) |
|
|
97
|
+
| Tables | `Table` with Tabulator `columns` (charts-and-tables.md) |
|
|
98
|
+
| States | `Loading`, `Empty`, `ErrorBox` |
|
|
99
|
+
| Delete and other destructive actions | `ConfirmButton` - acts on the second click |
|
|
100
|
+
| HTML from outside (an email, an article) | `SafeHtml` - a sandboxed frame; never put such HTML in the page |
|
|
101
|
+
| Numbers, money, percents, dates | `fmt.num`, `fmt.money`, `fmt.pct`, `fmt.date`; `sum`, `groupSum`, `uniq` for rows |
|
|
72
102
|
|
|
73
103
|
## How it is shown, and what that allows
|
|
74
104
|
|
|
75
105
|
The chat opens the page in a side panel next to the conversation (often 400 -
|
|
76
|
-
600 px wide) or in a full browser tab. Either way it runs sandboxed
|
|
106
|
+
600 px wide) or in a full browser tab. Either way it runs sandboxed. The
|
|
107
|
+
build reports most of these; know them so you do not write them:
|
|
77
108
|
|
|
78
109
|
- It reaches files and functions of its folder **by a path relative to the
|
|
79
|
-
page**: `
|
|
110
|
+
page**: `useData('data.json')`, `<img src="logo.png">`, `api('api/orders')`,
|
|
80
111
|
and `..` to go up. Never `file:///`, `/workspace/...`, `http://localhost`
|
|
81
112
|
or an `.api.py` name in the page.
|
|
113
|
+
- Images and other small files can also be imported (`import logo from
|
|
114
|
+
'./logo.png'`) and are inlined.
|
|
82
115
|
- Scripts, styles and fonts load only from `cdn.jsdelivr.net`, `unpkg.com`
|
|
83
116
|
and `cdnjs.cloudflare.com`. Any other host is blocked - no external APIs,
|
|
84
117
|
images, map tiles or analytics from the page. A page that needs an outside
|
|
85
118
|
service calls a Python function that does.
|
|
86
|
-
- No `localStorage`, `sessionStorage`, cookies or `IndexedDB
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
with a second click, as the app template does.
|
|
119
|
+
- No `localStorage`, `sessionStorage`, cookies or `IndexedDB`. Keep view
|
|
120
|
+
state in hooks or `useHashState`, and anything to keep in the database.
|
|
121
|
+
- No `alert`, `confirm` or `prompt` (blocked): `ConfirmButton` for a delete,
|
|
122
|
+
`ErrorBox` for a message. No form submission: handle `onClick` /
|
|
123
|
+
`onKeyDown`, or a `<form onSubmit>` that calls `e.preventDefault()`.
|
|
92
124
|
- Links to other sites need `target="_blank" rel="noopener"`.
|
|
93
125
|
- The page cannot call the agent or any tool.
|
|
94
126
|
|
|
95
|
-
## Layout -
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
127
|
+
## Layout - narrow first
|
|
128
|
+
|
|
129
|
+
The page is opened **most often in the chat's side panel, 360 - 600 px
|
|
130
|
+
wide**, and sometimes in a full tab up to 1600 px. Design it for the narrow
|
|
131
|
+
panel first, then let it spread out when there is room - never the other way
|
|
132
|
+
round. At every width:
|
|
133
|
+
|
|
134
|
+
1. **Everything is reachable.** Every list, form, button and piece of
|
|
135
|
+
content the wide page shows can be reached on the narrow one - shown
|
|
136
|
+
stacked, or one tap away. Nothing is hidden because there is no room.
|
|
137
|
+
2. **Nothing scrolls sideways.** The page scrolls down, never across; long
|
|
138
|
+
text wraps.
|
|
139
|
+
3. **Controls stay usable**: inputs full width in forms, buttons that wrap to
|
|
140
|
+
the next line instead of shrinking or overflowing.
|
|
141
|
+
|
|
142
|
+
How, with the kit:
|
|
143
|
+
|
|
144
|
+
- One column by default: `Stack` for blocks one under another. `Grid` puts
|
|
145
|
+
cards side by side only when each gets at least `min` px, and stacks them
|
|
146
|
+
otherwise.
|
|
147
|
+
- **A list and what opens from it** (mail and a message, orders and an
|
|
148
|
+
order, items and an edit form) is always `Split`. Narrow, it shows the list,
|
|
149
|
+
or only what is open with a Back button; wide, both side by side. Whatever
|
|
150
|
+
the user can open - **including a "New …" or edit form** - goes in
|
|
151
|
+
`detail`, and `detail` is `null` when nothing is open:
|
|
152
|
+
|
|
153
|
+
```tsx
|
|
154
|
+
const [openId, setOpenId] = useHashState('open');
|
|
155
|
+
const [composing, setComposing] = useState(false);
|
|
156
|
+
<Split
|
|
157
|
+
list={<Inbox onOpen={setOpenId} onNew={() => setComposing(true)} />}
|
|
158
|
+
detail={
|
|
159
|
+
composing ? (
|
|
160
|
+
<Compose onDone={() => setComposing(false)} />
|
|
161
|
+
) : openId ? (
|
|
162
|
+
<Message id={openId} />
|
|
163
|
+
) : null
|
|
164
|
+
}
|
|
165
|
+
onBack={() => {
|
|
166
|
+
setComposing(false);
|
|
167
|
+
setOpenId('');
|
|
168
|
+
}}
|
|
169
|
+
placeholder="Pick a message, or write a new one."
|
|
170
|
+
/>;
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Never place a second panel beside a list any other way (two `Card`s in a
|
|
174
|
+
`Grid`, your own two-column css): on a narrow panel one of them ends up
|
|
175
|
+
below the other or out of reach.
|
|
176
|
+
|
|
177
|
+
- `Toolbar` and `.row` wrap their controls; keep each control at its natural
|
|
178
|
+
width. A form is a `Stack` of `Field`s inside a `Card`: its inputs take the
|
|
179
|
+
full width.
|
|
180
|
+
- A row of a list (sender, subject, date, actions) is a `.row` that wraps, or
|
|
181
|
+
two lines - not fixed columns.
|
|
182
|
+
- Your own css: no fixed widths over 360 px, no fixed grid columns
|
|
183
|
+
(`grid-template-columns: 160px 1fr`), no `display: none` in a media or
|
|
184
|
+
container query, no `100vh` or `overflow: hidden` on the page. The build
|
|
185
|
+
rejects them. Wider layouts go inside `@container page (min-width: …)` - the
|
|
186
|
+
page's width, not the window's, since the panel can be narrow on a wide
|
|
187
|
+
screen.
|
|
188
|
+
- Anything you add inside a grid or flex row gets `min-width: 0` (the kit's
|
|
189
|
+
cards and stacks have it), or a chart, table or long text pushes the page
|
|
190
|
+
wider than the panel.
|
|
191
|
+
- Light and dark: colours come from the kit's CSS variables, switched by
|
|
192
|
+
`prefers-color-scheme`. Use `var(--fg)`, `var(--muted)`, `var(--line)`,
|
|
193
|
+
`var(--accent)`, `var(--danger)` - no hard-coded colours. For styles of
|
|
194
|
+
your own, `import './app.css'`.
|
|
195
|
+
- Order, top to bottom: controls in one wrapping row; then headline numbers;
|
|
196
|
+
then charts or the list; the detailed table last.
|
|
111
197
|
|
|
112
198
|
## Page code
|
|
113
199
|
|
|
114
|
-
-
|
|
115
|
-
|
|
116
|
-
|
|
200
|
+
- `export default function App()` in `app.tsx`; `export const title = '...'`
|
|
201
|
+
names the browser tab. The build mounts `App` - never call `createRoot`.
|
|
202
|
+
- State lives in hooks; everything on the page is drawn from it, so the
|
|
203
|
+
numbers, charts and lists always agree. Derive filtered rows and chart
|
|
204
|
+
options with `useMemo` from the data and the filters.
|
|
205
|
+
- **The page speaks the user's language, never the builder's.** Nothing the
|
|
206
|
+
user sees - titles, subtitles, labels, footers, empty states, error
|
|
207
|
+
messages, tooltips - names how it is made: no "stored in SQLite", "saved
|
|
208
|
+
to the database", "via the API", "calls a Python function", "loaded from
|
|
209
|
+
data.json", "built with React", no table, file, function or endpoint
|
|
210
|
+
names. Say what it means for the user instead: "Saved", "Your items",
|
|
211
|
+
"Updated just now", "Couldn't load orders - try again". A source line
|
|
212
|
+
names where the numbers come from in business terms ("Q1 orders export"),
|
|
213
|
+
not the file or service.
|
|
117
214
|
- Show "Loading…", an empty state ("No orders yet", "No data for these
|
|
118
|
-
filters") and errors
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
215
|
+
filters") and errors on the page with `Loading`, `Empty` and `ErrorBox` -
|
|
216
|
+
never fail silently. `useCall` and `useAction` catch errors for you.
|
|
217
|
+
- JSX escapes values: `{row.name}` is always safe. Never
|
|
218
|
+
`dangerouslySetInnerHTML` or `innerHTML`; `SafeHtml` for HTML from outside.
|
|
219
|
+
- Touch `window` and `document` only in `useEffect` and in handlers: the
|
|
220
|
+
build renders `App` once without a browser.
|
|
221
|
+
- Type the data: an interface for each row and for the functions you call
|
|
222
|
+
(`ItemsApi`). The build then catches a misspelt field before the user does.
|
|
223
|
+
- A long page is several components: put them in `components/` and import
|
|
224
|
+
them (`import { Inbox } from './components/Inbox'`).
|
|
225
|
+
|
|
226
|
+
## Build and check before you link it
|
|
125
227
|
|
|
126
228
|
```sh
|
|
127
|
-
|
|
128
|
-
grep -nE "file://|/workspace|localhost|localStorage|sessionStorage|<form|alert\(|confirm\(|prompt\(" *.html || echo "paths ok"
|
|
129
|
-
grep -noE "(src|href)=\"https?://[^\"]+" *.html | grep -vE "cdn.jsdelivr.net|unpkg.com|cdnjs.cloudflare.com" || echo "hosts ok"
|
|
130
|
-
[ ! -f data.json ] || python3 -c "import json; json.load(open('data.json'))" && echo "data ok"
|
|
229
|
+
node /skills/html-page/scripts/build.ts /workspace/pages/sales-q1
|
|
131
230
|
```
|
|
132
231
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
232
|
+
It type-checks, checks the rules above, renders `App` once, and writes
|
|
233
|
+
`index.html` - or writes nothing and lists what to fix as `file:line:col
|
|
234
|
+
message`. Fix each one and build again until it prints `✓ wrote index.html`.
|
|
235
|
+
|
|
236
|
+
- Build after **every** change to the page's `.tsx`, `.ts` or `.css`. The user
|
|
237
|
+
sees `index.html`, not your source. `node .../build.ts <folder> --check`
|
|
238
|
+
says whether it is out of date.
|
|
239
|
+
- Never edit `index.html` by hand: the next build replaces it.
|
|
240
|
+
- A page with functions: call each of them from the shell first
|
|
241
|
+
(python-functions.md, "Test before you link it").
|
|
242
|
+
- The build runs in the sandbox, where its tools are installed.
|
|
136
243
|
|
|
137
244
|
## Hand it over
|
|
138
245
|
|
|
139
246
|
- The main findings with their numbers in two to four sentences, or for an
|
|
140
247
|
app what it does - the reply must stand on its own for someone who never
|
|
141
248
|
opens the page.
|
|
142
|
-
- Then the page as a link, alone in its own paragraph:
|
|
249
|
+
- Then the page as a link, alone in its own paragraph (the build prints it):
|
|
143
250
|
|
|
144
251
|
[Sales Q1 dashboard](file:///workspace/pages/sales-q1/index.html)
|
|
145
252
|
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
"""The app's API: every public function here can be called from
|
|
1
|
+
"""The app's API: every public function here can be called from app.tsx.
|
|
2
2
|
|
|
3
|
-
const items = api('api/items');
|
|
3
|
+
const items = api<ItemsApi>('api/items');
|
|
4
4
|
await items.add_item({ title: 'Milk' }) -> add_item(title="Milk")
|
|
5
5
|
|
|
6
|
+
Keep the page's ItemsApi interface in step with these functions.
|
|
6
7
|
Names starting with `_` and anything imported are not callable from the page.
|
|
7
8
|
Try them from a shell before linking the app:
|
|
8
9
|
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// An app: a list kept in SQLite by api/items.api.py, with add, check off, filter and delete.
|
|
2
|
+
// Copy the whole folder, rename api/items.api.py to what it holds, and change both together.
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import {
|
|
5
|
+
api,
|
|
6
|
+
Card,
|
|
7
|
+
ConfirmButton,
|
|
8
|
+
Empty,
|
|
9
|
+
ErrorBox,
|
|
10
|
+
Loading,
|
|
11
|
+
Page,
|
|
12
|
+
Select,
|
|
13
|
+
useAction,
|
|
14
|
+
useCall,
|
|
15
|
+
} from '@page/kit';
|
|
16
|
+
|
|
17
|
+
// ---- The page: replace from here ----
|
|
18
|
+
// The functions of api/items.api.py as the page calls them: keep this in step with the Python.
|
|
19
|
+
|
|
20
|
+
type Show = 'all' | 'open' | 'done';
|
|
21
|
+
|
|
22
|
+
interface Item {
|
|
23
|
+
id: number;
|
|
24
|
+
title: string;
|
|
25
|
+
done: boolean;
|
|
26
|
+
created: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface Me {
|
|
30
|
+
id: string;
|
|
31
|
+
name: string;
|
|
32
|
+
role: string | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface ItemsApi {
|
|
36
|
+
me(): Promise<Me | null>;
|
|
37
|
+
list_items(args: { show: Show }): Promise<Item[]>;
|
|
38
|
+
add_item(args: { title: string }): Promise<Item>;
|
|
39
|
+
set_done(args: { id: number; done: boolean }): Promise<Item>;
|
|
40
|
+
remove_item(args: { id: number }): Promise<{ removed: number }>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const items = api<ItemsApi>('api/items');
|
|
44
|
+
|
|
45
|
+
export const title = 'Items';
|
|
46
|
+
|
|
47
|
+
const SHOW = [
|
|
48
|
+
{ value: 'all', label: 'All' },
|
|
49
|
+
{ value: 'open', label: 'Open' },
|
|
50
|
+
{ value: 'done', label: 'Done' },
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
export default function App() {
|
|
54
|
+
const [show, setShow] = useState<Show>('all');
|
|
55
|
+
const [text, setText] = useState('');
|
|
56
|
+
|
|
57
|
+
// Who is signed in comes from the server (me() in api/items.api.py), never the page.
|
|
58
|
+
const me = useCall(() => items.me(), []);
|
|
59
|
+
const list = useCall(() => items.list_items({ show }), [show]);
|
|
60
|
+
|
|
61
|
+
// Every change: call, then load the list again, so the page shows what is really saved.
|
|
62
|
+
const add = useAction(async (title: string) => {
|
|
63
|
+
await items.add_item({ title });
|
|
64
|
+
setText('');
|
|
65
|
+
}, list.reload);
|
|
66
|
+
const setDone = useAction(
|
|
67
|
+
(id: number, done: boolean) => items.set_done({ id, done }),
|
|
68
|
+
list.reload,
|
|
69
|
+
);
|
|
70
|
+
const remove = useAction((id: number) => items.remove_item({ id }), list.reload);
|
|
71
|
+
|
|
72
|
+
const submit = () => {
|
|
73
|
+
const title = text.trim();
|
|
74
|
+
if (title) void add.run(title);
|
|
75
|
+
};
|
|
76
|
+
const error = add.error ?? setDone.error ?? remove.error ?? list.error ?? me.error;
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<Page
|
|
80
|
+
width="narrow"
|
|
81
|
+
title={title}
|
|
82
|
+
subtitle="Add, check off and remove items. Changes are saved."
|
|
83
|
+
actions={me.data && <span className="muted">Signed in as {me.data.name}</span>}
|
|
84
|
+
>
|
|
85
|
+
<ErrorBox error={error} onRetry={list.error ? list.reload : undefined} />
|
|
86
|
+
|
|
87
|
+
<Card>
|
|
88
|
+
<div className="row">
|
|
89
|
+
<input
|
|
90
|
+
type="text"
|
|
91
|
+
placeholder="New item"
|
|
92
|
+
aria-label="New item"
|
|
93
|
+
maxLength={200}
|
|
94
|
+
value={text}
|
|
95
|
+
onChange={(e) => setText(e.target.value)}
|
|
96
|
+
onKeyDown={(e) => e.key === 'Enter' && submit()}
|
|
97
|
+
/>
|
|
98
|
+
<button
|
|
99
|
+
className="primary"
|
|
100
|
+
disabled={add.busy || !text.trim()}
|
|
101
|
+
onClick={submit}
|
|
102
|
+
>
|
|
103
|
+
Add
|
|
104
|
+
</button>
|
|
105
|
+
<Select
|
|
106
|
+
value={show}
|
|
107
|
+
options={SHOW}
|
|
108
|
+
onChange={(value) => setShow(value as Show)}
|
|
109
|
+
/>
|
|
110
|
+
</div>
|
|
111
|
+
</Card>
|
|
112
|
+
|
|
113
|
+
<Card>
|
|
114
|
+
{list.loading && !list.data ? (
|
|
115
|
+
<Loading />
|
|
116
|
+
) : !list.data?.length ? (
|
|
117
|
+
<Empty>{show === 'all' ? 'Nothing here yet.' : `No ${show} items.`}</Empty>
|
|
118
|
+
) : (
|
|
119
|
+
<ul className="list">
|
|
120
|
+
{list.data.map((item) => (
|
|
121
|
+
<li key={item.id} className={item.done ? 'done' : undefined}>
|
|
122
|
+
<input
|
|
123
|
+
type="checkbox"
|
|
124
|
+
aria-label="Done"
|
|
125
|
+
checked={item.done}
|
|
126
|
+
disabled={setDone.busy}
|
|
127
|
+
onChange={(e) => void setDone.run(item.id, e.target.checked)}
|
|
128
|
+
/>
|
|
129
|
+
<span className="grow">{item.title}</span>
|
|
130
|
+
<ConfirmButton onConfirm={() => remove.run(item.id)}>
|
|
131
|
+
Delete
|
|
132
|
+
</ConfirmButton>
|
|
133
|
+
</li>
|
|
134
|
+
))}
|
|
135
|
+
</ul>
|
|
136
|
+
)}
|
|
137
|
+
</Card>
|
|
138
|
+
</Page>
|
|
139
|
+
);
|
|
140
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// A dashboard: filters, headline numbers, two charts and a table, from data.json beside the page.
|
|
2
|
+
// Copy this to <page>/app.tsx and keep the shape: data, then derived rows, then the page.
|
|
3
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
4
|
+
import {
|
|
5
|
+
Card,
|
|
6
|
+
Chart,
|
|
7
|
+
Empty,
|
|
8
|
+
ErrorBox,
|
|
9
|
+
Grid,
|
|
10
|
+
Kpi,
|
|
11
|
+
Kpis,
|
|
12
|
+
Loading,
|
|
13
|
+
Page,
|
|
14
|
+
Select,
|
|
15
|
+
Table,
|
|
16
|
+
Toolbar,
|
|
17
|
+
fmt,
|
|
18
|
+
groupSum,
|
|
19
|
+
sum,
|
|
20
|
+
uniq,
|
|
21
|
+
useData,
|
|
22
|
+
type Column,
|
|
23
|
+
} from '@page/kit';
|
|
24
|
+
|
|
25
|
+
// ---- The page: replace from here with the real data's fields. ----
|
|
26
|
+
// data.json, written by your script: { "title", "subtitle", "source", "rows": [Row, ...] }.
|
|
27
|
+
// Live data instead: `const report = useCall(() => api<ReportApi>('api/report').get_data(), []);`
|
|
28
|
+
// with get_data() in api/report.api.py returning the same shape (references/python-functions.md).
|
|
29
|
+
|
|
30
|
+
interface Row {
|
|
31
|
+
date: string;
|
|
32
|
+
category: string;
|
|
33
|
+
region: string;
|
|
34
|
+
amount: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface Report {
|
|
38
|
+
title: string;
|
|
39
|
+
subtitle?: string;
|
|
40
|
+
source?: string;
|
|
41
|
+
rows: Row[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const title = 'Dashboard';
|
|
45
|
+
|
|
46
|
+
const columns: Column[] = [
|
|
47
|
+
{ title: 'Date', field: 'date', headerFilter: 'input', minWidth: 100 },
|
|
48
|
+
{ title: 'Category', field: 'category', headerFilter: 'input' },
|
|
49
|
+
{ title: 'Region', field: 'region', headerFilter: 'input' },
|
|
50
|
+
{
|
|
51
|
+
title: 'Amount',
|
|
52
|
+
field: 'amount',
|
|
53
|
+
hozAlign: 'right',
|
|
54
|
+
sorter: 'number',
|
|
55
|
+
formatter: (cell: { getValue(): number }) => fmt.num(cell.getValue(), 2),
|
|
56
|
+
},
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
export default function App() {
|
|
60
|
+
const report = useData<Report>('data.json');
|
|
61
|
+
const [category, setCategory] = useState('');
|
|
62
|
+
const [region, setRegion] = useState('');
|
|
63
|
+
|
|
64
|
+
const all = report.data?.rows;
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (report.data) document.title = report.data.title;
|
|
67
|
+
}, [report.data]);
|
|
68
|
+
const rows = useMemo(
|
|
69
|
+
() =>
|
|
70
|
+
(all ?? []).filter(
|
|
71
|
+
(r) => (!category || r.category === category) && (!region || r.region === region),
|
|
72
|
+
),
|
|
73
|
+
[all, category, region],
|
|
74
|
+
);
|
|
75
|
+
const byCategory = useMemo(() => {
|
|
76
|
+
const totals = groupSum(rows, 'category', 'amount').sort((a, b) => b[1] - a[1]);
|
|
77
|
+
return {
|
|
78
|
+
xAxis: {
|
|
79
|
+
type: 'category',
|
|
80
|
+
data: totals.map((d) => d[0]),
|
|
81
|
+
axisLabel: { interval: 0, rotate: totals.length > 5 ? 30 : 0 },
|
|
82
|
+
},
|
|
83
|
+
yAxis: { type: 'value' },
|
|
84
|
+
series: [{ type: 'bar', data: totals.map((d) => d[1]) }],
|
|
85
|
+
};
|
|
86
|
+
}, [rows]);
|
|
87
|
+
const overTime = useMemo(() => {
|
|
88
|
+
const totals = groupSum(rows, 'date', 'amount').sort((a, b) => a[0].localeCompare(b[0]));
|
|
89
|
+
return {
|
|
90
|
+
xAxis: { type: 'category', data: totals.map((d) => d[0]) },
|
|
91
|
+
yAxis: { type: 'value' },
|
|
92
|
+
dataZoom: totals.length > 30 ? [{ type: 'inside' }] : [],
|
|
93
|
+
series: [
|
|
94
|
+
{
|
|
95
|
+
type: 'line',
|
|
96
|
+
showSymbol: false,
|
|
97
|
+
areaStyle: { opacity: 0.15 },
|
|
98
|
+
data: totals.map((d) => d[1]),
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
};
|
|
102
|
+
}, [rows]);
|
|
103
|
+
|
|
104
|
+
if (report.error) {
|
|
105
|
+
return (
|
|
106
|
+
<Page title={title}>
|
|
107
|
+
<ErrorBox error={report.error} onRetry={report.reload} />
|
|
108
|
+
</Page>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
if (!report.data || !all) {
|
|
112
|
+
return (
|
|
113
|
+
<Page title={title}>
|
|
114
|
+
<Loading />
|
|
115
|
+
</Page>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
return (
|
|
119
|
+
<Page title={report.data.title} subtitle={report.data.subtitle} footer={report.data.source}>
|
|
120
|
+
<Toolbar>
|
|
121
|
+
<Select
|
|
122
|
+
label="Category"
|
|
123
|
+
all="All"
|
|
124
|
+
value={category}
|
|
125
|
+
options={uniq(all, 'category')}
|
|
126
|
+
onChange={setCategory}
|
|
127
|
+
/>
|
|
128
|
+
<Select
|
|
129
|
+
label="Region"
|
|
130
|
+
all="All"
|
|
131
|
+
value={region}
|
|
132
|
+
options={uniq(all, 'region')}
|
|
133
|
+
onChange={setRegion}
|
|
134
|
+
/>
|
|
135
|
+
</Toolbar>
|
|
136
|
+
|
|
137
|
+
<Kpis>
|
|
138
|
+
<Kpi label="Total amount" value={fmt.num(sum(rows, 'amount'))} />
|
|
139
|
+
<Kpi label="Rows" value={fmt.num(rows.length)} />
|
|
140
|
+
<Kpi label="Categories" value={fmt.num(uniq(rows, 'category').length)} />
|
|
141
|
+
</Kpis>
|
|
142
|
+
|
|
143
|
+
{rows.length === 0 ? (
|
|
144
|
+
<Card>
|
|
145
|
+
<Empty>No data for these filters.</Empty>
|
|
146
|
+
</Card>
|
|
147
|
+
) : (
|
|
148
|
+
<Grid>
|
|
149
|
+
<Card title="Amount by category">
|
|
150
|
+
<Chart option={byCategory} onClick={(e) => setCategory(e.name)} />
|
|
151
|
+
</Card>
|
|
152
|
+
<Card title="Amount over time">
|
|
153
|
+
<Chart option={overTime} />
|
|
154
|
+
</Card>
|
|
155
|
+
<Card title="Rows" wide>
|
|
156
|
+
<Table data={rows} columns={columns} />
|
|
157
|
+
</Card>
|
|
158
|
+
</Grid>
|
|
159
|
+
)}
|
|
160
|
+
</Page>
|
|
161
|
+
);
|
|
162
|
+
}
|