- Replace hand-rolled buttons with @payloadcms/ui Button (secondary dry run, primary apply) since the admin bundle has no Tailwind for shadcn - Real spacing values throughout: Payload defines no --spacing-* vars, so gaps used to collapse to zero; use px literals and var(--base) - Framed panels with theme-elevation borders, uppercase table headers, stats strip with collapsed line boxes so small caps sit evenly inside the card padding
209 lines
7.1 KiB
TypeScript
209 lines
7.1 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
import { Button } from "@payloadcms/ui";
|
|
import { runAwardEvaluation, applyAwardGrants } from "@/lib/awards/automationActions";
|
|
import type { EvaluationResult } from "@/lib/awards/automation";
|
|
|
|
const panelStyle: React.CSSProperties = {
|
|
border: "1px solid var(--theme-elevation-150)",
|
|
borderRadius: "3px",
|
|
background: "var(--theme-elevation-50)",
|
|
};
|
|
|
|
/**
|
|
* Admin view (award-rules collection): preview which members currently
|
|
* qualify under the active rules, then apply the grants. Both actions are
|
|
* idempotent; the sweeper skips members who already hold the award.
|
|
*/
|
|
export default function AwardAutomationView() {
|
|
const [result, setResult] = React.useState<EvaluationResult | null>(null);
|
|
const [busy, setBusy] = React.useState<"preview" | "apply" | null>(null);
|
|
const [error, setError] = React.useState<string | null>(null);
|
|
|
|
const runPreview = async () => {
|
|
setBusy("preview");
|
|
setError(null);
|
|
const res = await runAwardEvaluation();
|
|
setBusy(null);
|
|
if (!res.success) {
|
|
setError(res.error);
|
|
return;
|
|
}
|
|
setResult(res.result);
|
|
};
|
|
|
|
const runApply = async () => {
|
|
setBusy("apply");
|
|
setError(null);
|
|
const res = await applyAwardGrants();
|
|
setBusy(null);
|
|
if (!res.success) {
|
|
setError(res.error);
|
|
return;
|
|
}
|
|
setResult(res.result);
|
|
};
|
|
|
|
const grantCount = result ? result.plans.filter((p) => p.kind === "grant").length : 0;
|
|
const upgradeCount = result ? result.plans.filter((p) => p.kind === "upgrade").length : 0;
|
|
|
|
return (
|
|
<div
|
|
style={{
|
|
padding: "var(--base) var(--base) calc(var(--base) * 2)",
|
|
maxWidth: "72rem",
|
|
display: "flex",
|
|
flexDirection: "column",
|
|
gap: "24px",
|
|
}}
|
|
>
|
|
<header style={{ display: "flex", flexDirection: "column", gap: "8px" }}>
|
|
<h1 style={{ margin: 0 }}>Award Automation</h1>
|
|
<p style={{ margin: 0, color: "var(--theme-elevation-600)", maxWidth: "44rem" }}>
|
|
Evaluate every active award rule against all members. Preview first: nothing is written
|
|
until you apply. Applying is safe to repeat, members who already hold an award are
|
|
skipped.
|
|
</p>
|
|
</header>
|
|
|
|
<div style={{ display: "flex", gap: "12px" }}>
|
|
<Button buttonStyle="secondary" onClick={runPreview} disabled={busy !== null}>
|
|
{busy === "preview" ? "Evaluating..." : "Evaluate now (dry run)"}
|
|
</Button>
|
|
<Button
|
|
buttonStyle="primary"
|
|
onClick={runApply}
|
|
disabled={busy !== null || !result || result.plans.length === 0}
|
|
>
|
|
{busy === "apply" ? "Applying..." : "Apply grants"}
|
|
</Button>
|
|
</div>
|
|
|
|
{error && (
|
|
<p
|
|
style={{
|
|
margin: 0,
|
|
padding: "8px 12px",
|
|
color: "var(--theme-error-500)",
|
|
...panelStyle,
|
|
borderColor: "var(--theme-error-200)",
|
|
}}
|
|
>
|
|
{error}
|
|
</p>
|
|
)}
|
|
|
|
{result && (
|
|
<section style={{ display: "flex", flexDirection: "column", gap: "24px" }}>
|
|
<div
|
|
style={{
|
|
display: "flex",
|
|
flexWrap: "wrap",
|
|
gap: "var(--base)",
|
|
padding: "16px 20px",
|
|
...panelStyle,
|
|
}}
|
|
>
|
|
{[
|
|
{ label: "Mode", value: result.dryRun ? "Dry run" : "Applied" },
|
|
{ label: "Rules evaluated", value: String(result.rulesEvaluated) },
|
|
{ label: "Members checked", value: String(result.membersChecked) },
|
|
{
|
|
label: result.dryRun ? "Would grant" : "Granted",
|
|
value: String(result.dryRun ? grantCount : result.granted),
|
|
},
|
|
{
|
|
label: result.dryRun ? "Would upgrade" : "Upgraded",
|
|
value: String(result.dryRun ? upgradeCount : result.upgraded),
|
|
},
|
|
{ label: "Skipped", value: String(result.skips.length) },
|
|
].map((stat) => (
|
|
<div
|
|
key={stat.label}
|
|
style={{ display: "flex", flexDirection: "column", gap: "2px", minWidth: "7rem" }}
|
|
>
|
|
<span
|
|
style={{
|
|
fontSize: "0.625rem",
|
|
lineHeight: 1,
|
|
textTransform: "uppercase",
|
|
letterSpacing: "0.04em",
|
|
color: "var(--theme-elevation-500)",
|
|
}}
|
|
>
|
|
{stat.label}
|
|
</span>
|
|
<span style={{ fontSize: "1.25rem", lineHeight: 1.1 }}>{stat.value}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
{result.plans.length > 0 ? (
|
|
<div style={{ overflowX: "auto", ...panelStyle }}>
|
|
<table style={{ width: "100%", borderCollapse: "collapse" }}>
|
|
<thead>
|
|
<tr style={{ borderBottom: "1px solid var(--theme-elevation-150)" }}>
|
|
{["Member", "Rule", "Award", "Metric value", "Action"].map((col) => (
|
|
<th
|
|
key={col}
|
|
style={{
|
|
textAlign: "left",
|
|
padding: "12px 16px",
|
|
fontSize: "0.625rem",
|
|
lineHeight: 1,
|
|
textTransform: "uppercase",
|
|
letterSpacing: "0.04em",
|
|
color: "var(--theme-elevation-500)",
|
|
fontWeight: 600,
|
|
}}
|
|
>
|
|
{col}
|
|
</th>
|
|
))}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{result.plans.map((plan, i) => (
|
|
<tr
|
|
key={`${plan.ruleId}-${plan.userId}-${i}`}
|
|
style={{ borderBottom: "1px solid var(--theme-elevation-100)" }}
|
|
>
|
|
{[
|
|
plan.username,
|
|
plan.ruleLabel,
|
|
plan.awardName,
|
|
plan.metricValue.toFixed(2),
|
|
plan.kind === "upgrade"
|
|
? `Upgrade (retires award #${plan.replacesAwardId})`
|
|
: "Grant",
|
|
].map((cell, j) => (
|
|
<td
|
|
key={j}
|
|
style={{ padding: "12px 16px", verticalAlign: "top" }}
|
|
>
|
|
{cell}
|
|
</td>
|
|
))}
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
) : (
|
|
<p
|
|
style={{
|
|
margin: 0,
|
|
padding: "20px",
|
|
color: "var(--theme-elevation-600)",
|
|
...panelStyle,
|
|
}}
|
|
>
|
|
No members currently qualify for anything new.
|
|
</p>
|
|
)}
|
|
</section>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|