File size: 12,244 Bytes
3c8d218
 
 
 
 
ef865f2
 
3c8d218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef865f2
3c8d218
 
 
 
 
 
 
7457a6f
 
 
 
3c8d218
7457a6f
 
3c8d218
 
 
 
7457a6f
3c8d218
 
 
 
 
 
 
 
 
 
7457a6f
3c8d218
7457a6f
3c8d218
7457a6f
 
3c8d218
 
7457a6f
 
 
3c8d218
 
 
 
 
7457a6f
3c8d218
 
 
 
 
 
 
 
 
 
 
7457a6f
 
3c8d218
7457a6f
3c8d218
 
7457a6f
3c8d218
7457a6f
3c8d218
 
 
 
7457a6f
3c8d218
 
 
 
 
 
7457a6f
3c8d218
 
 
 
7457a6f
3c8d218
 
 
 
 
 
7457a6f
3c8d218
 
 
 
7457a6f
3c8d218
 
 
 
 
 
7457a6f
3c8d218
 
 
 
7457a6f
3c8d218
 
 
 
 
 
 
 
 
 
 
 
7457a6f
 
3c8d218
7457a6f
3c8d218
 
 
7457a6f
3c8d218
 
 
 
 
 
 
7457a6f
 
3c8d218
 
 
 
 
7457a6f
 
 
3c8d218
 
 
 
7457a6f
3c8d218
 
 
 
 
 
7457a6f
3c8d218
 
 
 
 
 
7457a6f
3c8d218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from 'react';
import { Settings, Save, Trash2, Key, User, Lock, Plus, X, RefreshCw, Server, Database } from 'lucide-react';
import { useAppContext } from './AppContext';

const ProfilePage = () => {
  const {
    activeSpecId, activeSpecName, storedCredentials, saveProfile, addToast,
  } = useAppContext();

  const [profileForm, setProfileForm] = useState({ name: '', email: '', password: '', token: '' });
  const [extraKvs, setExtraKvs] = useState([]);
  const [isSaving, setIsSaving] = useState(false);

  // Sync stored credentials into local form
  useEffect(() => {
    if (!storedCredentials) return;
    
    setProfileForm({
      name: storedCredentials.name || '',
      email: storedCredentials.email || storedCredentials.username || '',
      password: '', // Don't prefill password
      token: storedCredentials.access_token || storedCredentials.token || ''
    });
    
    const standardKeys = new Set(['name', 'email', 'username', 'password', 'access_token', 'token', 'refresh_token', 'id_token']);
    const kvs = Object.entries(storedCredentials)
      .filter(([k, v]) => !standardKeys.has(k) && k !== '_clear')
      .map(([k, v]) => ({ key: k, val: String(v) }));
    setExtraKvs(kvs);
  }, [storedCredentials]);

  const handleSave = async () => {
    if (!activeSpecId) return;
    setIsSaving(true);
    try {
      const payload = { 
         name: profileForm.name,
         username: profileForm.email,
         extra: {}
      };
      if (profileForm.password) payload.password = profileForm.password;
      if (profileForm.token) payload.extra.token = profileForm.token;
      
      extraKvs.forEach(kv => {
        if (kv.key && kv.val) payload.extra[kv.key] = kv.val;
      });
      await saveProfile(payload);
    } catch (err) {
      console.error("Save failed", err);
      addToast('error', err.message || "Failed to save profile.");
    } finally {
      setIsSaving(false);
    }
  };

  if (!activeSpecId) {
    return (
      <div className="flex-1 flex flex-col items-center justify-center p-8 text-center bg-[var(--bg-primary)] animate-in fade-in duration-500">
        <div className="p-8 bg-[var(--bg-secondary)] border border-[var(--border-subtle)] rounded-3xl shadow-2xl max-w-md w-full animate-in zoom-in duration-300">
           <div className="w-16 h-16 bg-[var(--bg-tertiary)] border border-[var(--border-subtle)] rounded-2xl flex items-center justify-center mx-auto mb-6">
              <Server size={32} className="text-[var(--text-secondary)] opacity-50" />
           </div>
           <h2 className="text-2xl font-semibold text-[var(--text-primary)] mb-2">No Collection Selected</h2>
           <p className="text-[var(--text-secondary)] text-sm mb-8 leading-relaxed">
             Test user profiles are isolated per collection. Please select an API specification first to manage its credentials.
           </p>
           <button 
             onClick={() => window.dispatchEvent(new CustomEvent('nav-change', { detail: 'collections' }))} 
             className="w-full py-3 bg-[var(--accent-gradient)] hover:opacity-90 text-white text-sm font-bold rounded-xl transition shadow-lg flex items-center justify-center space-x-2"
           >
             <Database size={16} />
             <span>Browse Collections</span>
           </button>
        </div>
      </div>
    );
  }

  return (
    <div className="flex-1 flex flex-col bg-[var(--bg-primary)] overflow-hidden animate-in fade-in slide-in-from-bottom-2 duration-300">
      {/* Header */}
      <div className="h-14 border-b border-[var(--border-subtle)] flex items-center justify-between px-6 bg-[var(--bg-secondary)]">
        <div className="flex items-center space-x-4">
          <h1 className="text-lg font-medium text-[var(--text-primary)] flex items-center space-x-2">
            <Settings size={20} className="text-[var(--accent-primary)]" />
            <span>Test User Profile</span>
          </h1>
          <div className="h-6 w-px bg-[var(--border-subtle)]" />
          <div className="text-xs text-[var(--text-secondary)]">
            Current Spec: <span className="text-[var(--accent-primary)] font-mono">{activeSpecName}</span>
          </div>
        </div>
        <button 
          onClick={handleSave}
          disabled={isSaving}
          className="bg-[var(--accent-gradient)] hover:opacity-90 text-white px-6 py-1.5 text-xs font-semibold rounded-lg flex items-center space-x-2 transition shadow-lg disabled:opacity-50"
        >
          {isSaving ? <RefreshCw size={14} className="animate-spin" /> : <Save size={14} />}
          <span>Save Profile</span>
        </button>
      </div>

      <div className="flex-1 overflow-y-auto p-8">
        <div className="max-w-3xl mx-auto space-y-8">
          {/* Core Credentials */}
          <section className="space-y-4">
            <div className="flex items-center space-x-3 mb-6">
              <div className="p-2 bg-[var(--accent-primary)]/10 border border-[var(--accent-primary)]/20 rounded-lg">
                <Lock size={18} className="text-[var(--accent-primary)]" />
              </div>
              <h3 className="text-sm font-semibold text-[var(--text-primary)]">Core Authentication</h3>
            </div>
            
            <div className="grid grid-cols-1 md:grid-cols-2 gap-6 bg-[var(--bg-secondary)] p-6 rounded-2xl border border-[var(--border-subtle)] shadow-sm">
              <div className="space-y-2">
                 <label className="text-[10px] uppercase font-bold text-[var(--text-secondary)] tracking-wider flex items-center gap-2">
                    Full Name
                 </label>
                 <input 
                   type="text" 
                   className="w-full bg-[var(--bg-primary)] border border-[var(--border-subtle)] text-[var(--text-primary)] p-2.5 text-sm rounded-lg outline-none focus:border-[var(--accent-primary)] transition-colors" 
                   value={profileForm.name} 
                   onChange={e => setProfileForm({...profileForm, name: e.target.value})} 
                   placeholder="e.g. John Doe"
                 />
              </div>
              <div className="space-y-2">
                 <label className="text-[10px] uppercase font-bold text-[var(--text-secondary)] tracking-wider flex items-center gap-2">
                    Email / Username
                 </label>
                 <input 
                   type="text" 
                   className="w-full bg-[var(--bg-primary)] border border-[var(--border-subtle)] text-[var(--text-primary)] p-2.5 text-sm rounded-lg outline-none focus:border-[var(--accent-primary)] transition-colors" 
                   value={profileForm.email} 
                   onChange={e => setProfileForm({...profileForm, email: e.target.value})} 
                   placeholder="e.g. john.doe@example.com"
                 />
              </div>
              <div className="space-y-2">
                 <label className="text-[10px] uppercase font-bold text-[var(--text-secondary)] tracking-wider flex items-center gap-2">
                    Password
                 </label>
                 <input 
                   type="password" 
                   className="w-full bg-[var(--bg-primary)] border border-[var(--border-subtle)] text-[var(--text-primary)] p-2.5 text-sm rounded-lg outline-none focus:border-[var(--accent-primary)] transition-colors" 
                   value={profileForm.password} 
                   onChange={e => setProfileForm({...profileForm, password: e.target.value})} 
                   placeholder="Leave blank to keep current"
                 />
              </div>
              <div className="space-y-2">
                 <label className="text-[10px] uppercase font-bold text-[var(--text-secondary)] tracking-wider flex items-center gap-2">
                    Static Access Token
                 </label>
                 <input 
                   type="text" 
                   className="w-full bg-[var(--bg-primary)] border border-[var(--border-subtle)] text-[var(--text-primary)] p-2.5 text-sm rounded-lg outline-none focus:border-[var(--accent-primary)] transition-colors" 
                   value={profileForm.token} 
                   onChange={e => setProfileForm({...profileForm, token: e.target.value})} 
                   placeholder="Skip login flow with a manual token"
                 />
              </div>
            </div>
          </section>

          {/* Extra Variables */}
          <section className="space-y-4">
            <div className="flex items-center justify-between mb-4 px-1">
              <div className="flex items-center space-x-3">
                <div className="p-2 bg-[var(--accent-secondary)]/10 border border-[var(--accent-secondary)]/20 rounded-lg">
                  <Key size={18} className="text-[var(--accent-secondary)]" />
                </div>
                <h3 className="text-sm font-semibold text-[var(--text-primary)]">Custom Fields & Context</h3>
              </div>
              <button 
                onClick={() => setExtraKvs([...extraKvs, {key: '', val: ''}])} 
                className="text-xs bg-[var(--bg-tertiary)] hover:bg-[var(--bg-secondary)] text-[var(--text-primary)] px-3 py-1.5 rounded-lg border border-[var(--border-subtle)] flex items-center gap-1.5 transition shadow-sm"
              >
                <Plus size={14} /> 
                <span>Add Variables</span>
              </button>
            </div>
            
            {extraKvs.length === 0 ? (
              <div className="py-16 bg-[var(--bg-secondary)] border-2 border-dashed border-[var(--border-subtle)] rounded-2xl text-center shadow-inner">
                 <p className="text-[var(--text-secondary)] text-sm italic">No custom variables defined. Use these for OTPs, User IDs, or custom test data pointers.</p>
              </div>
            ) : (
              <div className="grid grid-cols-1 gap-3">
                {extraKvs.map((kv, i) => (
                  <div key={i} className="flex space-x-3 group animate-in slide-in-from-left-2 duration-300">
                    <div className="flex-1 bg-[var(--bg-secondary)] border border-[var(--border-subtle)] rounded-xl overflow-hidden shadow-sm flex items-center group-hover:border-[var(--text-secondary)]/30 transition-colors">
                      <div className="bg-[var(--bg-primary)] border-r border-[var(--border-subtle)] px-3 py-2 flex items-center justify-center">
                         <span className="text-[10px] text-[var(--text-secondary)] font-bold uppercase tracking-tighter w-4 text-center">KV</span>
                      </div>
                      <input 
                        type="text" 
                        placeholder="Key" 
                        className="w-1/3 bg-transparent border-r border-[var(--border-subtle)] text-[var(--accent-primary)] px-4 py-2.5 text-xs outline-none focus:bg-[var(--bg-tertiary)] transition-colors font-mono" 
                        value={kv.key} 
                        onChange={e => { const n = [...extraKvs]; n[i].key = e.target.value; setExtraKvs(n); }} 
                      />
                      <input 
                        type="text" 
                        placeholder="Value" 
                        className="flex-1 bg-transparent text-[var(--accent-secondary)] px-4 py-2.5 text-xs outline-none focus:bg-[var(--bg-tertiary)] transition-colors font-mono" 
                        value={kv.val} 
                        onChange={e => { const n = [...extraKvs]; n[i].val = e.target.value; setExtraKvs(n); }} 
                      />
                    </div>
                    <button 
                      onClick={() => setExtraKvs(extraKvs.filter((_, idx) => idx !== i))} 
                      className="text-[var(--text-secondary)] hover:text-red-400 p-2 opacity-0 group-hover:opacity-100 transition-all self-center"
                    >
                      <Trash2 size={16} />
                    </button>
                  </div>
                ))}
              </div>
            )}
          </section>
        </div>
      </div>
    </div>
  );
};

export default ProfilePage;