import React, { useState } from 'react'; import { Brain, Shield, Lock, KeyRound, UserPlus, LogIn, Server, Share2, Check, AlertCircle, } from 'lucide-react'; import { AuthStatus, User } from '../../lib/types'; import { api, triggerHaptic } from '../../lib/api'; interface SetupAndAuthViewProps { authStatus: AuthStatus; onAuthenticated: (user: User) => void; } export const SetupAndAuthView: React.FC = ({ authStatus, onAuthenticated, }) => { // --- Setup Wizard State --- const [setupName, setSetupName] = useState(''); const [setupEmail, setSetupEmail] = useState(''); const [setupPassword, setSetupPassword] = useState(''); const [instanceName, setInstanceName] = useState(authStatus.instanceName || 'MindSift'); const [sharingMode, setSharingMode] = useState<'open' | 'teams_only' | 'strict_private'>( 'teams_only' ); const [allowRegistration, setAllowRegistration] = useState(false); const [createSampleData, setCreateSampleData] = useState(false); // --- Login / Invite / Register State --- const [authTab, setAuthTab] = useState<'login' | 'invite' | 'register'>('login'); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [displayName, setDisplayName] = useState(''); const [inviteCode, setInviteCode] = useState(''); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const handleCompleteSetup = async (e: React.FormEvent) => { e.preventDefault(); setError(null); setSubmitting(true); triggerHaptic(15); try { const res = await api.completeSetup({ email: setupEmail.trim(), password: setupPassword, displayName: setupName.trim(), instanceName: instanceName.trim() || 'MindSift', sharingMode, allowRegistration, createSampleData, }); onAuthenticated(res.user); } catch (err: any) { setError(err.message || 'Fehler bei der Ersteinrichtung'); } finally { setSubmitting(false); } }; const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setError(null); setSubmitting(true); triggerHaptic(15); try { const res = await api.login(email.trim(), password); onAuthenticated(res.user); } catch (err: any) { setError(err.message || 'Ungültige Anmeldedaten'); } finally { setSubmitting(false); } }; const handleRegisterOrInvite = async (e: React.FormEvent) => { e.preventDefault(); setError(null); setSubmitting(true); triggerHaptic(15); try { const code = authTab === 'invite' ? inviteCode.trim() : ''; const res = await api.register(email.trim(), password, displayName.trim(), code); onAuthenticated(res.user); } catch (err: any) { setError(err.message || 'Registrierung fehlgeschlagen'); } finally { setSubmitting(false); } }; // ============================================================================ // FIRST-BOOT SERVER SETUP WIZARD // ============================================================================ if (authStatus.needsSetup) { return (
{/* Top Brand Bar */}

MindSift Ersteinrichtung

Initiales Administrator-Konto & Server-Konfiguration

First Boot

Willkommen auf deiner neuen MindSift-Instanz. Es existiert noch kein Benutzerkonto in der Datenbank. Lege jetzt das erste Server-Admin-Konto an und bestimme die Sicherheitseinstellungen.

{error && (
{error}
)} {/* Section 1: Admin Account */}

1. Server-Administrator anlegen

setSetupName(e.target.value)} placeholder="z.B. Admin / Dein Name" required autoFocus className="w-full h-8 px-2.5 input-field" />
setSetupEmail(e.target.value)} placeholder="admin@deinedomain.de" required className="w-full h-8 px-2.5 input-field" />
setSetupPassword(e.target.value)} placeholder="Mindestens 4 Zeichen" minLength={4} required className="w-full h-8 px-2.5 input-field" />
{/* Section 2: Server & Isolation */}

2. Instanz & Berechtigungen

setInstanceName(e.target.value)} placeholder="MindSift" className="w-full h-8 px-2.5 input-field" />
); } // ============================================================================ // STANDARD LOGIN / INVITE CODE / REGISTRATION VIEW // ============================================================================ return (
{/* Top Header */}

{authStatus.instanceName || 'MindSift'}

Der Kopf frei in drei Sekunden

{/* Navigation Tabs */}
{authStatus.allowRegistration && ( )}
{/* Form Body */}
{error && (
{error}
)} {authTab === 'login' && (
{authStatus.demoMode && (
🧪 Demo-Modus aktiv Klick zum Ausfüllen:
)}
setEmail(e.target.value)} placeholder="admin oder name@beispiel.de" required autoFocus className="w-full h-8 px-2.5 input-field" />
setPassword(e.target.value)} placeholder="••••••••" required className="w-full h-8 px-2.5 input-field" />
{!authStatus.allowRegistration && (

Öffentliche Registrierung ist deaktiviert. Zugang nur mit Konto oder{' '} .

)}
)} {(authTab === 'invite' || authTab === 'register') && (
{authTab === 'invite' && (
setInviteCode(e.target.value.toUpperCase())} placeholder="MS-XXXXXXXX" required autoFocus className="w-full h-8 px-2.5 input-field font-mono uppercase" />
)}
setDisplayName(e.target.value)} placeholder="Dein Name" required className="w-full h-8 px-2.5 input-field" />
setEmail(e.target.value)} placeholder="name@beispiel.de" required className="w-full h-8 px-2.5 input-field" />
setPassword(e.target.value)} placeholder="Mindestens 4 Zeichen" minLength={4} required className="w-full h-8 px-2.5 input-field" />
)}
); };