From 50c086acee4f948a602cdb66d9d2f1d8c126726f Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Sun, 4 Aug 2024 03:18:41 +0530 Subject: [PATCH] feat: handle attribute selection --- src/components/organisms/BrowserWindow.tsx | 70 ++++++++++++++++++---- 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/src/components/organisms/BrowserWindow.tsx b/src/components/organisms/BrowserWindow.tsx index 86820a80..c042d026 100644 --- a/src/components/organisms/BrowserWindow.tsx +++ b/src/components/organisms/BrowserWindow.tsx @@ -40,12 +40,15 @@ const getAttributeOptions = (tagName: string): AttributeOption[] => { }; - export const BrowserWindow = () => { const [canvasRef, setCanvasReference] = useState | undefined>(undefined); const [screenShot, setScreenShot] = useState(""); const [highlighterData, setHighlighterData] = useState<{ rect: DOMRect, selector: string, elementInfo: ElementInfo | null; } | null>(null); const [showConfirmation, setShowConfirmation] = useState(false); + const [showAttributeModal, setShowAttributeModal] = useState(false); + const [attributeOptions, setAttributeOptions] = useState([]); + const [selectedElement, setSelectedElement] = useState<{selector: string, info: ElementInfo | null} | null>(null); + const { socket } = useSocketStore(); const { width, height } = useBrowserDimensionsStore(); @@ -113,14 +116,50 @@ export const BrowserWindow = () => { clickY >= highlightRect.top && clickY <= highlightRect.bottom ) { - addBrowserStep('', highlighterData.elementInfo?.innerText || '', { - selector: highlighterData.selector, - tag: highlighterData.elementInfo?.tagName - }); + const options = getAttributeOptions(highlighterData.elementInfo?.tagName || ''); + if (options.length > 1) { + setAttributeOptions(options); + setSelectedElement({ + selector: highlighterData.selector, + info: highlighterData.elementInfo + }); + setShowAttributeModal(true); + } else { + addBrowserStep('', highlighterData.elementInfo?.innerText || '', { + selector: highlighterData.selector, + tag: highlighterData.elementInfo?.tagName, + attribute: 'innerText' + }); + } } } }; + const handleAttributeSelection = (attribute: string) => { + if (selectedElement) { + let data = ''; + switch (attribute) { + case 'href': + data = selectedElement.info?.url || ''; + break; + case 'src': + data = selectedElement.info?.imageUrl || ''; + break; + default: + data = selectedElement.info?.innerText || ''; + } + + addBrowserStep('', data, { + selector: selectedElement.selector, + tag: selectedElement.info?.tagName, + attribute: attribute + }); + } + setShowAttributeModal(false); + }; + + + const handleConfirmation = (confirmed: boolean) => { if (confirmed) { console.log(`User confirmed interaction with: ${highlighterData?.selector}`); @@ -133,21 +172,30 @@ export const BrowserWindow = () => { return (
{ - getText === true && showConfirmation ? ( + getText === true ? ( setShowConfirmation(false)} + isOpen={showAttributeModal} + onClose={() => {}} canBeClosed={false} > - handleConfirmation(true)} onNo={() => handleConfirmation(false)} - /> + /> */} +
+

Select Attribute

+ {attributeOptions.map((option) => ( + + ))} +
+
) : null } - {(getText === true && !showConfirmation && highlighterData?.rect != null && highlighterData?.rect.top != null) && canvasRef?.current ? + {(getText === true && highlighterData?.rect != null && highlighterData?.rect.top != null) && canvasRef?.current ?