Spaces:
Running

WorkPilot-AI / web /src /components /inbox /diff-view.tsx
Aynkhn's picture
Deploy WorkPilot demo (part 3)
03608f2 verified
Raw History Blame Contribute Delete
7.46 kB
"use client";
import { ArrowRightIcon } from "lucide-react";
import { ProvenanceBadge } from "@/components/inbox/provenance-badge";
import { previewDiff } from "@/lib/diff";
import { formatMoney, formatValue, humanize, isMoneyField } from "@/lib/format";
import { useI18n } from "@/lib/i18n";
import type { Provenance } from "@/lib/types";
import { cn } from "@/lib/utils";
function Value({ field, value, muted }: { field: string; value: unknown; muted?: boolean }) {
const { locale } = useI18n();
return (
<span
dir="ltr"
className={cn(
"tabular break-all",
typeof value === "string" && !isMoneyField(field) ? "font-mono text-[13px]" : "",
muted && "text-muted-foreground",
)}
>
{formatValue(field, value, locale)}
</span>
);
}
/** Structured before → after of the tool's dry run: what will actually change. */
export function DiffView({ preview }: { preview: Record<string, unknown> }) {
const { t } = useI18n();
const { rows, details } = previewDiff(preview);
if (!rows.length && !details.length) {
return <p className="text-sm text-muted-foreground">{t("inbox.noPreview")}</p>;
}
return (
<div className="space-y-3">
{rows.length > 0 && (
<div className="overflow-hidden rounded-md border" data-testid="diff">
<table className="w-full text-sm">
<thead className="bg-muted/60 text-xs text-muted-foreground">
<tr>
<th className="px-3 py-1.5 text-start font-medium">{t("inbox.field")}</th>
<th className="px-3 py-1.5 text-start font-medium">{t("inbox.before")}</th>
<th className="w-6" aria-hidden />
<th className="px-3 py-1.5 text-start font-medium">{t("inbox.after")}</th>
</tr>
</thead>
<tbody>
{rows.map((r) => (
<tr
key={r.field}
data-testid={`diff-${r.field}`}
data-changed={r.changed}
className="border-t"
>
<td className="px-3 py-2 text-muted-foreground">{humanize(r.field)}</td>
<td className="px-3 py-2">
{r.hasBefore ? (
<span className={cn(r.changed && "line-through decoration-destructive/60")}>
<Value field={r.field} value={r.before} muted />
</span>
) : (
<span className="text-muted-foreground">—</span>
)}
</td>
<td className="text-muted-foreground" aria-hidden>
<ArrowRightIcon className="size-3.5 rtl:rotate-180" />
</td>
<td className="px-3 py-2">
<span
className={cn(
"rounded px-1 py-0.5 font-medium",
r.changed && "bg-accent text-accent-foreground",
)}
>
<Value field={r.field} value={r.after} />
</span>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
{details.length > 0 && (
<dl className="grid grid-cols-[minmax(7rem,auto)_1fr] gap-x-4 gap-y-1 text-sm">
{details.map(([k, v]) => (
<div key={k} className="contents">
<dt className="text-muted-foreground">{humanize(k)}</dt>
<dd className="min-w-0 whitespace-pre-wrap">
<Value field={k} value={v} />
</dd>
</div>
))}
</dl>
)}
</div>
);
}
/** The exact arguments the tool will run with, each with where it came from. */
export function ArgsTable({
args,
provenance,
}: {
args: Record<string, unknown>;
provenance: Record<string, Provenance>;
}) {
const { t } = useI18n();
return (
<div className="overflow-hidden rounded-md border" data-testid="args">
<table className="w-full text-sm">
<thead className="bg-muted/60 text-xs text-muted-foreground">
<tr>
<th className="px-3 py-1.5 text-start font-medium">{t("inbox.field")}</th>
<th className="px-3 py-1.5 text-start font-medium">{t("inbox.value")}</th>
<th className="px-3 py-1.5 text-end font-medium">{t("inbox.source")}</th>
</tr>
</thead>
<tbody>
{Object.entries(args).map(([k, v]) => (
<tr
key={k}
className={cn("border-t", provenance[k] === "untrusted" && "bg-warning-soft/40")}
>
<td className="px-3 py-2 align-top font-mono text-[12px] text-muted-foreground" dir="ltr">
{k}
</td>
<td className="px-3 py-2 align-top" data-testid={`arg-${k}`}>
<Value field={k} value={v} />
</td>
<td className="px-3 py-2 text-end align-top">
<ProvenanceBadge value={provenance[k]} field={k} />
</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
/** Live facts from the system of record (what the policy compared against). */
export function FactsList({
facts,
args,
}: {
facts: Record<string, unknown>;
args: Record<string, unknown>;
}) {
const { t, locale } = useI18n();
const entries = Object.entries(facts);
if (!entries.length) return null;
const amount = typeof args.amount === "number" ? args.amount : null;
const total = typeof facts.order_total === "number" ? facts.order_total : null;
return (
<div className="rounded-md border border-dashed p-3" data-testid="facts">
<div className="mb-2 flex items-center justify-between gap-2">
<h4 className="text-xs font-medium text-muted-foreground">{t("inbox.facts")}</h4>
<ProvenanceBadge value="trusted" />
</div>
{amount !== null && total !== null && total > 0 && (
<div className="mb-3 space-y-1">
<p className="text-sm" dir="ltr">
<span className="tabular font-medium">
{t("inbox.totalOnOrder", {
amount: formatMoney(amount, locale),
total: formatMoney(total, locale),
})}
</span>
</p>
<div
className="h-1.5 overflow-hidden rounded-full bg-muted"
role="meter"
aria-valuemin={0}
aria-valuemax={total}
aria-valuenow={amount}
>
<div
className={cn("h-full rounded-full", amount > total ? "bg-destructive" : "bg-primary")}
style={{ width: `${Math.min(100, (amount / total) * 100)}%` }}
/>
</div>
</div>
)}
<dl className="grid grid-cols-[minmax(7rem,auto)_1fr] gap-x-4 gap-y-1 text-sm">
{entries.map(([k, v]) => (
<div key={k} className="contents">
<dt className="text-muted-foreground">{humanize(k)}</dt>
<dd data-testid={`fact-${k}`}>
<Value field={k} value={v} />
</dd>
</div>
))}
</dl>
</div>
);
}