# Examples · Booki

> Try an invoice and three Norwegian website routes with and without Booki, then explore the controls and build your own.

Source: https://booki.dev/docs/examples

Agent

# Examples

Try the live Booki demos on the site’s agent bridge and read the code behind them.

## One invoice, two ways

Both forms are live. An agent booked the same invoice on each, and the page counted every character it read and wrote. Both paid. Compare what it took.

Plain HTML + browser tools

Agent working…

| Account | Debit | Credit |
| --- | --- | --- |
|  |  |  |
|  |  |  |
|  |  |  |

**0 calls, as the model sees them**

**The code**

```
import { useState, type FormEvent, type RefObject } from "react";

import { useLang } from "@/lib/lang";

const sum = (form: HTMLFormElement, column: string) =>
  [1, 2, 3].reduce((total, line) => {
    const cell = form.elements.namedItem(`${column}${line}`) as HTMLInputElement;
    return total + (Number.parseFloat(cell.value) || 0);
  }, 0);

/** The invoice as plain semantic HTML: labels, inputs, a select, a table. */
export function SemanticInvoice({ formRef }: { formRef: RefObject<HTMLFormElement | null> }) {
  const { t } = useLang();
  const [totals, setTotals] = useState({ debit: 0, credit: 0 });
  const [paid, setPaid] = useState(false);

  function onInput(event: FormEvent<HTMLFormElement>) {
    const form = event.currentTarget;
    setTotals({ debit: sum(form, "debit"), credit: sum(form, "credit") });
  }

  return (
    <form
      data-plain-html=""
      ref={formRef}
      aria-label={t("Invoice", "Faktura")}
      onInput={onInput}
      onSubmit={(event) => {
        event.preventDefault();
        setPaid(true);
      }}
    >
      <label>
        {t("Amount excl. tax", "Beløp eks. avgift")}
        <input name="amount" inputMode="decimal" defaultValue="2490.00" />
      </label>
      <label>
        {t("Sales tax", "Omsetningsavgift")}
        <select name="tax" defaultValue="TAX0">
          <option value="TAX0">{t("0% tax", "0 % avgift")}</option>
          <option value="TAX8875">{t("8.875% tax", "8,875 % avgift")}</option>
        </select>
      </label>
      <table>
        <thead>
          <tr>
            <th>{t("Account", "Konto")}</th>
            <th>{t("Debit", "Debet")}</th>
            <th>{t("Credit", "Kredit")}</th>
          </tr>
        </thead>
        <tbody>
          {[1, 2, 3].map((line) => (
            <tr key={line}>
              <td>
                <input name={`account${line}`} aria-label={t(`Account, line ${line}`, `Konto, linje ${line}`)} />
              </td>
              <td>
                <input name={`debit${line}`} aria-label={t(`Debit, line ${line}`, `Debet, linje ${line}`)} />
              </td>
              <td>
                <input name={`credit${line}`} aria-label={t(`Credit, line ${line}`, `Kredit, linje ${line}`)} />
              </td>
            </tr>
          ))}
        </tbody>
      </table>
      <output>
        {t("Debit", "Debet")} {totals.debit.toFixed(2)} · {t("Credit", "Kredit")} {totals.credit.toFixed(2)}
      </output>
      <button type="submit" disabled={paid}>
        {t("Pay invoice", "Betal faktura")}
      </button>
      <output>{paid ? t("Payment sent.", "Betaling sendt.") : ""}</output>
    </form>
  );
}
```

Booki + the agent bridge

Agent working…

| Account | Name | Debit | Credit |
| --- | --- | --- | --- |
| In balance | $0.00 | $0.00 |  |

In balance · $0.00 / $0.00

**0 calls, as the model sees them**

**The code**

```
import type { VoucherLine } from "@booki-aui/core/voucher";
import { AmountField, TaxCodeSelect, VoucherComposer } from "@booki-aui/react";
import { Button } from "@ui-sdk/react/button";
import { useState } from "react";

import { useLang } from "@/lib/lang";

/** The same invoice as Booki controls on the agent bridge. */
export function BookiInvoice() {
  const { t } = useLang();
  const [minor, setMinor] = useState(249_000);
  const [code, setCode] = useState("TAX0");
  const [lines, setLines] = useState<VoucherLine[]>([]);
  const [paid, setPaid] = useState(false);

  return (
    <div className="acc-invoice">
      <label>
        {t("Amount excl. tax", "Beløp eks. avgift")}
        <AmountField value={minor} onValueChange={setMinor} aria-label={t("Amount excl. tax", "Beløp eks. avgift")} />
      </label>
      <label>
        {t("Sales tax", "Omsetningsavgift")}
        <TaxCodeSelect value={code} onValueChange={setCode} />
      </label>
      <VoucherComposer lines={lines} onLinesChange={setLines} aria-label={t("Voucher", "Bilag")} />
      <Button
        type="button"
        disabled={paid}
        data-agent-confirm="Pays the invoice from the company account"
        onClick={() => setPaid(true)}
      >
        {t("Pay invoice", "Betal faktura")}
      </Button>
      <output>{paid ? t("Payment sent.", "Betaling sendt.") : ""}</output>
    </div>
  );
}
```

Form only, on both sides. Tokens are characters ÷ 4. Browser tools return the page after each action.

## Flexible

Controls ship unstyled, and a country is data. Switch the look and only the person's view changes. Switch the country and the agent learns new rules.

The person sees

11 siffer, formatert som XXXX XX XXXXX

The agent reads

```

```

## Extensible

Your own control joins the bridge. This one is 65 lines, and Booki does not ship it.

The person sees

The agent reads

```

```

Press an action: the agent calls it, the control answers.

**cost-centre.tsx**

```
import { createChange, definePrimitive } from "@ui-sdk/core";
import { createParts } from "@ui-sdk/react/primitive";

import { useLang } from "@/lib/lang";

const CENTRES = [
  { value: "100", label: "100 Sales", nb: "100 Salg" },
  { value: "420", label: "420 Marketing", nb: "420 Markedsføring" },
  { value: "700", label: "700 Operations", nb: "700 Drift" },
];

/** A control Booki does not ship. Defining it puts it on the agent bridge. */
const costCentre = definePrimitive({
  name: "cost-centre",
  options: { defaultValue: "string" },
  state: (options: { defaultValue?: string }) => ({
    value: options.defaultValue ?? "100",
  }),
  api: (context) => {
    const change = createChange(context);
    return {
      setCostCentre: (code: string, event?: Event) => {
        if (!CENTRES.some((centre) => centre.value === String(code))) {
          throw new Error(`No cost centre "${code}". Use 100, 420 or 700.`);
        }
        return change("value", String(code), { reason: "set-cost-centre", event });
      },
    };
  },
  slots: (slot) => ({
    // data-booki: take @booki-aui/styles' base look for fields and buttons, like Booki's own controls.
    root: slot({ role: "group", attrs: () => ({ "data-booki": "" }) }),
    select: slot({
      attrs: (state) => ({ value: state.value, "aria-label": "Cost centre" }),
      on: {
        // "input" is the adapter's name for React's onChange.
        input: (event, { api }) => {
          api.setCostCentre((event.target as HTMLSelectElement).value, event);
        },
      },
    }),
  }),
  agent: {
    actions: ["setCostCentre"],
    closed: true,
    items: () => CENTRES.map(({ value, label }) => ({ value, label })),
  },
});

const parts = createParts(costCentre, { root: "div", select: "select" });

export function CostCentre() {
  const { t } = useLang();
  return (
    <parts.Root aria-label={t("Cost centre", "Kostnadssted")}>
      <parts.Select aria-label={t("Cost centre", "Kostnadssted")}>
        {CENTRES.map((centre) => (
          <option key={centre.value} value={centre.value}>
            {t(centre.label, centre.nb)}
          </option>
        ))}
      </parts.Select>
    </parts.Root>
  );
}
```
