Needs some assistance in the sidebar closing

I am working on a Sidebar with Next.js, Tailwindcss, Shadcn-ui and i have written almost the same logic for opening the drawer if i click on a Sidebar icon. But i want to close the sidebar if i click again on the sidebar icon but my current code is not working properly. Can someone please look into it and tell where i am doing wrong.

Here is my SidebarOptions component code:

"use client";
import React, { useEffect, useRef, useState } from "react";
import { sidebarData } from "@/constants/sidebarData";
import Link from "next/link";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { HelpCircle, LogOut } from "lucide-react";
import { GoHome } from "react-icons/go";

const SidebarOptions = ({ onLinkClick, user }) => {
  const [isSubmenuVisible, setIsSubmenuVisible] = useState(false);
  const [openSection, setOpenSection] = useState(null);
  const sidebarRef = useRef();

  const handleSectionClick = (section) => {
    const sectionData = dynamicSidebarData.find((sec) => sec.title === section);

    if (!sectionData || sectionData.links.length === 0) {
      return;
    }

    if (openSection === section) {
      // Close the drawer if clicking on the same section
      setIsSubmenuVisible(false);
      setOpenSection(null);
    } else {
      // Open the drawer for a new section
      setOpenSection(section);
      setIsSubmenuVisible(true);
    }
  };

  useEffect(() => {
    const handleClickOutside = (event) => {
      if (sidebarRef.current && !sidebarRef.current.contains(event.target)) {
        setOpenSection(null);
      }
    };

    if (openSection) {
      document.addEventListener("mousedown", handleClickOutside);
    } else {
      document.removeEventListener("mousedown", handleClickOutside);
    }

    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, [openSection]);

  // Dynamic path modification for the "Edit Public Profile" link
  const dynamicSidebarData = sidebarData.map((section) => {
    if (section.title === "Profile") {
      return {
        ...section,
        links: section.links.map((link) =>
          link.name === "Edit Public Profile"
            ? {
                ...link,
                path: `/dashboard/profile/${user._id}`,
              }
            : link
        ),
      };
    }
    return section;
  });

  return (
    <TooltipProvider>
      <div className="flex h-screen">
        <div className="flex flex-col w-12 bg-white border-r">
          <div className="flex flex-col px-2 mb-2">
            <Tooltip className="bg-black text-white">
              <TooltipTrigger>
                <Link
                  href="/dashboard"
                  className="flex flex-col items-center mb-3 p-2 text-[#F6F6F6] hover:bg-[#DFEDEE] 
                rounded cursor-pointertransition-all duration-200 ease-in-out transform"
                >
                  <GoHome className="text-muted-foreground size-4" />
                </Link>
              </TooltipTrigger>
            </Tooltip>
            {dynamicSidebarData.map((section, sectionIndex) => (
              <Tooltip key={sectionIndex} className="bg-black text-white">
                <TooltipTrigger>
                  <div
                    className="flex flex-col items-center mb-3 p-2 text-[#F6F6F6] hover:bg-[#DFEDEE] rounded cursor-pointer
                                transition-all duration-200 ease-in-out transform"
                    onClick={() => handleSectionClick(section.title)}
                  >
                    {section.icon}
                  </div>
                </TooltipTrigger>
                <TooltipContent
                  side="right"
                  className="mr-2 bg-black text-white font-bold"
                >
                  <p>{section.title}</p>
                </TooltipContent>
              </Tooltip>
            ))}
          </div>
          <div className="flex flex-col items-center mt-36 py-4">
            <Tooltip className="bg-black text-white">
              <TooltipTrigger>
                <Link href="/help" legacyBehavior>
                  <a className="flex flex-col items-center mb-2 p-2 rounded">
                    <HelpCircle className="text-muted-foreground size-4" />
                  </a>
                </Link>
              </TooltipTrigger>
              <TooltipContent side="right" className="mr-2">
                <p className="">Help</p>
              </TooltipContent>
            </Tooltip>
            <Tooltip className="bg-black text-white">
              <TooltipTrigger>
                <Link href="/logout" legacyBehavior>
                  <a className="flex flex-col items-center mb-6 p-2 rounded">
                    <LogOut className="text-rose-500 size-4" />
                  </a>
                </Link>
              </TooltipTrigger>
              <TooltipContent side="right" className="mr-2">
                <p className="">Logout</p>
              </TooltipContent>
            </Tooltip>
          </div>
        </div>
        {openSection && (
          <div
            ref={sidebarRef}
            className={`absolute left-16 top-0 backdrop-blur-md bg-white/50 bg-white border-r p-4 w-[200px] z-[99999] h-full overflow-y-auto
                      transform transition-all duration-300 ease-in-out
                      ${
                        isSubmenuVisible
                          ? "translate-x-0 opacity-100"
                          : "-translate-x-full opacity-0"
                      }`}
          >
            <h2 className="text-xl font-bold mb-4">{openSection}</h2>

            {dynamicSidebarData
              .find((section) => section.title === openSection)
              .links.map((link, linkIndex) => (
                <div
                  key={linkIndex}
                  className="flex items-center gap-2 my-2 p-2 hover:bg-gray-200 rounded cursor-pointer"
                  onClick={() => {
                    if (onLinkClick) {
                      onLinkClick(link.path);
                    }
                    setOpenSection(null);
                  }}
                >
                  <Link href={link.path} className="text-gray-700">
                    {link.name}
                  </Link>
                </div>
              ))}
          </div>
        )}
      </div>
    </TooltipProvider>
  );
};

export default SidebarOptions;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật