# Audit Log

> Who did what in a workspace, and when.

Page: https://uicontext.com/docs/components/audit-log

Group: Settings.

## When to use

Who did what in a workspace, and when. Use [Activity Feed](https://uicontext.com/docs/components/activity-feed.md) instead when the log is one record's history.

## Usage

```ts
import { AuditLog, type AuditEntry } from "./ui/uicontext/components/Audit.ts"

const Message = UiSchema.TaggedEnum({
	FilteredAudit: { selected: Schema.Record(Schema.String, Schema.String) },
	RequestedMoreAudit: {},
})

export const WorkspaceAudit = (
	h: Html.H<typeof Message.Type>,
	entries: ReadonlyArray<AuditEntry>,
	selected: Readonly<Record<string, string>>,
	hasMore: boolean,
) =>
	AuditLog(h, {
		props: {
			label: "Audit log",
			entries,
			hasMore,
			selected,
			filters: [{ id: "who", label: "Who", field: "actorKind", options: [{ value: "agent", label: "Agents" }] }],
		},
		onEmit: {
			RequestedFilters: ({ selected }) => Message.FilteredAudit({ selected }),
			RequestedMore: () => Message.RequestedMoreAudit(),
			RequestedQuery: () => undefined,
			RequestedExport: () => undefined,
			RequestedRowAction: () => undefined,
		},
	})
```

## Examples

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

### Audit log

Search, filter, load older entries and export what matches.

This example also uses Members. Install it too:

```sh
npx shadcn@latest add checkfu/uicontext/members
```

```ts title="src/demo.ts" source="www/src/examples/audit-log/demo.ts"
import { Command, Component, UiSchema } from "effect/ui"
import { Html, HtmlFiles } from "effect/ui/html"
import { Effect, Schema } from "effect"
import { AuditLog, type AuditFilter, auditToCsv, filterAudit } from "./ui/uicontext/components/Audit.ts"
import { auditEntries } from "./workspace.ts"

// Three hundred entries filtered and searched in memory, 25 at a time. Export makes a CSV of
// every match, not only the rows on screen. With a server, send the filters and the search with
// the request instead.

const all = auditEntries(300)
const PAGE = 25

const filters: ReadonlyArray<AuditFilter> = [
	{
		id: "who",
		label: "Who",
		field: "actorKind",
		options: [
			{ value: "person", label: "People" },
			{ value: "agent", label: "Agents" },
			{ value: "routine", label: "Routines" },
			{ value: "integration", label: "Integrations" },
		],
	},
	{
		id: "outcome",
		label: "Outcome",
		field: "status",
		options: [
			{ value: "succeeded", label: "Done" },
			{ value: "failed", label: "Failed" },
			{ value: "refused", label: "Refused" },
			{ value: "waiting", label: "Held" },
		],
	},
]

const Message = UiSchema.TaggedEnum({
	FilteredLog: { selected: Schema.Record(Schema.String, Schema.String) },
	SearchedLog: { query: Schema.String },
	RequestedMoreLog: {},
	ExportedLog: { format: Schema.Literals(["csv", "json"]), selected: Schema.Record(Schema.String, Schema.String), query: Schema.String },
})

// A real app asks its server for the file; here it's made from the entries in memory.
const DownloadLog = Command.make(
	"DownloadLog",
	{ args: { format: Schema.Literals(["csv", "json"]), selected: Schema.Record(Schema.String, Schema.String), query: Schema.String } },
	({ format, selected, query }) => {
		const rows = filterAudit(all, filters, selected, query)
		const file =
			format === "csv"
				? new File([auditToCsv(rows)], "audit-log.csv", { type: "text/csv" })
				: new File([JSON.stringify(rows, null, 2)], "audit-log.json", { type: "application/json" })
		return HtmlFiles.download(file).pipe(Effect.as(Component.noop))
	},
)

export const AuditDemo = Html.component({
	name: "AuditDemo",
	model: Schema.Struct({ selected: Schema.Record(Schema.String, Schema.String), query: Schema.String, shown: Schema.Number }),
	message: Message,
}).make({
	init: () => ({ selected: {}, query: "", shown: PAGE }),
	update: {
		FilteredLog: ($, { selected }) => $.evolve({ selected, shown: PAGE }),
		SearchedLog: ($, { query }) => $.evolve({ query, shown: PAGE }),
		RequestedMoreLog: ($) => $.evolve({ shown: (n) => n + PAGE }),
		ExportedLog: ($, args) => $.run(DownloadLog.make(args)),
	},
	view: (h, model) => {
		const matching = filterAudit(all, filters, model.selected, model.query)
		return AuditLog(h, {
			props: {
				agentId: "audit-demo",
				label: "Audit log",
				entries: matching.slice(0, model.shown),
				total: matching.length,
				hasMore: matching.length > model.shown,
				filters,
				selected: model.selected,
				search: { placeholder: "Person, action or place" },
				query: model.query,
				exportFormats: ["csv", "json"],
				timeZone: "Europe/Lisbon",
				locale: "en-GB",
			},
			onEmit: {
				RequestedFilters: ({ selected }) => Message.FilteredLog({ selected }),
				RequestedQuery: ({ query }) => Message.SearchedLog({ query }),
				RequestedMore: () => Message.RequestedMoreLog(),
				RequestedExport: (args) => Message.ExportedLog(args),
				RequestedRowAction: () => undefined,
			},
		})
	},
})
```

### Sign-ins

A row action on each live session. Ending one marks its entry ended.

This example also uses Members. Install it too:

```sh
npx shadcn@latest add checkfu/uicontext/members
```

```ts title="src/sign-ins.ts" source="www/src/examples/audit-log/sign-ins.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import { Schema } from "effect"
import { AuditLog, type AuditEntry } from "./ui/uicontext/components/Audit.ts"
import { ana, ben } from "./workspace.ts"

// Sign-ins with an action on each live one. Ending a session marks the entry ended.

const entries: ReadonlyArray<AuditEntry> = [
	{ id: "s1", at: "2026-12-01T09:12:00Z", actor: ana, action: "Signed in with", target: { label: "a passkey" }, place: "Lisbon, Portugal", detail: "Chrome on macOS", result: { status: "info", label: "Active" }, rowAction: "End session" },
	{ id: "s2", at: "2026-11-30T22:40:00Z", actor: ana, action: "Signed in with", target: { label: "a magic link" }, place: "Porto, Portugal", detail: "Safari on iPhone", result: { status: "info", label: "Active" }, rowAction: "End session" },
	{ id: "s3", at: "2026-11-30T18:03:00Z", actor: ben, action: "Tried to sign in with", target: { label: "a password" }, place: "Lagos, Nigeria", result: { status: "refused", reason: "Wrong password, 5 times. Locked for 15 minutes." } },
	{ id: "s4", at: "2026-11-29T08:55:00Z", actor: ben, action: "Signed in with", target: { label: "a password" }, place: "Lagos, Nigeria", detail: "Firefox on Windows", result: { status: "info", label: "Ended" } },
]

const Message = UiSchema.TaggedEnum({ EndedSession: { id: Schema.String } })

export const SignIns = Html.component({
	name: "AuditSignIns",
	model: Schema.Struct({ ended: Schema.Array(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ ended: [] }),
	update: { EndedSession: ($, { id }) => $.evolve({ ended: (e) => [...e, id] }) },
	view: (h, { ended }) =>
		AuditLog(h, {
			props: {
				agentId: "audit-sign-ins",
				label: "Sign-ins",
				timeZone: "Europe/Lisbon",
				locale: "en-GB",
				entries: entries.map((e) => {
					if (!ended.includes(e.id)) return e
					const { rowAction: _, ...rest } = e
					return { ...rest, result: { status: "info" as const, label: "Ended by you" } }
				}),
			},
			onEmit: {
				RequestedRowAction: ({ id }) => Message.EndedSession({ id }),
				RequestedFilters: () => undefined,
				RequestedQuery: () => undefined,
				RequestedExport: () => undefined,
				RequestedMore: () => undefined,
			},
		}),
})
```

### Empty

A new workspace's log says what will show up in it.

```ts title="src/empty.ts" source="www/src/examples/audit-log/empty.ts"
import type { Html } from "effect/ui/html"
import { AuditLog } from "./ui/uicontext/components/Audit.ts"

// A workspace whose log has nothing in it yet.

export const EmptyLog = <M>(h: Html.H<M>) =>
	AuditLog(h, {
		props: {
			agentId: "audit-empty",
			label: "Audit log",
			entries: [],
			emptyTitle: "Nothing recorded yet",
			emptyHint: "Sign-ins, setting changes and every agent action show here.",
		},
		onEmit: {
			RequestedFilters: () => undefined,
			RequestedQuery: () => undefined,
			RequestedExport: () => undefined,
			RequestedMore: () => undefined,
			RequestedRowAction: () => undefined,
		},
	})
```

### Sample data

The examples above import these files.

```ts title="src/workspace.ts" source="www/src/examples/_shared/workspace.ts"
import type { Actor } from "./ui/uicontext/components/domain/Actor.ts"
import type { AuditEntry } from "./ui/uicontext/components/Audit.ts"
import type { Member, MemberRole } from "./ui/uicontext/components/Members.ts"

// The Northwind Freight workspace: its people, their roles, and a month of its audit log. The
// people and entries are made from fixed lists, so every visitor sees the same workspace.

export const ana: Actor = { id: "ana", name: "Ana Lopez", kind: "person" }
export const ben: Actor = { id: "ben", name: "Ben Okafor", kind: "person" }
export const mira: Actor = { id: "mira", name: "Mira Patel", kind: "person" }
export const scout: Actor = { id: "scout", name: "Scout", kind: "agent" }
export const digest: Actor = { id: "digest", name: "Monday digest", kind: "routine" }
export const billing: Actor = { id: "billing-sync", name: "Billing sync", kind: "integration" }

export const roles: ReadonlyArray<MemberRole> = [
	{ value: "owner", label: "Owner", description: "Everything, including billing and deleting the workspace." },
	{ value: "admin", label: "Admin", description: "Members, connections and settings. Not billing." },
	{ value: "member", label: "Member", description: "Builds and runs automations. Can't change workspace settings." },
	{ value: "guest", label: "Guest", description: "Sees only what's shared with them." },
]

const first = ["Ana", "Ben", "Mira", "Tomás", "Yuki", "Priya", "Owen", "Leila", "Jonas", "Amara", "Diego", "Hana", "Felix", "Sana", "Noor", "Ravi"]
const last = ["Lopez", "Okafor", "Patel", "Novak", "Tanaka", "Iyer", "Brennan", "Haddad", "Berg", "Mensah", "Ruiz", "Kim", "Wagner", "Qureshi", "Ali", "Shah"]

/** `count` people of Northwind Freight: Ana owns it, a few admins, mostly members, some guests. */
export const people = (count: number): ReadonlyArray<Member> =>
	Array.from({ length: count }, (_, i) => {
		if (i === 0) return { ...ana, email: "ana@northwind.example", role: "owner" }
		if (i === 1) return { ...ben, email: "ben@northwind.example", role: "admin" }
		if (i === 2) return { ...mira, email: "mira@northwind.example", role: "member" }
		const given = first[i % first.length] ?? "Sam"
		const family = last[Math.floor(i / first.length) % last.length] ?? "Lee"
		const id = `p${i}`
		return {
			id,
			name: `${given} ${family}`,
			kind: "person" as const,
			email: `${given.toLowerCase().replace("á", "a")}.${family.toLowerCase()}${i > 255 ? i : ""}@northwind.example`,
			role: i % 23 === 0 ? "admin" : i % 11 === 0 ? "guest" : "member",
			...(i % 37 === 0 ? { gone: "Deactivated" } : {}),
		}
	})

const actions: ReadonlyArray<Omit<AuditEntry, "id" | "at">> = [
	{ actor: ben, action: "Changed the role of", target: { label: "Mira Patel" }, kind: "members", place: "Workspace", result: { status: "succeeded" }, detail: "Member → Admin" },
	{ actor: scout, onBehalfOf: ana, via: "a routine", action: "Posted a reply in", target: { label: "#support" }, kind: "messages", place: "#support", result: { status: "succeeded" } },
	{ actor: scout, onBehalfOf: ben, action: "Called", target: { label: "api.payments.example" }, kind: "network", place: "api.payments.example", result: { status: "refused", reason: "Outside the allowed hosts" } },
	{ actor: ana, action: "Rotated the secret", target: { label: "CRM_API_KEY" }, kind: "secrets", place: "Production", result: { status: "succeeded" } },
	{ actor: digest, action: "Sent the weekly digest to", target: { label: "#sales" }, kind: "routines", place: "#sales", result: { status: "succeeded" }, detail: "3 tasks" },
	{ actor: billing, action: "Synced invoices from", target: { label: "Ledger" }, kind: "connections", place: "Billing", result: { status: "failed", reason: "The connection expired" } },
	{ actor: ana, action: "Signed in from", target: { label: "a new device" }, kind: "sessions", place: "Lisbon, Portugal", result: { status: "info", label: "Recorded" }, rowAction: "End session" },
	{ actor: scout, onBehalfOf: mira, via: "MCP", action: "Asked to delete", target: { label: "Q3 forecast" }, kind: "records", place: "Finance", result: { status: "waiting", label: "Held for approval" } },
	{ actor: mira, action: "Turned off", target: { label: "Exports" }, kind: "settings", place: "Workspace", result: { status: "succeeded" } },
	{ actor: ben, action: "Invited", target: { label: "owen.brennan@northwind.example" }, kind: "members", place: "Workspace", result: { status: "succeeded" }, detail: "as Member" },
]

const pad = (n: number) => String(n).padStart(2, "0")

/** `count` entries, newest first, about 40 minutes apart, from midday on Dec 1, 2026 backwards. */
export const auditEntries = (count: number): ReadonlyArray<AuditEntry> =>
	Array.from({ length: count }, (_, i) => {
		const minutes = i * 41
		const at = new Date(Date.UTC(2026, 11, 1, 12, 0) - minutes * 60_000)
		const template = actions[(i * 7) % actions.length]!
		return {
			...template,
			id: `e${i}`,
			at: `${at.getUTCFullYear()}-${pad(at.getUTCMonth() + 1)}-${pad(at.getUTCDate())}T${pad(at.getUTCHours())}:${pad(at.getUTCMinutes())}:00Z`,
		}
	})
```

## API reference

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `agentId` | `string` | — | This log's id for agents, unique on the page: "audit-admin" |
| `label` (required) | `string` | — | "Activity", "Audit log", "Network events" |
| `entries` (required) | `AuditEntry[]` | — | What happened, newest first: who did what to which target, and when |
| `filters` | `AuditFilter[]` | — | The filters above the log, each a field and its choices |
| `selected` | `Record<string, string>` | — | The chosen value per filter id; "" or absent for all |
| `query` | `string` | — | The search text, controlled: matches who, what, the target, where and the reason |
| `search` | `{ placeholder }` | — | Show the search field; its placeholder is `Search the log` unless you give one |
| `exportFormats` | `("csv" \| "json")[]` | — | Formats the log can be exported in; shows Export, which asks for the current filters |
| `exporting` | `boolean` | — | An export is being made: the button shows busy |
| `total` | `number` | — | How many entries match in all, when the app knows: "Showing 50 of 1,284" |
| `hasMore` | `boolean` | — | More entries exist past these |
| `loadingMore` | `boolean` | — | More entries are loading: the load-more button shows busy |
| `rowActionConsequential` | `boolean` | `true` | Is the row action consequential (ending a session is); true by default |
| `emptyTitle` | `string` | `Nothing recorded yet` | What an empty log says; `Nothing recorded yet` by default |
| `emptyHint` | `string` | — | A hint under the empty title |
| `timeZone` | `string` | — | The time zone (IANA) times are shown in |
| `locale` | `string` | — | The locale times are formatted in; the browser's by default |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedFilters` | `{ selected: Record<string, string> }` | The whole selection after the change |
| `RequestedMore` | `{}` | Load the entries past these |
| `RequestedRowAction` | `{ id: string }` | An entry's action was pressed, such as ending a session |
| `RequestedQuery` | `{ query: string }` | The search text changed; filter on the server, or with `filterAudit` |
| `RequestedExport` | `{ format: "csv" \| "json"; selected: Record<string, string>; query: string }` | Export every entry the current filters and search match, not only the loaded ones |

**Agent actions**

| Action | Description |
| --- | --- |
| `ChoseFilter` | Filter by one field: { filter, value } (from page_query "filter=value"); value "" shows all. |
| `UpdatedQuery` | Search the log by who, what, target, place or reason |
| `ClickedExport` | Export every entry the current filters and search match, as csv or json |
| `ClickedMore` | Load older entries |
| `ClickedRowAction` | Take an entry's row action (its rowAction, such as ending a session). Target: { id, rowAction, result }. |

## Keyboard and accessibility

- The log is a captioned table; each time is a `<time>` in the reader's time zone.
- Filters are native selects inside a `search` landmark, and loading older entries is announced.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/audit-log
```

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

It writes these files:

- `src/ui/uicontext/components/Audit.ts`
- `src/ui/uicontext/components/Button.ts`
- `src/ui/uicontext/components/FormControls.ts`
- `src/ui/uicontext/components/Glyph.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/Actor.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/domain/Glyph.ts`
- `src/ui/uicontext/components/domain/Run.ts`
- `src/ui/uicontext/components/domain/Time.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/variants.ts`

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

## Source

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

## 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.
