Spaces:
Running
Running
File size: 7,460 Bytes
03608f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | "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>
);
}
|