From 5e173b760e2efda21eac62ebb1206fbcae3b6f7a Mon Sep 17 00:00:00 2001 From: shrimbly Date: Sat, 3 Jan 2026 21:25:15 +1300 Subject: [PATCH] prevent discarding prompt edits --- src/components/modals/PromptEditorModal.tsx | 50 ++++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/components/modals/PromptEditorModal.tsx b/src/components/modals/PromptEditorModal.tsx index 631c4c84..d4d7f52f 100644 --- a/src/components/modals/PromptEditorModal.tsx +++ b/src/components/modals/PromptEditorModal.tsx @@ -20,6 +20,7 @@ export const PromptEditorModal: React.FC = ({ 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') { @@ -46,11 +47,23 @@ export const PromptEditorModal: React.FC = ({ } }, [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') { - onClose(); + handleAttemptClose(); } }; @@ -61,7 +74,7 @@ export const PromptEditorModal: React.FC = ({ return () => { window.removeEventListener('keydown', handleKeyDown); }; - }, [isOpen, onClose]); + }, [isOpen, handleAttemptClose]); const handleSubmit = useCallback(() => { onSubmit(prompt); @@ -76,10 +89,10 @@ export const PromptEditorModal: React.FC = ({ (e: React.MouseEvent) => { // Only close if clicking the backdrop itself, not the dialog content if (e.target === e.currentTarget) { - onClose(); + handleAttemptClose(); } }, - [onClose] + [handleAttemptClose] ); if (!isOpen) return null; @@ -89,7 +102,7 @@ export const PromptEditorModal: React.FC = ({ className="fixed inset-0 z-[100] flex items-center justify-center bg-black/50" onClick={handleBackdropClick} > -
+
{/* Header */}

@@ -129,7 +142,7 @@ export const PromptEditorModal: React.FC = ({ {/* Footer with buttons */}
+ + {/* Confirmation overlay */} + {showConfirmation && ( +
+
+

+ You have unsaved changes +

+
+ + +
+
+
+ )}

);