'use client' import { useState, useMemo } from 'react' import { useRouter } from 'next/navigation' import { trpc } from '@/lib/trpc/client' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { PhoneInput } from '@/components/ui/phone-input' import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { toast } from 'sonner' import { ExpertiseSelect } from '@/components/shared/expertise-select' import { User, Phone, Tags, Bell, CheckCircle, Loader2, ArrowRight, ArrowLeft, } from 'lucide-react' type Step = 'name' | 'phone' | 'tags' | 'preferences' | 'complete' export default function OnboardingPage() { const router = useRouter() const [step, setStep] = useState('name') // Form state const [name, setName] = useState('') const [phoneNumber, setPhoneNumber] = useState('') const [expertiseTags, setExpertiseTags] = useState([]) const [notificationPreference, setNotificationPreference] = useState< 'EMAIL' | 'WHATSAPP' | 'BOTH' | 'NONE' >('EMAIL') // Fetch feature flags const { data: featureFlags } = trpc.settings.getFeatureFlags.useQuery() const whatsappEnabled = featureFlags?.whatsappEnabled ?? false const completeOnboarding = trpc.user.completeOnboarding.useMutation() // Dynamic steps based on WhatsApp availability const steps: Step[] = useMemo(() => { if (whatsappEnabled) { return ['name', 'phone', 'tags', 'preferences', 'complete'] } // Skip phone step if WhatsApp is disabled return ['name', 'tags', 'preferences', 'complete'] }, [whatsappEnabled]) const currentIndex = steps.indexOf(step) const totalVisibleSteps = steps.length - 1 // Exclude 'complete' from count const goNext = () => { if (step === 'name' && !name.trim()) { toast.error('Please enter your name') return } const nextIndex = currentIndex + 1 if (nextIndex < steps.length) { setStep(steps[nextIndex]) } } const goBack = () => { const prevIndex = currentIndex - 1 if (prevIndex >= 0) { setStep(steps[prevIndex]) } } const handleComplete = async () => { try { await completeOnboarding.mutateAsync({ name, phoneNumber: phoneNumber || undefined, expertiseTags, notificationPreference, }) setStep('complete') toast.success('Welcome to MOPC!') // Redirect after a short delay setTimeout(() => { router.push('/jury') }, 2000) } catch (error) { toast.error(error instanceof Error ? error.message : 'Failed to complete onboarding') } } return (
{/* Progress indicator */}
{steps.slice(0, -1).map((s, i) => (
))}

Step {currentIndex + 1} of {totalVisibleSteps}

{/* Step 1: Name */} {step === 'name' && ( <> Welcome to MOPC Let's get your profile set up. What should we call you?
setName(e.target.value)} placeholder="Enter your full name" autoFocus />
)} {/* Step 2: Phone (only if WhatsApp enabled) */} {step === 'phone' && whatsappEnabled && ( <> Contact Information Optionally add your phone number for WhatsApp notifications
setPhoneNumber(value || '')} defaultCountry="MC" placeholder="Enter phone number" />

Select your country and enter your number for WhatsApp notifications

)} {/* Step 3: Tags */} {step === 'tags' && ( <> Your Expertise Select tags that describe your areas of expertise. This helps us match you with relevant projects.
)} {/* Step 4: Preferences */} {step === 'preferences' && ( <> Notification Preferences How would you like to receive notifications?
{whatsappEnabled && !phoneNumber && (

Add a phone number to enable WhatsApp notifications

)}

Summary

Name: {name}

{whatsappEnabled && phoneNumber && (

Phone:{' '} {phoneNumber}

)}

Expertise:{' '} {expertiseTags.length > 0 ? expertiseTags.join(', ') : 'None selected'}

)} {/* Step 5: Complete */} {step === 'complete' && (

Welcome, {name}!

Your profile is all set up. You'll be redirected to your dashboard shortly.

)}
) }