diff --git a/compass/components/Drawer/Drawer.tsx b/compass/components/Drawer/Drawer.tsx index 44a9eab..2b67e2f 100644 --- a/compass/components/Drawer/Drawer.tsx +++ b/compass/components/Drawer/Drawer.tsx @@ -1,4 +1,4 @@ -import { Dispatch, FunctionComponent, ReactNode, SetStateAction } from "react"; +import { Dispatch, FunctionComponent, ReactNode, SetStateAction, useEffect, useRef } from "react"; import React, { useState } from "react"; import { ChevronDoubleLeftIcon } from "@heroicons/react/24/solid"; import { StarIcon as SolidStarIcon, UserIcon } from "@heroicons/react/24/solid"; @@ -47,6 +47,47 @@ const Drawer: FunctionComponent = ({ const [isFull, setIsFull] = useState(false); const [isFavorite, setIsFavorite] = useState(false); const [tempRowContent, setTempRowContent] = useState(rowContent); + const drawerRef = useRef(null); + + useEffect(() => { + const handleClickOutside = (event: MouseEvent) => { + if (drawerRef.current && !drawerRef.current.contains(event.target as Node)) { + if (setRowContent && isOpen) { + // Check if any values have changed + const hasChanges = Object.keys(tempRowContent).some( + (key) => tempRowContent[key] !== rowContent[key] + ); + + if (hasChanges) { + handleUpdate().then((response) => { + if (response.ok) { + setRowContent((prev: any) => { + return prev.map((row: any) => { + if (row.id === tempRowContent.id) { + return tempRowContent; + } + return row; + }); + }); + } + }); + } + } + setIsOpen(false); + if (isFull) { + setIsFull(false); + } + } + }; + + if (isOpen) { + document.addEventListener('mousedown', handleClickOutside); + } + + return () => { + document.removeEventListener('mousedown', handleClickOutside); + }; + }, [isOpen, tempRowContent, rowContent]); const handleTempRowContentChangeHTML = ( e: React.ChangeEvent @@ -114,9 +155,8 @@ const Drawer: FunctionComponent = ({ const toggleFavorite = () => setIsFavorite(!isFavorite); - const drawerClassName = `fixed top-0 right-0 w-1/2 h-full bg-white transform ease-in-out duration-300 z-20 ${ - isOpen ? "translate-x-0 shadow-xl" : "translate-x-full" - } ${isFull ? "w-full" : "w-1/2"}`; + const drawerClassName = `fixed top-0 right-0 w-1/2 h-full bg-white transform ease-in-out duration-300 z-20 ${isOpen ? "translate-x-0 shadow-xl" : "translate-x-full" + } ${isFull ? "w-full" : "w-1/2"}`; const iconComponent = isFull ? ( @@ -140,7 +180,7 @@ const Drawer: FunctionComponent = ({ > Open -
+
@@ -186,7 +226,7 @@ const Drawer: FunctionComponent = ({ = ({ } setPresetOptions={ detail.presetOptionsSetter || - (() => {}) + (() => { }) } singleValue={true} onTagsChange={( @@ -236,7 +276,7 @@ const Drawer: FunctionComponent = ({ = ({ } setPresetOptions={ detail.presetOptionsSetter || - (() => {}) + (() => { }) } onTagsChange={( tags: Set @@ -260,7 +300,7 @@ const Drawer: FunctionComponent = ({ ) : (
{value && - value.length > 0 ? ( + value.length > 0 ? ( value.map( ( tag: string,