'use client' import { useCallback, useEffect, useState } from 'react' import { ExternalLink, ShoppingCart, Loader2, AlertCircle } from 'lucide-react' import { Button } from '@/components/ui/button' import { getTopPartsForManual, type PartForPage } from '@/lib/parts-lookup' interface PartsPanelProps { manualFilename: string className?: string } export function PartsPanel({ manualFilename, className = '' }: PartsPanelProps) { const [parts, setParts] = useState([]) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) const loadParts = useCallback(async () => { setIsLoading(true) setError(null) try { const result = await getTopPartsForManual(manualFilename, 5) setParts(result.parts) setError(result.error ?? null) } catch (err) { console.error('Error loading parts:', err) setParts([]) setError('Could not load parts') } finally { setIsLoading(false) } }, [manualFilename]) useEffect(() => { if (manualFilename) { void loadParts() } }, [loadParts, manualFilename]) const hasListings = parts.some((part) => part.ebayListings.length > 0) const renderStatusCard = (title: string, message: string) => (
Parts

{title}

{message}

) if (isLoading) { return (
Parts
Loading parts...
) } if (error && !hasListings) { const loweredError = error.toLowerCase() const statusMessage = error.includes('eBay API not configured') ? 'Set EBAY_APP_ID in the app environment so live listings can load.' : loweredError.includes('rate limit') || loweredError.includes('exceeded') ? 'eBay is temporarily rate-limited. Try again in a minute.' : error return renderStatusCard( 'eBay unavailable', statusMessage, ) } if (parts.length === 0) { return (
Parts
No parts data extracted for this manual yet
) } if (!hasListings) { return (
Parts
No live eBay matches found for these parts yet
) } return (
Parts ({parts.length})
{error && (

Live eBay listings are unavailable right now.

{error.includes('eBay API not configured') ? 'Set EBAY_APP_ID in the app environment, then reload the panel.' : error.toLowerCase().includes('rate limit') || error.toLowerCase().includes('exceeded') ? 'eBay is temporarily rate-limited. Reload after a short wait.' : error}

)} {parts.map((part, index) => (
{/* Part Header */}
{part.partNumber}
{part.description && (
{part.description}
)}
{/* eBay Listings */} {part.ebayListings.length > 0 && ( )} {/* View All Listings Button */} {part.ebayListings.length > 2 && (
)}
))} {/* View All Parts Button */} {parts.length > 3 && (
)}
) }