From 9608fe9df367095ec559915774f72b8b3d16f7a4 Mon Sep 17 00:00:00 2001 From: Muhammad Zayyad Mukhtar <95658387+El-swaggerito@users.noreply.github.com> Date: Wed, 29 Apr 2026 11:45:51 +0100 Subject: [PATCH 1/2] refactor: enhance code documentation and component consistency - Add comprehensive JSDoc comments to all major components and utilities - Improve UI component variants and accessibility attributes - Standardize error handling and logging across API routes - Enhance type safety in Stellar integration modules - Update formatting utilities with additional helper functions --- src/app/api/pnl/route.ts | 26 ++++- src/app/api/upload/route.ts | 24 +++- src/app/page.tsx | 68 +++++++++-- src/components/SwapInterface.tsx | 110 +++++++++++++----- src/components/ui/Button.tsx | 49 +++++++- src/components/ui/NetworkFeeIndicator.tsx | 77 ++++++++++--- src/lib/format.ts | 78 +++++++++---- src/lib/parser.ts | 75 ++++++++----- src/lib/stellar.ts | 131 ++++++++++++++++------ src/stores/tokenStore.ts | 79 +++++++++++-- src/stores/useWeb3Store.ts | 66 +++++++++-- 11 files changed, 617 insertions(+), 166 deletions(-) diff --git a/src/app/api/pnl/route.ts b/src/app/api/pnl/route.ts index f57e640..e89b3a7 100644 --- a/src/app/api/pnl/route.ts +++ b/src/app/api/pnl/route.ts @@ -1,21 +1,39 @@ +/** + * Profit and Loss (PnL) API Route. + * Generates synthetic historical performance data for the dashboard charts. + * This is used for demonstrating portfolio tracking features. + */ + import { NextResponse } from 'next/server'; +/** + * Historical data point for the PnL chart. + */ interface PnLData { + /** Localized date string (e.g., "Jan 12") */ date: string; + /** The portfolio value at that specific point in time */ value: number; } +/** + * GET handler for the PnL endpoint. + * Returns a 30-day series of simulated portfolio values. + */ export async function GET() { - // Generate dummy PnL data for the last 30 days + // Generate mock PnL data for the last 30 days const data: PnLData[] = []; const today = new Date(); - let currentValue = 10000; // Starting value + + // Starting seed value for the simulation + let currentValue = 10000; for (let i = 29; i >= 0; i--) { const date = new Date(today); date.setDate(date.getDate() - i); - // Random walk with slight upward trend + // Simulate a random walk with a slight positive bias (0.45 instead of 0.50) + // and a volatility factor of 200 const change = (Math.random() - 0.45) * 200; currentValue += change; @@ -25,5 +43,7 @@ export async function GET() { }); } + // Return the series as a JSON response return NextResponse.json(data); } + diff --git a/src/app/api/upload/route.ts b/src/app/api/upload/route.ts index 94c21e9..4380ead 100644 --- a/src/app/api/upload/route.ts +++ b/src/app/api/upload/route.ts @@ -1,8 +1,30 @@ +/** + * Document Upload API Route. + * Handles the secure uploading of invoice documents to IPFS/Pinata. + * Currently disabled for maintenance or pending further security implementation. + */ + import { NextRequest, NextResponse } from 'next/server'; +/** + * POST handler for the upload endpoint. + * Currently returns a 503 Service Unavailable error as the feature is locked. + * + * @param {NextRequest} request - The incoming upload request. + */ export async function POST(request: NextRequest) { + // 1. Log the attempt for security auditing + const clientIp = request.headers.get('x-forwarded-for') || 'unknown'; + console.log(`[UploadAPI] Blocked upload attempt from ${clientIp}`); + + // 2. Return a consistent error response return NextResponse.json( - { error: 'Upload temporarily disabled' }, + { + error: 'Upload service temporarily disabled', + reason: 'Undergoing maintenance', + retryAfter: 3600 + }, { status: 503 } ); } + diff --git a/src/app/page.tsx b/src/app/page.tsx index 16537de..02b443e 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,3 +1,9 @@ +/** + * TradeFlow Main Dashboard Page. + * This is the primary entry point for the application, providing users with + * a high-level overview of their assets, protocol status, and the RWA pipeline. + */ + "use client"; import React, { useState, useEffect } from "react"; @@ -19,63 +25,103 @@ import TabNavigation from "../components/TabNavigation"; import { useWatchlist } from "../hooks/useWatchlist"; import StarIcon from "../components/StarIcon"; +/** + * The root component for the TradeFlow dashboard. + * Manages high-level state for wallet connection, active tabs, and invoice data. + */ export default function Page() { + // --- Component State --- + /** The public Stellar address of the connected user */ const [address, setAddress] = useState(""); + /** List of invoices fetched from the backend pipeline */ const [invoices, setInvoices] = useState([]); + /** Loading state for initial data fetch */ const [loading, setLoading] = useState(false); + /** Controls visibility of the Invoice Minting modal */ const [showMintForm, setShowMintForm] = useState(false); + /** Controls visibility of the Wallet Selection modal */ const [isModalOpen, setIsModalOpen] = useState(false); + /** Currently active navigation tab (dashboard or watchlist) */ const [activeTab, setActiveTab] = useState("dashboard"); + + /** Watchlist management hook */ const { toggleWatchlist, isInWatchlist } = useWatchlist(); - // 1. Connect Stellar Wallet (supports Freighter, Albedo, xBull) + // --- Handlers --- + + /** + * Triggers the Stellar wallet connection flow. + * Supports multiple providers via the stellar-wallets-kit. + * + * @param {WalletType} walletType - The ID of the wallet provider (e.g., Freighter). + */ const handleConnectWallet = async (walletType: WalletType) => { try { const userInfo = await connectWallet(walletType); if (userInfo && userInfo.publicKey) { setAddress(userInfo.publicKey); - console.log("Wallet connected:", userInfo.publicKey, "Type:", userInfo.walletType); + console.log("[Dashboard] Wallet connected:", userInfo.publicKey, "Provider:", userInfo.walletType); } } catch (e: any) { - console.error("Connection failed:", e.message); + console.error("[Dashboard] Connection failed:", e.message); + // In production, this would be a user-friendly toast notification alert(e.message || "Failed to connect to wallet."); } }; - // 2. Fetch Invoices from your Repo 2 API + /** + * Fetches the latest verified assets from the RWA pipeline. + * Currently points to a local mock API for development. + */ const fetchInvoices = async () => { setLoading(true); try { + // TODO: Replace with environment-aware API base URL const res = await fetch("http://localhost:3000/invoices"); const data = await res.json(); setInvoices(data); } catch (e) { - console.error("API not running"); + console.warn("[Dashboard] Asset pipeline API not reachable. Check if local server is running."); } finally { setLoading(false); } }; + // --- Lifecycle Hooks --- + useEffect(() => { fetchInvoices(); }, []); + + /** + * Debugging utility for testing transaction status notifications. + */ const handleTestToast = () => { - useTransactionToast().loading(); - useTransactionToast().success(); - useTransactionToast().error(); + const toast = useTransactionToast(); + toast.loading(); + setTimeout(() => toast.success(), 2000); }; + /** + * Callback triggered when a new invoice is successfully submitted for minting. + * + * @param {any} data - The validated invoice metadata. + */ const handleInvoiceMint = (data: any) => { - console.log("Invoice data received:", data); + console.log("[Dashboard] Mint request received:", data); setShowMintForm(false); - // TODO: Chain integration will be handled separately + // TODO: Initiate Soroban contract call for minting the NFT }; + // --- Configuration --- + + /** Tab definitions for the main navigation */ const tabs = [ { id: "dashboard", label: "Dashboard" }, - { id: "watchlist", label: "Watchlist", icon: }, + { id: "watchlist", label: "Watchlist", icon: