From b0e7ae35afda29698bf215ac3925eebc5c9597a4 Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Wed, 29 May 2024 16:36:20 -0400 Subject: [PATCH] clean up more of the code --- frontend/src/components/Preview.tsx | 55 ++++++++++++++----- .../components/select-and-edit/EditPopup.tsx | 11 ++-- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/frontend/src/components/Preview.tsx b/frontend/src/components/Preview.tsx index eabad4e..c643e69 100644 --- a/frontend/src/components/Preview.tsx +++ b/frontend/src/components/Preview.tsx @@ -6,29 +6,38 @@ import EditPopup from "./select-and-edit/EditPopup"; interface Props { code: string; device: "mobile" | "desktop"; + doUpdate: ( + selectedUpdateInstruction?: string, + selectedElement?: HTMLElement + ) => void; + inSelectAndEditMode: boolean; } -function Preview({ code, device }: Props) { +function Preview({ code, device, doUpdate, inSelectAndEditMode }: Props) { const iframeRef = useRef(null); // Don't update code more often than every 200ms. const throttledCode = useThrottle(code, 200); - const [selectedElement, setSelectedElement] = useState( - null - ); + const inSelectAndEditModeRef = useRef(inSelectAndEditMode); // Create a ref for the state + + const [selectedElement, setSelectedElement] = useState< + HTMLElement | undefined + >(undefined); const [popupVisible, setPopupVisible] = useState(false); const [popupPosition, setPopupPosition] = useState({ x: 0, y: 0 }); - const [editText, setEditText] = useState(""); - console.log(selectedElement); + function removeHighlight(element: HTMLElement) { + element.style.outline = ""; + element.style.backgroundColor = ""; + return element; + } function updateHighlight(targetElement: HTMLElement) { setSelectedElement((prev) => { // Remove style from previous element if (prev) { - prev.style.outline = ""; - prev.style.backgroundColor = ""; + removeHighlight(prev); } // Add style to new element targetElement.style.outline = "2px dashed #1846db"; @@ -38,6 +47,11 @@ function Preview({ code, device }: Props) { } function handleClick(event: MouseEvent) { + // Return if not in select and edit mode + if (!inSelectAndEditModeRef.current) { + return; + } + const { clientX, clientY } = event; // Prevent default to avoid issues like label clicks triggering textareas, etc. @@ -70,13 +84,28 @@ function Preview({ code, device }: Props) { setPopupVisible(true); } - function handleEditSubmit() { - if (selectedElement) { - selectedElement.innerText = editText; - } + function handleEditSubmit(editText: string) { + doUpdate( + editText, + selectedElement ? removeHighlight(selectedElement) : selectedElement + ); + setSelectedElement(undefined); setPopupVisible(false); } + useEffect(() => { + if (!inSelectAndEditMode) { + if (selectedElement) removeHighlight(selectedElement); + setSelectedElement(undefined); + setPopupVisible(false); + } + }, [inSelectAndEditMode, selectedElement]); + + // Update the ref whenever the state changes + useEffect(() => { + inSelectAndEditModeRef.current = inSelectAndEditMode; + }, [inSelectAndEditMode]); + useEffect(() => { const iframe = iframeRef.current; if (iframe) { @@ -111,8 +140,6 @@ function Preview({ code, device }: Props) { {...{ popupVisible, popupPosition, - editText, - setEditText, handleEditSubmit, }} /> diff --git a/frontend/src/components/select-and-edit/EditPopup.tsx b/frontend/src/components/select-and-edit/EditPopup.tsx index 6026e68..a833f00 100644 --- a/frontend/src/components/select-and-edit/EditPopup.tsx +++ b/frontend/src/components/select-and-edit/EditPopup.tsx @@ -1,22 +1,19 @@ -import React, { useEffect, useRef } from "react"; +import React, { useEffect, useRef, useState } from "react"; import { Textarea } from "../ui/textarea"; import { Button } from "../ui/button"; interface EditPopupProps { popupVisible: boolean; popupPosition: { x: number; y: number }; - editText: string; - setEditText: (text: string) => void; - handleEditSubmit: () => void; + handleEditSubmit: (editText: string) => void; } const EditPopup: React.FC = ({ popupVisible, popupPosition, - editText, - setEditText, handleEditSubmit, }) => { + const [editText, setEditText] = useState(""); const textareaRef = useRef(null); useEffect(() => { @@ -45,7 +42,7 @@ const EditPopup: React.FC = ({ placeholder="Tell the AI what to change about this element..." />
- +
);