import React, { useState } from 'react' import { useNavigate } from 'react-router-dom' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faChevronRight, faRightFromBracket, faRightToBracket, faUserCircle, faGear, faHome, faPlus, faNewspaper, faTimes, faBolt } from '@fortawesome/free-solid-svg-icons' import { Hooks } from 'applesauce-react' import { useEventModel } from 'applesauce-react/hooks' import { Models } from 'applesauce-core' import { Accounts } from 'applesauce-accounts' import { RelayPool } from 'applesauce-relay' import IconButton from './IconButton' import AddBookmarkModal from './AddBookmarkModal' import { createWebBookmark } from '../services/webBookmarkService' import { RELAYS } from '../config/relays' interface SidebarHeaderProps { onToggleCollapse: () => void onLogout: () => void onOpenSettings: () => void relayPool: RelayPool | null isMobile?: boolean } const SidebarHeader: React.FC = ({ onToggleCollapse, onLogout, onOpenSettings, relayPool, isMobile = false }) => { const [isConnecting, setIsConnecting] = useState(false) const [showAddModal, setShowAddModal] = useState(false) const navigate = useNavigate() const activeAccount = Hooks.useActiveAccount() const accountManager = Hooks.useAccountManager() const profile = useEventModel(Models.ProfileModel, activeAccount ? [activeAccount.pubkey] : null) const handleLogin = async () => { try { setIsConnecting(true) const account = await Accounts.ExtensionAccount.fromExtension() accountManager.addAccount(account) accountManager.setActive(account) } catch (error) { console.error('Login failed:', error) alert('Login failed. Please install a nostr browser extension and try again.\n\nIf you aren\'t on nostr yet, start here: https://nstart.me/') } finally { setIsConnecting(false) } } const getProfileImage = () => { return profile?.picture || null } const getUserDisplayName = () => { if (!activeAccount) return 'Unknown User' if (profile?.name) return profile.name if (profile?.display_name) return profile.display_name if (profile?.nip05) return profile.nip05 return `${activeAccount.pubkey.slice(0, 8)}...${activeAccount.pubkey.slice(-8)}` } const handleSaveBookmark = async (url: string, title?: string, description?: string, tags?: string[]) => { if (!activeAccount || !relayPool) { throw new Error('Please login to create bookmarks') } await createWebBookmark(url, title, description, tags, activeAccount, relayPool, RELAYS) } const profileImage = getProfileImage() return ( <>
{isMobile ? ( ) : ( )}
navigate('/me') : (isConnecting ? () => {} : handleLogin) } style={{ cursor: 'pointer' }} > {profileImage ? ( {getUserDisplayName()} ) : ( )}
navigate('/')} title="Home" ariaLabel="Home" variant="ghost" /> navigate('/explore')} title="Explore" ariaLabel="Explore" variant="ghost" /> navigate('/support')} title="Support" ariaLabel="Support" variant="ghost" /> {activeAccount && ( setShowAddModal(true)} title="Add bookmark" ariaLabel="Add bookmark" variant="ghost" /> )} {activeAccount ? ( ) : ( {} : handleLogin} title={isConnecting ? "Connecting..." : "Login"} ariaLabel="Login" variant="ghost" /> )}
{showAddModal && ( setShowAddModal(false)} onSave={handleSaveBookmark} /> )} ) } export default SidebarHeader