# Change diff

The change as files: one tab per file, each named by the thing it is (a field, a layout) as well as its path, with a unified diff that highlights the exact words that changed inside a line.

Its strings are overridden under `changeDiff` in LocaleProvider's `strings`.

- Registry name: `change-diff` (`@candor/change-diff`)
- Tier: Pro. Installing it needs a Pro, Premium or Team licence key: the person runs `npx candor-ui key <key>` themselves; never ask for the key in chat or write it to a file.
- Docs: https://candor.themethodic.com/docs/change-diff

## Install

```bash
npx candor-ui add change-diff
```

Run `npx candor-ui@latest init` once first (https://candor.themethodic.com/get-started.md). Without the CLI: `npx shadcn@latest add @candor/change-diff`.
It brings in: entity-chip, locale, tokens, work, utils.
npm packages: @base-ui/react@^1.9.0, lucide-react@^1.67.0.

## Example

```tsx
"use client"

import type { FileDiff } from "@/lib/candor/work"
import { ChangeDiff } from "@/components/candor/change-diff"

const files: FileDiff[] = [
  {
    path: "db/migrations/0142_add_customer_region.sql",
    entity: { kind: "migration", name: "0142_add_customer_region", label: "Add customers.region" },
    change: "create",
    language: "sql",
    hunks: [
      {
        lines: [
          { kind: "add", text: "CREATE TYPE region AS ENUM ('us', 'eu', 'apac', 'other');", newNo: 1 },
          { kind: "add", text: "ALTER TABLE customers ADD COLUMN region region;", newNo: 2 },
          { kind: "add", text: "CREATE INDEX customers_region_idx ON customers (region);", newNo: 3 },
        ],
      },
    ],
  },
  {
    path: "web/src/customers/CustomerHeader.tsx",
    entity: { kind: "component", name: "web/src/customers/CustomerHeader.tsx", label: "CustomerHeader" },
    change: "update",
    language: "tsx",
    hunks: [
      {
        header: "@@ function CustomerHeader @@",
        lines: [
          { kind: "context", text: '      <Fact label="Plan" value={customer.plan} />', oldNo: 43, newNo: 43 },
          { kind: "remove", text: '      <Fact label="Partner tier" value={customer.partnerTierOld} />', oldNo: 44 },
          { kind: "add", text: '      <Fact label="Region" value={regionLabel(customer.region)} />', newNo: 44 },
          { kind: "context", text: "    </dl>", oldNo: 45, newNo: 45 },
        ],
      },
    ],
  },
]

export default function Example() {
  return <ChangeDiff className="max-w-3xl" files={files} />
}
```

## <ChangeDiff>

Props:

- `files: FileDiff[]`: The changed files, one tab each.
- `className?: string | undefined`

## Strings (`changeDiff`)

The fixed words, English by default. Override any of them under `changeDiff` in `<LocaleProvider strings={{ changeDiff: { … } }}>`; the table is exported as `changeDiffStrings`.

- `label`: Changes
- `files`: { one: "{count} file", other: "{count} files" }
- `fileTabs`: Files
- `create`: New
- `update`: Changed
- `delete`: Deleted
- `added`: Added: 
- `removed`: Removed: 

## Types from other items

The props above take these. Each is defined by another item; import it from the path shown.

### FileDiff

Import: `import type { FileDiff } from "@/lib/candor/work"` (item `work`, https://candor.themethodic.com/docs/work.md)

```ts
{
  path: string;
  /** What the file is, e.g. a field or a layout. */
  entity?: EntityRef | undefined;
  change: "create" | "update" | "delete";
  /** e.g. "xml", "ts", "python"; shown as a label. */
  language?: string | undefined;
  hunks: DiffHunk[];
}
```

### EntityRef

Import: `import type { EntityRef } from "@/lib/candor/chat"` (item `chat`, https://candor.themethodic.com/docs/chat.md)

```ts
{
  kind: EntityKind;
  /** The identifier, e.g. "orders.status" or "billing.invoice.due_date". */
  name: string;
  /** A friendlier label when there is one, e.g. "Region". */
  label?: string | undefined;
  /** Where it lives, when more than one place is in scope, e.g. "Shop staging". */
  location?: string | undefined;
}
```

### DiffHunk

Import: `import type { DiffHunk } from "@/lib/candor/work"` (item `work`, https://candor.themethodic.com/docs/work.md)

```ts
{ header?: string | undefined; lines: DiffLine[] }
```

## Motion

- **tint** (tint): A file tab's text colour changes when the pointer is over it, and its background and shadow change when the person selects it; a ring fades in on keyboard focus.

Set with `--cd-motion-change-diff-<moment>-*` in your brand file; the families are in the design reference.

## Rules for using Candor UI

- Components take plain typed data. Never pass model output as HTML or render it as a link; messages are typed blocks.
- A control that can't be used says why: wrap it in `<Locked reason="…">`, don't just disable it.
- Don't show a value you don't have: leave it out (it renders as —) rather than guess. An estimate is labelled as one.
- Nothing is pre-selected in a decision a person makes, and the agent is named from your app's data, never from model output.
- Your app's words go in `LocaleProvider`: its fixed strings in `strings`, its nouns in `nouns`; entity kinds and agent roles in `EntityKindsProvider` and `AgentRolesProvider`.
