import React, { useEffect, useRef, useState } from 'react'; import { Camera, ExternalLink, Globe2, Instagram, Linkedin, Loader2, Save, UserRound, Youtube } from 'lucide-react'; import { useAuth } from '../context/AuthContext'; import { Profile, ProfileInput, instructorApi, profileApi } from '../services/api'; import { CourseCoverImage } from '../components/CourseCoverImage'; import { useToast } from '../context/ToastContext'; import { useNavigate } from 'react-router-dom'; const emptyProfile = (user: { id: string; email: string; name: string; role: string }): Profile => ({ id: user.id, email: user.email, name: user.name, role: user.role === 'superadmin' ? 'admin' : user.role === 'professor' ? 'instructor' : 'student', avatarImageUrl: null, headline: '', bio: '', websiteUrl: null, linkedinUrl: null, instagramUrl: null, youtubeUrl: null, isPublic: true, }); const inputClass = 'w-full rounded-xl border border-white/10 bg-zinc-900 px-4 py-3 text-sm text-white outline-none transition focus:border-orange-500/70 focus:ring-2 focus:ring-orange-500/15'; export const ProfilePage: React.FC = () => { const { user, updateCurrentUser } = useAuth(); const { showToast } = useToast(); const navigate = useNavigate(); const fileInput = useRef(null); const [profile, setProfile] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); const [isUploading, setIsUploading] = useState(false); const canPublishProfile = user?.role === 'professor' || user?.role === 'superadmin'; useEffect(() => { if (!user) return; profileApi.me() .then((response) => setProfile(response.data)) .catch(() => { setProfile(emptyProfile(user)); showToast('Não foi possível carregar todos os dados do perfil.', 'error'); }) .finally(() => setIsLoading(false)); }, [showToast, user]); if (isLoading || !profile || !user) { return
; } const update = (key: K, value: Profile[K]) => setProfile((current) => current ? { ...current, [key]: value } : current); const uploadAvatar = async (file?: File) => { if (!file) return; if (!file.type.startsWith('image/')) { showToast('Selecione uma imagem JPG, PNG ou WebP.', 'error'); return; } setIsUploading(true); try { const response = await instructorApi.uploadCover(file); update('avatarImageUrl', response.data.coverImageUrl); showToast('Foto enviada. Salve o perfil para publicar a alteração.', 'success'); } catch { showToast('Não foi possível enviar a foto.', 'error'); } finally { setIsUploading(false); } }; const save = async (event: React.FormEvent) => { event.preventDefault(); const input: ProfileInput = { name: profile.name, avatarImageUrl: profile.avatarImageUrl, headline: profile.headline, bio: profile.bio, websiteUrl: profile.websiteUrl, linkedinUrl: profile.linkedinUrl, instagramUrl: profile.instagramUrl, youtubeUrl: profile.youtubeUrl, isPublic: profile.isPublic, }; setIsSaving(true); try { const response = await profileApi.update(input); setProfile(response.data); updateCurrentUser({ id: user.id, email: user.email, name: response.data.name, role: response.data.role }); showToast('Perfil salvo com sucesso.', 'success'); } catch { showToast('Não foi possível salvar o perfil. Verifique os links informados.', 'error'); } finally { setIsSaving(false); } }; return (

Sua conta

Meu perfil

{canPublishProfile ? 'Apresente sua experiência e conecte os alunos aos seus cursos.' : 'Mantenha seus dados pessoais atualizados.'}

{canPublishProfile && profile.isPublic && }
{profile.avatarImageUrl ? :
} {canPublishProfile && }

{profile.name}

{user.email}

{canPublishProfile &&

A foto e a apresentação ficam visíveis no seu perfil público.

}
{ void uploadAvatar(event.target.files?.[0]); event.currentTarget.value = ''; }} />
{canPublishProfile && <>