"use client" import { useMemo, useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Textarea } from "@/components/ui/textarea" import { ScrollArea } from "@/components/ui/scroll-area" import { SelectDevice } from "@/components/ui/select-device" import { cn } from "@/lib/utils" import { getHealthUrl, config } from "@/lib/config" export function PromptSidePanel({ className, selectedDevice = 'desktop', onDeviceChange }: { className?: string selectedDevice?: 'desktop' | 'tablet' | 'mobile' onDeviceChange?: (device: 'desktop' | 'tablet' | 'mobile') => void }) { const [collapsed, setCollapsed] = useState(false) const [prompt, setPrompt] = useState( "Dashboard with header, left sidebar, 3 stats cards, a line chart and a data table, plus footer.", ) const [isGenerating, setIsGenerating] = useState(false) const [backendStatus, setBackendStatus] = useState<'connected' | 'disconnected' | 'checking'>('checking') const examples = useMemo( () => [ "Landing page with header, hero, 3x2 feature grid, and footer.", "Settings screen: header, list of toggles, and save button.", "E‑commerce product page: header, 2-column gallery/details, reviews, sticky add-to-cart.", "Dashboard: header, left sidebar, 3 stats cards, line chart, data table, footer.", "Signup page: header, 2-column form, callout, submit button.", "Admin panel: header, left navigation, main content area with data tables, and footer.", "Product catalog: header, search bar, filter sidebar, 4x3 product grid, pagination.", "Blog layout: header, featured post hero, 2-column article list, sidebar with categories.", ], [], ) // Check backend connection status useEffect(() => { const checkBackendStatus = async () => { try { setBackendStatus('checking') const response = await fetch(getHealthUrl(), { method: 'GET', headers: { 'Content-Type': 'application/json' }, }) if (response.ok) { setBackendStatus('connected') } else { setBackendStatus('disconnected') } } catch (error) { console.error('Backend health check failed:', error) setBackendStatus('disconnected') } } checkBackendStatus() // Check status every 10 seconds const interval = setInterval(checkBackendStatus, config.ui.statusCheckInterval) return () => clearInterval(interval) }, []) const dispatchGenerate = async (text: string) => { setIsGenerating(true) // Dispatch the event for the canvas to handle with device information window.dispatchEvent(new CustomEvent("tldraw:generate", { detail: { prompt: text, device: selectedDevice } })) // Wait a bit to show the loading state setTimeout(() => setIsGenerating(false), 1000) } const dispatchClear = () => { window.dispatchEvent(new Event("tldraw:clear")) } const handleDeviceChange = (device: 'desktop' | 'tablet' | 'mobile') => { console.log('DEBUG: PromptSidePanel handleDeviceChange called with:', device) console.log('DEBUG: Current selectedDevice prop:', selectedDevice) console.log('DEBUG: onDeviceChange function exists:', !!onDeviceChange) if (onDeviceChange) { console.log('DEBUG: Calling onDeviceChange with:', device) onDeviceChange(device) } else { console.warn('DEBUG: onDeviceChange function not provided') } } const getBackendStatusIcon = () => { switch (backendStatus) { case 'connected': return '🟢' case 'disconnected': return '🔴' case 'checking': return '🟡' default: return '⚪' } } const getBackendStatusText = () => { switch (backendStatus) { case 'connected': return 'AI Backend Connected' case 'disconnected': return 'AI Backend Disconnected' case 'checking': return 'Checking Backend...' default: return 'Unknown Status' } } return (