import React, { useState, useEffect, useCallback } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { useAuth } from '@/contexts/AuthContext'; import { usePezkuwi } from '@/contexts/PezkuwiContext'; import { supabase } from '@/lib/supabase'; import { LanguageSwitcher } from './LanguageSwitcher'; import { PezkuwiWalletButton } from './PezkuwiWalletButton'; import NotificationBell from './notifications/NotificationBell'; import { fetchUserTikis, getPrimaryRole, getTikiDisplayName, getTikiEmoji } from '@pezkuwi/lib/tiki'; import { getAllScores, type UserScores } from '@pezkuwi/lib/scores'; import { getKycStatus } from '@pezkuwi/lib/kyc'; // Avatar pool (same as mobile) const AVATAR_POOL = [ { id: 'avatar1', emoji: 'πŸ‘¨πŸ»' }, { id: 'avatar2', emoji: 'πŸ‘¨πŸΌ' }, { id: 'avatar3', emoji: 'πŸ‘¨πŸ½' }, { id: 'avatar4', emoji: 'πŸ‘¨πŸΎ' }, { id: 'avatar5', emoji: 'πŸ‘©πŸ»' }, { id: 'avatar6', emoji: 'πŸ‘©πŸΌ' }, { id: 'avatar7', emoji: 'πŸ‘©πŸ½' }, { id: 'avatar8', emoji: 'πŸ‘©πŸΎ' }, { id: 'avatar9', emoji: 'πŸ§”πŸ»' }, { id: 'avatar10', emoji: 'πŸ§”πŸΌ' }, { id: 'avatar11', emoji: 'πŸ§”πŸ½' }, { id: 'avatar12', emoji: 'πŸ§”πŸΎ' }, { id: 'avatar13', emoji: 'πŸ‘³πŸ»β€β™‚οΈ' }, { id: 'avatar14', emoji: 'πŸ‘³πŸΌβ€β™‚οΈ' }, { id: 'avatar15', emoji: 'πŸ‘³πŸ½β€β™‚οΈ' }, { id: 'avatar16', emoji: 'πŸ§•πŸ»' }, { id: 'avatar17', emoji: 'πŸ§•πŸΌ' }, { id: 'avatar18', emoji: 'πŸ§•πŸ½' }, ]; const getEmojiFromAvatarId = (avatarId: string): string => { const avatar = AVATAR_POOL.find(a => a.id === avatarId); return avatar ? avatar.emoji : 'πŸ‘€'; }; // App icon definition interface AppItem { title: string; icon: string; route: string; comingSoon?: boolean; requiresAuth?: boolean; } // Section definition interface AppSection { titleKey: string; emoji: string; borderColor: string; apps: AppItem[]; } const APP_SECTIONS: AppSection[] = [ { titleKey: 'mobile.section.finance', emoji: 'πŸ’°', borderColor: 'border-l-green-500', apps: [ { title: 'mobile.app.wallet', icon: 'πŸ‘›', route: '/wallet' }, { title: 'mobile.app.bank', icon: '🏦', route: '/wallet', comingSoon: true }, { title: 'mobile.app.exchange', icon: 'πŸ’±', route: '/dex', requiresAuth: true }, { title: 'mobile.app.p2p', icon: '🀝', route: '/p2p', requiresAuth: true }, { title: 'mobile.app.b2b', icon: 'πŸ€–', route: '/bereketli', requiresAuth: true }, { title: 'mobile.app.bacZekat', icon: 'πŸ’°', route: '/wallet', comingSoon: true }, { title: 'mobile.app.launchpad', icon: 'πŸš€', route: '/launchpad' }, ], }, { titleKey: 'mobile.section.governance', emoji: 'πŸ›οΈ', borderColor: 'border-l-red-500', apps: [ { title: 'mobile.app.president', icon: 'πŸ‘‘', route: '/elections', requiresAuth: true }, { title: 'mobile.app.assembly', icon: 'πŸ›οΈ', route: '/citizens/government', comingSoon: true }, { title: 'mobile.app.vote', icon: 'πŸ—³οΈ', route: '/elections', requiresAuth: true }, { title: 'mobile.app.validators', icon: 'πŸ›‘οΈ', route: '/wallet' }, { title: 'mobile.app.justice', icon: 'βš–οΈ', route: '/citizens/government', comingSoon: true }, { title: 'mobile.app.proposals', icon: 'πŸ“œ', route: '/citizens/government' }, { title: 'mobile.app.polls', icon: 'πŸ“Š', route: '/citizens/government', comingSoon: true }, { title: 'mobile.app.identity', icon: 'πŸ†”', route: '/identity' }, ], }, { titleKey: 'mobile.section.social', emoji: 'πŸ’¬', borderColor: 'border-l-blue-500', apps: [ { title: 'mobile.app.whatsKurd', icon: 'πŸ’¬', route: '/message', comingSoon: true }, { title: 'mobile.app.forum', icon: 'πŸ“°', route: '/forum' }, { title: 'mobile.app.kurdMedia', icon: 'πŸ“Ί', route: '/forum', comingSoon: true }, { title: 'mobile.app.events', icon: 'πŸ“…', route: '/forum', comingSoon: true }, { title: 'mobile.app.help', icon: '❓', route: '/docs' }, { title: 'mobile.app.music', icon: '🎡', route: '/forum', comingSoon: true }, { title: 'mobile.app.vpn', icon: 'πŸ›‘οΈ', route: '/forum', comingSoon: true }, { title: 'mobile.app.referral', icon: 'πŸ‘₯', route: '/dashboard', requiresAuth: true }, ], }, { titleKey: 'mobile.section.education', emoji: 'πŸ“š', borderColor: 'border-l-yellow-500', apps: [ { title: 'mobile.app.university', icon: 'πŸŽ“', route: '/education', comingSoon: true }, { title: 'mobile.app.perwerde', icon: 'πŸ“–', route: '/education', requiresAuth: true }, { title: 'mobile.app.certificates', icon: 'πŸ†', route: '/education', comingSoon: true }, { title: 'mobile.app.research', icon: 'πŸ”¬', route: '/education', comingSoon: true }, ], }, ]; const MobileHomeLayout: React.FC = () => { const navigate = useNavigate(); const location = useLocation(); const { t } = useTranslation(); const { user } = useAuth(); const { peopleApi, isPeopleReady, selectedAccount } = usePezkuwi(); // Profile state const [profileData, setProfileData] = useState<{ full_name?: string | null; avatar_url?: string | null; created_at?: string; } | null>(null); // Blockchain state const [tikis, setTikis] = useState([]); const [scores, setScores] = useState({ trustScore: 0, referralScore: 0, stakingScore: 0, tikiScore: 0, totalScore: 0, }); const [kycStatus, setKycStatus] = useState('NotStarted'); const [loadingScores, setLoadingScores] = useState(false); const fetchProfile = useCallback(async () => { if (!user) return; try { const { data } = await supabase .from('profiles') .select('*') .eq('id', user.id) .maybeSingle(); if (data) setProfileData(data); } catch { /* profile fetch is best-effort */ } }, [user]); const fetchBlockchainData = useCallback(async () => { if (!selectedAccount || !peopleApi || !isPeopleReady) return; setLoadingScores(true); try { const [userTikis, allScores, status] = await Promise.all([ fetchUserTikis(peopleApi, selectedAccount.address), getAllScores(peopleApi, selectedAccount.address), getKycStatus(peopleApi, selectedAccount.address), ]); setTikis(userTikis); setScores(allScores); setKycStatus(status); } catch { /* blockchain fetch is best-effort */ } finally { setLoadingScores(false); } }, [selectedAccount, peopleApi, isPeopleReady]); useEffect(() => { fetchProfile(); }, [fetchProfile]); useEffect(() => { if (selectedAccount && peopleApi && isPeopleReady) fetchBlockchainData(); }, [fetchBlockchainData, selectedAccount, peopleApi, isPeopleReady]); const primaryRole = tikis.length > 0 ? getPrimaryRole(tikis) : 'Visitor'; const displayName = profileData?.full_name || user?.email?.split('@')[0] || 'Heval'; const memberSince = profileData?.created_at ? new Date(profileData.created_at).toLocaleDateString('en-US', { month: 'short', year: 'numeric' }) : user?.created_at ? new Date(user.created_at).toLocaleDateString('en-US', { month: 'short', year: 'numeric' }) : 'N/A'; const avatarEmoji = profileData?.avatar_url && !profileData.avatar_url.startsWith('http') ? getEmojiFromAvatarId(profileData.avatar_url) : 'πŸ‘€'; const currentTab = location.pathname === '/be-citizen' ? 'citizen' : location.pathname === '/dashboard' ? 'referral' : 'home'; return (
{/* ── HEADER ── */}
{/* Left: Avatar + Greeting */}
{profileData?.avatar_url?.startsWith('http') ? ( avatar ) : (
{avatarEmoji}
)} {/* Online dot */}

{t('mobile.greeting', 'Rojbaş')}, {displayName}

{getTikiEmoji(primaryRole)} {getTikiDisplayName(primaryRole)}
{/* Right: Actions */}
{/* ── SCROLLABLE CONTENT ── */}
{/* ── SCORE CARDS (horizontal scroll) ── */}
{/* Card 1: Member Since OR Login/Sign Up */} {user ? ( ) : ( navigate('/login') }} /> )} {/* Role - always visible, shows Visitor for guests */} {/* Total Score */} navigate('/login') } : undefined} /> {/* Trust Score */} navigate('/login') } : undefined} /> {/* Referral Score */} navigate('/login') } : undefined} /> {/* Staking Score */} navigate('/login') } : undefined} /> {/* Tiki Score */} navigate('/login') } : undefined} /> {/* KYC Status */} navigate('/login') } : kycStatus === 'NotStarted' ? { label: t('mobile.apply', 'Apply'), onClick: () => navigate('/be-citizen') } : undefined} />
{/* ── APP SECTIONS ── */} {APP_SECTIONS.map((section) => (
{/* Section header */}

{t(section.titleKey)} {section.emoji}

{/* App grid - 4 per row */}
{section.apps.map((app) => { const needsLogin = app.requiresAuth && !user; return ( ); })}
))} {/* Bottom spacing for tab bar */}
{/* ── BOTTOM TAB BAR ── */}
navigate('/')} /> navigate('/be-citizen')} accent /> navigate(user ? '/dashboard' : '/login')} />
); }; // ── Sub-components ── function ScoreCard({ icon, label, value, sub, color, action }: { icon: string; label: string; value: string; sub?: string; color: string; action?: { label: string; onClick: () => void }; }) { return (
{icon}

{label}

{value}

{sub &&

{sub}

} {action && ( )}
); } function TabButton({ icon, label, active, onClick, accent }: { icon: string; label: string; active: boolean; onClick: () => void; accent?: boolean; }) { return ( ); } export default MobileHomeLayout;