import React, { useState, useEffect, useCallback } from 'react'; const FONT_SIZE_STORAGE_KEY = 'prompt-editor-font-size'; const DEFAULT_FONT_SIZE = 14; const MIN_FONT_SIZE = 10; const MAX_FONT_SIZE = 24; const FONT_SIZE_OPTIONS = [10, 12, 14, 16, 18, 20, 24]; interface PromptEditorModalProps { isOpen: boolean; initialPrompt: string; onSubmit: (prompt: string) => void; onClose: () => void; } export const PromptEditorModal: React.FC = ({ isOpen, initialPrompt, onSubmit, onClose, }) => { const [prompt, setPrompt] = useState(initialPrompt); const [showConfirmation, setShowConfirmation] = useState(false); const [fontSize, setFontSize] = useState(() => { // Load font size from localStorage on mount if (typeof window !== 'undefined') { const saved = localStorage.getItem(FONT_SIZE_STORAGE_KEY); if (saved) { const parsed = parseInt(saved, 10); if (!isNaN(parsed) && parsed >= MIN_FONT_SIZE && parsed <= MAX_FONT_SIZE) { return parsed; } } } return DEFAULT_FONT_SIZE; }); // Update local state when initial prompt changes useEffect(() => { setPrompt(initialPrompt); }, [initialPrompt]); // Save font size to localStorage when it changes useEffect(() => { if (typeof window !== 'undefined') { localStorage.setItem(FONT_SIZE_STORAGE_KEY, fontSize.toString()); } }, [fontSize]); // Track unsaved changes const hasUnsavedChanges = prompt !== initialPrompt; // Handle close attempt - show confirmation if there are unsaved changes const handleAttemptClose = useCallback(() => { if (hasUnsavedChanges) { setShowConfirmation(true); } else { onClose(); } }, [hasUnsavedChanges, onClose]); // Handle Escape key to close useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { handleAttemptClose(); } }; if (isOpen) { window.addEventListener('keydown', handleKeyDown); } return () => { window.removeEventListener('keydown', handleKeyDown); }; }, [isOpen, handleAttemptClose]); const handleSubmit = useCallback(() => { onSubmit(prompt); onClose(); }, [prompt, onSubmit, onClose]); const handleFontSizeChange = useCallback((e: React.ChangeEvent) => { setFontSize(parseInt(e.target.value, 10)); }, []); const handleBackdropClick = useCallback( (e: React.MouseEvent) => { // Only close if clicking the backdrop itself, not the dialog content if (e.target === e.currentTarget) { handleAttemptClose(); } }, [handleAttemptClose] ); const handleDismissConfirmation = useCallback(() => { setShowConfirmation(false); }, []); const handleConfirmationBackdropClick = useCallback( (e: React.MouseEvent) => { // Only dismiss if clicking the backdrop itself, not the confirmation dialog if (e.target === e.currentTarget) { handleDismissConfirmation(); } }, [handleDismissConfirmation] ); if (!isOpen) return null; return (
{/* Header */}

Edit Prompt

{/* Box containing toolbar and textarea */}
{/* Toolbar - header of the box */}
{/* Font Size Control */}
{/* Textarea */}