|
| 1 | +import { useState } from "react"; |
| 2 | +import type { TokenEntry } from "../hooks/useTokens"; |
| 3 | +import { |
| 4 | + Dialog, |
| 5 | + DialogContent, |
| 6 | + DialogHeader, |
| 7 | + DialogTitle, |
| 8 | + DialogDescription, |
| 9 | +} from "../../components/ui/dialog"; |
| 10 | +import { Check, Copy, Eye, EyeOff, Pencil, Plus, Trash2, X } from "lucide-react"; |
| 11 | + |
| 12 | +interface TokenManagerDialogProps { |
| 13 | + open: boolean; |
| 14 | + onClose: () => void; |
| 15 | + tokens: TokenEntry[]; |
| 16 | + activeTokenId: string | null; |
| 17 | + onSetActive: (id: string) => void; |
| 18 | + onAdd: (token: string, label: string) => string | null; |
| 19 | + onRemove: (id: string) => void; |
| 20 | + onUpdate: (id: string, label: string) => void; |
| 21 | +} |
| 22 | + |
| 23 | +export function TokenManagerDialog({ |
| 24 | + open, |
| 25 | + onClose, |
| 26 | + tokens, |
| 27 | + activeTokenId, |
| 28 | + onSetActive, |
| 29 | + onAdd, |
| 30 | + onRemove, |
| 31 | + onUpdate, |
| 32 | +}: TokenManagerDialogProps) { |
| 33 | + const [newToken, setNewToken] = useState(""); |
| 34 | + const [newLabel, setNewLabel] = useState(""); |
| 35 | + const [addError, setAddError] = useState(""); |
| 36 | + const [editingId, setEditingId] = useState<string | null>(null); |
| 37 | + const [editLabel, setEditLabel] = useState(""); |
| 38 | + const [visibleTokenId, setVisibleTokenId] = useState<string | null>(null); |
| 39 | + const [copiedId, setCopiedId] = useState<string | null>(null); |
| 40 | + |
| 41 | + const handleCopy = (id: string, token: string) => { |
| 42 | + navigator.clipboard.writeText(token).then(() => { |
| 43 | + setCopiedId(id); |
| 44 | + setTimeout(() => setCopiedId(null), 1500); |
| 45 | + }); |
| 46 | + }; |
| 47 | + |
| 48 | + const handleAdd = () => { |
| 49 | + const error = onAdd(newToken, newLabel); |
| 50 | + if (error) { |
| 51 | + setAddError(error); |
| 52 | + return; |
| 53 | + } |
| 54 | + setNewToken(""); |
| 55 | + setNewLabel(""); |
| 56 | + setAddError(""); |
| 57 | + }; |
| 58 | + |
| 59 | + const handleStartEdit = (entry: TokenEntry) => { |
| 60 | + setEditingId(entry.id); |
| 61 | + setEditLabel(entry.label); |
| 62 | + }; |
| 63 | + |
| 64 | + const handleSaveEdit = (id: string) => { |
| 65 | + onUpdate(id, editLabel.trim() || "Unnamed"); |
| 66 | + setEditingId(null); |
| 67 | + }; |
| 68 | + |
| 69 | + const handleSwitch = (id: string) => { |
| 70 | + onSetActive(id); |
| 71 | + onClose(); |
| 72 | + }; |
| 73 | + |
| 74 | + return ( |
| 75 | + <Dialog open={open} onOpenChange={(o) => { if (!o) onClose(); }}> |
| 76 | + <DialogContent className="max-w-md rounded-2xl border-border bg-surface-1 p-6 shadow-2xl"> |
| 77 | + <DialogHeader> |
| 78 | + <DialogTitle className="font-display text-lg font-semibold text-text-primary"> |
| 79 | + Token Manager |
| 80 | + </DialogTitle> |
| 81 | + <DialogDescription className="text-sm text-text-muted"> |
| 82 | + Manage API tokens for RCS authentication. |
| 83 | + </DialogDescription> |
| 84 | + </DialogHeader> |
| 85 | + |
| 86 | + {/* Token list */} |
| 87 | + <div className="space-y-1 max-h-64 overflow-y-auto"> |
| 88 | + {tokens.map((entry) => ( |
| 89 | + <div key={entry.id} className="group flex items-center gap-1"> |
| 90 | + {editingId === entry.id ? ( |
| 91 | + <div className="flex flex-1 items-center gap-2 rounded-lg bg-surface-2 px-3 py-1.5"> |
| 92 | + <input |
| 93 | + value={editLabel} |
| 94 | + onChange={(e) => setEditLabel(e.target.value)} |
| 95 | + onKeyDown={(e) => { |
| 96 | + if (e.key === "Enter") handleSaveEdit(entry.id); |
| 97 | + if (e.key === "Escape") setEditingId(null); |
| 98 | + }} |
| 99 | + className="flex-1 rounded border border-border bg-surface-1 px-2 py-1 text-sm text-text-primary focus:border-brand focus:outline-none" |
| 100 | + autoFocus |
| 101 | + /> |
| 102 | + <button |
| 103 | + onClick={() => handleSaveEdit(entry.id)} |
| 104 | + className="text-brand hover:text-brand-light transition-colors" |
| 105 | + > |
| 106 | + <Check className="h-4 w-4" /> |
| 107 | + </button> |
| 108 | + <button |
| 109 | + onClick={() => setEditingId(null)} |
| 110 | + className="text-text-muted hover:text-text-primary transition-colors" |
| 111 | + > |
| 112 | + <X className="h-4 w-4" /> |
| 113 | + </button> |
| 114 | + </div> |
| 115 | + ) : ( |
| 116 | + <> |
| 117 | + <button |
| 118 | + onClick={() => handleSwitch(entry.id)} |
| 119 | + className={`flex flex-1 items-center justify-between rounded-lg px-3 py-2 text-sm transition-colors ${ |
| 120 | + activeTokenId === entry.id |
| 121 | + ? "bg-brand/10 text-brand" |
| 122 | + : "text-text-secondary hover:bg-surface-2" |
| 123 | + }`} |
| 124 | + > |
| 125 | + <div className="flex flex-col items-start min-w-0"> |
| 126 | + <span className="font-medium truncate w-full">{entry.label}</span> |
| 127 | + <span className="text-xs text-text-muted font-mono"> |
| 128 | + {visibleTokenId === entry.id |
| 129 | + ? entry.token |
| 130 | + : `${entry.token.slice(0, 6)}${"\u2022".repeat(6)}`} |
| 131 | + </span> |
| 132 | + </div> |
| 133 | + {activeTokenId === entry.id && <Check className="h-4 w-4 flex-shrink-0" />} |
| 134 | + </button> |
| 135 | + <button |
| 136 | + onClick={() => setVisibleTokenId(visibleTokenId === entry.id ? null : entry.id)} |
| 137 | + className="rounded p-1 text-text-muted opacity-0 group-hover:opacity-100 hover:text-text-primary transition-all" |
| 138 | + title="Toggle token visibility" |
| 139 | + > |
| 140 | + {visibleTokenId === entry.id ? <EyeOff className="h-3.5 w-3.5" /> : <Eye className="h-3.5 w-3.5" />} |
| 141 | + </button> |
| 142 | + <button |
| 143 | + onClick={() => handleCopy(entry.id, entry.token)} |
| 144 | + className="rounded p-1 text-text-muted opacity-0 group-hover:opacity-100 hover:text-text-primary transition-all" |
| 145 | + title="Copy token" |
| 146 | + > |
| 147 | + {copiedId === entry.id ? <Check className="h-3.5 w-3.5 text-status-active" /> : <Copy className="h-3.5 w-3.5" />} |
| 148 | + </button> |
| 149 | + <button |
| 150 | + onClick={() => handleStartEdit(entry)} |
| 151 | + className="rounded p-1 text-text-muted opacity-0 group-hover:opacity-100 hover:text-text-primary transition-all" |
| 152 | + title="Edit label" |
| 153 | + > |
| 154 | + <Pencil className="h-3.5 w-3.5" /> |
| 155 | + </button> |
| 156 | + <button |
| 157 | + onClick={() => onRemove(entry.id)} |
| 158 | + className="rounded p-1 text-text-muted opacity-0 group-hover:opacity-100 hover:text-status-error transition-all" |
| 159 | + title="Delete token" |
| 160 | + > |
| 161 | + <Trash2 className="h-3.5 w-3.5" /> |
| 162 | + </button> |
| 163 | + </> |
| 164 | + )} |
| 165 | + </div> |
| 166 | + ))} |
| 167 | + |
| 168 | + {tokens.length === 0 && ( |
| 169 | + <div className="py-4 text-center text-sm text-text-muted"> |
| 170 | + No tokens saved yet. Add one below. |
| 171 | + </div> |
| 172 | + )} |
| 173 | + </div> |
| 174 | + |
| 175 | + {/* Add form */} |
| 176 | + <div className="border-t border-border pt-4 space-y-3"> |
| 177 | + <div className="text-sm font-medium text-text-secondary">Add Token</div> |
| 178 | + <div className="space-y-2"> |
| 179 | + <input |
| 180 | + type="text" |
| 181 | + value={newToken} |
| 182 | + onChange={(e) => { |
| 183 | + setNewToken(e.target.value); |
| 184 | + setAddError(""); |
| 185 | + }} |
| 186 | + placeholder="API Token" |
| 187 | + className="w-full rounded-lg border border-border bg-surface-2 px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:border-brand focus:outline-none font-mono" |
| 188 | + onKeyDown={(e) => { |
| 189 | + if (e.key === "Enter") handleAdd(); |
| 190 | + }} |
| 191 | + /> |
| 192 | + <div className="flex gap-2"> |
| 193 | + <input |
| 194 | + type="text" |
| 195 | + value={newLabel} |
| 196 | + onChange={(e) => setNewLabel(e.target.value)} |
| 197 | + placeholder="Label (optional)" |
| 198 | + className="flex-1 rounded-lg border border-border bg-surface-2 px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:border-brand focus:outline-none" |
| 199 | + onKeyDown={(e) => { |
| 200 | + if (e.key === "Enter") handleAdd(); |
| 201 | + }} |
| 202 | + /> |
| 203 | + <button |
| 204 | + onClick={handleAdd} |
| 205 | + disabled={!newToken.trim()} |
| 206 | + className="rounded-lg bg-brand px-3 py-2 text-white hover:bg-brand-light disabled:opacity-50 transition-colors" |
| 207 | + > |
| 208 | + <Plus className="h-4 w-4" /> |
| 209 | + </button> |
| 210 | + </div> |
| 211 | + </div> |
| 212 | + {addError && <div className="text-xs text-status-error">{addError}</div>} |
| 213 | + </div> |
| 214 | + </DialogContent> |
| 215 | + </Dialog> |
| 216 | + ); |
| 217 | +} |
0 commit comments