Skip to content

Latest commit

 

History

History
182 lines (148 loc) · 5.12 KB

File metadata and controls

182 lines (148 loc) · 5.12 KB

NetworkSelector Component

Overview

The NetworkSelector component is a custom dropdown for network selection in the TradeFlow Web application. It supports switching between Stellar Mainnet and Stellar Testnet, providing a professional multi-chain interface for future expansion.

Features Implemented

✅ Core Requirements

  • Custom Dropdown Component: Built as NetworkSelector.tsx with full dropdown functionality
  • Stellar Branding: Features a custom Stellar logo next to network names
  • Network Options: Supports "Stellar Mainnet" and "Stellar Testnet"
  • React State Management: Uses useState to track the currently selected network
  • Testnet Warning Badge: Displays a yellow warning badge when Testnet is selected

✅ Additional Features

  • Click Outside to Close: Dropdown closes when clicking outside the component
  • Hover Effects: Smooth transitions and hover states for better UX
  • Responsive Design: Works well on different screen sizes
  • Accessibility: Proper ARIA labels and keyboard navigation support
  • Selected State Indicator: Visual indicator for the currently selected network

Component Structure

Props

interface NetworkSelectorProps {
  onNetworkChange?: (network: Network) => void;
}

Network Type

export type Network = "mainnet" | "testnet";

Integration

Location

  • Component: src/components/NetworkSelector.tsx
  • Integration: Added to Navbar.tsx next to the wallet button

Usage

import NetworkSelector, { Network } from "./src/components/NetworkSelector";

// In your component
<NetworkSelector 
  onNetworkChange={(network) => {
    console.log("Network changed to:", network);
    // Handle network change logic
  }}
/>

Visual Design

Stellar Logo

  • Custom gradient design (blue to purple)
  • Circular shape with white center dot
  • Consistent with Stellar branding

Testnet Warning Badge

  • Yellow background with dark text
  • Alert triangle icon
  • Positioned at top-right of the selector
  • Only visible when testnet is selected

Dropdown Styling

  • Dark theme matching the app design
  • Slate color palette
  • Smooth transitions and hover effects
  • Z-index management for proper layering

Technical Implementation

State Management

const [selectedNetwork, setSelectedNetwork] = useState<Network>("mainnet");
const [isOpen, setIsOpen] = useState(false);

Click Outside Handler

useEffect(() => {
  const handleClickOutside = (event: MouseEvent) => {
    if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
      setIsOpen(false);
    }
  };
  // Event listener setup and cleanup
}, []);

Network Data Structure

const networks = [
  {
    id: "mainnet" as Network,
    name: "Stellar Mainnet",
    description: "Production network"
  },
  {
    id: "testnet" as Network,
    name: "Stellar Testnet", 
    description: "Development network"
  }
];

Testing

Test File

A standalone test file has been created: test-network-selector.html

  • Opens in browser to test component functionality
  • Demonstrates all features without requiring the full app setup
  • Shows real-time network selection changes

Manual Testing Checklist

  • Dropdown opens and closes correctly
  • Network selection updates state
  • Testnet warning badge appears/disappears
  • Click outside closes dropdown
  • Hover effects work properly
  • Responsive design on different screen sizes

Future Enhancements

Multi-Chain Support

The component is designed for easy expansion to support additional networks:

// Easy to add new networks
const networks = [
  // Existing networks...
  {
    id: "ethereum" as Network,
    name: "Ethereum Mainnet",
    description: "Ethereum network"
  },
  {
    id: "polygon" as Network,
    name: "Polygon",
    description: "Polygon network"
  }
];

Potential Improvements

  1. Network Status Indicators: Show network health/status
  2. Custom Networks: Allow users to add custom RPC endpoints
  3. Network Switching Confirmation: Add confirmation modal for network changes
  4. Network-Specific Features: Show/hide features based on selected network
  5. Persistence: Save network preference to localStorage

Files Modified/Created

New Files

  • src/components/NetworkSelector.tsx - Main component
  • test-network-selector.html - Standalone test file
  • NetworkSelector-README.md - This documentation

Modified Files

  • Navbar.tsx - Integrated NetworkSelector next to wallet button

Dependencies

The component uses:

  • react and react-dom - Core React functionality
  • lucide-react - Icons (ChevronDown, AlertTriangle)
  • Tailwind CSS - Styling

All dependencies are already included in the project's package.json.

Conclusion

The NetworkSelector component successfully fulfills all requirements:

  • ✅ Custom dropdown with network selection
  • ✅ Stellar logo integration
  • ✅ React state management
  • ✅ Testnet warning badge
  • ✅ Professional multi-chain interface ready for future expansion

The component is production-ready and follows the existing codebase patterns and styling conventions.