# Pages, panels and status

> Interface Songstress draws for you, from a tree of its own components, in the places it gives your extension.

Page: https://songstress.app/docs/develop/interface/

Your extension's interface fits Songstress because Songstress draws it. You say what you mean (a page, a list, a
tree, a choice to make, a notice, a status in the player) as JSON, and each app draws it with its own controls: the
web and desktop apps in their design, the phones natively. What happens on a tap is JSON too: an action, which the
app carries out. An extension never sets a colour, never reaches the app's own screens, and never shows anything
without its mark.

## Places [#places]

| Place | Declared in | Web and desktop | Phones | Drawn from |
| --- | --- | --- | --- | --- |
| A page | `pages` | The main area, and an entry in the sidebar with `sidebar` | A page of its own, and an entry in the Library tab's "Extensions" group with `sidebar` | Its renderer's screen |
| A sheet | A `navigate` action with `present: "sheet"` | A page over the screen it came from | A sheet | The page's renderer |
| A panel | `panels` | A section of Songstress's own screen, at its `location` | The same | Its renderer's blocks |
| A status item | `status` | The player bar | The full player | Its renderer's status |
| A menu item | `menus` | The menu at its location | One "Extensions" submenu in that menu | A declared command |
| The settings page | `settings` | The extension's own page | The extension's own page in Settings | The manifest |
| A notice | A `notify` action, `songstress.ui.notify` | Over the app, until it is closed | The same | The notice |
| A view | `views` | A browser tab (web), the system's browser (desktop) | The in-app browser | Your own HTML |

A renderer is a function your entry registers under the declared id, in the entry the manifest names in `render`:
`songstress.ui.pages.register(id, render)`, `songstress.ui.panels.register(id, render)` or
`songstress.ui.status.register(id, render)`. It is told who is looking, in which app (`client`), and what the screen
shows. What the server entry renders shows in every app. A page's renderer is told:

What a page's renderer is told.

| Field | Type | Description |
|---|---|---|
| `userId` | `string` | The member looking at the page. |
| `client` | `ClientInfo` | The app that will draw the tree. |
| `params` | `Record<string, string>` | What the page was opened with (`Destination.params`). |
| `search?` | `string \| undefined` | What the person typed in the page's search field. |
| `cursor?` | `string \| undefined` | The `nextCursor` of the rows already shown, when more are asked for. |
| `node?` | `string \| undefined` | On a tree, the id of the node the person opened: answer the nodes under it. |

## Pages [#pages]

A page is a screen of its own, opened by an action, or from the apps' navigation when it sets `sidebar`. Up to 20.
`when` holds back its navigation entry, never the page.

A page the apps draw from a tree of Songstress's own blocks, built by the extension's code.

| Field | Type | Description |
|---|---|---|
| `id` | `string` | The page's name. Code registers its renderer under it, and it is the page's address. |
| `title` | `string` | The page's heading. |
| `render` | `Side` | The entry that builds the page's tree. A page built in the server entry shows in every app. |
| `icon?` | `string \| undefined` | The icon of the page's sidebar entry. |
| `sidebar?` | `boolean \| undefined` | Give the page an entry in the apps' own navigation: the sidebar of the web and desktop apps, the Library tab of the phones. |
| `when?` | `string \| undefined` | The page's entry in the apps' navigation shows only while this holds. |

```ts title="src/server.ts"
import { songstress } from 'songstress/server'

songstress.ui.pages.register('recent', async (context) => {
  const sent = (await songstress.storage.get<string[]>('sent', { userId: context.userId })) ?? []
  return {
    type: 'list',
    sections: [{ title: 'Sent today', items: sent.map((songId) => ({ type: 'song' as const, songId })) }],
    empty: { title: 'Nothing sent yet', text: 'Play something, and it shows here.', icon: 'music' },
  }
})
```

A page answers one of five screens:

| `type` | What it is |
| --- | --- |
| `list` | Sections of rows, with an optional search field, an empty state and more rows on demand (`nextCursor`) |
| `grid` | Sections of cards, with the same |
| `tree` | Nodes that hold nodes: folders, groups, anything that holds more of itself; with an optional search field and an empty state |
| `detail` | Blocks, read from top to bottom |
| `form` | Sections of fields and one button that hands them to a command |

**Rows and cards.** `row` and `card` are your own: a title, a subtitle, an icon or an image, an action on a tap, a
menu. `song`, `album`, `artist` and `playlist` are Songstress's own, named by library id and drawn exactly as
Songstress draws them elsewhere, with their own menus.

**Trees.** A `tree` holds `nodes`: each your own `node` or a library `song`. A node holds what's under it as
`children`, given now, or `expandable`, which says it has some: when the person opens it, the page is rendered again
with the node's id in `context.node`, and your answer's `nodes` are what's under it. The web and desktop apps open a
node in place (`open` opens it at first); the phones open a node that holds others as a page of its own.

**Blocks**, what a `detail` screen, a section and a panel are built from: `stack`, `section`, `text`, `icon`,
`image`, `badge`, `progress`, `banner`, `divider`, `spacer`, `collapsible`, `button`, `menu`, `shelf`, and the four
library types.

**Fields**, what a `form` holds: `text-field`, `switch`, `checkbox`, `slider`, `picker`, `date-picker`. The app keeps
what's typed until the form is submitted; the command then gets the values by field name in `context.values`.

Every component's exact shape is in the [API reference](/docs/develop/reference/songstress/#type-Screen).

## Panels [#panels]

A panel is a section inside one of Songstress's own screens. Up to 10.

A section the extension's code builds inside one of Songstress's own screens.

| Field | Type | Description |
|---|---|---|
| `id` | `string` | The panel's name. Code registers its renderer under it. |
| `title` | `string` | The panel's heading. |
| `location` | `PanelLocation` | Which of Songstress's screens the panel is drawn in. |
| `render` | `Side` | The entry that builds the panel's tree. |
| `when?` | `string \| undefined` | The panel is drawn only while this holds. |

| `location` | Where it is drawn | Its renderer is told |
| --- | --- | --- |
| `now-playing` | The player's now-playing view | The song playing |
| `home` | Home, after Songstress's own shelves | |
| `artist` | An artist's page | The artist |
| `album` | An album's page | The album |
| `playlist` | A playlist's page | The playlist's id |

A song, an artist or an album is handed over whole to an extension granted `library.read`, and left out for any
other. A panel's renderer is told:

What a panel's renderer is told: who looks, and what the screen shows.
The song, artist or album is handed over whole to an extension granted
`library.read`, and left out for any other.

| Field | Type | Description |
|---|---|---|
| `userId` | `string` | The member looking at the panel. |
| `client` | `ClientInfo` | The app it is drawn in. |
| `song?` | `Song \| undefined` | The song playing, on a "now-playing" panel. |
| `artist?` | `Artist \| undefined` | The artist of the page, on an "artist" panel. |
| `album?` | `Album \| undefined` | The album of the page, on an "album" panel. |
| `playlist?` | `Pick<Playlist, "id"> \| undefined` | The playlist of the page, on a "playlist" panel: its id, to an extension granted `library.read`. Playlists themselves are not read for extensions yet. |

It answers its blocks, or `null` to show nothing:

```ts title="src/server.ts"
import { songstress } from 'songstress/server'

songstress.ui.panels.register('plays', async ({ album }) => {
  if (!album) return null
  return { children: [{ type: 'text', text: `${album.songCount ?? 0} songs on ${album.name}`, style: 'caption' }] }
})
```

## Status items [#status-items]

A status item is your extension's state at a glance in the player: a few words and an icon ("Sent", a check), in a
tone, with what a tap does. Up to 2.

An item in the player: an icon and a few words the extension's code keeps current.

| Field | Type | Description |
|---|---|---|
| `id` | `string` | The status item's name. Code registers its renderer under it. |
| `title` | `string` | What the item is, for people who do not see it, and where the player lists it. |
| `render` | `Side` | The entry that says what the item shows, for the song playing. |
| `icon?` | `string \| undefined` | The icon shown until the renderer answers. |
| `when?` | `string \| undefined` | The item is drawn only while this holds. |

It is rendered for the song playing: when the player shows it, when the song changes, and when it is refreshed. Its
renderer answers what it shows, or `null` to show nothing now:

What a status item shows in the player: an icon and a few words, the
extension's own state at a glance.

| Field | Type | Description |
|---|---|---|
| `text?` | `string \| undefined` | A few words; left out, the icon alone, named by the status item's declared title for people who do not see it. |
| `icon?` | `string \| undefined` | An icon from Songstress's icon set. |
| `tone?` | `Tone \| undefined` | Its colour, by meaning; the theme decides the colour itself. |
| `tooltip?` | `string \| undefined` | A line more, shown on hover or a long press. |
| `progress?` | `number \| undefined` | From 0 to 1: how far some work of the extension's is. |
| `busy?` | `boolean \| undefined` | Work is going on, with no telling how far it is. |
| `action?` | `Action \| undefined` | What choosing it does. |

It is told who is looking and the song playing:

What a status item's renderer is told: who looks, and the song playing,
whole to an extension granted `library.read`.

| Field | Type | Description |
|---|---|---|
| `userId` | `string` | The member whose player shows the item. |
| `client` | `ClientInfo` | The app it is drawn in. |
| `song?` | `Song \| undefined` | The song playing, whole to an extension granted `library.read`. |

```ts title="src/server.ts"
import { songstress } from 'songstress/server'

songstress.ui.status.register('sent', async ({ userId, song }) => {
  if (!song) return null
  const last = await songstress.storage.get<string>('last', { userId })
  return last === song.id ? { text: 'Sent', icon: 'check', tone: 'success' } : { busy: true }
})
```

The apps show as many status items as their player has room for, in the order of the extensions' names.

## Views [#views]

A view is your own HTML file, for what Songstress's components can't draw: a visualiser, a game, a chart of your own.
Up to 10. A view and a page are opened the same way, so they share their ids.

A free-form page: the extension's own HTML, opened in a sandboxed frame.

| Field | Type | Description |
|---|---|---|
| `id` | `string` | The view's name, and its address. |
| `title` | `string` | The view's heading. |
| `path` | `string` | The HTML file the browser opens, from the package root. |
| `icon?` | `string \| undefined` | The icon of the view's sidebar entry. |
| `sidebar?` | `boolean \| undefined` | Give the view an entry in the sidebar of the web and desktop apps. |

An app opens a view in the browser, apart from the app: a tab on the web, the system's browser on the desktop, the
in-app browser on a phone. It gets no `songstress` API and nothing of the app's: no storage, no session, no player.
It is served by the member's own server at an address of its own that expires, and is handed the `params` it was
opened with as the address's query. Any data it needs, it fetches itself. What must react to the app, or work
offline, belongs in a declared page.

## Actions [#actions]

What a tap does:

| `type` | What it does |
| --- | --- |
| `invoke` | Runs one of your commands |
| `navigate` | Opens one of your pages or views, or Songstress's page for something in the library; as a page or as a sheet |
| `play` | Plays library songs now, as a tap on a song does |
| `queue` | Adds library songs to the queue, at its end or next |
| `openUrl` | Opens an http or https address in the browser |
| `copy` | Copies text |
| `share` | Opens the device's share sheet |
| `toast` | Shows a short message that goes away by itself |
| `notify` | Shows a notice that stays until it is closed, with up to two buttons |
| `confirm` | Asks first, then carries out another action |
| `pick` | Asks the person to choose, then hands their choice to a command |
| `input` | Asks for a line of text, then hands it to a command |
| `refresh` | Renders the page, panel or status item it came from again |

A command answers with an action for the app to carry out, so your extension can lead a person through steps that
each run on their own: a pick hands its answer to a command, which answers with an input, whose answer goes to
another command, which answers with a notice.

### Asking [#asking]

- **`pick`** is Songstress's picker: a `title`, the `items` to choose from (each an `id` and a `title`, and
  optionally a `subtitle`, an `icon` or an `image`), a search field with an optional `placeholder`, and `many` for a
  choice of several (`picked` marks those chosen at first). Then its `command` runs with the action's `input`, and
  `context.answer` holds the id chosen, or the ids. Up to 100 items.
- **`input`** is one field: a `title`, a `prompt`, a `placeholder`, the `value` it holds at first, `multiline`, a
  `maxLength` (up to 2000), `required`, and `submit`, the word on its button. Then its `command` runs, and
  `context.answer` is the text. Never a password: a secret is a setting.
- **`confirm`** asks yes or no, then carries out its `action`.

Asks the person to choose, with Songstress's own picker (a search field
over a long list), then runs a command with what they chose. Nothing runs
when they choose nothing and close it.

| Field | Type | Description |
|---|---|---|
| `type` | `"pick"` | Always `"pick"`: which kind this is. |
| `title` | `string` | What the picker asks, as its heading. |
| `placeholder?` | `string \| undefined` | The hint in the search field. |
| `items` | `PickItem[]` | What to choose from, up to 100. |
| `many?` | `boolean \| undefined` | Several may be chosen. Left out, one. |
| `command` | `string` | The command that is handed the choice: `CommandContext.answer` is the id chosen, or the ids for a pick of several. |
| `input?` | `unknown` | JSON, handed to the command as its input. |

Asks the person for a line of text, then runs a command with it. Never a
password: a secret is a setting. Nothing runs when they close it.

| Field | Type | Description |
|---|---|---|
| `type` | `"input"` | Always `"input"`: which kind this is. |
| `title` | `string` | What is asked, as a heading. |
| `prompt?` | `string \| undefined` | A line under the title: what to write. |
| `placeholder?` | `string \| undefined` | The hint while the field is empty. |
| `value?` | `string \| undefined` | What the field holds at first. |
| `multiline?` | `boolean \| undefined` | Several lines. |
| `maxLength?` | `number \| undefined` | The most characters it takes. |
| `required?` | `boolean \| undefined` | It cannot be sent empty. |
| `submit?` | `string \| undefined` | The word on the button that sends it. Left out, "Done". |
| `command` | `string` | The command that is handed the text, as `CommandContext.answer`. |
| `input?` | `unknown` | JSON, handed to the command as its input. |

`context.source` says which asked. A question may lead to one more question and no further: a third in a row is no
action at all. Nothing runs when the person closes what was asked.

```ts title="src/server.ts"
import { songstress } from 'songstress/server'

songstress.commands.register('choose-chart', async () => ({
  action: {
    type: 'pick',
    title: 'Which chart?',
    items: [
      { id: 'week', title: 'This week' },
      { id: 'year', title: 'This year' },
    ],
    command: 'show-chart',
  },
}))

songstress.commands.register('show-chart', async (_input, context) => ({
  action: { type: 'navigate', to: { page: 'chart', params: { period: String(context.answer) } } },
}))
```

### Notices [#notices]

A `toast` goes away by itself. A notice stays until the person closes it: a `title`, a `message`, a `tone` (`info`,
`success`, `warning` or `danger`), and up to two `buttons`, each a `title` and an `action`. The app shows it with
your extension's name.

A notice: what an extension has to tell, with up to two things to do
about it. It stays until the person closes it or picks one.

| Field | Type | Description |
|---|---|---|
| `title` | `string` | What the notice is about, in a few words. |
| `message?` | `string \| undefined` | A sentence or two more. |
| `tone?` | `"danger" \| "info" \| "success" \| "warning" \| undefined` | Left out, "info". |
| `buttons?` | `{ title: string; action: Action; }[] \| undefined` | Each a title and what it does; at most two. |

Code may also tell a notice unasked, while the member has an app open, with `songstress.ui.notify(notice)`. The
server entry names the member (`songstress.ui.notify(notice, { userId })`) and every app they have open shows it.
Nothing is kept for an app that is closed. At most ten an hour for one member: more reject with `limit-exceeded`.

### Live [#live]

What you draw is rendered when it is shown, and again when the person pulls to refresh, when a `refresh` action asks,
and when your code asks with `songstress.ui.refresh(target)`, where `target` is `{ page }`, `{ panel }` or
`{ status }` with the id. The server entry names the member: `songstress.ui.refresh({ status: 'sent' }, { userId })`.
Asked more often than once every five seconds for one target, an app renders it once.

## Conditions [#conditions]

A menu item, a page's navigation entry, a panel and a status item may name a `when`: a condition the app holds it
to, as VS Code's `when` clauses do. Up to 200 characters:

- a key alone (true when its value is true, a text that isn't empty, or a number that isn't 0), or compared to a value
  with `==` or `!=`;
- a value is `'text'` in single quotes, a number, `true` or `false`;
- `!` turns a condition round, `&&` and `||` join two, and parentheses group them, nested at most eight deep.

| Key | Its value | Known in |
| --- | --- | --- |
| `platform` | `web`, `desktop`, `ios` or `android` | Every place |
| `clientCode`, `views` | The app's capabilities | Every place |
| `admin` | The member is the server's admin | Every place |
| `signedIn` | The extension's sign-in has an account: the server's, or with scope `user` the member's own | Every place |
| `playing` | This device's player is playing | Every place |
| `setting.<key>` | One of your settings, as this member and device have it (never a secret) | Every place |
| `favorite` | The song, album or artist the menu was opened on is a favourite | The menus of a song, an album, an artist, the queue and the player |

```json title="songstress.json"
{ "command": "send-again", "when": "signedIn && setting.mode == 'manual' && platform != 'web'" }
```

The validator refuses a condition no app can read (`WHEN_INVALID`), and a key no app knows there, a setting you
didn't declare or a secret one, `signedIn` with no sign-in, or a `platform` that is none of the four
(`WHEN_KEY_UNKNOWN`). An app that can't read a condition leaves the item out.

## What a tree can't do [#what-a-tree-cant-do]

By design, a tree can't run script or timers in the app, choose a colour, a font, a size or a position (colour is
said by meaning, `tone`, and the theme decides the rest), load an image from the device, ask for a password, draw
outside its places, or grow without bound: the apps cap a tree's size, depth and rows, and how often it is redrawn.

An app says which version of the components it draws (`client.ui`; this is version 2, which added trees, status
items, notices, `pick` and `input`). A component it doesn't know shows as "needs a newer app", and an action it
doesn't know does nothing: read `client.ui` and answer what that app draws.
