Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
115 lines
4.1 KiB
TypeScript
115 lines
4.1 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
import { ArrowRight, Check, Loader2, X } from "lucide-react";
|
|
import { ApiError, previewDiscount } from "@/lib/api";
|
|
import { friendlyErrorMessage } from "@/src/shared/errors/friendly";
|
|
|
|
interface DiscountCodeFieldProps {
|
|
/** The plan the preview is validated against (see caller for how it picks one). */
|
|
readonly planKey: string;
|
|
/** Called with the validated code on success, or `null` when cleared/invalid. */
|
|
readonly onApplied: (code: string | null) => void;
|
|
}
|
|
|
|
type FieldState =
|
|
| { status: "idle" }
|
|
| { status: "checking" }
|
|
| { status: "applied"; savingsLabel: string }
|
|
| { status: "error"; message: string };
|
|
|
|
/**
|
|
* Inline "have a promo code?" field for the pricing checkout. Validates
|
|
* server-side via `POST /billing/discount/preview` before the buyer ever
|
|
* reaches checkout, so a typo or an already-used code is caught immediately
|
|
* rather than failing deep in the provider's hosted page.
|
|
*/
|
|
export function DiscountCodeField({ planKey, onApplied }: DiscountCodeFieldProps) {
|
|
const [code, setCode] = React.useState("");
|
|
const [state, setState] = React.useState<FieldState>({ status: "idle" });
|
|
|
|
const handleApply = React.useCallback(() => {
|
|
const trimmed = code.trim();
|
|
if (!trimmed) return;
|
|
setState({ status: "checking" });
|
|
void previewDiscount({ code: trimmed, plan_key: planKey })
|
|
.then((preview) => {
|
|
const savingsLabel =
|
|
preview.kind === "percent" && preview.percent_off
|
|
? `${preview.percent_off}% off applied`
|
|
: `$${preview.savings_usd.toFixed(2)} off applied`;
|
|
setState({ status: "applied", savingsLabel });
|
|
onApplied(trimmed);
|
|
})
|
|
.catch((err: unknown) => {
|
|
const message =
|
|
err instanceof ApiError ? friendlyErrorMessage(err) : "That code didn't work.";
|
|
setState({ status: "error", message });
|
|
onApplied(null);
|
|
});
|
|
}, [code, planKey, onApplied]);
|
|
|
|
const handleClear = React.useCallback(() => {
|
|
setCode("");
|
|
setState({ status: "idle" });
|
|
onApplied(null);
|
|
}, [onApplied]);
|
|
|
|
if (state.status === "applied") {
|
|
return (
|
|
<p className="inline-flex items-center gap-1.5 text-sm text-primary">
|
|
<Check className="size-3.5" aria-hidden />
|
|
<code className="font-mono">{code.trim()}</code> — {state.savingsLabel}
|
|
<button
|
|
type="button"
|
|
onClick={handleClear}
|
|
aria-label="Remove discount code"
|
|
className="ml-1 text-muted-foreground hover:text-foreground"
|
|
>
|
|
<X className="size-3.5" aria-hidden />
|
|
</button>
|
|
</p>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="flex flex-col items-center gap-1">
|
|
<div className="flex items-center gap-1.5">
|
|
<input
|
|
type="text"
|
|
value={code}
|
|
onChange={(event) => {
|
|
setCode(event.target.value);
|
|
if (state.status === "error") setState({ status: "idle" });
|
|
}}
|
|
onKeyDown={(event) => {
|
|
if (event.key === "Enter") {
|
|
event.preventDefault();
|
|
handleApply();
|
|
}
|
|
}}
|
|
placeholder="Promo code"
|
|
aria-label="Promo code"
|
|
className="h-9 w-36 rounded-lg border border-border bg-background px-3 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={handleApply}
|
|
disabled={!code.trim() || state.status === "checking"}
|
|
aria-label="Apply promo code"
|
|
className="inline-flex size-9 items-center justify-center rounded-lg border border-border bg-background text-muted-foreground transition-colors hover:text-foreground disabled:opacity-40"
|
|
>
|
|
{state.status === "checking" ? (
|
|
<Loader2 className="size-4 animate-spin" aria-hidden />
|
|
) : (
|
|
<ArrowRight className="size-4" aria-hidden />
|
|
)}
|
|
</button>
|
|
</div>
|
|
{state.status === "error" && (
|
|
<p className="text-xs text-destructive">{state.message}</p>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|