import React, { useState } from 'react'; import { useAuth } from '../../context/AuthContext'; import { useBlockchain } from '../../context/BlockchainContext'; import { useNotifications } from '../../context/NotificationContext'; import { unlinkAccount } from '../../lib/auth/blockchainLinking'; interface ProfileProps { onLogout?: () => void; onOpenVaultBrowser?: () => void; } export const Profile: React.FC = ({ onLogout, onOpenVaultBrowser }) => { const { session, updateSession, clearSession } = useAuth(); const { wallet, linkedAccount, isConnecting, connect, linkWebCryptoAccount, disconnect } = useBlockchain(); const { addNotification } = useNotifications(); const [vaultPath, setVaultPath] = useState(session.obsidianVaultPath || ''); const [isEditingVault, setIsEditingVault] = useState(false); const [isLinking, setIsLinking] = useState(false); const handleVaultPathChange = (e: React.ChangeEvent) => { setVaultPath(e.target.value); }; const handleSaveVaultPath = () => { updateSession({ obsidianVaultPath: vaultPath }); setIsEditingVault(false); }; const handleCancelVaultEdit = () => { setVaultPath(session.obsidianVaultPath || ''); setIsEditingVault(false); }; const handleDisconnectVault = () => { setVaultPath(''); updateSession({ obsidianVaultPath: undefined, obsidianVaultName: undefined }); setIsEditingVault(false); console.log('🔧 Vault disconnected from profile'); }; const handleChangeVault = () => { if (onOpenVaultBrowser) { onOpenVaultBrowser(); } }; const handleConnectWallet = async () => { try { await connect(); addNotification('Wallet connected successfully', 'success'); } catch (error: any) { addNotification(error.message || 'Failed to connect wallet', 'error'); } }; const handleLinkAccount = async () => { if (!wallet) { addNotification('Please connect your wallet first', 'error'); return; } setIsLinking(true); try { const result = await linkWebCryptoAccount(session.username); if (result.success) { addNotification('Account linked successfully!', 'success'); } else { addNotification(result.error || 'Failed to link account', 'error'); } } catch (error: any) { addNotification(error.message || 'Failed to link account', 'error'); } finally { setIsLinking(false); } }; const handleUnlinkAccount = () => { if (unlinkAccount(session.username)) { disconnect(); addNotification('Account unlinked successfully', 'success'); } else { addNotification('Failed to unlink account', 'error'); } }; const handleLogout = () => { // Clear the session clearSession(); // Update the auth context updateSession({ username: '', authed: false, backupCreated: null, }); // Call the onLogout callback if provided if (onLogout) onLogout(); }; if (!session.authed || !session.username) { return null; } return (

Welcome, {session.username}!

Obsidian Vault

{/* Current Vault Display */}
{session.obsidianVaultName ? (
Current Vault: {session.obsidianVaultName}
{session.obsidianVaultPath === 'folder-selected' ? 'Folder selected (path not available)' : session.obsidianVaultPath}
) : (
No Obsidian vault configured
)}
{/* Change Vault Button */}
{session.obsidianVaultPath && ( )}
{/* Advanced Settings (Collapsible) */}
Advanced Settings
{isEditingVault ? (
) : (
{session.obsidianVaultPath ? ( {session.obsidianVaultPath === 'folder-selected' ? 'Folder selected (path not available)' : session.obsidianVaultPath} ) : ( No vault configured )}
)}
{/* Blockchain Wallet Section */}

Blockchain Wallet

{/* Current Wallet Display */}
{wallet ? (
Connected Wallet: {wallet.address.slice(0, 6)}...{wallet.address.slice(-4)}
Chain ID: {wallet.chainId}
{linkedAccount && (
✓ Linked to Web Crypto account
)}
) : (
No wallet connected
)}
{/* Wallet Actions */}
{!wallet ? ( ) : ( <> {!linkedAccount ? ( ) : ( )} )}
{/* Linked Account Details */} {linkedAccount && (
Wallet Details

Ethereum Address:

{linkedAccount.ethereumAddress} {linkedAccount.proxyContractAddress && ( <>

Proxy Contract:

{linkedAccount.proxyContractAddress} )}

Linked on {new Date(linkedAccount.linkedAt).toLocaleDateString()}

)}
{!session.backupCreated && (

Remember to back up your encryption keys to prevent data loss!

)}
); };