File size: 11,523 Bytes
ad68e43
 
81ff144
 
 
 
ffac2f3
ad68e43
 
81ff144
ad68e43
 
 
 
 
 
 
 
 
81ff144
 
 
 
ad68e43
 
 
 
 
 
 
81ff144
 
 
 
ffac2f3
 
81ff144
 
 
ad68e43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81ff144
 
 
 
 
 
 
 
 
 
 
 
ad68e43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81ff144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad68e43
81ff144
 
 
ad68e43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81ff144
 
 
 
 
 
 
 
ffac2f3
81ff144
 
ad68e43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81ff144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad68e43
81ff144
 
ad68e43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81ff144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import React, { useEffect, useMemo, useState } from 'react';
import { Bot, CheckCircle2, KeyRound, LogOut, Server, Settings, Shield, UserCircle, Users } from 'lucide-react';
import { motion } from 'framer-motion';
import { useAuth } from '../context/useAuth';
import { getDefaultModel, getDefaultProvider, providerOptions, saveProviderDefaults } from '../services/llmConfig';
import type { SupportedProvider } from '../services/llmConfig';
import { getApiUrl, getAppVersion, getSupabaseUrl } from '../services/runtimeConfig';
import type { UiMode } from '../services/uiMode';
import { supabase } from '../services/supabase';

interface ProfileRow {
  id: string;
  role: 'user' | 'manager' | 'admin';
  full_name: string | null;
  avatar_url: string | null;
}

const SettingsView: React.FC<{ uiMode: UiMode; onUiModeChange: (mode: UiMode) => void }> = ({ uiMode, onUiModeChange }) => {
  const { user, session, profile, signOut, refreshProfile } = useAuth();
  const initialProvider = useMemo(() => getDefaultProvider(), []);
  const [provider, setProvider] = useState<SupportedProvider>(initialProvider);
  const [model, setModel] = useState(getDefaultModel(initialProvider));
  const [saved, setSaved] = useState(false);
  const [fullName, setFullName] = useState('');
  const [avatarUrl, setAvatarUrl] = useState('');
  const [profileMessage, setProfileMessage] = useState<string | null>(null);
  const [savingProfile, setSavingProfile] = useState(false);
  const [profiles, setProfiles] = useState<ProfileRow[]>([]);
  const [profilesLoading, setProfilesLoading] = useState(false);
  const [adminMessage, setAdminMessage] = useState<string | null>(null);

  const config = useMemo(() => {
    const apiUrl = getApiUrl() || 'Same origin';
    const supabaseUrl = getSupabaseUrl() || 'Not configured';
    const appVersion = getAppVersion();
    return { apiUrl, supabaseUrl, appVersion };
  }, []);

  const providerModels = providerOptions.find((option) => option.id === provider)?.models ?? [];
  const isAdmin = profile?.role === 'admin';

  useEffect(() => {
    setFullName(profile?.full_name ?? '');
    setAvatarUrl(profile?.avatar_url ?? '');
  }, [profile]);

  useEffect(() => {
    if (!isAdmin) return;

    const loadProfiles = async () => {
      setProfilesLoading(true);
      const { data } = await supabase
        .from('profiles')
        .select('id,role,full_name,avatar_url')
        .order('created_at', { ascending: false });
      setProfiles(data ?? []);
      setProfilesLoading(false);
    };

    loadProfiles();
  }, [isAdmin, adminMessage]);

  const updateProvider = (value: SupportedProvider) => {
    setProvider(value);
    setModel(getDefaultModel(value));
    setSaved(false);
  };

  const saveDefaults = () => {
    saveProviderDefaults(provider, model);
    setSaved(true);
  };

  const saveProfile = async () => {
    if (!user) return;
    setSavingProfile(true);
    setProfileMessage(null);

    const { error } = await supabase
      .from('profiles')
      .upsert(
        {
          id: user.id,
          role: profile?.role ?? 'user',
          full_name: fullName.trim() || null,
          avatar_url: avatarUrl.trim() || null,
          updated_at: new Date().toISOString(),
        },
        { onConflict: 'id' }
      );

    if (error) {
      setProfileMessage(error.message);
      setSavingProfile(false);
      return;
    }

    await refreshProfile();
    setProfileMessage('Profile updated.');
    setSavingProfile(false);
  };

  const updateUserRole = async (profileId: string, role: 'user' | 'admin') => {
    setAdminMessage(null);
    const { error } = await supabase
      .from('profiles')
      .update({ role, updated_at: new Date().toISOString() })
      .eq('id', profileId);

    if (error) {
      setAdminMessage(error.message);
      return;
    }

    setProfiles((current) => current.map((entry) => (entry.id === profileId ? { ...entry, role } : entry)));
    if (profileId === user?.id) {
      await refreshProfile();
    }
    setAdminMessage('User roles updated.');
  };

  return (
    <motion.div initial={{ opacity: 0, y: 20 }} animate={{ opacity: 1, y: 0 }} className="settings-page animate-fade-in">
      <div className="panel-heading">
        <Settings size={32} color="var(--accent)" />
        <div>
          <h2>Settings</h2>
          <p style={{ color: 'var(--text-dim)', fontSize: '0.9rem' }}>Review environment, session, and security configuration.</p>
        </div>
      </div>

      <div className="settings-grid">
        <section className="glass-panel settings-section">
          <div className="settings-section-title">
            <UserCircle size={22} color="var(--accent)" />
            <h3>Session</h3>
          </div>
          <SettingRow label="Email" value={user?.email ?? 'Unknown'} />
          <SettingRow label="User ID" value={user?.id ?? 'Unknown'} />
          <SettingRow label="Provider" value={user?.app_metadata?.provider ?? 'email'} />
          <SettingRow label="Role" value={profile?.role ?? 'user'} />
          <SettingRow label="Expires" value={session?.expires_at ? new Date(session.expires_at * 1000).toLocaleString() : 'Unknown'} />
        </section>

        <section className="glass-panel settings-section">
          <div className="settings-section-title">
            <UserCircle size={22} color="var(--accent)" />
            <h3>Profile</h3>
          </div>
          <label>
            <span>Full Name</span>
            <input value={fullName} onChange={(event) => setFullName(event.target.value)} placeholder="Your name" />
          </label>
          <label>
            <span>Avatar URL</span>
            <input value={avatarUrl} onChange={(event) => setAvatarUrl(event.target.value)} placeholder="https://..." />
          </label>
          <button className="btn btn-primary" onClick={saveProfile} disabled={savingProfile}>
            <CheckCircle2 size={18} />
            {savingProfile ? 'Saving...' : 'Save Profile'}
          </button>
          {profileMessage && <p style={{ color: profileMessage.includes('updated') ? 'var(--success)' : 'var(--danger)' }}>{profileMessage}</p>}
        </section>

        <section className="glass-panel settings-section">
          <div className="settings-section-title">
            <Server size={22} color="var(--accent)" />
            <h3>Runtime</h3>
          </div>
          <SettingRow label="API URL" value={config.apiUrl} />
          <SettingRow label="Supabase URL" value={config.supabaseUrl} />
          <SettingRow label="Frontend Mode" value={import.meta.env.MODE} />
          <SettingRow label="App Version" value={config.appVersion} />
        </section>

        <section className="glass-panel settings-section">
          <div className="settings-section-title">
            <Bot size={22} color="var(--accent)" />
            <h3>Workspace Mode</h3>
          </div>
          <p style={{ color: 'var(--text-dim)' }}>Guided keeps the product focused on the main workflow. Expert exposes the full tool surface.</p>
          <label>
            <span>Mode</span>
            <select value={uiMode} onChange={(event) => onUiModeChange(event.target.value as UiMode)}>
              <option value="guided">Guided</option>
              <option value="expert">Expert</option>
            </select>
          </label>
        </section>

        <section className="glass-panel settings-section">
          <div className="settings-section-title">
            <Bot size={22} color="var(--accent)" />
            <h3>LLM Defaults</h3>
          </div>
          <p style={{ color: 'var(--text-dim)' }}>These defaults are used when creating new agents from the UI. API keys stay in backend `.env`.</p>
          <label>
            <span>Default Provider</span>
            <select value={provider} onChange={(event) => updateProvider(event.target.value as SupportedProvider)}>
              {providerOptions.map((option) => (
                <option key={option.id} value={option.id}>{option.label}</option>
              ))}
            </select>
          </label>
          <label>
            <span>Default Model</span>
            <select value={model} onChange={(event) => { setModel(event.target.value); setSaved(false); }}>
              {providerModels.map((providerModel) => (
                <option key={providerModel} value={providerModel}>{providerModel}</option>
              ))}
            </select>
          </label>
          <button className="btn btn-primary" onClick={saveDefaults}>
            <CheckCircle2 size={18} />
            Save LLM Defaults
          </button>
          {saved && <p style={{ color: 'var(--success)' }}>LLM defaults saved.</p>}
        </section>

        <section className="glass-panel settings-section">
          <div className="settings-section-title">
            <Shield size={22} color="var(--accent)" />
            <h3>Security</h3>
          </div>
          <SettingRow label="Authentication" value="Supabase Auth" />
          <SettingRow label="Database Access" value="RLS protected" />
          <SettingRow label="Marketplace Deploy" value="Owned by user_id" />
          <SettingRow label="Admin Access" value={isAdmin ? 'Enabled' : 'Disabled'} />
        </section>

        {isAdmin && (
          <section className="glass-panel settings-section" style={{ gridColumn: '1 / -1' }}>
            <div className="settings-section-title">
              <Users size={22} color="var(--accent)" />
              <h3>User Management</h3>
            </div>
            <p style={{ color: 'var(--text-dim)' }}>Manage profile roles for users in this workspace.</p>
            {adminMessage && <div className="inline-status">{adminMessage}</div>}
            <div className="task-list">
              {profilesLoading && <p style={{ color: 'var(--text-dim)' }}>Loading users...</p>}
              {!profilesLoading && profiles.length === 0 && <p style={{ color: 'var(--text-dim)' }}>No profiles found.</p>}
              {profiles.map((entry) => (
                <div key={entry.id} className="task-row">
                  <div style={{ flex: 1 }}>
                    <strong>{entry.full_name || 'Unnamed user'}</strong>
                    <p>{entry.id}</p>
                  </div>
                  <select
                    value={entry.role}
                    onChange={(event) => updateUserRole(entry.id, event.target.value as 'user' | 'admin')}
                    style={{ maxWidth: '160px' }}
                  >
                    <option value="user">User</option>
                    <option value="admin">Admin</option>
                  </select>
                </div>
              ))}
            </div>
          </section>
        )}

        <section className="glass-panel settings-section">
          <div className="settings-section-title">
            <KeyRound size={22} color="var(--accent)" />
            <h3>Account</h3>
          </div>
          <p style={{ color: 'var(--text-dim)' }}>Signing out clears the local Supabase session and returns to the login screen.</p>
          <button className="btn btn-glass" onClick={signOut}>
            <LogOut size={18} />
            Sign Out
          </button>
        </section>
      </div>
    </motion.div>
  );
};

const SettingRow: React.FC<{ label: string; value: string }> = ({ label, value }) => (
  <div className="setting-row">
    <span>{label}</span>
    <strong title={value}>{value}</strong>
  </div>
);

export default SettingsView;