# Connections

> Connect outside services, and see each connection's status.

Page: https://uicontext.com/docs/components/connections

Group: Settings.

## When to use

Connect outside services, and see each connection's status. Use [Secrets](https://uicontext.com/docs/components/secrets.md) instead when you store a key, not a connection.

## Usage

```ts
import { ConnectorCard, type ConnectorStatus } from "./ui/uicontext/components/Connect.ts"

const Message = UiSchema.TaggedEnum({
	ConnectedMail: {},
	DisconnectedMail: {},
})

export const MailConnection = (h: Html.H<typeof Message.Type>, status: ConnectorStatus, account: string | undefined) =>
	ConnectorCard(h, {
		props: {
			id: "mail",
			name: "Mail",
			description: "Send email as you.",
			status,
			...(account === undefined ? {} : { account }),
			can: ["connect", "disconnect"],
			disconnectWarning: "Workflows that send email pause.",
		},
		onEmit: {
			RequestedConnect: () => Message.ConnectedMail(),
			RequestedReconnect: () => Message.ConnectedMail(),
			RequestedDisconnect: () => Message.DisconnectedMail(),
			RequestedSecondary: () => undefined,
			RequestedApplies: () => undefined,
		},
	})
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Connect, reconnect and disconnect

Connecting shows progress until the app reports back; an expired sign-in offers Reconnect, and disconnecting says what stops first.

```ts title="src/demo.ts" source="www/src/examples/connections/demo.ts"
import { Command, UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Effect, Schema } from "effect"
import { CatalogConnector, ConnectorCatalog } from "./ui/uicontext/components/Connect.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

const styles = stylex.create({ frame: { inlineSize: "100%" } })

const Message = UiSchema.TaggedEnum({
	Connect: { id: Schema.String },
	Disconnect: { id: Schema.String },
	Finished: { id: Schema.String, status: Schema.Literals(["connected", "disconnected"]) },
})

// Stands in for the app's sign-in window and the provider's answer.
const Authorize = Command.make("ConnectionsDemo.Authorize", { args: { id: Schema.String, to: Schema.Literals(["connected", "disconnected"]) } }, ({ id, to }) =>
	Effect.sleep("900 millis").pipe(Effect.as(Message.Finished({ id, status: to }))))

const seed: ReadonlyArray<CatalogConnector> = [
	{ id: "chat", name: "Team Chat", category: "Communication", description: "Read channels you choose and post replies.", status: "connected", account: "ana@northwind.example", usedBy: "Used by 4 workflows", can: ["connect", "disconnect"], disconnectWarning: "4 workflows pause until you connect again." },
	{ id: "mail", name: "Mail", category: "Communication", description: "Send email as you.", status: "expired", account: "ana@northwind.example", detail: "Sign-in expired Nov 27. Workflows that send email are paused.", can: ["connect", "disconnect"] },
	{ id: "crm", name: "Pipeline CRM", category: "Sales", description: "Contacts, companies and deals.", status: "available", can: ["connect"] },
	{ id: "warehouse", name: "Data Warehouse", category: "Data", description: "Query and load tables.", status: "unavailable", lockedReason: "Turned off by your admin" },
]

export const Connections = Html.component({
	name: "Connections",
	model: Schema.Array(CatalogConnector),
	message: Message,
}).make({
	init: () => seed,
	update: {
		Connect: ($, { id }) =>
			$.set($.model.map((c) => (c.id === id ? { ...c, status: "connecting" as const, detail: "Approve access in the sign-in window." } : c))).run(Authorize.make({ id, to: "connected" })),
		Disconnect: ($, { id }) =>
			$.set($.model.map((c) => (c.id === id ? { ...c, status: "disconnecting" as const } : c))).run(Authorize.make({ id, to: "disconnected" })),
		Finished: ($, { id, status }) =>
			$.model.map((c) => { if (c.id !== id) return c; const { account: _account, detail: _detail, ...rest } = c; return status === "connected" ? { ...rest, status, account: "ana@northwind.example", can: ["connect", "disconnect"] as const } : { ...rest, status } }),
	},
	view: (h, connectors) =>
		h.div.attrs(sx(styles.frame)).children(
			ConnectorCatalog(h, {
				props: { label: "Connections", agentId: "connections-demo", connectors, searchable: true },
				onEmit: {
					RequestedConnect: ({ id }) => Message.Connect({ id }),
					RequestedReconnect: ({ id }) => Message.Connect({ id }),
					RequestedDisconnect: ({ id }) => Message.Disconnect({ id }),
					RequestedSecondary: () => undefined,
					RequestedApplies: () => undefined,
				},
			}),
		),
})
```

## API reference

### ConnectorCard (component)

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `id` (required) | `string` | — | The connector's id, unique among its siblings |
| `agentId` | `string` | `connector:<id>` | This card's id for agents, unique on the page; `connector:<id>` by default |
| `name` (required) | `string` | — | The service's name: "Google Drive" |
| `description` | `string` | — | What it lets an agent do, in a sentence |
| `icon` | `string` | — | An image URL for the app's icon; the name's first letter otherwise |
| `status` (required) | `ConnectorStatus` | — | Where the connection stands: available, connected, syncing, expired, failed and so on |
| `statusLabel` | `string` | — | Replaces the status's default words |
| `detail` | `string` | — | A line under the status: "Last synced 2 minutes ago", "Expired Nov 3", "Waiting for the workspace admin" |
| `account` | `string` | — | The account it signs in as: "ana@example.com" |
| `owner` | `string` | — | Whose connection it is, when shared: "Shared by Ben", "Your own account" |
| `usedBy` | `string` | — | What relies on it: "Used by 4 workflows" |
| `permissions` | `string[]` | — | What it may do, plainly: ["Read issues", "Post messages"] |
| `can` | `Can(["connect", "disconnect"])` | — | What this person may do: "connect" connects or reconnects it (and changes where it applies), "disconnect" disconnects it. Left out, they may connect and not disconnect. |
| `lockedReason` | `string` | — | Why it can't be connected: "Turned off by your admin", "Data is still being removed" |
| `requiredReason` | `string` | — | Why it can't be disconnected: "Required by your team" |
| `disconnectWarning` | `string` | — | What stops on disconnect: "Indexed pages are deleted. 4 workflows pause." |
| `connectLabel` | `string` | `Connect` | The connect button's words; `Connect` by default |
| `secondary` | `ConnectorStep` | — | A second step: "Manage", "Retry sync", "Cancel request" |
| `appliesTo` | `{ legend, options }` | — | Where it may be used, as checkboxes |
| `layout` | `"card" \| "row"` | — | A card in a grid, or a row in a list |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedConnect` | `{}` | Start connecting: the app runs the sign-in |
| `RequestedReconnect` | `{}` | Sign in again to a connection that expired or failed |
| `RequestedDisconnect` | `{}` | Disconnect, after the person confirmed |
| `RequestedSecondary` | `{}` | The second step was pressed: "Manage", "Retry sync" |
| `RequestedApplies` | `{ values: string[] }` | Every value that should be checked now |

**Agent actions**

| Action | Description |
| --- | --- |
| `ClickedConnect` | Start connecting. The person finishes the sign-in; you never handle credentials. |
| `ClickedReconnect` | Start reconnecting an expired or broken connection. The person finishes the sign-in. |
| `ConfirmedDisconnect` (asks first) | Disconnect. Name { name, account } as the target. |
| `ToggledApplies` | Check or uncheck one place it applies to (value from page_query). Checking widens access: name the checked values as the target. |

### ConnectorCatalog (component)

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `agentId` | `string` | `catalog:<label>` | The id agents know this catalog by; `catalog:<label>` by default |
| `label` (required) | `string` | — | "Connectors", "Apps" |
| `connectors` (required) | `CatalogConnector[]` | — | The services, each with its category and status |
| `layout` | `"card" \| "row"` | — | Cards in a grid (browsing) or rows in a list (what's connected) |
| `searchable` | `boolean` | — | Show the search field and category filter; by default only for more than 6 connectors |
| `searchPlaceholder` | `string` | `Name or what it does` | The search field's placeholder; `Name or what it does` by default |
| `emptyTitle` | `string` | `Nothing to connect yet` | What an empty catalog says; `Nothing to connect yet` by default |
| `emptyHint` | `string` | — | A hint under the empty title at what to do next |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedConnect` | `{ id: string }` | Start connecting a service: the app runs the sign-in |
| `RequestedReconnect` | `{ id: string }` | Sign in again to a connection that expired or failed |
| `RequestedDisconnect` | `{ id: string }` | Disconnect a service, after the person confirmed |
| `RequestedSecondary` | `{ id: string }` | A service's second step was pressed: "Manage", "Retry sync" |
| `RequestedApplies` | `{ id: string; values: string[] }` | What a service applies to changed: every value that should be checked now |

**Agent actions**

| Action | Description |
| --- | --- |
| `UpdatedQuery` | Search connectors by name or description |
| `ChoseCategory` | Show one category (value from page_query), or "" for all |

## Keyboard and accessibility

- Each connector is a labelled group; its status is text, never color alone.
- Disconnecting asks first and says what stops.
- The catalog's search is a `search` landmark, and its count of results is announced as you filter.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/connections
```

With npm: `npx shadcn@latest add checkfu/uicontext/connections`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/connections`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/connections`.

It writes these files:

- `src/ui/uicontext/components/Button.ts`
- `src/ui/uicontext/components/Connect.ts`
- `src/ui/uicontext/components/FormControls.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/ShowText.ts`
- `src/ui/uicontext/components/States.ts`
- `src/ui/uicontext/components/StatusBadge.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/agent/Agent.ts`
- `src/ui/uicontext/components/button.stylex.ts`
- `src/ui/uicontext/components/domain/Permission.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/variants.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Connect.ts](https://github.com/checkfu/uicontext/blob/main/src/Connect.ts)

## Used in

- [Workflow builder](https://uicontext.com/blocks/automation-builder.md)
- [Bots](https://uicontext.com/blocks/bot-console.md)

## Related

- [Sign-in Form](https://uicontext.com/docs/components/sign-in-form.md) (`sign-in-form`): Sign in, accept an invitation and manage sessions.
- [Settings](https://uicontext.com/docs/components/settings.md) (`settings`): A settings page with sections, fields and a save bar.
- [Env Editor](https://uicontext.com/docs/components/env-editor.md) (`env-editor`): Names and values such as API keys and URLs, one row each: secrets hidden, a pasted .env file split into rows, mistakes marked.
- [Setting List](https://uicontext.com/docs/components/setting-list.md) (`setting-list`): Settings as rows, each with its control.
- [Inherited Settings](https://uicontext.com/docs/components/inherited-settings.md) (`inherited-settings`): Settings that a workspace sets and a team or project can override.
- [Members](https://uicontext.com/docs/components/members.md) (`members`): Workspace members, their roles and pending invitations.
