# Panel Update Tool

`panel_update` changes a panel: its assets, its vendor libraries or its settings, or restores one of its versions. A change of assets, vendor libraries, workflows or tables writes one new version from the version with the id `from`, or from the current one; the name and description change in place. The new version is what people see at once, unless the agent stages it.

## When to Use

The agent uses it to change a panel it read with `panel_get`: write an asset whole, patch one by its id, remove an asset, replace the vendor libraries, or change the name, description, workflows or tables. With only `from`, it restores that version, to bring back an earlier one as the newest. `switch: false` stages the version until `panel_switch` publishes it.

## Parameters

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `panel` | string | Yes | The panel's id |
| `from` | string | No | The id of the version the change is based on. Left out, the version people see. Stated alone, the version to restore |
| `files` | array | No | Asset changes, applied in order: `{ name, content }` adds an asset or replaces it whole; `{ asset, patch }` patches the asset with that id and stores the result as a new asset; `{ remove }` drops the asset with that name |
| `vendors` | array of strings | No | The vendor identifiers the version may use, replacing the list, such as `verdalia-ui.js`. Stored exactly as given: a vendor listed without one it imports is refused. Left out, the base version's list carries over |
| `name` | patch | No | The panel's name; a page showing the panel keeps its own name. Clearing it names the panel `Untitled panel` |
| `description` | patch | No | What the panel is for. Clearing it leaves it empty |
| `workflows` | patch | No | Every workflow the panel may call, replacing the list, each by its id or its slug as `list_workflows` gives them; the slug aliases the id and either is accepted. Each is stored as the workflow's id. Clearing it empties the list |
| `tables` | patch | No | Every table the panel may read with `verdalia.api.queryTable`, replacing the list, each by its id or its slug as `list_tables` gives them; the slug aliases the id and either is accepted. Each is stored as the table's id. The panel can read every row and column of each one. Clearing it empties the list |
| `switch` | boolean | No | Whether the new version becomes what people see; default `true`. `false` stages it |

Each patch is one of:

- `{"op": "set", "value": ...}` replaces the value: a string for `name` and `description`, an array of strings for `workflows` and `tables`
- `{"op": "clear"}` clears it
- `{"op": "unchanged"}`, or omitting the field, keeps it

At least one of these is given besides `panel`. A new version is written from the base when `from` is given, when `files` holds an entry, or when the stated `vendors`, `workflows` or `tables` differ from the base version's; otherwise only the name and description are saved, no version is written, and they take effect at once. A call that changes both is written as one: when any part is refused, nothing changes.

A call that gives only `from`, with or without `switch`, restores that version: the new version holds the same assets, main, workflows, tables and vendor libraries as `from`, and becomes what people see unless `switch` is `false`. A restore stores no asset.

Vendor libraries import one another, and a list must name every library each one imports: `verdalia-preact-hooks.js` and `verdalia-preact-jsx-runtime.js` need `verdalia-preact.js`; `verdalia-ui.js` needs `verdalia-preact.js`, `verdalia-preact-hooks.js` and `verdalia-htm.js`. The others need none.

An asset is a script (`.js`, `.mjs`), a stylesheet (`.css`), an SVG image (`.svg`) or the main `.html`, at most 1,048,576 bytes, the main at most 262,144. The HTML references an asset as `asset:NAME`. Two entries for one name apply in order: a patch after `{ name, content }` applies to that content. A version holds exactly one `.html` asset, its main, and the new version's main is the base version's main, by name: removing it or adding a second `.html` asset is refused.

`{ asset, patch }` names an asset of the base version by its `id`, as `panel_get` lists it. A patch is hunks. A hunk starts with a line beginning `@@`; anything after the `@@` is ignored. Then come the lines around the change prefixed with a space, removed lines with `-`, and added lines with `+`. Unchanged and removed lines are copied exactly from `main_html` as `panel_get` returned it, or from `content` as `panel_file_get` returned it. A hunk of only `+` lines appends to the end.

```diff
@@
 <h1>Lights</h1>
-<button id=on>On</button>
+<button id=on>On</button><button id=off>Off</button>
```

Every hunk must match exactly one place in the asset, trailing whitespace aside. If any hunk does not, nothing changes.

A panel that should show changes made elsewhere listens with `verdalia.api.onEvent(key, listener)` for the events a workflow's Send User Event step sends with "Send to listening clients" on, and reads again when an event names what it shows, rather than polling: the payload carries only an id or a small status, never the data itself. `get_docs("panels")` describes it with every other `window.verdalia` call.

## Returns

| Field | Type | Description |
|-------|------|-------------|
| `id` | string | The panel's id |
| `revision` | integer | The revision of its settings |
| `version` | string | The new version's id; absent when no version was written |
| `current_version` | string or null | The version people see after the change; absent when no version was written |
| `previous_version` | string | The version the change was made from, to switch back to with `panel_switch`; absent when no version was written |
| `patched` | array | For each patched asset: `name`, and `landed`, for each hunk `hunk` (its number), `lines` (where it landed in the asset now, such as `3-4`, `7`, or `removed before line 5`) and `preview` (those lines, numbered) |
| `page_urls` | array of strings | The full URL of every navigation page that shows the panel; empty when none does |
| `editor_url` | string | The full URL of the panel's editor in Settings |

The agent can send its next patch from the preview without reading the panel again.

## Example Scenario

```json
{
  "panel": "01JC4Z8Q2V6M3K9T1R5X7B0N4D",
  "files": [
    { "asset": "01JC6B8C9D0E1F2G3H4J5K6M7N", "patch": "@@\n <h1>Lights</h1>\n+<div id=\"app\"></div><script type=\"module\" src=\"asset:app.js\"></script>\n" },
    { "name": "app.js", "content": "import { html, render, Card } from '@verdalia/ui'\nrender(html`<${Card} title=\"Office\" />`, document.getElementById('app'))\n" }
  ],
  "vendors": ["verdalia-ui.js", "verdalia-ui.css", "verdalia-preact.js", "verdalia-preact-hooks.js", "verdalia-htm.js"]
}
```

To restore a version:

```json
{
  "panel": "01JC4Z8Q2V6M3K9T1R5X7B0N4D",
  "from": "01JC5A2B3C4D5E6F7G8H9J0K1M"
}
```

To rename the panel and take away every workflow it may call:

```json
{
  "panel": "01JC4Z8Q2V6M3K9T1R5X7B0N4D",
  "name": { "op": "set", "value": "Office lights" },
  "workflows": { "op": "clear" }
}
```

## Notes

- A patch of an asset the base version does not list is refused: `No version lists an asset with the id <id>`
- A hunk that matches nothing is refused, naming the asset, with the lines it expected and the closest current lines, numbered
- A hunk that matches more than one place is refused with the lines it matches; unchanged lines above or below it narrow it to one
- A patch that leaves the asset as it was is refused, and so is dropping a name the base version does not hold: `The version lists no file named <name>`
- A file over 1,048,576 bytes, a main HTML over 262,144 bytes, a version of more than 64 assets or more than 4,194,304 bytes, an `.html` asset that is not the main, a name over 67 characters with its extension and a name with another extension are refused, and nothing is written. Refusals state sizes in bytes
- A vendor library's name, such as `verdalia-ui.js`, is refused as a file name. Vendor libraries are named in `vendors`, never written as assets
- A vendor identifier that is not one Verdalia ships is refused
- A vendor list that leaves out a library one of its vendors imports is refused, naming both
- A write that would take the workspace's panel assets past 52,428,800 bytes (50 MB) is refused, with the bytes in use and the bytes the write adds; people free space in Settings → Panels → Storage
- A table that no id or slug names is refused, naming the reference. A slug that names more than one table is refused the same way rather than picking one. A table already on the version's list that has since been deleted is kept until it is removed
- A workflow that no id or slug names is refused, naming the reference
- A `from` id the panel does not have is refused: `The panel has no version with the id <id>`
- The write carries no revision: a patch names an asset, so one made from a stale read fails to match rather than overwriting
- Risk: write
