import EllipsisVerticalIcon from '@heroicons/react/16/solid/EllipsisVerticalIcon' import { LatLng } from 'leaflet' import { Link, useNavigate } from 'react-router-dom' import { toast } from 'react-toastify' import { useAuth } from '#components/Auth/useAuth' import { useMyProfile } from '#components/Map/hooks/useMyProfile' import { usePopupForm } from '#components/Map/hooks/usePopupForm' import { useAppState } from './hooks/useAppState' import type { Item } from '#types/Item' export const UserControl = () => { const { isAuthenticated, user, logout } = useAuth() const appState = useAppState() const { myProfile } = useMyProfile() const navigate = useNavigate() const { setPopupForm } = usePopupForm() // Use myProfile or create a fallback object for display const userProfile: Partial = myProfile ?? { id: 'new', name: user?.first_name ?? '', text: '', } const onLogout = async () => { await toast.promise(logout(), { success: { render() { return 'Bye bye' }, // other options icon: '👋', }, error: { render({ data }) { return JSON.stringify(data) }, }, pending: 'logging out ..', }) } const handleEdit = () => { if (!myProfile?.layer) { void navigate(userProfile.id ? `/edit-item/${userProfile.id}` : '#') return } if (myProfile.layer.itemType.small_form_edit && myProfile.position) { void navigate('/') // Wait for navigation to complete before setting popup setTimeout(() => { if (myProfile.position && myProfile.layer) { setPopupForm({ position: new LatLng( myProfile.position.coordinates[1], myProfile.position.coordinates[0], ), layer: myProfile.layer, item: myProfile, }) } }, 100) } else { void navigate(userProfile.id ? `/edit-item/${userProfile.id}` : '#') } } const avatar: string | undefined = userProfile.image && appState.assetsApi.url ? appState.assetsApi.url + userProfile.image : userProfile.image_external return ( <> {isAuthenticated ? (
{avatar && (
User avatar
)}
{userProfile.name ?? user?.first_name}
) : (
Login
{!appState.hideSignup && (
Sign Up
)}
  • Login
  • {!appState.hideSignup && (
  • Sign Up
  • )}
)} ) }