|
| 1 | +import React, { useState, useCallback } from 'react'; |
| 2 | +import type { LicenseResponse } from '../../../services/worker/http/routes/LicenseRoutes.js'; |
| 3 | + |
| 4 | +interface LicenseGateProps { |
| 5 | + license: LicenseResponse | null; |
| 6 | + onActivated: () => void; |
| 7 | +} |
| 8 | + |
| 9 | +export function LicenseGate({ license, onActivated }: LicenseGateProps) { |
| 10 | + const [key, setKey] = useState(''); |
| 11 | + const [error, setError] = useState<string | null>(null); |
| 12 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 13 | + |
| 14 | + const handleSubmit = useCallback(async () => { |
| 15 | + const trimmed = key.trim(); |
| 16 | + if (!trimmed) return; |
| 17 | + |
| 18 | + setError(null); |
| 19 | + setIsSubmitting(true); |
| 20 | + |
| 21 | + try { |
| 22 | + const res = await fetch('/api/license/activate', { |
| 23 | + method: 'POST', |
| 24 | + headers: { 'Content-Type': 'application/json' }, |
| 25 | + body: JSON.stringify({ key: trimmed }), |
| 26 | + }); |
| 27 | + const data = await res.json(); |
| 28 | + |
| 29 | + if (data.success) { |
| 30 | + setKey(''); |
| 31 | + setError(null); |
| 32 | + onActivated(); |
| 33 | + } else { |
| 34 | + setError(data.error ?? 'Activation failed'); |
| 35 | + } |
| 36 | + } catch { |
| 37 | + setError('Connection failed. Is the Pilot worker running?'); |
| 38 | + } finally { |
| 39 | + setIsSubmitting(false); |
| 40 | + } |
| 41 | + }, [key, onActivated]); |
| 42 | + |
| 43 | + const handleKeyDown = useCallback((e: React.KeyboardEvent) => { |
| 44 | + if (e.key === 'Enter' && !isSubmitting) { |
| 45 | + handleSubmit(); |
| 46 | + } |
| 47 | + }, [handleSubmit, isSubmitting]); |
| 48 | + |
| 49 | + const isExpired = license?.isExpired === true; |
| 50 | + const title = isExpired ? 'License Expired' : 'License Required'; |
| 51 | + const subtitle = isExpired |
| 52 | + ? 'Your Claude Pilot license has expired. Please activate a new license to continue using the Console.' |
| 53 | + : 'Claude Pilot Console requires an active license or trial. Activate your license key below to get started.'; |
| 54 | + |
| 55 | + return ( |
| 56 | + <div className="min-h-screen flex items-center justify-center bg-base-200 p-4"> |
| 57 | + <div className="card bg-base-100 shadow-xl w-full max-w-md"> |
| 58 | + <div className="card-body items-center text-center gap-4"> |
| 59 | + <div className="text-5xl mb-2"> |
| 60 | + {isExpired ? '\u{1F6AB}' : '\u{1F512}'} |
| 61 | + </div> |
| 62 | + |
| 63 | + <h1 className="card-title text-2xl">{title}</h1> |
| 64 | + <p className="text-base-content/60 text-sm">{subtitle}</p> |
| 65 | + |
| 66 | + <div className="w-full space-y-3 mt-2"> |
| 67 | + <input |
| 68 | + type="text" |
| 69 | + className="input input-bordered w-full" |
| 70 | + placeholder="Enter your license key" |
| 71 | + value={key} |
| 72 | + onChange={(e) => { setKey(e.target.value); setError(null); }} |
| 73 | + onKeyDown={handleKeyDown} |
| 74 | + disabled={isSubmitting} |
| 75 | + autoFocus |
| 76 | + /> |
| 77 | + |
| 78 | + {error && ( |
| 79 | + <p className="text-error text-sm text-left">{error}</p> |
| 80 | + )} |
| 81 | + |
| 82 | + <button |
| 83 | + className="btn btn-primary w-full" |
| 84 | + onClick={handleSubmit} |
| 85 | + disabled={isSubmitting || !key.trim()} |
| 86 | + > |
| 87 | + {isSubmitting ? 'Activating...' : 'Activate License'} |
| 88 | + </button> |
| 89 | + </div> |
| 90 | + |
| 91 | + <div className="divider text-base-content/40 text-xs my-1">or</div> |
| 92 | + |
| 93 | + <a |
| 94 | + href="https://claude-pilot.com/#pricing" |
| 95 | + target="_blank" |
| 96 | + rel="noopener noreferrer" |
| 97 | + className="btn btn-outline btn-sm w-full" |
| 98 | + > |
| 99 | + Get a License |
| 100 | + </a> |
| 101 | + |
| 102 | + <p className="text-base-content/40 text-xs mt-2"> |
| 103 | + Visit{' '} |
| 104 | + <a |
| 105 | + href="https://claude-pilot.com" |
| 106 | + target="_blank" |
| 107 | + rel="noopener noreferrer" |
| 108 | + className="text-primary hover:underline" |
| 109 | + > |
| 110 | + claude-pilot.com |
| 111 | + </a> |
| 112 | + {' '}to learn more about Claude Pilot. |
| 113 | + </p> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + ); |
| 118 | +} |
0 commit comments