File size: 2,959 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useEffect, useState } from "react";
import { useTranslations } from "next-intl";
import { Loader2 } from "lucide-react";

import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
  captureCreatorCodeFromUrl,
  clearCreatorCode,
  lookupCreatorCode,
  storeCreatorCode,
} from "@/lib/creator-code";
import { cn } from "@/lib/utils";

/** "Have a creator code?" for new subscribers; the code is applied by startUpgrade. */
export function CreatorCodeField({ className }: { className?: string }) {
  const t = useTranslations("billing");
  const [applied, setApplied] = useState<string | null>(null);
  const [isOpen, setIsOpen] = useState(false);
  const [value, setValue] = useState("");
  const [isChecking, setIsChecking] = useState(false);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    void captureCreatorCodeFromUrl().then(setApplied);
  }, []);

  const apply = async (event: React.FormEvent) => {
    event.preventDefault();
    setIsChecking(true);
    setError(null);
    const code = await lookupCreatorCode(value);
    setIsChecking(false);
    if (!code) {
      setError(t("creatorCode.invalid"));
      return;
    }
    storeCreatorCode(code);
    setApplied(code);
    setIsOpen(false);
    setValue("");
  };

  if (applied) {
    return (
      <p className={cn("text-xs text-muted-foreground", className)}>
        {t.rich("creatorCode.applied", {
          code: applied,
          mono: (chunks) => <span className="font-mono font-medium text-foreground">{chunks}</span>,
        })}{" "}
        <button
          type="button"
          className="underline underline-offset-2 hover:text-foreground"
          onClick={() => {
            clearCreatorCode();
            setApplied(null);
          }}
        >
          {t("creatorCode.remove")}
        </button>
      </p>
    );
  }

  if (!isOpen) {
    return (
      <button
        type="button"
        onClick={() => setIsOpen(true)}
        className={cn("text-xs text-muted-foreground underline-offset-2 hover:text-foreground hover:underline", className)}
      >
        {t("creatorCode.prompt")}
      </button>
    );
  }

  return (
    <form onSubmit={apply} className={cn("space-y-1.5", className)}>
      <div className="flex gap-2">
        <Input
          value={value}
          onChange={(event) => setValue(event.target.value)}
          placeholder={t("creatorCode.placeholder")}
          aria-label={t("creatorCode.placeholder")}
          maxLength={20}
          autoFocus
          className="h-8 font-mono text-sm uppercase"
        />
        <Button type="submit" size="sm" variant="outline" disabled={!value.trim() || isChecking}>
          {isChecking ? <Loader2 className="size-3.5 animate-spin" /> : t("creatorCode.apply")}
        </Button>
      </div>
      {error && <p className="text-xs text-red-600">{error}</p>}
    </form>
  );
}