# Encounter state

The client reports the encounter as a [`NablaConnectState`](/connect/sdk/reference/type-aliases/NablaConnectState.md) snapshot. Use it to enable or disable your controls and to decide when it is safe to leave.

## The state object[​](#the-state-object "Direct link to The state object")

Every `stateChanged` event and `getCurrentState()` give a snapshot with these fields:

| Field              | Type                                                                            | Meaning                                                                                                                                                                                                                    |
| ------------------ | ------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `state`            | `EncounterState`                                                                | Where the encounter is. See [States](#states).                                                                                                                                                                             |
| `availableActions` | array of `AvailableAction`                                                      | Commands the iframe accepts right now.                                                                                                                                                                                     |
| `canBeClosed`      | `boolean`                                                                       | `true` when closing or navigating away loses nothing.                                                                                                                                                                      |
| `reason`           | `COMMAND`, `NOTE_GENERATION_FAILED`, or `null`                                  | Why this snapshot was emitted: `COMMAND` when it followed a command you sent, `NOTE_GENERATION_FAILED` when the note generation failed (see `failureReason`), `null` when the change came from the iframe or the provider. |
| `failureReason`    | `TRANSCRIPT_TOO_SHORT`, `NO_MEDICAL_DATA`, `RATE_LIMITED`, `UNKNOWN`, or `null` | Set when `reason` is `NOTE_GENERATION_FAILED`.                                                                                                                                                                             |

## States[​](#states "Direct link to States")

| `state`              | Meaning                                                                                         |
| -------------------- | ----------------------------------------------------------------------------------------------- |
| `NOT_STARTED`        | No recording yet. `availableActions` is empty while the provider finishes the Nabla onboarding. |
| `RECORDING_STARTING` | Start in progress.                                                                              |
| `RECORDING_STARTED`  | Live capture.                                                                                   |
| `RECORDING_STOPPING` | Stop in progress, audio being uploaded.                                                         |
| `PAUSED`             | Recording stopped, transcript present.                                                          |
| `NOTE_GENERATING`    | Note generation in progress.                                                                    |
| `NOTE_READY`         | Note generated, export allowed.                                                                 |

[Commands](/connect/sdk/guides/commands.md) move the encounter between states:

<!-- -->

`exportNote` does not change the state.

## Available actions[​](#available-actions "Direct link to Available actions")

`availableActions` is a subset of `START_RECORDING`, `STOP_RECORDING`, `GENERATE_NOTE` and `EXPORT_NOTE`. Hide or disable the controls that are not listed.

## Closing safely[​](#closing-safely "Direct link to Closing safely")

`canBeClosed` does not depend on the state. It is `false` while audio is recording or being uploaded, and while note edits are not saved yet. Generating a note does not change it: the generation runs on Nabla's servers. Any state can have either value, so read it from the snapshot, or `await client.waitUntilClosable()`, to decide whether teardown is safe. See [Leave and switch encounters](/connect/sdk/guides/leave-and-switch.md).

## Failed note generation[​](#failed-note-generation "Direct link to Failed note generation")

There is no failed state. A failed generation sends a snapshot with `reason` set to `NOTE_GENERATION_FAILED` and `failureReason` set, on the state the encounter falls back to, usually `PAUSED`.

## Listening to changes[​](#listening-to-changes "Direct link to Listening to changes")

`stateChanged` fires on load, every time `state`, `availableActions`, or `canBeClosed` changes, when a note generation fails, and after a command the iframe rejected (with the current state).

Events are not replayed. A `stateChanged` listener added before `ready` receives the first state. One added later only receives future changes. Use `await client.ready` or `getCurrentState()` to bootstrap. `getCurrentState()` returns `null` until the first state, and after the client ended.

Exceptions in your listeners do not stop the SDK.
