"use client"
import IconSidebar from "@/components/Icons/Sidebar"
import Tooltip from "@/components/Tooltip"
import NavbarIconButton from "../IconButton"
import { useSidebar } from "../../../providers/sidebar"
import clsx from "clsx"
import { useEffect, useState } from "react"
const NavbarSidebarButton = () => {
const { desktopSidebarOpen, setDesktopSidebarOpen } = useSidebar()
const [isApple, setIsApple] = useState(false)
const toggleSidebar = () => {
setDesktopSidebarOpen((prevValue) => !prevValue)
}
useEffect(() => {
setIsApple(navigator.userAgent.toLowerCase().indexOf("mac") !== 0)
function isEditingContent(event: KeyboardEvent) {
const element = event.target as HTMLElement
const tagName = element.tagName
return (
element.isContentEditable ||
tagName === "INPUT" ||
tagName === "SELECT" ||
tagName === "TEXTAREA"
)
}
function sidebarShortcut(e: KeyboardEvent) {
if (
(e.metaKey || e.ctrlKey) &&
e.key.toLowerCase() === "i" &&
!isEditingContent(e)
) {
e.preventDefault()
toggleSidebar()
}
}
window.addEventListener("keydown", sidebarShortcut)
return () => {
window.removeEventListener("keydown", sidebarShortcut)
}
}, [])
const getPlatformKey = () =>
`
${isApple ? "⌘" : "Ctrl"}
`
return (
Close sidebar ${getPlatformKey()}
I`
: `Lock sidebar open ${getPlatformKey()}
I`
}
>
)
}
export default NavbarSidebarButton