|
| 1 | +import { useState, useCallback, useEffect } from "react"; |
| 2 | +import { X } from "lucide-react"; |
| 3 | +import { SERVICES, SERVICE_LABELS } from "@/lib/constants"; |
| 4 | +import type { CreateCredentialPayload } from "@/api/types"; |
| 5 | + |
| 6 | +interface AddCredentialModalProps { |
| 7 | + open: boolean; |
| 8 | + onClose: () => void; |
| 9 | + onSubmit: (payload: CreateCredentialPayload) => Promise<boolean>; |
| 10 | +} |
| 11 | + |
| 12 | +export function AddCredentialModal({ |
| 13 | + open, |
| 14 | + onClose, |
| 15 | + onSubmit, |
| 16 | +}: AddCredentialModalProps) { |
| 17 | + const [service, setService] = useState<string>(SERVICES[0]); |
| 18 | + const [name, setName] = useState(""); |
| 19 | + const [apiKey, setApiKey] = useState(""); |
| 20 | + const [submitting, setSubmitting] = useState(false); |
| 21 | + |
| 22 | + // Reset form when modal opens |
| 23 | + useEffect(() => { |
| 24 | + if (open) { |
| 25 | + setService(SERVICES[0]); |
| 26 | + setName(""); |
| 27 | + setApiKey(""); |
| 28 | + } |
| 29 | + }, [open]); |
| 30 | + |
| 31 | + // Escape key closes modal |
| 32 | + useEffect(() => { |
| 33 | + if (!open) return; |
| 34 | + const handler = (e: KeyboardEvent) => { |
| 35 | + if (e.key === "Escape") onClose(); |
| 36 | + }; |
| 37 | + window.addEventListener("keydown", handler); |
| 38 | + return () => window.removeEventListener("keydown", handler); |
| 39 | + }, [open, onClose]); |
| 40 | + |
| 41 | + const canSubmit = name.trim().length > 0 && apiKey.trim().length > 0; |
| 42 | + |
| 43 | + const handleSubmit = useCallback( |
| 44 | + async (e: React.FormEvent) => { |
| 45 | + e.preventDefault(); |
| 46 | + if (!canSubmit || submitting) return; |
| 47 | + setSubmitting(true); |
| 48 | + const ok = await onSubmit({ |
| 49 | + service, |
| 50 | + name: name.trim(), |
| 51 | + api_key: apiKey.trim(), |
| 52 | + }); |
| 53 | + setSubmitting(false); |
| 54 | + if (ok) onClose(); |
| 55 | + }, |
| 56 | + [service, name, apiKey, canSubmit, submitting, onSubmit, onClose], |
| 57 | + ); |
| 58 | + |
| 59 | + if (!open) return null; |
| 60 | + |
| 61 | + return ( |
| 62 | + <div |
| 63 | + className="fixed inset-0 z-50 flex items-center justify-center bg-black/60" |
| 64 | + onClick={(e) => { |
| 65 | + if (e.target === e.currentTarget) onClose(); |
| 66 | + }} |
| 67 | + > |
| 68 | + <div className="w-full max-w-md rounded-xl border border-border bg-surface p-6 shadow-2xl"> |
| 69 | + {/* Header */} |
| 70 | + <div className="flex items-center justify-between"> |
| 71 | + <h2 className="text-lg font-semibold text-text">Add Credential</h2> |
| 72 | + <button |
| 73 | + onClick={onClose} |
| 74 | + className="rounded-md p-1 text-text-tertiary transition-colors hover:bg-surface-hover hover:text-text" |
| 75 | + > |
| 76 | + <X size={16} /> |
| 77 | + </button> |
| 78 | + </div> |
| 79 | + |
| 80 | + {/* Form */} |
| 81 | + <form onSubmit={handleSubmit} className="mt-5 space-y-4"> |
| 82 | + {/* Service */} |
| 83 | + <div> |
| 84 | + <label className="mb-1.5 block text-[11px] font-semibold uppercase tracking-[0.08em] text-text-tertiary"> |
| 85 | + Service |
| 86 | + </label> |
| 87 | + <select |
| 88 | + value={service} |
| 89 | + onChange={(e) => setService(e.target.value)} |
| 90 | + className="w-full rounded-lg border border-border bg-surface-input px-3 py-2 text-sm text-text focus:border-brand/50 focus:outline-none focus:ring-1 focus:ring-brand/20" |
| 91 | + > |
| 92 | + {SERVICES.map((s) => ( |
| 93 | + <option key={s} value={s}> |
| 94 | + {SERVICE_LABELS[s]} |
| 95 | + </option> |
| 96 | + ))} |
| 97 | + </select> |
| 98 | + </div> |
| 99 | + |
| 100 | + {/* Name */} |
| 101 | + <div> |
| 102 | + <label className="mb-1.5 block text-[11px] font-semibold uppercase tracking-[0.08em] text-text-tertiary"> |
| 103 | + Name |
| 104 | + </label> |
| 105 | + <input |
| 106 | + type="text" |
| 107 | + value={name} |
| 108 | + onChange={(e) => setName(e.target.value)} |
| 109 | + placeholder="e.g. Production API Key" |
| 110 | + className="w-full rounded-lg border border-border bg-surface-input px-3 py-2 text-sm text-text placeholder:text-text-tertiary/50 focus:border-brand/50 focus:outline-none focus:ring-1 focus:ring-brand/20" |
| 111 | + /> |
| 112 | + </div> |
| 113 | + |
| 114 | + {/* API Key */} |
| 115 | + <div> |
| 116 | + <label className="mb-1.5 block text-[11px] font-semibold uppercase tracking-[0.08em] text-text-tertiary"> |
| 117 | + API Key |
| 118 | + </label> |
| 119 | + <input |
| 120 | + type="password" |
| 121 | + value={apiKey} |
| 122 | + onChange={(e) => setApiKey(e.target.value)} |
| 123 | + placeholder="sk-..." |
| 124 | + className="w-full rounded-lg border border-border bg-surface-input px-3 py-2 font-mono text-sm text-text placeholder:text-text-tertiary/50 focus:border-brand/50 focus:outline-none focus:ring-1 focus:ring-brand/20" |
| 125 | + /> |
| 126 | + </div> |
| 127 | + |
| 128 | + {/* Actions */} |
| 129 | + <div className="flex items-center justify-end gap-2 pt-2"> |
| 130 | + <button |
| 131 | + type="button" |
| 132 | + onClick={onClose} |
| 133 | + className="rounded-lg px-3 py-1.5 text-sm text-text-tertiary transition-colors hover:bg-surface-hover hover:text-text" |
| 134 | + > |
| 135 | + Cancel |
| 136 | + </button> |
| 137 | + <button |
| 138 | + type="submit" |
| 139 | + disabled={!canSubmit || submitting} |
| 140 | + className="rounded-lg bg-brand px-4 py-1.5 text-sm font-medium text-white transition-all duration-150 hover:bg-brand-hover disabled:opacity-40" |
| 141 | + > |
| 142 | + {submitting ? "Adding..." : "Add Credential"} |
| 143 | + </button> |
| 144 | + </div> |
| 145 | + </form> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + ); |
| 149 | +} |
0 commit comments