# Feedback

Use feedback APIs to notify users, confirm actions, and display forms for user input.

Dialogs belong to the operation that opened them: when it is cancelled, the call stops waiting and rejects with the cancellation reason.

## `notify()`

### `notify(message: string, options?): Promise<void>`

Show a notification message.

```ts
import { notify } from "azot";

await notify("Authorization successful");
```

With actions:

```ts
import { notify } from "azot";

await notify("Content is available only with a subscription", {
  title: "Error",
  style: "destructive",
  primaryAction: {
    title: "Go to pricing page",
    onAction: () => window.open("https://example.com/pricing", "_blank"),
  },
});
```

`options`:

- `title?: string`
- `style?: 'default' | 'destructive'`
- `primaryAction?: ActionOptions`
- `secondaryAction?: ActionOptions`

## `confirm()`

### `confirm(message: string, options?): Promise<boolean>`

Ask user confirmation.

```ts
import { confirm } from "azot";

const preferSignIn = await confirm(
  "For registered users, video is available in higher quality. Do you want to sign in?",
);

if (preferSignIn) {
  await confirm("Enter code 123456 at https://example.tv/auth and then press Enter to continue");
}
```

With custom actions:

```ts
import { confirm } from "azot";

const shouldSignIn = await confirm("Authentication issues detected. Login again?", {
  title: "Authentication error",
  style: "destructive",
  primaryAction: {
    title: "Login",
  },
  secondaryAction: {
    title: "Continue without login",
  },
});
```

## `prompt()`

### `prompt(message: string, options): Promise<PromptResult<TForm>>`

Ask for structured input.

```ts
import { prompt } from "azot";

const values = await prompt("Enter credentials", {
  title: "Authorization",
  form: {
    email: {
      type: "text",
      title: "Email",
      placeholder: "name@example.com",
    },
    password: {
      type: "password",
      title: "Password",
    },
    remember: {
      type: "checkbox",
      title: "Remember me",
      defaultValue: "true",
      required: false,
    },
  },
  primaryAction: { title: "Sign in" },
  secondaryAction: { title: "Cancel", style: "cancel" },
});

console.log(values.email); // string
console.log(values.password); // string
console.log(values.age); // number
console.log(values.remember); // boolean
```

### Images

`confirm()` and `prompt()` can show an image above the message, such as a QR code or a captcha. Pass a public image as an http(s) URL:

```ts
await confirm("Scan the QR code in the Example app", {
  image: { src: "https://cdn.example.tv/qr/123.png", alt: "Login QR code" },
});
```

Azot loads URL images without cookies or headers. If the image needs your session, fetch it yourself and pass the bytes as a `Blob` or `Uint8Array`. Fetching it through the extension's `fetch` also keeps any cookies the response sets, which a captcha usually checks on submit:

```ts
import { prompt } from "azot";

const captcha = await fetch("https://example.tv/captcha", {
  headers: { authorization: `Bearer ${token}` },
});
const { code } = await prompt("Enter the code from the image", {
  image: { src: await captcha.blob(), alt: "Captcha" },
  form: { code: { type: "text", title: "Code" } },
});
```

PNG, JPEG, GIF, WebP and AVIF images up to 4 MB are accepted. Azot detects the format from the bytes, so the declared content type doesn't matter. Any other image makes the call throw a `TypeError` before the dialog opens. The CLI can't show images: it prints the URL, or saves the bytes to a temporary file, prints its path and deletes it when the dialog closes.

## Types

### `DialogImage`

```ts
type DialogImage = {
  src: string | Blob | Uint8Array;
  alt?: string;
};
```

### `ActionOptions`

```ts
type ActionOptions = {
  title: string;
  style?: "default" | "destructive" | "cancel";
  onAction?: () => void | Promise<void>;
};
```

### `PromptField`

```ts
type PromptField = {
  type: "text" | "password" | "number" | "checkbox";
  title?: string;
  description?: string;
  placeholder?: string;
  defaultValue?: string;
  required?: boolean;
  autoFocus?: boolean;
};
```

Fields are required by default. Set `required: false` on a field to allow an empty value.
