import React, { useRef, useState } from "react"; import { Button } from "./ui/button"; import { Input } from "./ui/input"; import { Label } from "./ui/label"; import { Dialog, DialogContent, DialogTitle } from "./ui/dialog"; import type { User as UserType } from "../App"; import { toast } from "sonner"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./ui/select"; import { ChevronLeft, ChevronRight } from "lucide-react"; interface OnboardingProps { user: UserType; onComplete: (user: UserType) => void; onSkip: () => void; } // ✅ Bio step removed. Total steps: 4 const TOTAL_STEPS = 4; export function Onboarding({ user, onComplete, onSkip }: OnboardingProps) { const [currentStep, setCurrentStep] = useState(1); // Step 1: Basic const [name, setName] = useState(user.name ?? ""); const [email, setEmail] = useState(user.email ?? ""); // Step 2: Academic const [studentId, setStudentId] = useState(user.studentId ?? ""); const [department, setDepartment] = useState(user.department ?? ""); const [yearLevel, setYearLevel] = useState(user.yearLevel ?? ""); const [major, setMajor] = useState(user.major ?? ""); // Step 3: Preferences (controlled, not defaultValue-only) const [learningStyle, setLearningStyle] = useState(user.learningStyle ?? "visual"); const [learningPace, setLearningPace] = useState(user.learningPace ?? "moderate"); // Step 4: Photo const [photoPreview, setPhotoPreview] = useState(user.avatarUrl ?? null); const fileInputRef = useRef(null); const handlePhotoSelect = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; if (!file.type.startsWith("image/")) { toast.error("Please select an image file"); return; } if (file.size > 2 * 1024 * 1024) { toast.error("File size must be less than 2MB"); return; } const reader = new FileReader(); reader.onload = (ev) => setPhotoPreview(ev.target?.result as string); reader.readAsDataURL(file); }; const handleChangePhotoClick = () => fileInputRef.current?.click(); const handleNext = () => { if (currentStep < TOTAL_STEPS) setCurrentStep((s) => s + 1); else handleComplete(); }; const handlePrevious = () => { if (currentStep > 1) setCurrentStep((s) => s - 1); }; const handleSkip = () => onSkip(); const handleComplete = () => { if (!name.trim() || !email.trim()) { toast.error("Please fill in all required fields"); return; } // ✅ IMPORTANT: Onboarding must NOT overwrite bio. // We merge ...user first to preserve any Clare-generated bio. const next: UserType = { ...user, name: name.trim(), email: email.trim(), studentId: studentId.trim() || undefined, department: department.trim() || undefined, yearLevel: yearLevel || undefined, major: major.trim() || undefined, learningStyle: learningStyle || undefined, learningPace: learningPace || undefined, avatarUrl: photoPreview || undefined, onboardingCompleted: true, // ❌ do NOT set bio here }; onComplete(next); toast.success("Profile setup completed!"); }; const renderStepContent = () => { switch (currentStep) { case 1: return (

Basic Information

Let's start with your basic information

setName(e.target.value)} placeholder="Enter your full name" />
setEmail(e.target.value)} placeholder="Enter your email" />
); case 2: return (

Academic Background

Tell us about your academic information

setStudentId(e.target.value)} placeholder="Enter your student ID" />
setDepartment(e.target.value)} placeholder="Enter your department" />
setMajor(e.target.value)} placeholder="Enter your major" />
); case 3: return (

Learning Preferences

Help us personalize your learning experience

); case 4: return (

Profile Picture

Upload a photo to personalize your profile (optional)

{photoPreview ? ( Profile ) : ( (name?.charAt(0) || "U").toUpperCase() )}

JPG, PNG or GIF. Max size 2MB

); default: return null; } }; return ( { if (!open) onSkip(); }}>
{/* Header */}
Welcome! Let's set up your profile

Step {currentStep} of {TOTAL_STEPS}

{/* Progress indicator */}
{Array.from({ length: TOTAL_STEPS }).map((_, index) => (
))}
{/* Content */}
{renderStepContent()}
{/* Footer */}
{currentStep > 1 && ( )}
); }