File size: 8,510 Bytes
bf27873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { FormEvent, useEffect, useMemo, useRef, useState } from "react";
import { useNavigate } from "react-router-dom";
import { useApiKey } from "../context/ApiKeyContext";
import "./gate.css";

type Phase = "idle" | "handshake" | "cleared";

const REDUCED_MOTION_QUERY = "(prefers-reduced-motion: reduce)";

function delay(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

function maskKey(key: string): string {
  const trimmed = key.trim();
  if (trimmed.length <= 8) return "••••";
  return `${trimmed.slice(0, 4)}••••${trimmed.slice(-4)}`;
}

function stampDate(): string {
  return new Date()
    .toLocaleDateString("en-GB", { day: "2-digit", month: "short", year: "numeric" })
    .toUpperCase();
}

export default function Gate() {
  const { apiKeys, activeKeyId, addApiKey, setActiveKeyId } = useApiKey();
  const navigate = useNavigate();

  const prefersReducedMotion = useMemo(
    () => window.matchMedia(REDUCED_MOTION_QUERY).matches,
    []
  );

  const hasStoredKeys = apiKeys.length > 0;
  const [enterMode, setEnterMode] = useState<"resume" | "fresh">(
    hasStoredKeys ? "resume" : "fresh"
  );

  const [workspaceName, setWorkspaceName] = useState("");
  const [apiKey, setApiKey] = useState("");
  const [showKey, setShowKey] = useState(false);
  const [resumeKeyId, setResumeKeyId] = useState(activeKeyId ?? apiKeys[0]?.id ?? "");
  const [error, setError] = useState<string | null>(null);
  const [phase, setPhase] = useState<Phase>("idle");

  const resumeKey = apiKeys.find((k) => k.id === resumeKeyId);

  const aliveRef = useRef(true);
  const timersRef = useRef<number[]>([]);
  useEffect(() => {
    aliveRef.current = true;
    return () => {
      aliveRef.current = false;
      timersRef.current.forEach((t) => clearTimeout(t));
    };
  }, []);

  function finish(opts: { name: string; key: string; resumeId?: string }) {
    if (!aliveRef.current) return;
    if (opts.resumeId) {
      setActiveKeyId(opts.resumeId);
    } else {
      addApiKey(opts.name, opts.key, true);
    }
    navigate("/", { replace: true });
  }

  async function runHandshake(opts: { name: string; key: string; resumeId?: string }) {
    setPhase("handshake");
    setError(null);

    if (prefersReducedMotion) {
      finish(opts);
      return;
    }

    await delay(620);
    if (!aliveRef.current) return;
    setPhase("cleared");
    const t = window.setTimeout(() => {
      finish(opts);
    }, 900);
    timersRef.current.push(t);
  }

  function handleSubmit(event: FormEvent<HTMLFormElement>) {
    event.preventDefault();
    if (phase !== "idle") return;

    if (enterMode === "resume" && resumeKey) {
      void runHandshake({
        name: resumeKey.name,
        key: resumeKey.key,
        resumeId: resumeKey.id,
      });
      return;
    }

    const name = workspaceName.trim();
    const key = apiKey.trim();
    if (!name) {
      setError("Name your workspace — anything you will recognize later.");
      return;
    }
    if (!key) {
      setError("Paste your Smartlead API key to open the module.");
      return;
    }
    void runHandshake({ name, key });
  }

  function enterDemo() {
    if (phase !== "idle") return;
    setEnterMode("fresh");
    setWorkspaceName("Demo Workspace");
    setApiKey("demo_bridge_e");
    setError(null);
    void runHandshake({ name: "Demo Workspace", key: "demo_bridge_e" });
  }

  const busy = phase !== "idle";
  const buttonLabel =
    phase === "idle"
      ? "OPEN MODULE"
      : phase === "handshake"
        ? "OPENING…"
        : "CLEARED";

  return (
    <div className="gate">
      <h1 className="gate-sr">BRIDGE Modul E — connect a Smartlead workspace</h1>

      <span className="gate-cropmark tl" aria-hidden="true" />
      <span className="gate-cropmark tr" aria-hidden="true" />
      <span className="gate-cropmark bl" aria-hidden="true" />
      <span className="gate-cropmark br" aria-hidden="true" />

      <div className="gate-formwrap">
        {phase === "cleared" && (
          <svg className="gate-stamp" viewBox="0 0 120 120" aria-hidden="true">
            <circle cx="60" cy="60" r="56" fill="none" stroke="currentColor" strokeWidth="2.5" />
            <circle cx="60" cy="60" r="47" fill="none" stroke="currentColor" strokeWidth="1.2" />
            <path id="gate-stamp-arc" d="M 24 60 A 36 36 0 0 1 96 60" fill="none" />
            <text fontSize="10.5" fontWeight="700" letterSpacing="2.5" fill="currentColor">
              <textPath href="#gate-stamp-arc" startOffset="50%" textAnchor="middle">
                CLEARED FOR DISPATCH
              </textPath>
            </text>
            <text x="60" y="63" textAnchor="middle" fontSize="15" fontWeight="800" letterSpacing="1.5" fill="currentColor">
              MODUL·E
            </text>
            <text x="60" y="78" textAnchor="middle" fontSize="8.5" fontWeight="600" letterSpacing="2" fill="currentColor">
              {stampDate()}
            </text>
          </svg>
        )}

        <div className="gate-card">
          <form onSubmit={handleSubmit} noValidate>
            {enterMode === "resume" && hasStoredKeys ? (
              <div className="gate-field">
                <label htmlFor="gate-resume">Stored workspace</label>
                <select
                  id="gate-resume"
                  value={resumeKeyId}
                  onChange={(e) => setResumeKeyId(e.target.value)}
                  disabled={busy}
                >
                  {apiKeys.map((k) => (
                    <option key={k.id} value={k.id}>
                      {k.name} — {maskKey(k.key)}
                    </option>
                  ))}
                </select>
              </div>
            ) : (
              <>
                <div className="gate-field">
                  <label htmlFor="gate-name">Workspace name</label>
                  <input
                    id="gate-name"
                    type="text"
                    value={workspaceName}
                    onChange={(e) => setWorkspaceName(e.target.value)}
                    placeholder="e.g. Acme growth pod"
                    autoComplete="off"
                    disabled={busy}
                  />
                </div>
                <div className="gate-field">
                  <label htmlFor="gate-key">Smartlead API key</label>
                  <div className="gate-key-row">
                    <input
                      id="gate-key"
                      type={showKey ? "text" : "password"}
                      value={apiKey}
                      onChange={(e) => setApiKey(e.target.value)}
                      placeholder="Paste your Smartlead key"
                      autoComplete="off"
                      spellCheck={false}
                      disabled={busy}
                      className="gate-key-input"
                    />
                    <button
                      type="button"
                      className="gate-peek"
                      onClick={() => setShowKey((v) => !v)}
                      disabled={busy || apiKey.length === 0}
                    >
                      {showKey ? "Hide" : "Show"}
                    </button>
                  </div>
                </div>
              </>
            )}

            {error && (
              <p className="gate-error" role="alert">
                {error}
              </p>
            )}

            <button
              type="submit"
              className="gate-cta"
              data-phase={phase}
              disabled={busy}
            >
              {buttonLabel}
            </button>

            {enterMode === "resume" ? (
              <button
                type="button"
                className="gate-switch"
                onClick={() => {
                  setEnterMode("fresh");
                  setError(null);
                }}
                disabled={busy}
              >
                Use a different key
              </button>
            ) : (
              <>
                <div className="gate-or">
                  <span aria-hidden="true" />
                  OR
                  <span aria-hidden="true" />
                </div>
                <button type="button" className="gate-demo" onClick={enterDemo} disabled={busy}>
                  Enter with demo data
                </button>
              </>
            )}
          </form>
        </div>
      </div>
    </div>
  );
}