"use client" import React from "react" import { useEffect, useRef, useState } from "react" import { X, SquareTerminal } from "lucide-react" import { cn } from "../lib/utils" interface CliDrawerProps { // This would be your actual terminal component TerminalComponent?: React.ComponentType } const DRAWER_OPEN_KEY = "ablyCliDrawerOpen"; const DRAWER_HEIGHT_KEY = "ablyCliDrawerHeight"; export function CliDrawer({ TerminalComponent }: CliDrawerProps) { // Initialize state directly to false const [isOpen, setIsOpen] = useState(() => { try { const savedOpen = localStorage.getItem(DRAWER_OPEN_KEY); // Ensure parsing happens correctly and default to false on error or null return savedOpen ? !!JSON.parse(savedOpen) : false; } catch (error) { console.error("Error reading drawer open state from localStorage:", error); return false; // Default to closed on error } }); // Use state for height, load initial value below const [height, setHeight] = useState(0); const [isDragging, setIsDragging] = useState(false) const [startY, setStartY] = useState(0) const [startHeight, setStartHeight] = useState(0) const drawerRef = useRef(null) const dragHandleRef = useRef(null) // Restore useEffect for loading height from localStorage, default to 40% useEffect(() => { const savedHeight = localStorage.getItem(DRAWER_HEIGHT_KEY); const minTopMargin = 30; // 30px from top const maxHeight = window.innerHeight - minTopMargin; const defaultHeight = Math.min(window.innerHeight * 0.4, maxHeight); // Default 40% vh but respect max const initialHeight = savedHeight ? Math.min(Number.parseInt(savedHeight), maxHeight) : defaultHeight; setHeight(initialHeight); }, []); // Restore useEffect for saving height to localStorage useEffect(() => { if (height > 0) { // Save height whenever it's valid, not just when closed localStorage.setItem(DRAWER_HEIGHT_KEY, height.toString()); } }, [height]); // Add useEffect for saving open state to localStorage useEffect(() => { localStorage.setItem(DRAWER_OPEN_KEY, JSON.stringify(isOpen)); }, [isOpen]); // Handle viewport resize to ensure drawer respects minimum top margin useEffect(() => { const handleResize = () => { const minTopMargin = 30; // 30px from top const maxHeight = window.innerHeight - minTopMargin; // If current height would make drawer too tall, adjust it if (height > maxHeight) { setHeight(maxHeight); } }; window.addEventListener('resize', handleResize); // Call once on mount to ensure proper sizing handleResize(); return () => { window.removeEventListener('resize', handleResize); }; }, [height]); // Handle mouse events for resizing useEffect(() => { const handleMouseMove = (e: MouseEvent) => { if (!isDragging) return const deltaY = e.clientY - startY const minTopMargin = 30; // 30px from top const maxHeight = window.innerHeight - minTopMargin; const newHeight = Math.max(200, Math.min(maxHeight, startHeight - deltaY)) setHeight(newHeight) } const handleMouseUp = () => { if (isDragging) { setIsDragging(false) document.body.style.cursor = "default" // Height saving is now handled by the other useEffect // localStorage.setItem(DRAWER_HEIGHT_KEY, height.toString()) } } if (isDragging) { document.addEventListener("mousemove", handleMouseMove) document.addEventListener("mouseup", handleMouseUp) } return () => { document.removeEventListener("mousemove", handleMouseMove) document.removeEventListener("mouseup", handleMouseUp) } }, [isDragging, startY, startHeight]) const handleDragStart = (e: React.MouseEvent) => { setIsDragging(true) setStartY(e.clientY) setStartHeight(height) document.body.style.cursor = "ns-resize" } const toggleDrawer = () => { setIsOpen(!isOpen) } // Terminal icon component embedded directly in this file const TerminalIcon = ({ className }: { className?: string }) => (
) // Add logging just before render console.log(`[CliDrawer Render] isOpen = ${isOpen}`); return ( <> {/* Tab button when drawer is closed */} {!isOpen && ( )} {/* Conditionally render the entire drawer content only when open */} {isOpen && (
{/* Drag handle */}
{/* Header bar */}
Ably Shell { /* Env lozenge hidden for now: TEST MODE */ }
{/* Terminal content area */}
{TerminalComponent && }
)} ) }