# The sidebar and badges

> Entries in the sidebar's Extensions section (the Library tab's group on a phone), what each opens, and a badge your code sets.

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

Every app has one place for what extensions add to its navigation: the **Extensions** section of the sidebar on the
web and the desktop, the last section (below System for an admin, below Library for a member, and last in the
collapsed rail too), and the **Extensions** group of the Library tab on a phone. An entry there opens one of your
pages or views, or runs one of your commands, and may carry a badge your code sets. Ask for
`"songstress": "^0.1.13"`.

<Shot name="web/ext-sidebar-section" alt="The sidebar's Extensions section: entries of several extensions, Sync now with a dot and Recent plays with a badge of 99+" />

## Declare an entry [#declare-an-entry]

Up to five entries an extension, under `contributes.sidebar`:

```json title="songstress.json"
"engines": { "songstress": "^0.1.13" },
"hooks": { "server": ["onInstall"] },
"contributes": {
  "pages": [{ "id": "inbox", "title": "Inbox", "render": "server" }],
  "commands": [{ "id": "sync", "title": "Sync now", "run": "server" }],
  "jobs": [{ "id": "check", "title": "Check for new songs" }],
  "sidebar": [
    { "id": "inbox", "title": "Inbox", "icon": "notification", "page": "inbox" },
    { "id": "sync", "title": "Sync now", "icon": "sync", "command": "sync", "order": 90, "when": "setting.mode == 'manual'" }
  ]
}
```

One entry of the extension's in the sidebar: a page, a view or a command, and when it shows.

| Field | Type | Description |
|---|---|---|
| `id` | `string` | The entry's name: code sets its badge by it. |
| `title` | `string` | The entry's label. |
| `icon?` | `IconName \| undefined` | The entry's icon. Left out, the extension's mark. |
| `order?` | `number \| undefined` | Where the entry stands among the extension's own: lower first. Left out, after those that say, in the order written. |
| `when?` | `string \| undefined` | The entry shows only while this holds. |
| `page?` | `string \| undefined` | The page it opens. An entry names one of page, view and command. |
| `view?` | `string \| undefined` | The view it opens: in the app where the view is inApp and the app draws in-app views, in the browser where it opens views, and nowhere else. |
| `command?` | `string \| undefined` | The command it runs. |

Each entry names exactly one target, a `page`, a `view` or a `command` the manifest declares (`SIDEBAR_TARGET`).
A view's entry opens it inside the app where it is [in-app](/docs/develop/views/) and the app draws in-app views, in
the browser where the app opens views, and nowhere else. `when` holds the entry back while its
[condition](/docs/develop/interface/#conditions) is false: name the setting, the sign-in or your own
[context key](/docs/develop/status-and-context/#context-keys) it waits for.

A page with `"sidebar": true`, a view with `"sidebar": true` and an item of `menus.sidebar` still ask for an entry, as
they did before 0.1.13: an app lists the declared entries first, then those, five an extension in all.

## Set a badge [#set-a-badge]

A badge is a count from 1 to 9999 (shown as "99+" over 99), a dot (`'dot'`), or nothing (`null` or 0). The server
entry sets it for one member or, said in words, for every member; a call with neither answers `invalid`, so one
that forgets its member never shows one member's count to everyone:

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

// A check a member starts: what it found waits on the Inbox page, and the entry's badge says how many.
songstress.jobs.register('check', async (run) => {
  if (!run.userId) return
  const member = { userId: run.userId }
  const found = ['s1', 's2', 's3'] // the songs this run found for the member
  await songstress.storage.set('waiting', found, member)
  await songstress.ui.sidebar.setBadge('inbox', found.length, member)
})

// The page clears the member's own badge once they have seen it.
songstress.ui.pages.register('inbox', async (context) => {
  const member = { userId: context.userId }
  const waiting = (await songstress.storage.get<string[]>('waiting', member)) ?? []
  await songstress.ui.sidebar.setBadge('inbox', null, member)
  return {
    type: 'list',
    sections: [{ title: 'Waiting for you', items: waiting.map((songId) => ({ type: 'song' as const, songId })) }],
    empty: { title: 'Nothing waiting', icon: 'notification' },
  }
})

// Once installed, a dot on every member's entry, said in words.
songstress.hooks.onInstall(async () => {
  await songstress.ui.sidebar.setBadge('inbox', 'dot', { everyone: true })
})
```

The server keeps what it sets, so a badge is still there after a relaunch, and tells each of the member's open apps
at once. Client code sets a badge on its own device, in memory, for as long as its code runs there:

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

songstress.hooks.onTrackChange(async () => {
  await songstress.ui.sidebar.setBadge('inbox', 'dot')
})
```

An app lays this device's badge over the member's, and the member's over every member's. The server entry writes ten
a second for one member and one a second for every member, client code ten a second; more answer `limit-exceeded`.

## Good entries [#good-entries]

- One entry is usually enough. A tool with several pages gives them one first page that links on, as the
  [UX guidelines](/docs/develop/guidelines/) say.
- A badge counts what waits for the person, and goes when they have seen it.
- Entries show on every app, phones included: give a page or a command for a phone, since a view opens in the
  browser there.

## Test it [#test-it]

The mock host keeps what your code sets, as an app shows it to a member: run the job, then read
`host.surface('ana').badges` for the badge it left. See
[Test and debug](/docs/develop/testing/).
