# Status and context keys

> Set what a status item says from your code at any time, and declare keys your own conditions read, so menus, entries, panels and overlays follow your extension's state.

Page: https://songstress.app/docs/develop/status-and-context/

Two calls let your code say what your interface shows, without waiting for a render: `ui.status.set` for what a
status item says, and `ui.context.set` for keys your `when` conditions read. Both came with API 0.1.13: ask for
`"songstress": "^0.1.13"`.

## Status from code [#status-from-code]

A [status item](/docs/develop/interface/#status-items) is a few words and an icon in the player bar (the full player
on a phone). Its renderer answers what it shows when the player draws it; `ui.status.set` says it from anywhere, a
hook, a job or a press, until you hand it back with `null`:

<Shot name="web/ext-status" alt="The player bar with Tunelog's status item, Sent with a check, beside the player's own buttons" />

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

songstress.jobs.register('sync', async (run) => {
  if (!run.userId) return
  const member = { userId: run.userId }
  await songstress.ui.status.set('sync', { text: 'Syncing', busy: true }, member)
  // … the work …
  await songstress.ui.status.set('sync', { text: 'Synced', icon: 'check', tone: 'success' }, member)
})

// Back to its renderer once the member has seen it.
songstress.commands.register('dismiss', async (_input, context) => {
  await songstress.ui.status.set('sync', null, { userId: context.userId })
})
```

A value is what a renderer would answer (`StatusContent`, without functions): `text` (24 characters), `icon`, a
`sprite` of your package's own drawn in the icon's place, `tone`, `tooltip`, `progress`, `busy` and `action`. It shows
where the item shows, even in an app that cannot run its renderer, a store build of the phones say. A status item set
only from code may answer `null` from its renderer.

A status sprite may give each of its frames a length of its own (`durations`), for an icon that breathes rather than
ticks.

## Context keys [#context-keys]

A [condition](/docs/develop/interface/#conditions) reads what the app knows (`platform`, `signedIn`, a setting). Your
own keys let it read your extension's state too. Declare them, up to 20, each a `key`, a `type` (`boolean`, `number`
or `string`, a text of at most 100 characters) and optionally a `default`:

```json title="songstress.json"
"engines": { "songstress": "^0.1.13" },
"contributes": {
  "context": [{ "key": "asleep", "type": "boolean" }, { "key": "mood", "type": "string", "default": "calm" }],
  "commands": [{ "id": "wake", "title": "Wake it", "run": "client" }],
  "menus": { "player/actions": [{ "command": "wake", "when": "ctx.asleep" }] },
  "overlays": [{ "id": "pet", "title": "Pet", "render": "client", "size": { "width": 160, "height": 96 }, "when": "!ctx.asleep" }]
}
```

A key the extension's code sets and its conditions read, as ctx.<key>. Its type decides what it holds.

| Field | Type | Description |
|---|---|---|
| `key` | `string` | The name code sets it by. |
| `type` | `"boolean" \| "number" \| "string"` | True or false. A number. A text of at most 100 characters. |
| `default?` | `string \| number \| boolean \| undefined` | The value until code sets one. Left out, false. The value until code sets one. Left out, 0. The value until code sets one. Left out, empty. |

Then set them from code, and every menu item, page entry, panel, status item, sidebar entry and overlay under a
condition that reads them follows at once:

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

songstress.hooks.onPause(async () => {
  await songstress.ui.context.set('asleep', true)
})

songstress.commands.register('wake', async () => {
  await songstress.ui.context.set('asleep', null) // back to its default
})
```

The validator refuses a condition that reads a key the manifest does not declare (`WHEN_KEY_UNKNOWN`).

## Who sees what [#who-sees-what]

Who sets a state decides where it lives, for both calls, and for [badges](/docs/develop/sidebar/#set-a-badge) and
[overlays](/docs/develop/overlays/#show-it-and-hide-it-from-code) too:

- **The server entry** sets it for one member (`{ userId }`) or for every member, said in words
  (`{ everyone: true }`): a call with neither answers `invalid`, so one that forgets its member never shows one
  member's state to everyone. The server keeps it across a relaunch and tells each of the member's open apps at once.
- **Client code** sets it on its own device, in memory, until its code stops running there.

An app lays this device's state over the member's, and the member's over every member's, key by key. A state is 16 KiB
at most. The server entry writes ten a second for one member and one a second for every member, client code ten a
second (`SURFACE_CAPS`); more answer `limit-exceeded`.

## Test it [#test-it]

`host.surface(userId)` answers what your code set, as an app shows it to that member: `badges`, `context`, `status`
and `overlays`. See [Test and debug](/docs/develop/testing/).
